From 45c9487874ac61f5116d475e1b553f5b13f498f0 Mon Sep 17 00:00:00 2001 From: Mathijs Riezebos Date: Thu, 26 Jan 2017 19:33:31 +0100 Subject: [PATCH] Made everything not error on world-load -Added a few default pocket jsons -Errors on world-load were because the above were missing. -Added a testing schematic (old schematic format) -Made everything work somehow --- build.gradle | 4 +- .../java/com/zixiken/dimdoors/DimDoors.java | 4 +- .../com/zixiken/dimdoors/shared/DDConfig.java | 6 +- .../dimdoors/shared/PocketSavedData.java | 3 - .../dimdoors/shared/SchematicHandler.java | 105 +++++++++++++++--- .../dimdoors/shared/util/Schematic.java | 12 +- .../pockets/json/defaultPersonal.json | 13 +++ .../dimdoors/pockets/json/defaultPublic.json | 13 +++ .../schematic/defaultPersonal_3.schematic | Bin 0 -> 890 bytes 9 files changed, 129 insertions(+), 31 deletions(-) create mode 100644 src/main/resources/assets/dimdoors/pockets/json/defaultPersonal.json create mode 100644 src/main/resources/assets/dimdoors/pockets/json/defaultPublic.json create mode 100644 src/main/resources/assets/dimdoors/pockets/schematic/defaultPersonal_3.schematic diff --git a/build.gradle b/build.gradle index c08187e9..1c8cea9f 100644 --- a/build.gradle +++ b/build.gradle @@ -14,7 +14,7 @@ apply plugin: 'net.minecraftforge.gradle.forge' ext.modversion = "3.0.0-a1" ext.mcversion = "1.10.2" ext.forgeversion = "12.18.3.2221" -ext.spongeSchematicVersion = "1" +//spongeSchematicVersion = "1" version = mcversion + "-" + modversion group= "com.zixiken.dimdoors" // http://maven.apache.org/guides/mini/guide-naming-conventions.html @@ -30,7 +30,7 @@ minecraft { runDir = "run" mappings = "stable_29" replace '${version}', project.version - replace '${spongeSchematicVersion}', ext.spongeSchematicVersion + //replace '${spongeSchematicVersion}', spongeSchematicVersion // makeObfSourceJar = false // an Srg named sources jar is made by default. uncomment this to disable. } diff --git a/src/main/java/com/zixiken/dimdoors/DimDoors.java b/src/main/java/com/zixiken/dimdoors/DimDoors.java index 729dc40a..90962389 100644 --- a/src/main/java/com/zixiken/dimdoors/DimDoors.java +++ b/src/main/java/com/zixiken/dimdoors/DimDoors.java @@ -2,6 +2,7 @@ package com.zixiken.dimdoors; import com.zixiken.dimdoors.shared.DDConfig; import com.zixiken.dimdoors.shared.DDProxyCommon; +import com.zixiken.dimdoors.shared.PocketRegistry; import com.zixiken.dimdoors.shared.items.ModItems; import com.zixiken.dimdoors.shared.PocketSavedData; import com.zixiken.dimdoors.shared.RiftRegistry; @@ -58,8 +59,9 @@ public class DimDoors { public void serverLoad(FMLServerStartingEvent event) { //@todo event.registerServerCommand( new DDCommand() ); //to register commands that this mod offers? RiftRegistry.Instance.reset(); - PocketSavedData.get(getDefWorld()); + PocketRegistry.Instance.reset(); RiftSavedData.get(getDefWorld()); + PocketSavedData.get(getDefWorld()); SchematicHandler.Instance.loadSchematics(); } diff --git a/src/main/java/com/zixiken/dimdoors/shared/DDConfig.java b/src/main/java/com/zixiken/dimdoors/shared/DDConfig.java index 56c04e47..adb27d6b 100644 --- a/src/main/java/com/zixiken/dimdoors/shared/DDConfig.java +++ b/src/main/java/com/zixiken/dimdoors/shared/DDConfig.java @@ -47,11 +47,7 @@ public class DDConfig { // Load config configurationFolder = new File(event.getModConfigurationDirectory(), "/DimDoors"); if (!configurationFolder.exists()) { - try { - configurationFolder.createNewFile(); - } catch (IOException ex) { - Logger.getLogger(DDConfig.class.getName()).log(Level.SEVERE, "Dimdoors config folder could not be created.", ex); - } + configurationFolder.mkdirs(); } Configuration config = new Configuration(event.getSuggestedConfigurationFile()); config.load(); diff --git a/src/main/java/com/zixiken/dimdoors/shared/PocketSavedData.java b/src/main/java/com/zixiken/dimdoors/shared/PocketSavedData.java index 679ee7d1..bfeb56c6 100644 --- a/src/main/java/com/zixiken/dimdoors/shared/PocketSavedData.java +++ b/src/main/java/com/zixiken/dimdoors/shared/PocketSavedData.java @@ -54,9 +54,6 @@ public class PocketSavedData extends DDSavedData { @Override public void readFromNBT(NBTTagCompound pocketnbt) { - // Reset - PocketRegistry.Instance.reset(); - // Load NBT if (pocketnbt != null) { if (pocketnbt.hasKey("pockets")) { diff --git a/src/main/java/com/zixiken/dimdoors/shared/SchematicHandler.java b/src/main/java/com/zixiken/dimdoors/shared/SchematicHandler.java index a695d1cb..b0f767a1 100644 --- a/src/main/java/com/zixiken/dimdoors/shared/SchematicHandler.java +++ b/src/main/java/com/zixiken/dimdoors/shared/SchematicHandler.java @@ -11,8 +11,11 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.zixiken.dimdoors.DimDoors; +import java.io.DataInputStream; import java.io.File; import java.io.IOException; +import java.io.InputStream; +import java.io.StringWriter; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Files; @@ -22,8 +25,10 @@ import java.util.List; import java.util.Random; import java.util.logging.Level; import java.util.logging.Logger; +import java.util.zip.GZIPInputStream; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.CompressedStreamTools; +import org.apache.commons.io.IOUtils; /** * @@ -86,38 +91,100 @@ public class SchematicHandler { } } - private List loadTemplatesFromJson(String nameString, int maxPocketSize) { //depending on the "jSonType" value in the jSon, this might load several variations of a pocket at once, hence loadTemplate -->s<-- + private static List loadTemplatesFromJson(String nameString, int maxPocketSize) { //depending on the "jSonType" value in the jSon, this might load several variations of a pocket at once, hence loadTemplate -->s<-- + InputStream jsonJarStream = DimDoors.class.getResourceAsStream("/assets/dimdoors/pockets/json/" + nameString + ".json"); + String schematicJarDirectory = "/assets/dimdoors/pockets/schematic/"; + //init jsons config folder File jsonFolder = new File(DDConfig.configurationFolder, "/Jsons"); - File jsonFile = new File(jsonFolder, "/" + nameString + ".json"); + if (!jsonFolder.exists()) { + jsonFolder.mkdirs(); + } + File jsonFile = new File(jsonFolder, "/" + nameString + ".json"); //@todo this could probably be moved a few lines down + //init schematics config folder File schematicFolder = new File(DDConfig.configurationFolder, "/Schematics"); - String jsonString = null; - try { - jsonString = readFile(jsonFile.getAbsolutePath(), StandardCharsets.UTF_8); - } catch (IOException ex) { - Logger.getLogger(SchematicHandler.class.getName()).log(Level.SEVERE, "Template Json file for template " + nameString + " was not found in template folder.", ex); + if (!schematicFolder.exists()) { + schematicFolder.mkdirs(); + } + + //load the json and convert it to a JsonObject + String jsonString; + if (jsonJarStream != null) { + StringWriter writer = new StringWriter(); + try { + IOUtils.copy(jsonJarStream, writer, StandardCharsets.UTF_8); + } catch (IOException ex) { + Logger.getLogger(SchematicHandler.class.getName()).log(Level.SEVERE, "Json-file " + nameString + ".json did not load correctly from jar. Skipping loading of this template.", ex); + return new ArrayList(); + } + jsonString = writer.toString(); + } else if (jsonFile.exists()) { + DimDoors.log(SchematicHandler.class, "Json-file " + nameString + ".json was not found in the jar. Loading from config directory instead."); + try { + jsonString = readFile(jsonFile.getAbsolutePath(), StandardCharsets.UTF_8); + } catch (IOException ex) { + Logger.getLogger(SchematicHandler.class.getName()).log(Level.SEVERE, "Json-file " + nameString + ".json did not load correctly from config folder. Skipping loading of this template.", ex); + return new ArrayList(); + } + } else { + DimDoors.warn(SchematicHandler.class, "Json-file " + nameString + ".json was not found in the jar or config directory. Skipping loading of this template."); + return new ArrayList(); } JsonParser parser = new JsonParser(); JsonElement jsonElement = parser.parse(jsonString); JsonObject jsonTemplate = jsonElement.getAsJsonObject(); + DimDoors.log(SchematicHandler.class, "Checkpoint 1 reached"); + //Generate and get templates (without a schematic) of all variations that are valid for the current "maxPocketSize" List validTemplates = getAllValidVariations(jsonTemplate, maxPocketSize); + DimDoors.log(SchematicHandler.class, "Checkpoint 4 reached; " + validTemplates.size() + " templates were loaded"); for (PocketTemplate template : validTemplates) { //it's okay to "tap" this for-loop, even if validTemplates is empty. + InputStream schematicStream = DimDoors.class.getResourceAsStream(schematicJarDirectory + template.getName() + ".schem"); //@todo also check for other schematics + InputStream oldVersionSchematicStream = DimDoors.class.getResourceAsStream(schematicJarDirectory + template.getName() + ".schematic"); //@todo also check for other schematics File schematicFile = new File(schematicFolder, "/" + template.getName() + ".schem"); + File oldVersionSchematicFile = new File(schematicFolder, "/" + template.getName() + ".schem"); NBTTagCompound schematicNBT; + Schematic schematic = null; - try { - schematicNBT = CompressedStreamTools.read(schematicFile); - schematic = Schematic.loadFromNBT(schematicNBT); - } catch (IOException ex) { - Logger.getLogger(SchematicHandler.class.getName()).log(Level.SEVERE, "Schematic file for schematic " + template.getName() + " was not found in template folder.", ex); + if (schematicStream != null) { + try { + GZIPInputStream schematicZipStream = new GZIPInputStream(schematicStream); + schematicNBT = CompressedStreamTools.read(new DataInputStream(schematicZipStream)); + schematic = Schematic.loadFromNBT(schematicNBT); + } catch (IOException ex) { + Logger.getLogger(SchematicHandler.class.getName()).log(Level.SEVERE, "Schematic file " + template.getName() + ".schem did not load correctly from jar.", ex); + } + } else if (oldVersionSchematicStream != null) { + try { + GZIPInputStream schematicZipStream = new GZIPInputStream(oldVersionSchematicStream); + schematicNBT = CompressedStreamTools.read(new DataInputStream(schematicZipStream)); + schematic = Schematic.loadFromNBT(schematicNBT); + } catch (IOException ex) { + Logger.getLogger(SchematicHandler.class.getName()).log(Level.SEVERE, "Schematic file " + template.getName() + ".schematic did not load correctly from jar.", ex); + } + } else if (schematicFile.exists()) { + try { + schematicNBT = CompressedStreamTools.read(schematicFile); + schematic = Schematic.loadFromNBT(schematicNBT); + } catch (IOException ex) { + Logger.getLogger(SchematicHandler.class.getName()).log(Level.SEVERE, "Schematic file " + template.getName() + ".schem did not load correctly from config folder.", ex); + } + } else if (oldVersionSchematicFile.exists()) { + try { + schematicNBT = CompressedStreamTools.read(oldVersionSchematicFile); + schematic = Schematic.loadFromNBT(schematicNBT); + } catch (IOException ex) { + Logger.getLogger(SchematicHandler.class.getName()).log(Level.SEVERE, "Schematic file " + template.getName() + ".schematic did not load correctly from config folder.", ex); + } + } else { + DimDoors.warn(SchematicHandler.class, "Schematic '" + template.getName() + "' was not found in the jar or config directory, neither with the .schem extension, nor with the .schematic extension."); } - if (schematic != null && (schematic.getWidth() > (template.getSize()) * 16 || schematic.getLength() > (template.getSize()) * 16)) { + if (schematic != null + && (schematic.getWidth() > (template.getSize()) * 16 || schematic.getLength() > (template.getSize()) * 16)) { schematic = null; - DimDoors.log(this.getClass(), "Schematic " + template.getName() + ".schem was bigger than specified in " + nameString + ".json and therefore wasn't loaded"); + DimDoors.log(Schematic.class, "Schematic " + template.getName() + " was bigger than specified in " + nameString + ".json and therefore wasn't loaded"); } template.setSchematic(schematic); } - //@todo check for json files in both directories (inside the mod jar, and inside the dimdoors config folder) return validTemplates; } @@ -127,14 +194,14 @@ public class SchematicHandler { return new String(encoded, encoding); } - private List getAllValidVariations(JsonObject jsonTemplate, int maxPocketSize) { + private static List getAllValidVariations(JsonObject jsonTemplate, int maxPocketSize) { String jsonType = jsonTemplate.get("jsonType").getAsString(); EnumPocketType pocketType = EnumPocketType.getFromInt(jsonTemplate.get("pocketType").getAsInt()); JsonArray variations = jsonTemplate.getAsJsonArray("variations"); List pocketTemplates = new ArrayList(); JsonObject chosenVariation = null; //only applicable if jsonType == "Singular" - int chosenVariationSize = 0; //only applicable if jsonType == "Singular" + int chosenVariationSize = -1; //only applicable if jsonType == "Singular" List validVariations = new ArrayList(); //put all valid variation JsonObjects into an array list for (int i = 0; i < variations.size(); i++) { @@ -142,6 +209,7 @@ public class SchematicHandler { int variationSize = variation.get("size").getAsInt(); if (variationSize > maxPocketSize) { + DimDoors.log(SchematicHandler.class, "Checkpoint 2 reached; Variation size " + variationSize + " is bigger than maxPocketSize " + maxPocketSize + "."); //do not add it } else if (jsonType.equals("Singular")) { if (variationSize > chosenVariationSize) { @@ -154,12 +222,13 @@ public class SchematicHandler { } else if (jsonType.equals("Multiple")) { validVariations.add(variation); } else { //@todo more options? - DimDoors.log(this.getClass(), "JsonType " + jsonType + " is not a valid JsonType. Json was not loaded."); + DimDoors.log(SchematicHandler.class, "JsonType " + jsonType + " is not a valid JsonType. Json was not loaded."); } } if (chosenVariation != null) { validVariations.add(chosenVariation); } + DimDoors.log(SchematicHandler.class, "Checkpoint 3 reached; " + validVariations.size() + " variations were selected."); //convert the valid variations arraylist to a list of pocket templates for (JsonObject variation : validVariations) { diff --git a/src/main/java/com/zixiken/dimdoors/shared/util/Schematic.java b/src/main/java/com/zixiken/dimdoors/shared/util/Schematic.java index 57b04ec6..80c59d92 100644 --- a/src/main/java/com/zixiken/dimdoors/shared/util/Schematic.java +++ b/src/main/java/com/zixiken/dimdoors/shared/util/Schematic.java @@ -30,7 +30,15 @@ public class Schematic { private static final String[] oldDimDoorBlockNames = new String[]{ "Fabric of RealityPerm", "Fabric of Reality", - "Warp Door"}; + "Warp Door", + "Dummy", + "Dummy", + "Dummy", + "Dummy", + "Dummy", + "Dummy", + "Dummy", + "Dummy"}; private static final String[] newDimDoorBlockNames = new String[]{ "blockDoorQuartz", @@ -290,7 +298,7 @@ public class Schematic { public static Schematic loadOldDimDoorSchematicFromNBT(NBTTagCompound nbt) { //@todo, maybe make this a separate class, so values can be final so they HAVE TO be set in a newly designed constructor Schematic schematic = new Schematic(); - schematic.version = Integer.parseInt("${spongeSchematicVersion}"); //set in build.gradle + schematic.version = Integer.parseInt("1"); //@todo set in build.gradle ${spongeSchematicVersion} schematic.author = "Robijnvogel"; schematic.schematicName = "This schematic was converted from an MC 1.7.10 DimDoors schematic"; schematic.creationDate = System.currentTimeMillis(); diff --git a/src/main/resources/assets/dimdoors/pockets/json/defaultPersonal.json b/src/main/resources/assets/dimdoors/pockets/json/defaultPersonal.json new file mode 100644 index 00000000..41cff6a1 --- /dev/null +++ b/src/main/resources/assets/dimdoors/pockets/json/defaultPersonal.json @@ -0,0 +1,13 @@ +{ + "jsonType": "Singular", + "pocketType" : 0, + "variations": [ + { + "variantName": "defaultPersonal_3", + "size": 3, + "minDepth": 0, + "maxDepth": 0, + "weights": [100] + } + ] +} diff --git a/src/main/resources/assets/dimdoors/pockets/json/defaultPublic.json b/src/main/resources/assets/dimdoors/pockets/json/defaultPublic.json new file mode 100644 index 00000000..fa184a28 --- /dev/null +++ b/src/main/resources/assets/dimdoors/pockets/json/defaultPublic.json @@ -0,0 +1,13 @@ +{ + "jsonType": "Singular", + "pocketType" : 1, + "variations": [ + { + "variantName": "defaultPublic_3", + "size": 3, + "minDepth": 0, + "maxDepth": 0, + "weights": [100] + } + ] +} diff --git a/src/main/resources/assets/dimdoors/pockets/schematic/defaultPersonal_3.schematic b/src/main/resources/assets/dimdoors/pockets/schematic/defaultPersonal_3.schematic new file mode 100644 index 0000000000000000000000000000000000000000..de2d9c74264d12d200978559a5eaf31c900afcdd GIT binary patch literal 890 zcmb2|=3oE;7KXQX&*uq;OB{PBT>k0!;%(g1VnZS~z7pNAc+m=OEk%c>w^!s&aHzYT z^H^bi>Z?eH&<5jYE|=B*`Te#D5@E07UhH2bex&e~Nv)lArvL1Hvt^gP3cPHtSz9p` zY(9{`Uhmm~q{mndRAY9!93mb+QZ-EALyw ztU;#}x-+eQyi9}|DQU!W7;}Ee%@vC=8W)n*ZfVG*H2-V^?>*U-y4v@4cwd z(5*|ms&c1#g+^^X=XQV3FPjV3!>zt-4b=+G44u|1w{BJM`Rvdu*{efVU0S6hZ5t8F z+`lg*aQ)h#m0qhi<$MVU&8?LAb4zRLsx4Zl-S|VV9y{(P`W4O(5#L<%b64N1C}gql z)i#m8U#)^#J2iDN`>L=1?`^mUbl5hK9U(vGE%aKtYcklted#NLR=!(uYSYE$sn^|s z&e**IXl=pM=RBcWS7k#Y&8obg{4WcT5Ew0P+qm-df3X4rJ@BFj!roM|^sz3Yc?#J%(^2_A+tGu6n+Vz+8&5eSs&m@=_07^oz Ai2wiq literal 0 HcmV?d00001