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
This commit is contained in:
Mathijs Riezebos 2017-01-26 19:33:31 +01:00
parent 34c18d9ce5
commit 45c9487874
9 changed files with 129 additions and 31 deletions

View file

@ -14,7 +14,7 @@ apply plugin: 'net.minecraftforge.gradle.forge'
ext.modversion = "3.0.0-a1" ext.modversion = "3.0.0-a1"
ext.mcversion = "1.10.2" ext.mcversion = "1.10.2"
ext.forgeversion = "12.18.3.2221" ext.forgeversion = "12.18.3.2221"
ext.spongeSchematicVersion = "1" //spongeSchematicVersion = "1"
version = mcversion + "-" + modversion version = mcversion + "-" + modversion
group= "com.zixiken.dimdoors" // http://maven.apache.org/guides/mini/guide-naming-conventions.html group= "com.zixiken.dimdoors" // http://maven.apache.org/guides/mini/guide-naming-conventions.html
@ -30,7 +30,7 @@ minecraft {
runDir = "run" runDir = "run"
mappings = "stable_29" mappings = "stable_29"
replace '${version}', project.version 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. // makeObfSourceJar = false // an Srg named sources jar is made by default. uncomment this to disable.
} }

View file

@ -2,6 +2,7 @@ package com.zixiken.dimdoors;
import com.zixiken.dimdoors.shared.DDConfig; import com.zixiken.dimdoors.shared.DDConfig;
import com.zixiken.dimdoors.shared.DDProxyCommon; import com.zixiken.dimdoors.shared.DDProxyCommon;
import com.zixiken.dimdoors.shared.PocketRegistry;
import com.zixiken.dimdoors.shared.items.ModItems; import com.zixiken.dimdoors.shared.items.ModItems;
import com.zixiken.dimdoors.shared.PocketSavedData; import com.zixiken.dimdoors.shared.PocketSavedData;
import com.zixiken.dimdoors.shared.RiftRegistry; import com.zixiken.dimdoors.shared.RiftRegistry;
@ -58,8 +59,9 @@ public class DimDoors {
public void serverLoad(FMLServerStartingEvent event) { public void serverLoad(FMLServerStartingEvent event) {
//@todo event.registerServerCommand( new DDCommand() ); //to register commands that this mod offers? //@todo event.registerServerCommand( new DDCommand() ); //to register commands that this mod offers?
RiftRegistry.Instance.reset(); RiftRegistry.Instance.reset();
PocketSavedData.get(getDefWorld()); PocketRegistry.Instance.reset();
RiftSavedData.get(getDefWorld()); RiftSavedData.get(getDefWorld());
PocketSavedData.get(getDefWorld());
SchematicHandler.Instance.loadSchematics(); SchematicHandler.Instance.loadSchematics();
} }

View file

@ -47,11 +47,7 @@ public class DDConfig {
// Load config // Load config
configurationFolder = new File(event.getModConfigurationDirectory(), "/DimDoors"); configurationFolder = new File(event.getModConfigurationDirectory(), "/DimDoors");
if (!configurationFolder.exists()) { if (!configurationFolder.exists()) {
try { configurationFolder.mkdirs();
configurationFolder.createNewFile();
} catch (IOException ex) {
Logger.getLogger(DDConfig.class.getName()).log(Level.SEVERE, "Dimdoors config folder could not be created.", ex);
}
} }
Configuration config = new Configuration(event.getSuggestedConfigurationFile()); Configuration config = new Configuration(event.getSuggestedConfigurationFile());
config.load(); config.load();

View file

@ -54,9 +54,6 @@ public class PocketSavedData extends DDSavedData {
@Override @Override
public void readFromNBT(NBTTagCompound pocketnbt) { public void readFromNBT(NBTTagCompound pocketnbt) {
// Reset
PocketRegistry.Instance.reset();
// Load NBT // Load NBT
if (pocketnbt != null) { if (pocketnbt != null) {
if (pocketnbt.hasKey("pockets")) { if (pocketnbt.hasKey("pockets")) {

View file

@ -11,8 +11,11 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
import com.zixiken.dimdoors.DimDoors; import com.zixiken.dimdoors.DimDoors;
import java.io.DataInputStream;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
@ -22,8 +25,10 @@ import java.util.List;
import java.util.Random; import java.util.Random;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.zip.GZIPInputStream;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.CompressedStreamTools; import net.minecraft.nbt.CompressedStreamTools;
import org.apache.commons.io.IOUtils;
/** /**
* *
@ -86,38 +91,100 @@ public class SchematicHandler {
} }
} }
private List<PocketTemplate> 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<PocketTemplate> 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 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"); File schematicFolder = new File(DDConfig.configurationFolder, "/Schematics");
String jsonString = null; if (!schematicFolder.exists()) {
try { schematicFolder.mkdirs();
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); //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(); JsonParser parser = new JsonParser();
JsonElement jsonElement = parser.parse(jsonString); JsonElement jsonElement = parser.parse(jsonString);
JsonObject jsonTemplate = jsonElement.getAsJsonObject(); 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<PocketTemplate> validTemplates = getAllValidVariations(jsonTemplate, maxPocketSize); List<PocketTemplate> 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. 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 schematicFile = new File(schematicFolder, "/" + template.getName() + ".schem");
File oldVersionSchematicFile = new File(schematicFolder, "/" + template.getName() + ".schem");
NBTTagCompound schematicNBT; NBTTagCompound schematicNBT;
Schematic schematic = null; Schematic schematic = null;
try { if (schematicStream != null) {
schematicNBT = CompressedStreamTools.read(schematicFile); try {
schematic = Schematic.loadFromNBT(schematicNBT); GZIPInputStream schematicZipStream = new GZIPInputStream(schematicStream);
} catch (IOException ex) { schematicNBT = CompressedStreamTools.read(new DataInputStream(schematicZipStream));
Logger.getLogger(SchematicHandler.class.getName()).log(Level.SEVERE, "Schematic file for schematic " + template.getName() + " was not found in template folder.", ex); 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; 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); template.setSchematic(schematic);
} }
//@todo check for json files in both directories (inside the mod jar, and inside the dimdoors config folder)
return validTemplates; return validTemplates;
} }
@ -127,14 +194,14 @@ public class SchematicHandler {
return new String(encoded, encoding); return new String(encoded, encoding);
} }
private List<PocketTemplate> getAllValidVariations(JsonObject jsonTemplate, int maxPocketSize) { private static List<PocketTemplate> getAllValidVariations(JsonObject jsonTemplate, int maxPocketSize) {
String jsonType = jsonTemplate.get("jsonType").getAsString(); String jsonType = jsonTemplate.get("jsonType").getAsString();
EnumPocketType pocketType = EnumPocketType.getFromInt(jsonTemplate.get("pocketType").getAsInt()); EnumPocketType pocketType = EnumPocketType.getFromInt(jsonTemplate.get("pocketType").getAsInt());
JsonArray variations = jsonTemplate.getAsJsonArray("variations"); JsonArray variations = jsonTemplate.getAsJsonArray("variations");
List<PocketTemplate> pocketTemplates = new ArrayList(); List<PocketTemplate> pocketTemplates = new ArrayList();
JsonObject chosenVariation = null; //only applicable if jsonType == "Singular" 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<JsonObject> validVariations = new ArrayList(); List<JsonObject> validVariations = new ArrayList();
//put all valid variation JsonObjects into an array list //put all valid variation JsonObjects into an array list
for (int i = 0; i < variations.size(); i++) { for (int i = 0; i < variations.size(); i++) {
@ -142,6 +209,7 @@ public class SchematicHandler {
int variationSize = variation.get("size").getAsInt(); int variationSize = variation.get("size").getAsInt();
if (variationSize > maxPocketSize) { if (variationSize > maxPocketSize) {
DimDoors.log(SchematicHandler.class, "Checkpoint 2 reached; Variation size " + variationSize + " is bigger than maxPocketSize " + maxPocketSize + ".");
//do not add it //do not add it
} else if (jsonType.equals("Singular")) { } else if (jsonType.equals("Singular")) {
if (variationSize > chosenVariationSize) { if (variationSize > chosenVariationSize) {
@ -154,12 +222,13 @@ public class SchematicHandler {
} else if (jsonType.equals("Multiple")) { } else if (jsonType.equals("Multiple")) {
validVariations.add(variation); validVariations.add(variation);
} else { //@todo more options? } 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) { if (chosenVariation != null) {
validVariations.add(chosenVariation); 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 //convert the valid variations arraylist to a list of pocket templates
for (JsonObject variation : validVariations) { for (JsonObject variation : validVariations) {

View file

@ -30,7 +30,15 @@ public class Schematic {
private static final String[] oldDimDoorBlockNames = new String[]{ private static final String[] oldDimDoorBlockNames = new String[]{
"Fabric of RealityPerm", "Fabric of RealityPerm",
"Fabric of Reality", "Fabric of Reality",
"Warp Door"}; "Warp Door",
"Dummy",
"Dummy",
"Dummy",
"Dummy",
"Dummy",
"Dummy",
"Dummy",
"Dummy"};
private static final String[] newDimDoorBlockNames = new String[]{ private static final String[] newDimDoorBlockNames = new String[]{
"blockDoorQuartz", "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 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 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.author = "Robijnvogel";
schematic.schematicName = "This schematic was converted from an MC 1.7.10 DimDoors schematic"; schematic.schematicName = "This schematic was converted from an MC 1.7.10 DimDoors schematic";
schematic.creationDate = System.currentTimeMillis(); schematic.creationDate = System.currentTimeMillis();

View file

@ -0,0 +1,13 @@
{
"jsonType": "Singular",
"pocketType" : 0,
"variations": [
{
"variantName": "defaultPersonal_3",
"size": 3,
"minDepth": 0,
"maxDepth": 0,
"weights": [100]
}
]
}

View file

@ -0,0 +1,13 @@
{
"jsonType": "Singular",
"pocketType" : 1,
"variations": [
{
"variantName": "defaultPublic_3",
"size": 3,
"minDepth": 0,
"maxDepth": 0,
"weights": [100]
}
]
}