diff --git a/src/main/java/ninja/bytecode/iris/Iris.java b/src/main/java/ninja/bytecode/iris/Iris.java index d38dec12d..e7b0e8d82 100644 --- a/src/main/java/ninja/bytecode/iris/Iris.java +++ b/src/main/java/ninja/bytecode/iris/Iris.java @@ -1,18 +1,22 @@ package ninja.bytecode.iris; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.util.Enumeration; import java.util.UUID; import java.util.function.Function; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; import org.bukkit.Bukkit; import org.bukkit.Chunk; import org.bukkit.GameMode; import org.bukkit.Location; -import org.bukkit.Material; import org.bukkit.World; import org.bukkit.WorldCreator; -import org.bukkit.block.Biome; import org.bukkit.entity.Player; import org.bukkit.event.HandlerList; import org.bukkit.event.Listener; @@ -31,7 +35,9 @@ import ninja.bytecode.iris.util.Direction; import ninja.bytecode.shuriken.bench.Profiler; import ninja.bytecode.shuriken.collections.GMap; import ninja.bytecode.shuriken.collections.GSet; +import ninja.bytecode.shuriken.execution.J; import ninja.bytecode.shuriken.execution.TaskExecutor; +import ninja.bytecode.shuriken.format.F; import ninja.bytecode.shuriken.io.IO; import ninja.bytecode.shuriken.json.JSONException; import ninja.bytecode.shuriken.json.JSONObject; @@ -60,16 +66,18 @@ public class Iris extends JavaPlugin implements Listener values = new GMap<>(); instance = this; settings = new Settings(); + J.attempt(() -> createTempCache()); loadContent(); + processContent(); gen = new IrisGenerator(); genPool = new TaskExecutor(getTC(), settings.performance.threadPriority, "Iris Generator"); getServer().getPluginManager().registerEvents((Listener) this, this); getCommand("iris").setExecutor(new CommandIris()); getCommand("ish").setExecutor(new CommandIsh()); new WandManager(); - + // Debug world regens - + if(settings.performance.loadonstart) { GSet ws = new GSet<>(); @@ -90,25 +98,74 @@ public class Iris extends JavaPlugin implements Listener } } } + + private void processContent() + { + L.v("Processing Content"); + + for(SchematicGroup i : schematics.v()) + { + i.processVariants(); + } + } + + private static File internalResource(String resource) + { + return new File(System.getProperty("java.io.tmpdir") + "/Iris/" + resource); + } + + private void createTempCache() throws Throwable + { + File temp = new File(System.getProperty("java.io.tmpdir") + "/Iris/"); + temp.mkdirs(); + L.i("Iris Cache: " + temp.getAbsolutePath()); + ZipFile zipFile = new ZipFile(getFile()); + Enumeration entries = zipFile.entries(); + while(entries.hasMoreElements()) + { + ZipEntry entry = entries.nextElement(); + if(entry.getName().startsWith("pack/") && !entry.isDirectory()) + { + File f = new File(temp, entry.getName()); + f.getParentFile().mkdirs(); + InputStream stream = zipFile.getInputStream(entry); + FileOutputStream fos = new FileOutputStream(f); + IO.fullTransfer(stream, fos, 16921); + fos.close(); + stream.close(); + } + } + + zipFile.close(); + } private void loadContent() { L.i("Loading Content"); - + try { IrisPack master = new IrisPack(loadJSON("pack/manifest.json")); master.load(); } - + catch(Throwable e) { e.printStackTrace(); } + int m = 0; + + for(SchematicGroup i : schematics.v()) + { + m+=i.size(); + } L.i("Dimensions: " + dimensions.size()); L.i("Biomes: " + biomes.size()); - L.i("Schematic Groups: " + schematics.size()); + L.i("Object Groups: " + schematics.size()); + L.i("Objects: " + F.f(m)); + + L.flush(); } @@ -164,33 +221,74 @@ public class Iris extends JavaPlugin implements Listener values.get(w).put(t, d); } - + public static IrisDimension loadDimension(String s) throws JSONException, IOException { L.i("Loading Iris Dimension " + s); return new IrisDimension(loadJSON("pack/dimensions/" + s + ".json")); } - + public static IrisBiome loadBiome(String s) throws JSONException, IOException { L.i("Loading Iris Biome " + s); return new IrisBiome(loadJSON("pack/biomes/" + s + ".json")); } + public static SchematicGroup loadSchematicGroup(String s) + { + SchematicGroup g = SchematicGroup.load("pack/objects/" + s); + + if(g != null) + { + schematics.put(s, g); + L.i("Loaded Object Group: " + g.getName() + " (" + g.getSchematics().size() + " Objects)"); + return g; + } + + L.i("Cannot load Object Group: " + s); + + return null; + } + public static Schematic loadSchematic(String s) throws IOException { L.i("Loading Iris Object " + s); return Schematic.load(loadResource("pack/objects/" + s + ".ish")); } - + public static JSONObject loadJSON(String s) throws JSONException, IOException { return new JSONObject(IO.readAll(loadResource(s))); } - public static InputStream loadResource(String string) + public static File loadFolder(String string) { - L.v("Loading Resource: " + "Iris.jar/" + string); - return Iris.class.getResourceAsStream("/" + string); + File internal = internalResource(string); + + if(internal.exists()) + { + L.v("Loading Group: " + string); + return internal; + } + + L.f("Cannot find folder: " + internal.getAbsolutePath()); + return null; + } + + public static InputStream loadResource(String string) throws IOException + { + File internal = internalResource(string); + + if(internal.exists()) + { + L.v("Loading Resource: " + "Iris/" + string); + return new FileInputStream(internal); + } + + else + { + L.f("Cannot find Resource: " + string); + return null; + } } } diff --git a/src/main/java/ninja/bytecode/iris/generator/IrisGenerator.java b/src/main/java/ninja/bytecode/iris/generator/IrisGenerator.java index b8f2b6773..1a580867b 100644 --- a/src/main/java/ninja/bytecode/iris/generator/IrisGenerator.java +++ b/src/main/java/ninja/bytecode/iris/generator/IrisGenerator.java @@ -1,7 +1,5 @@ package ninja.bytecode.iris.generator; -import java.io.File; -import java.io.IOException; import java.util.List; import java.util.Random; @@ -34,8 +32,6 @@ import ninja.bytecode.iris.util.MB; import ninja.bytecode.iris.util.ParallelChunkGenerator; import ninja.bytecode.shuriken.collections.GList; import ninja.bytecode.shuriken.collections.GMap; -import ninja.bytecode.shuriken.execution.J; -import ninja.bytecode.shuriken.io.IO; import ninja.bytecode.shuriken.logging.L; import ninja.bytecode.shuriken.math.M; import ninja.bytecode.shuriken.math.RNG; @@ -260,73 +256,7 @@ public class IrisGenerator extends ParallelChunkGenerator private SchematicGroup loadSchematics(String folder) { - if(schematicCache.containsKey(folder)) - { - return schematicCache.get(folder); - } - - File f = new File(Iris.instance.getDataFolder(), "objects/" + folder); - GList s = new GList<>(); - GList flags = new GList<>(); - - if(f.exists() && f.isDirectory()) - { - for(File i : f.listFiles()) - { - if(i.isFile() && i.getName().endsWith(".ifl")) - { - try - { - flags.add(IO.readAll(i).split("\\Q\n\\E")); - } - - catch(IOException e) - { - e.printStackTrace(); - } - } - - if(i.isFile() && i.getName().endsWith(".ish")) - { - J.attempt(() -> - { - Schematic sc = Schematic.load(i); - s.add(sc); - }); - } - } - } - - for(String i : flags) - { - String flag = i.trim().toLowerCase(); - - if(flag.equals("center")) - { - for(Schematic j : s) - { - j.setCenteredHeight(); - } - - L.i("Centered " + s.size() + " Schematics"); - } - } - - L.i("Loaded " + s.size() + " Schematics in " + folder); - SchematicGroup g = new SchematicGroup(folder); - g.setSchematics(s); - g.setFlags(flags); - - for(String i : flags) - { - if(i.startsWith("priority=")) - { - J.attempt(() -> g.setPriority(Integer.valueOf(i.split("\\Q=\\E")[1]).intValue())); - } - } - - schematicCache.put(folder, g); - return g; + return Iris.schematics.get(folder); } private double getBiomedHeight(int x, int z, ChunkPlan plan) diff --git a/src/main/java/ninja/bytecode/iris/schematic/Schematic.java b/src/main/java/ninja/bytecode/iris/schematic/Schematic.java index 3319573c6..0215262dd 100644 --- a/src/main/java/ninja/bytecode/iris/schematic/Schematic.java +++ b/src/main/java/ninja/bytecode/iris/schematic/Schematic.java @@ -17,6 +17,7 @@ import org.bukkit.util.BlockVector; import ninja.bytecode.iris.Iris; import ninja.bytecode.iris.util.Catalyst12; import ninja.bytecode.iris.util.MB; +import ninja.bytecode.shuriken.collections.GList; import ninja.bytecode.shuriken.collections.GMap; import ninja.bytecode.shuriken.io.CustomOutputStream; import ninja.bytecode.shuriken.logging.L; @@ -28,6 +29,7 @@ public class Schematic private int h; private int d; private final GMap s; + private BlockVector mount; public Schematic(int w, int h, int d) { @@ -38,6 +40,57 @@ public class Schematic centeredHeight = false; } + public void computeMountShift() + { + int ly = Integer.MAX_VALUE; + + for(BlockVector i : s.k()) + { + if(i.getBlockY() < ly) + { + ly = i.getBlockY(); + } + } + + + GList fmount = new GList<>(); + + for(BlockVector i : s.k()) + { + if(i.getBlockY() == ly) + { + fmount.add(i); + } + } + + double avx[] = new double[fmount.size()]; + double avy[] = new double[fmount.size()]; + double avz[] = new double[fmount.size()]; + int c = 0; + + for(BlockVector i : fmount) + { + avx[c] = i.getBlockX(); + avy[c] = i.getBlockY(); + avz[c] = i.getBlockZ(); + c++; + } + + mount = new BlockVector(avg(avx), avg(avy), avg(avz)); + } + + private int avg(double[] v) + { + double g = 0; + + for(int i = 0; i < v.length; i++) + { + g+=v[i]; + } + + return (int) Math.round(g / (double) v.length); + } + public void setCenteredHeight() { this.centeredHeight = true; @@ -170,7 +223,7 @@ public class Schematic } } } - + public static Schematic load(InputStream in) throws IOException { Schematic s = new Schematic(1, 1, 1); @@ -185,7 +238,6 @@ public class Schematic Schematic s = new Schematic(1, 1, 1); FileInputStream fin = new FileInputStream(f); s.read(fin); - L.i("Loaded Schematic: " + f.getPath() + " Size: " + s.getSchematic().size()); return s; diff --git a/src/main/java/ninja/bytecode/iris/schematic/SchematicGroup.java b/src/main/java/ninja/bytecode/iris/schematic/SchematicGroup.java index 8bd24a1bf..bff689301 100644 --- a/src/main/java/ninja/bytecode/iris/schematic/SchematicGroup.java +++ b/src/main/java/ninja/bytecode/iris/schematic/SchematicGroup.java @@ -1,6 +1,11 @@ package ninja.bytecode.iris.schematic; +import java.io.File; +import java.io.IOException; + +import ninja.bytecode.iris.Iris; import ninja.bytecode.shuriken.collections.GList; +import ninja.bytecode.shuriken.logging.L; public class SchematicGroup { @@ -61,4 +66,41 @@ public class SchematicGroup { return getSchematics().size(); } + + public static SchematicGroup load(String string) + { + File folder = Iris.loadFolder(string); + + if(folder != null) + { + SchematicGroup g = new SchematicGroup(string); + + for(File i : folder.listFiles()) + { + if(i.getName().endsWith(".ish")) + { + try + { + Schematic s = Schematic.load(i); + g.getSchematics().add(s); + } + + catch(IOException e) + { + L.f("Cannot load Schematic: " + string + "/" + i.getName()); + L.ex(e); + } + } + } + + return g; + } + + return null; + } + + public void processVariants() + { + L.v("Processing " + name + " Objects"); + } } diff --git a/src/main/java/ninja/bytecode/iris/spec/IrisBiome.java b/src/main/java/ninja/bytecode/iris/spec/IrisBiome.java index bf7e56024..16b95c248 100644 --- a/src/main/java/ninja/bytecode/iris/spec/IrisBiome.java +++ b/src/main/java/ninja/bytecode/iris/spec/IrisBiome.java @@ -5,6 +5,7 @@ import java.lang.reflect.Field; import org.bukkit.Material; import org.bukkit.block.Biome; +import ninja.bytecode.iris.Iris; import ninja.bytecode.iris.util.MB; import ninja.bytecode.iris.util.PolygonGenerator; import ninja.bytecode.shuriken.collections.GList; @@ -97,7 +98,14 @@ public class IrisBiome J.attempt(() -> scatterChance = scatterFromJSON(o.getJSONArray("scatter"))); J.attempt(() -> simplexScatter = o.getString("surfaceType").equalsIgnoreCase("simplex")); J.attempt(() -> scatterSurface = o.getString("surfaceType").equalsIgnoreCase("scatter")); - J.attempt(() -> schematicGroups = strFromJSON(o.getJSONArray("schematics"))); + J.attempt(() -> { + schematicGroups = strFromJSON(o.getJSONArray("objects")); + + for(String i : schematicGroups.k()) + { + Iris.loadSchematicGroup(i); + } + }); } public JSONObject toJSON() @@ -110,7 +118,7 @@ public class IrisBiome J.attempt(() -> j.put("dirt", mbListToJSON(dirt))); J.attempt(() -> j.put("scatter", scatterToJson(scatterChance))); J.attempt(() -> j.put("surfaceType", simplexScatter ? "simplex" : scatterSurface ? "scatter" : "na")); - J.attempt(() -> j.put("schematics", strToJson(schematicGroups))); + J.attempt(() -> j.put("objects", strToJson(schematicGroups))); return j; } diff --git a/src/main/resources/pack/biomes/desert.json b/src/main/resources/pack/biomes/desert.json index cdf67b0de..9fc342864 100644 --- a/src/main/resources/pack/biomes/desert.json +++ b/src/main/resources/pack/biomes/desert.json @@ -9,5 +9,9 @@ ], "scatter":[ "DEAD_BUSH=0.008" + ], + "objects": [ + "tree/palm/medium=0.25", + "tree/palm/small=2.5" ] } \ No newline at end of file