From 07cc5fd7ed5b79604f2d7708343216cd9b372670 Mon Sep 17 00:00:00 2001 From: KennyTV <28825609+KennyTV@users.noreply.github.com> Date: Mon, 6 Jan 2020 16:34:39 +0100 Subject: [PATCH] Let users move and change block/sound mappings in the data folder --- .../api/ViaBackwardsPlatform.java | 9 +++++- .../api/data/VBMappingDataLoader.java | 28 ++++++++++++++----- .../data/BackwardsMappings.java | 2 +- .../data/BackwardsMappings.java | 2 +- .../data/BackwardsMappings.java | 2 +- .../nl/matsv/viabackwards/ViaFabricAddon.java | 12 +++++++- .../nl/matsv/viabackwards/SpongePlugin.java | 6 ++++ .../nl/matsv/viabackwards/VelocityPlugin.java | 6 ++++ 8 files changed, 55 insertions(+), 12 deletions(-) diff --git a/core/src/main/java/nl/matsv/viabackwards/api/ViaBackwardsPlatform.java b/core/src/main/java/nl/matsv/viabackwards/api/ViaBackwardsPlatform.java index b3982b7d..12830198 100644 --- a/core/src/main/java/nl/matsv/viabackwards/api/ViaBackwardsPlatform.java +++ b/core/src/main/java/nl/matsv/viabackwards/api/ViaBackwardsPlatform.java @@ -71,7 +71,7 @@ public interface ViaBackwardsPlatform { } /** - * Logger provided by the platform + * Logger provided by the platform. * * @return logger instance */ @@ -98,4 +98,11 @@ public interface ViaBackwardsPlatform { * Disable the plugin. */ void disable(); + + /** + * Returns ViaBackwards's data folder. + * + * @return data folder + */ + File getDataFolder(); } diff --git a/core/src/main/java/nl/matsv/viabackwards/api/data/VBMappingDataLoader.java b/core/src/main/java/nl/matsv/viabackwards/api/data/VBMappingDataLoader.java index e4022e2d..e60314a7 100644 --- a/core/src/main/java/nl/matsv/viabackwards/api/data/VBMappingDataLoader.java +++ b/core/src/main/java/nl/matsv/viabackwards/api/data/VBMappingDataLoader.java @@ -4,18 +4,32 @@ import nl.matsv.viabackwards.ViaBackwards; import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.data.MappingDataLoader; import us.myles.ViaVersion.util.GsonUtil; -import us.myles.viaversion.libs.gson.JsonArray; -import us.myles.viaversion.libs.gson.JsonElement; -import us.myles.viaversion.libs.gson.JsonObject; -import us.myles.viaversion.libs.gson.JsonPrimitive; +import us.myles.viaversion.libs.gson.*; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; +import java.io.*; import java.util.Map; +//TODO merge data loading with VV's MappingDataLoader, diff is in data folder and data inputstream public class VBMappingDataLoader { + public static JsonObject loadFromDataDir(String name) { + File file = new File(ViaBackwards.getPlatform().getDataFolder(), name); + if (!file.exists()) return loadData(name); + + // Load the file from the platform's directory if present + try (FileReader reader = new FileReader(file)) { + return GsonUtil.getGson().fromJson(reader, JsonObject.class); + } catch (JsonSyntaxException e) { + ViaBackwards.getPlatform().getLogger().warning(name + " is badly formatted!"); + e.printStackTrace(); + ViaBackwards.getPlatform().getLogger().warning("Falling back to resource's file!"); + return loadData(name); + } catch (IOException | JsonIOException e) { + e.printStackTrace(); + } + return null; + } + public static JsonObject loadData(String name) { InputStream stream = VBMappingDataLoader.class.getClassLoader().getResourceAsStream("assets/viabackwards/data/" + name); InputStreamReader reader = new InputStreamReader(stream); diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/data/BackwardsMappings.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/data/BackwardsMappings.java index dfb15f3a..bc4305d6 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/data/BackwardsMappings.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/data/BackwardsMappings.java @@ -30,7 +30,7 @@ public class BackwardsMappings { public static void init() { JsonObject mapping1_12 = MappingDataLoader.loadData("mapping-1.12.json"); JsonObject mapping1_13 = MappingDataLoader.loadData("mapping-1.13.json"); - JsonObject mapping1_12_2to1_13 = VBMappingDataLoader.loadData("mapping-1.12.2to1.13.json"); + JsonObject mapping1_12_2to1_13 = VBMappingDataLoader.loadFromDataDir("mapping-1.12.2to1.13.json"); ViaBackwards.getPlatform().getLogger().info("Loading 1.13 -> 1.12.2 block mapping..."); blockMappings = new BlockMappingsShortArray(mapping1_13.getAsJsonObject("blocks"), mapping1_12.getAsJsonObject("blocks"), mapping1_12_2to1_13.getAsJsonObject("blockstates")); diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_2to1_14/data/BackwardsMappings.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_2to1_14/data/BackwardsMappings.java index 5e2e3a88..aa837c60 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_2to1_14/data/BackwardsMappings.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_2to1_14/data/BackwardsMappings.java @@ -15,7 +15,7 @@ public class BackwardsMappings { public static void init() { JsonObject mapping1_13_2 = MappingDataLoader.loadData("mapping-1.13.2.json"); JsonObject mapping1_14 = MappingDataLoader.loadData("mapping-1.14.json"); - JsonObject mapping1_13_2to1_14 = VBMappingDataLoader.loadData("mapping-1.13.2to1.14.json"); + JsonObject mapping1_13_2to1_14 = VBMappingDataLoader.loadFromDataDir("mapping-1.13.2to1.14.json"); ViaBackwards.getPlatform().getLogger().info("Loading 1.14 -> 1.13.2 block mapping..."); blockStateMappings = new VBMappings(mapping1_14.getAsJsonObject("blockstates"), mapping1_13_2.getAsJsonObject("blockstates"), mapping1_13_2to1_14.getAsJsonObject("blockstates")); diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_14_4to1_15/data/BackwardsMappings.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_14_4to1_15/data/BackwardsMappings.java index c7c05af0..e89686c1 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_14_4to1_15/data/BackwardsMappings.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_14_4to1_15/data/BackwardsMappings.java @@ -15,7 +15,7 @@ public class BackwardsMappings { public static void init() { JsonObject mapping1_14 = MappingDataLoader.loadData("mapping-1.14.json"); JsonObject mapping1_15 = MappingDataLoader.loadData("mapping-1.15.json"); - JsonObject mapping1_14to1_15 = VBMappingDataLoader.loadData("mapping-1.14.4to1.15.json"); + JsonObject mapping1_14to1_15 = VBMappingDataLoader.loadFromDataDir("mapping-1.14.4to1.15.json"); ViaBackwards.getPlatform().getLogger().info("Loading 1.15 -> 1.14.4 mappings..."); blockStateMappings = new VBMappings(mapping1_15.getAsJsonObject("blockstates"), mapping1_14.getAsJsonObject("blockstates"), mapping1_14to1_15.getAsJsonObject("blockstates")); diff --git a/fabric/src/main/java/nl/matsv/viabackwards/ViaFabricAddon.java b/fabric/src/main/java/nl/matsv/viabackwards/ViaFabricAddon.java index e69745cd..3f17c7a5 100644 --- a/fabric/src/main/java/nl/matsv/viabackwards/ViaFabricAddon.java +++ b/fabric/src/main/java/nl/matsv/viabackwards/ViaFabricAddon.java @@ -16,19 +16,29 @@ import nl.matsv.viabackwards.api.ViaBackwardsPlatform; import nl.matsv.viabackwards.fabric.util.LoggerWrapper; import org.apache.logging.log4j.LogManager; +import java.io.File; +import java.nio.file.Path; import java.util.logging.Logger; public class ViaFabricAddon implements ViaBackwardsPlatform, Runnable { @Getter private final Logger logger = new LoggerWrapper(LogManager.getLogger("ViaBackwards")); + private File configDir; @Override public void run() { - this.init(FabricLoader.getInstance().getConfigDirectory().toPath().resolve("ViaBackwards").resolve("config.yml").toFile()); + Path configDirPath = FabricLoader.getInstance().getConfigDirectory().toPath().resolve("ViaBackwards"); + configDir = configDirPath.toFile(); + this.init(configDirPath.resolve("config.yml").toFile()); } @Override public void disable() { // Not possible } + + @Override + public File getDataFolder() { + return configDir; + } } diff --git a/sponge/src/main/java/nl/matsv/viabackwards/SpongePlugin.java b/sponge/src/main/java/nl/matsv/viabackwards/SpongePlugin.java index 2b5ba6c3..62dc75e3 100644 --- a/sponge/src/main/java/nl/matsv/viabackwards/SpongePlugin.java +++ b/sponge/src/main/java/nl/matsv/viabackwards/SpongePlugin.java @@ -22,6 +22,7 @@ import org.spongepowered.api.plugin.Dependency; import org.spongepowered.api.plugin.Plugin; import us.myles.ViaVersion.sponge.util.LoggerWrapper; +import java.io.File; import java.nio.file.Path; import java.util.logging.Logger; @@ -53,4 +54,9 @@ public class SpongePlugin implements ViaBackwardsPlatform { public void disable() { // Not possible } + + @Override + public File getDataFolder() { + return configPath.toFile(); + } } diff --git a/velocity/src/main/java/nl/matsv/viabackwards/VelocityPlugin.java b/velocity/src/main/java/nl/matsv/viabackwards/VelocityPlugin.java index c48decb8..4f263abc 100644 --- a/velocity/src/main/java/nl/matsv/viabackwards/VelocityPlugin.java +++ b/velocity/src/main/java/nl/matsv/viabackwards/VelocityPlugin.java @@ -22,6 +22,7 @@ import nl.matsv.viabackwards.api.ViaBackwardsPlatform; import nl.matsv.viabackwards.velocity.VersionInfo; import us.myles.ViaVersion.sponge.util.LoggerWrapper; +import java.io.File; import java.nio.file.Path; import java.util.logging.Logger; @@ -53,4 +54,9 @@ public class VelocityPlugin implements ViaBackwardsPlatform { public void disable() { // Not possible } + + @Override + public File getDataFolder() { + return configPath.toFile(); + } }