Skip to content

Commit

Permalink
Merge pull request #112 from mircokroon/ui-fixes
Browse files Browse the repository at this point in the history
UI fixes
  • Loading branch information
mircokroon authored Feb 16, 2021
2 parents 053c26b + 0804b9c commit 134f69a
Show file tree
Hide file tree
Showing 18 changed files with 182 additions and 100 deletions.
1 change: 0 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@
<version>3.3.0</version>
</dependency>


<!-- Test dependencies -->
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand Down
45 changes: 44 additions & 1 deletion src/main/java/Launcher.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,50 @@
import config.Config;

import util.PathUtils;

import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import static util.ExceptionHandling.attemptQuiet;

public class Launcher {
public static void main(String[] args) {
public static void main(String[] args) throws URISyntaxException {
fixCwd();

Config.init(args);
}

private static void fixCwd() throws URISyntaxException {
String cwd = System.getProperty("user.dir");

if (Files.isWritable(Paths.get(cwd))) {
PathUtils.setWorkingDirectory(cwd);
return;
}

// if we can't write to the working directory, try the jar file's location
Path jarPath = Paths.get(Launcher.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getParent();
if (Files.isWritable(jarPath)) {
System.out.println("Can't write to working directory. Writing to " + jarPath);
PathUtils.setWorkingDirectory(jarPath);

return;
}

// if we can't write there, try the Minecraft installation dir
Path mcPath = Paths.get(Config.getDefaultMinecraftPath(), "world-downloader");
attemptQuiet(() -> Files.createDirectories(mcPath));
if (Files.isWritable(mcPath)) {
System.out.println("Can't write to working directory. Writing to " + mcPath);
PathUtils.setWorkingDirectory(mcPath);

return;
}


System.err.println("Unable to write data. Consider running with more permissions.");
System.exit(1);
}
}
41 changes: 22 additions & 19 deletions src/main/java/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import proxy.ConnectionDetails;
import proxy.ConnectionManager;
import proxy.auth.AuthDetails;
import util.PathUtils;

import java.io.File;
import java.io.FileReader;
Expand All @@ -28,7 +29,7 @@

public class Config {
private static final int DEFAULT_VERSION = 340;
private static final Path CONFIG_PATH = Paths.get("cache", "config.json");
private static Path configPath;

private static Consumer<PacketBuilder> injector;
private static Config instance;
Expand All @@ -54,18 +55,20 @@ public static void setInstance(Config config) {
*/
private static Config createConfig() {
try {
File file = CONFIG_PATH.toFile();
File file = configPath.toFile();
if (file.exists() && file.isFile()) {
return new Gson().fromJson(new JsonReader(new FileReader(file)), Config.class);
}
} catch (Exception ex) {
System.out.println("Cannot read " + CONFIG_PATH.toString());
System.out.println("Cannot read " + configPath.toString());
ex.printStackTrace();
}
return new Config();
}

public static void init(String[] args) {
configPath = PathUtils.toPath("cache", "config.json");

instance = createConfig();
CmdLineParser parser = new CmdLineParser(instance);
try {
Expand Down Expand Up @@ -169,17 +172,17 @@ public void settingsComplete() {
private void writeSettings() {
try {
String contents = new GsonBuilder().setPrettyPrinting().create().toJson(this);
CONFIG_PATH.getParent().toFile().mkdirs();
Files.write(CONFIG_PATH, Collections.singleton(contents));
Files.createDirectories(configPath.getParent());
Files.write(configPath, Collections.singleton(contents));
} catch (Exception ex) {
ex.printStackTrace();
}
}

public static void clearSettings() {
try {
if (CONFIG_PATH.toFile().exists()) {
CONFIG_PATH.toFile().deleteOnExit();
if (configPath.toFile().exists()) {
configPath.toFile().deleteOnExit();
}
} catch (Exception ex) {
ex.printStackTrace();
Expand Down Expand Up @@ -208,9 +211,17 @@ public boolean isStarted() {
* Get the platform-specific default path for the Minecraft installation directory.
* @return the path as a string
*/
private static String getDefaultPath() {
public static String getDefaultMinecraftPath() {
if (SystemUtils.IS_OS_WINDOWS) {
return Paths.get("%appdata%", ".minecraft").toString();
String path = Paths.get("%appdata%", ".minecraft").toString();

// handle common %APPDATA% env variable for Windows
if (path.toUpperCase().contains("%APPDATA%") && System.getenv("appdata") != null) {
String appdataPath = System.getenv("appdata").replace("\\", "\\\\");
path = path.replaceAll("(?i)%APPDATA%", appdataPath);
}

return path;
} else if (SystemUtils.IS_OS_LINUX) {
return Paths.get(System.getProperty("user.home"), ".minecraft").toString();
} else if (SystemUtils.IS_OS_UNIX) {
Expand Down Expand Up @@ -259,15 +270,7 @@ private boolean writeChunks() {
* @return the contents of the file
*/
public static String getMinecraftPath() {
String path = instance.minecraftDir;

// handle common %APPDATA% env variable for Windows
if (path.toUpperCase().contains("%APPDATA%") && System.getenv("appdata") != null) {
String appdataPath = System.getenv("appdata").replace("\\", "\\\\");
path = path.replaceAll("(?i)%APPDATA%", appdataPath);
}

return path;
return instance.minecraftDir;
}

/**
Expand Down Expand Up @@ -321,7 +324,7 @@ public static Consumer<PacketBuilder> getPacketInjector() {

@Option(name = "--minecraft-dir", aliases = "-m",
usage = "Path to your Minecraft installation, used to retrieve Minecraft authentication details.")
public String minecraftDir = getDefaultPath();
public String minecraftDir = getDefaultMinecraftPath();

@Option(name = "--output", aliases = "-o",
usage = "The world output directory. If the world already exists, it will be updated.")
Expand Down
18 changes: 8 additions & 10 deletions src/main/java/game/data/WorldManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import se.llbit.nbt.NamedTag;
import se.llbit.nbt.StringTag;
import se.llbit.nbt.Tag;
import util.PathUtils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
Expand Down Expand Up @@ -156,11 +157,11 @@ public void setDimensionCodec(DimensionCodec codec) {

// We can immediately try to write the dimension data to the proper directory.
try {
Path p = Paths.get(Config.getWorldOutputDir(), "datapacks", "downloaded", "data");
Path p = PathUtils.toPath(Config.getWorldOutputDir(), "datapacks", "downloaded", "data");
if (codec.write(p)) {

// we need to copy that pack.mcmeta file from so that Minecraft will recognise the datapack
Path packMeta = Paths.get(p.getParent().toString(), "pack.mcmeta");
Path packMeta = PathUtils.toPath(p.getParent().toString(), "pack.mcmeta");
InputStream in = WorldManager.class.getClassLoader().getResourceAsStream("pack.mcmeta");
byte[] bytes = IOUtils.toByteArray(in);
Files.write(packMeta, bytes);
Expand Down Expand Up @@ -217,7 +218,7 @@ public void drawExistingChunks() throws IOException {
* Read from the save path to see which chunks have been saved already.
*/
private Stream<McaFile> getMcaFiles(Dimension dimension, boolean limit) throws IOException {
Path exportDir = Paths.get(Config.getWorldOutputDir(), dimension.getPath(), "region");
Path exportDir = PathUtils.toPath(Config.getWorldOutputDir(), dimension.getPath(), "region");

if (!exportDir.toFile().exists()) {
return Stream.empty();
Expand Down Expand Up @@ -249,12 +250,9 @@ private Stream<McaFile> getMcaFiles(Dimension dimension, boolean limit) throws I
*/
private void saveLevelData() throws IOException {
// make sure the folder exists
File directory = Paths.get(Config.getWorldOutputDir()).toFile();
if (!directory.exists()) {
directory.mkdirs();
}
Files.createDirectories(PathUtils.toPath(Config.getWorldOutputDir()));

File levelDat = Paths.get(Config.getWorldOutputDir(), "level.dat").toFile();
File levelDat = PathUtils.toPath(Config.getWorldOutputDir(), "level.dat").toFile();

// if there is no level.dat yet, make one from the default
InputStream fileInput;
Expand Down Expand Up @@ -531,7 +529,7 @@ public void deleteAllExisting() {
ChunkFactory.getInstance().clear();

try {
File dir = Paths.get(Config.getWorldOutputDir(), this.dimension.getPath(), "region").toFile();
File dir = PathUtils.toPath(Config.getWorldOutputDir(), this.dimension.getPath(), "region").toFile();

if (dir.isDirectory()) {
FileUtils.cleanDirectory(dir);
Expand Down Expand Up @@ -602,7 +600,7 @@ public Set<Coordinate2D> loadChunks(Collection<Coordinate2D> desired) {
List<Coordinate2D> value = entry.getValue();

String filename = "r." + key.getX() + "." + key.getZ() + ".mca";
File f = Paths.get(Config.getWorldOutputDir(), this.dimension.getPath(), "region", filename).toFile();
File f = PathUtils.toPath(Config.getWorldOutputDir(), this.dimension.getPath(), "region", filename).toFile();

if (!f.exists()) {
continue;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/game/data/chunk/Chunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ protected void addLevelNbtTags(CompoundTag map) {
}

private List<SpecificTag> getEntityList() {
return entities.stream().map(Entity::toNbt).collect(Collectors.toList());
return entities.stream().filter(Objects::nonNull).map(Entity::toNbt).collect(Collectors.toList());
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/game/data/chunk/ChunkBinary.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import game.data.region.McaFile;
import proxy.CompressionManager;
import se.llbit.nbt.NamedTag;
import util.PathUtils;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;

/**
Expand Down Expand Up @@ -51,8 +51,9 @@ public static ChunkBinary fromChunk(Chunk chunk) throws IOException {
// debug option - write all chunks nbt as text to files so it can be easily verified
if (Config.writeChunksAsNbt()) {
String filename = chunk.location.getX() + "_" + chunk.location.getZ();
Paths.get(Config.getWorldOutputDir(), "debug").toFile().mkdirs();
Path output = Paths.get(Config.getWorldOutputDir(), "debug", filename);

Path output = PathUtils.toPath(Config.getWorldOutputDir(), "debug", filename);
Files.createDirectories(output.getParent());
Files.write(output, Collections.singleton(nbt.tag.toString()));
}

Expand Down
7 changes: 3 additions & 4 deletions src/main/java/game/data/dimension/Biome.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package game.data.dimension;

import com.google.gson.*;
import se.llbit.nbt.*;
import util.PathUtils;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -41,8 +40,8 @@ public Biome(String namespace, String fullName, CompoundTag properties) {
* Write this biome to the world/biome folder.
*/
public void write(Path fromPath) throws IOException {
Path p = Paths.get(fromPath.toString(), namespace, "worldgen", "biome", path, name + ".json");
p.toFile().getParentFile().mkdirs();
Path p = PathUtils.toPath(fromPath.toString(), namespace, "worldgen", "biome", path, name + ".json");
Files.createDirectories(p.getParent());
Files.write(p, Collections.singleton(properties.json()));
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/game/data/dimension/Dimension.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import config.Config;
import game.data.WorldManager;
import se.llbit.nbt.SpecificTag;
import util.PathUtils;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.HashMap;
import java.util.Objects;
Expand Down Expand Up @@ -73,15 +73,15 @@ public String getPath() {
return "";
}

return Paths.get("dimensions", namespace, name).toString();
return PathUtils.toPath("dimensions", namespace, name).toString();
}

/**
* Write the dimension data to the dimension directory.
*/
public void write(Path prefix) throws IOException {
Path destination = Paths.get(prefix.toString(), namespace, "dimension", name + ".json");
destination.toFile().getParentFile().mkdirs();
Path destination = PathUtils.toPath(prefix.toString(), namespace, "dimension", name + ".json");
Files.createDirectories(destination.getParent());

DimensionDefinition definition = new DimensionDefinition(type);

Expand All @@ -104,7 +104,7 @@ public void registerType(SpecificTag dimensionNbt) {

// re-write since we write the dimension information on join otherwise
try {
write(Paths.get(Config.getWorldOutputDir(), "datapacks", "downloaded", "data"));
write(PathUtils.toPath(Config.getWorldOutputDir(), "datapacks", "downloaded", "data"));
} catch (IOException e) {
e.printStackTrace();
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/game/data/dimension/DimensionType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import se.llbit.nbt.CompoundTag;
import se.llbit.nbt.IntTag;
import util.PathUtils;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;

/**
Expand Down Expand Up @@ -45,8 +45,8 @@ public int getSignature() {
* Write the dimension type data to the dimension_type directory.
*/
public void write(Path prefix) throws IOException {
Path destination = Paths.get(prefix.toString(), namespace, "dimension_type", name + ".json");
destination.toFile().getParentFile().mkdirs();
Path destination = PathUtils.toPath(prefix.toString(), namespace, "dimension_type", name + ".json");
Files.createDirectories(destination.getParent());

Files.write(destination, Collections.singleton(DimensionCodec.GSON.toJson(properties)));
}
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/game/data/region/McaFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import game.data.chunk.Chunk;
import game.data.chunk.ChunkBinary;
import org.apache.commons.io.IOUtils;
import util.PathUtils;

import java.io.File;
import java.io.FileInputStream;
Expand All @@ -30,7 +31,7 @@ public class McaFile {
*/
public McaFile(File file) throws IOException {
chunkMap = readFile(file);
filePath = Paths.get(file.getAbsolutePath());
filePath = PathUtils.toPath(file.getAbsolutePath());
String[] bits = file.getName().split("\\.");
regionLocation = new Coordinate2D(Integer.parseInt(bits[1]), Integer.parseInt(bits[2]));
}
Expand Down Expand Up @@ -99,7 +100,7 @@ public McaFile(CoordinateDim2D pos, Map<Integer, ChunkBinary> chunkMap) {
regionLocation = pos.offsetRegion();

String filename = "r." + regionLocation.getX() + "." + regionLocation.getZ() + ".mca";
Path filePath = Paths.get(Config.getWorldOutputDir(),pos.getDimension().getPath(), "region", filename);
Path filePath = PathUtils.toPath(Config.getWorldOutputDir(),pos.getDimension().getPath(), "region", filename);

this.chunkMap = new HashMap<>();
if (filePath.toFile().exists()) {
Expand Down Expand Up @@ -140,10 +141,7 @@ public void write() throws IOException {
byte[] toWrite = join(locations, timestamps, chunkDataList, maxpos[0]);

// create directory if it doesn't already exist
File directory = filePath.getParent().toFile();
if (!directory.exists()) {
directory.mkdirs();
}
Files.createDirectories(filePath.getParent());

Files.write(filePath, toWrite);
}
Expand Down
Loading

0 comments on commit 134f69a

Please sign in to comment.