Skip to content

Commit

Permalink
Merge pull request #77 from mircokroon/srv-records
Browse files Browse the repository at this point in the history
SRV records, suspend saving, bug fixes
  • Loading branch information
mircokroon authored Oct 8, 2020
2 parents 1ffce78 + 2ac3dae commit 4acb71b
Show file tree
Hide file tree
Showing 15 changed files with 316 additions and 27 deletions.
37 changes: 33 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@
<java.version>9</java.version>
</properties>

<repositories>
<!-- Jetpack allows loading dependencies from GitHub repositories -->
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

<build>
<plugins>
<plugin>
Expand Down Expand Up @@ -70,14 +78,13 @@
<version>3.7.02</version>
</dependency>

<!-- Use NBT library through jitpack as the latest version is not on Maven -->
<dependency>
<groupId>se.llbit</groupId>
<groupId>com.github.llbit</groupId>
<artifactId>jo-nbt</artifactId>
<version>1.3.0</version>
<version>1baae2f49a</version>
</dependency>



<!-- https://mvnrepository.com/artifact/net.sourceforge.argparse4j/argparse4j -->
<dependency>
<groupId>net.sourceforge.argparse4j</groupId>
Expand All @@ -98,6 +105,28 @@
<version>3.10</version>
</dependency>

<!-- https://mvnrepository.com/artifact/dnsjava/dnsjava -->
<dependency>
<groupId>dnsjava</groupId>
<artifactId>dnsjava</artifactId>
<version>3.3.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.0-alpha1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.0-alpha0</version>
</dependency>


</dependencies>
</project>

4 changes: 4 additions & 0 deletions src/main/java/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ private static Namespace getArguments(String[] args) {
.setDefault(true)
.type(boolean.class)
.help("When false, set world type to a superflat void to prevent new chunks from being added.");
parser.addArgument("--enable-srv-lookup").dest("enable-srv-lookup")
.setDefault(true)
.type(boolean.class)
.help("When true, checks for true address using DNS service records");

Namespace ns = null;
try {
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/game/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public abstract class Game {
private static EncryptionManager encryptionManager;
private static CompressionManager compressionManager;
private static boolean enableWorldGen;
private static boolean enableSrvLookup;

public static int getDataVersion() {
return dataVersion;
Expand Down Expand Up @@ -105,6 +106,7 @@ public static void init(Namespace args) {
GuiManager.showGui();
}
enableWorldGen = args.getBoolean("enable-world-gen");
enableSrvLookup = args.getBoolean("enable-srv-lookup");

versionHandler = VersionHandler.createVersionHandler();
}
Expand Down Expand Up @@ -239,4 +241,8 @@ public static int getRenderDistance() {
public static boolean isWorldGenEnabled() {
return enableWorldGen;
}

public static boolean isSrvLookupEnabled() {
return enableSrvLookup;
}
}
7 changes: 7 additions & 0 deletions src/main/java/game/UnsupportedMinecraftVersionException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package game;

public class UnsupportedMinecraftVersionException extends RuntimeException {
public UnsupportedMinecraftVersionException(String message) {
super(message);
}
}
49 changes: 46 additions & 3 deletions src/main/java/game/data/WorldManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import game.data.region.McaFile;
import game.data.region.Region;
import gui.GuiManager;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import proxy.CompressionManager;
import se.llbit.nbt.ByteTag;
Expand All @@ -37,6 +38,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -66,6 +68,10 @@ public class WorldManager extends Thread {

private static boolean writeChunks;

private static boolean isPaused;

private static boolean isSaving;

private static ContainerManager containerManager;

private WorldManager() {
Expand Down Expand Up @@ -140,9 +146,8 @@ private static void saveLevelData() throws IOException {
File levelDat = Paths.get(Game.getExportDirectory(), "level.dat").toFile();

// if there is no level.dat yet, make one from the default
// TODO: fix memory leak caused by duplicates in the NBT tags
InputStream fileInput;
if (false && levelDat.isFile()) {
if (levelDat.isFile()) {
fileInput = new FileInputStream(levelDat);
} else {
fileInput = WorldManager.class.getClassLoader().getResourceAsStream("level.dat");
Expand Down Expand Up @@ -339,10 +344,18 @@ public void run() {
/**
* Save the world. Will tell all regions to save their chunks.
*/
private static void save() {
public static void save() {
if (!writeChunks) {
return;
}

// make sure we can't have two saving calls at once (due to save & exit)
if (isSaving) {
return;
}
isSaving = true;


if (!regions.isEmpty()) {
// convert the values to an array first to prevent blocking any threads
Region[] r = regions.values().toArray(new Region[regions.size()]);
Expand All @@ -368,6 +381,8 @@ private static void save() {

// remove empty regions
regions.entrySet().removeIf(el -> el.getValue().isEmpty());

isSaving = false;
}

public static ContainerManager getContainerManager() {
Expand All @@ -377,6 +392,34 @@ public static ContainerManager getContainerManager() {
return containerManager;
}

public static void pauseSaving() {
isPaused = true;
}

public static void resumeSaving() {
isPaused = false;
}

public static void deleteAllExisting() {
regions = new HashMap<>();
ChunkFactory.getInstance().clear();

try {
File dir = Paths.get(Game.getExportDirectory(), Game.getDimension().getPath(), "region").toFile();

if (dir.isDirectory()) {
FileUtils.cleanDirectory(dir);
}
} catch (IOException ex) {
System.out.println("Could not delete region files. Reason: " + ex.getMessage());
}

GuiManager.clearChunks();
}

public static boolean isPaused() {
return isPaused;
}


}
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/game/data/chunk/ChunkFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ public static ChunkFactory getInstance() {
}

private ChunkFactory() {
clear();
}

public void clear() {
this.tileEntities = new ConcurrentHashMap<>();
this.chunkEntities = new ConcurrentHashMap<>();
this.entities = new ConcurrentHashMap<>();
Expand Down Expand Up @@ -126,6 +130,11 @@ public void updateTileEntity(Coordinate3D position, SpecificTag entityData) {
* Need a non-static method to do this as we cannot otherwise call notify
*/
public synchronized void addChunk(DataTypeProvider provider) {
// if the world manager is currently paused, discard this chunk
if (WorldManager.isPaused()) {
return;
}

unparsedChunks.add(new ChunkParserPair(provider, Game.getDimension()));
notify();
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/game/data/region/McaFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
public class McaFile {
public final static int SECTOR_SIZE = 4096;
private Map<Integer, ChunkBinary> chunkMap;
private Path filePath;
private Coordinate2D regionLocation;
private final Path filePath;
private final Coordinate2D regionLocation;

/**
* Parse MCA from a given file location.
Expand Down
44 changes: 31 additions & 13 deletions src/main/java/game/data/registries/RegistryLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.gson.Gson;

import game.UnsupportedMinecraftVersionException;
import game.data.chunk.entity.EntityNames;
import game.data.chunk.palette.GlobalPalette;
import game.data.container.ItemRegistry;
Expand Down Expand Up @@ -125,8 +126,13 @@ private void generateReports() throws IOException, InterruptedException {
*/
private void moveReports() throws IOException {
ensureExists(destinationPath);
Files.move(REGISTRIES_GENERATED_PATH, registryPath, StandardCopyOption.REPLACE_EXISTING);
Files.move(BLOCKS_GENERATED_PATH, blocksPath, StandardCopyOption.REPLACE_EXISTING);

if (versionSupportsGenerators()) {
Files.move(REGISTRIES_GENERATED_PATH, registryPath, StandardCopyOption.REPLACE_EXISTING);
}
if (versionSupportsBlockGenerator()) {
Files.move(BLOCKS_GENERATED_PATH, blocksPath, StandardCopyOption.REPLACE_EXISTING);
}
}

/**
Expand All @@ -150,36 +156,48 @@ private void ensureExists(Path folder) {
}

public EntityNames generateEntityNames() throws IOException {
if (version.equals("1.12.2")) {
if (versionSupportsGenerators()) {
return EntityNames.fromRegistry(new FileInputStream(registryPath.toFile()));
} else if (version.equals("1.12.2")) {
return EntityNames.fromJson("1.12.2");
} else if (version.equals("1.13.2")) {
return EntityNames.fromJson("1.13.2");
} else {
return EntityNames.fromRegistry(new FileInputStream(registryPath.toFile()));
throw new UnsupportedMinecraftVersionException(version);
}
}

public GlobalPalette generateGlobalPalette() throws IOException {
if (version.equals("1.12.2")) {
return new GlobalPalette("1.12.2");
} else {
if (versionSupportsBlockGenerator()) {
return new GlobalPalette(new FileInputStream(blocksPath.toFile()));
} else {
return new GlobalPalette("1.12.2");
}
}

public MenuRegistry generateMenuRegistry() throws IOException {
if (version.equals("1.12.2")) {
return new MenuRegistry();
} else {
if (versionSupportsGenerators()) {
return MenuRegistry.fromRegistry(new FileInputStream(registryPath.toFile()));
} else {
return new MenuRegistry();
}
}

public ItemRegistry generateItemRegistry() throws IOException {
if (version.equals("1.12.2")) {
return new ItemRegistry();
} else {
if (versionSupportsGenerators()) {
return ItemRegistry.fromRegistry(new FileInputStream(registryPath.toFile()));
} else {
return new ItemRegistry();
}
}

public boolean versionSupportsBlockGenerator() {
return !version.startsWith("1.12");
}

public boolean versionSupportsGenerators() {
return versionSupportsBlockGenerator() && !version.startsWith("1.13");
}
}

class VersionMap extends HashMap<String, String> { }
7 changes: 7 additions & 0 deletions src/main/java/gui/CanvasHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ public class CanvasHandler extends JPanel implements ActionListener {
bindScroll();
}

public void clearChunks() {
chunkMap.clear();
drawableChunks.clear();

hasChanged = true;
}

private void bindScroll() {
this.addMouseWheelListener(mouseWheelEvent -> {
this.renderDistance = this.renderDistance + (mouseWheelEvent.getWheelRotation() * 2);
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/gui/GuiManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,11 @@ public static void drawExistingChunks(List<CoordinateDim2D> existing) {
existing.forEach(chunkGraphicsHandler::setChunkExists);
}
}

public static void clearChunks() {
if (chunkGraphicsHandler != null) {
chunkGraphicsHandler.clearChunks();
}
}
}

Loading

0 comments on commit 4acb71b

Please sign in to comment.