From 6179dbda8a11cc6d6db763f6641c9408fc357a2d Mon Sep 17 00:00:00 2001 From: Daniel Mills Date: Wed, 8 Jan 2020 18:53:08 -0500 Subject: [PATCH] Massive enhancements --- .../java/ninja/bytecode/iris/CommandIris.java | 29 +- .../java/ninja/bytecode/iris/CommandIsh.java | 56 ++-- src/main/java/ninja/bytecode/iris/Iris.java | 301 ++--------------- .../java/ninja/bytecode/iris/Settings.java | 4 +- .../java/ninja/bytecode/iris/WandManager.java | 156 --------- .../bytecode/iris/command/CommandIris.java | 132 ++++++++ .../bytecode/iris/command/CommandIsh.java | 261 +++++++++++++++ .../iris/controller/DebugController.java | 67 ++++ .../iris/controller/ExecutionController.java | 55 ++++ .../iris/controller/PackController.java | 230 +++++++++++++ .../iris/controller/TimingsController.java | 36 +++ .../iris/controller/WandController.java | 304 ++++++++++++++++++ .../iris/controller/WorldController.java | 65 ++++ .../iris/generator/IrisGenerator.java | 23 +- .../iris/generator/genobject/GenObject.java | 18 +- .../genobject/GenObjectDecorator.java | 27 +- .../generator/genobject/GenObjectGroup.java | 33 +- .../iris/generator/layer/GenLayerBiome.java | 2 +- .../ninja/bytecode/iris/pack/IrisBiome.java | 5 +- .../bytecode/iris/pack/IrisDimension.java | 14 +- .../ninja/bytecode/iris/pack/IrisPack.java | 11 +- .../ninja/bytecode/iris/util/ChunkPlan.java | 8 +- .../bytecode/iris/util/IrisController.java | 10 + .../bytecode/iris/util/IrisControllerSet.java | 73 +++++ .../iris/util/ParallelChunkGenerator.java | 14 +- .../ninja/bytecode/iris/util/WandUtil.java | 157 --------- 26 files changed, 1393 insertions(+), 698 deletions(-) delete mode 100644 src/main/java/ninja/bytecode/iris/WandManager.java create mode 100644 src/main/java/ninja/bytecode/iris/command/CommandIris.java create mode 100644 src/main/java/ninja/bytecode/iris/command/CommandIsh.java create mode 100644 src/main/java/ninja/bytecode/iris/controller/DebugController.java create mode 100644 src/main/java/ninja/bytecode/iris/controller/ExecutionController.java create mode 100644 src/main/java/ninja/bytecode/iris/controller/PackController.java create mode 100644 src/main/java/ninja/bytecode/iris/controller/TimingsController.java create mode 100644 src/main/java/ninja/bytecode/iris/controller/WandController.java create mode 100644 src/main/java/ninja/bytecode/iris/controller/WorldController.java create mode 100644 src/main/java/ninja/bytecode/iris/util/IrisController.java create mode 100644 src/main/java/ninja/bytecode/iris/util/IrisControllerSet.java delete mode 100644 src/main/java/ninja/bytecode/iris/util/WandUtil.java diff --git a/src/main/java/ninja/bytecode/iris/CommandIris.java b/src/main/java/ninja/bytecode/iris/CommandIris.java index 878bc1e95..71137a10c 100644 --- a/src/main/java/ninja/bytecode/iris/CommandIris.java +++ b/src/main/java/ninja/bytecode/iris/CommandIris.java @@ -10,8 +10,10 @@ import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import ninja.bytecode.iris.controller.TimingsController; +import ninja.bytecode.iris.controller.WorldController; import ninja.bytecode.iris.generator.IrisGenerator; -import ninja.bytecode.iris.spec.IrisBiome; +import ninja.bytecode.iris.pack.IrisBiome; import ninja.bytecode.shuriken.format.F; public class CommandIris implements CommandExecutor @@ -28,7 +30,7 @@ public class CommandIris implements CommandExecutor { msg(sender, "/iris timings - Iris Timings"); msg(sender, "/iris rtp [biome] - RTP to a biome"); - msg(sender, "/iris gen - Gen a new Iris World"); + msg(sender, "/iris reload - Reload & Recompile"); msg(sender, "/ish - Iris Schematic Commands"); } @@ -36,8 +38,8 @@ public class CommandIris implements CommandExecutor { if(args[0].equalsIgnoreCase("timings")) { - double t = Iris.profiler.getResult("terrain").getAverage(); - double d = Iris.profiler.getResult("decor").getAverage(); + double t = Iris.getController(TimingsController.class).getResult("terrain"); + double d = Iris.getController(TimingsController.class).getResult("decor"); msg(sender, "Generation: " + ChatColor.BOLD + ChatColor.WHITE + F.duration(t + d, 2)); msg(sender, " \\Terrain: " + ChatColor.BOLD + ChatColor.WHITE + F.duration(t, 2)); msg(sender, " \\Decor: " + ChatColor.BOLD + ChatColor.WHITE + F.duration(d, 2)); @@ -104,23 +106,10 @@ public class CommandIris implements CommandExecutor } } - if(args[0].equalsIgnoreCase("gen")) + if(args[0].equalsIgnoreCase("reload")) { - if(sender instanceof Player) - { - World wold = ((Player) sender).getWorld(); - World w = Iris.instance.createIrisWorld(); - ((Player) sender).teleport(new Location(w, 0, 256, 0)); - ((Player) sender).setFlying(true); - ((Player) sender).setGameMode(GameMode.CREATIVE); - wold.setAutoSave(false); - Bukkit.unloadWorld(wold, false); - } - - else - { - Iris.instance.createIrisWorld(); - } + msg(sender, "Reloading Iris..."); + Bukkit.getScheduler().scheduleSyncDelayedTask(Iris.instance, () -> Iris.instance.reload()); } } diff --git a/src/main/java/ninja/bytecode/iris/CommandIsh.java b/src/main/java/ninja/bytecode/iris/CommandIsh.java index 26c801658..f502b9626 100644 --- a/src/main/java/ninja/bytecode/iris/CommandIsh.java +++ b/src/main/java/ninja/bytecode/iris/CommandIsh.java @@ -15,11 +15,11 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.util.Vector; -import ninja.bytecode.iris.schematic.Schematic; +import ninja.bytecode.iris.controller.WandController; +import ninja.bytecode.iris.generator.genobject.GenObject; import ninja.bytecode.iris.util.Cuboid; import ninja.bytecode.iris.util.Cuboid.CuboidDirection; import ninja.bytecode.iris.util.Direction; -import ninja.bytecode.iris.util.WandUtil; import ninja.bytecode.shuriken.format.F; public class CommandIsh implements CommandExecutor @@ -53,10 +53,10 @@ public class CommandIsh implements CommandExecutor Player p = (Player) sender; if(args[0].equalsIgnoreCase("wand")) { - p.getInventory().addItem(WandUtil.createWand()); + p.getInventory().addItem(WandController.createWand()); p.playSound(p.getLocation(), Sound.ITEM_ARMOR_EQUIP_DIAMOND, 1f, 1.55f); } - + if(args[0].equalsIgnoreCase("id")) { @@ -66,7 +66,7 @@ public class CommandIsh implements CommandExecutor if(args[0].equalsIgnoreCase("save")) { - Schematic s = WandUtil.createSchematic(p.getInventory().getItemInMainHand(), p.getLocation()); + GenObject s = WandController.createSchematic(p.getInventory().getItemInMainHand(), p.getLocation()); File f = new File(Iris.instance.getDataFolder(), "schematics/" + args[1] + ".ish"); f.getParentFile().mkdirs(); try @@ -85,7 +85,7 @@ public class CommandIsh implements CommandExecutor if(args[0].equalsIgnoreCase("load")) { - Schematic s = new Schematic(1, 1, 1); + GenObject s = new GenObject(1, 1, 1); File f = new File(Iris.instance.getDataFolder(), "schematics/" + args[1] + ".ish"); if(!f.exists()) { @@ -97,7 +97,7 @@ public class CommandIsh implements CommandExecutor { FileInputStream fin = new FileInputStream(f); s.read(fin); - + boolean cursor = false; for(String i : args) { @@ -107,15 +107,15 @@ public class CommandIsh implements CommandExecutor break; } } - + Location at = p.getLocation(); - + if(cursor) { at = p.getTargetBlock(null, 64).getLocation(); } - - WandUtil.pasteSchematic(s, at); + + WandController.pasteSchematic(s, at); p.playSound(p.getLocation(), Sound.BLOCK_ENCHANTMENT_TABLE_USE, 1f, 1.25f); msg(p, "Pasted " + args[1] + " (" + F.f(s.getSchematic().size()) + " Blocks Modified)"); } @@ -128,7 +128,7 @@ public class CommandIsh implements CommandExecutor if(args[0].equalsIgnoreCase("xup")) { - Location[] b = WandUtil.getCuboid(p.getInventory().getItemInMainHand()); + Location[] b = WandController.getCuboid(p.getInventory().getItemInMainHand()); b[0].add(new Vector(0, 1, 0)); b[1].add(new Vector(0, 1, 0)); Location a1 = b[0].clone(); @@ -141,7 +141,7 @@ public class CommandIsh implements CommandExecutor a2.add(new Vector(0, 1, 0)); cursor = new Cuboid(a1, a2); } - + a1.add(new Vector(0, -1, 0)); a2.add(new Vector(0, -1, 0)); b[0] = a1; @@ -153,14 +153,14 @@ public class CommandIsh implements CommandExecutor cursor = cursor.contract(CuboidDirection.West); b[0] = cursor.getLowerNE(); b[1] = cursor.getUpperSW(); - p.getInventory().setItemInMainHand(WandUtil.createWand(b[0], b[1])); + p.getInventory().setItemInMainHand(WandController.createWand(b[0], b[1])); p.updateInventory(); p.playSound(p.getLocation(), Sound.ENTITY_ITEMFRAME_ROTATE_ITEM, 1f, 0.55f); } - + if(args[0].equalsIgnoreCase("shrinkwrap") || args[0].equalsIgnoreCase("shrink")) { - Location[] b = WandUtil.getCuboid(p.getInventory().getItemInMainHand()); + Location[] b = WandController.getCuboid(p.getInventory().getItemInMainHand()); Location a1 = b[0].clone(); Location a2 = b[1].clone(); Cuboid cursor = new Cuboid(a1, a2); @@ -172,15 +172,15 @@ public class CommandIsh implements CommandExecutor cursor = cursor.contract(CuboidDirection.Down); b[0] = cursor.getLowerNE(); b[1] = cursor.getUpperSW(); - p.getInventory().setItemInMainHand(WandUtil.createWand(b[0], b[1])); + p.getInventory().setItemInMainHand(WandController.createWand(b[0], b[1])); p.updateInventory(); p.playSound(p.getLocation(), Sound.ENTITY_ITEMFRAME_ROTATE_ITEM, 1f, 0.55f); } - + if(args[0].equalsIgnoreCase("expand")) { int amt = Integer.valueOf(args[1]); - Location[] b = WandUtil.getCuboid(p.getInventory().getItemInMainHand()); + Location[] b = WandController.getCuboid(p.getInventory().getItemInMainHand()); Location a1 = b[0].clone(); Location a2 = b[1].clone(); Cuboid cursor = new Cuboid(a1, a2); @@ -188,15 +188,15 @@ public class CommandIsh implements CommandExecutor cursor = cursor.expand(d, amt); b[0] = cursor.getLowerNE(); b[1] = cursor.getUpperSW(); - p.getInventory().setItemInMainHand(WandUtil.createWand(b[0], b[1])); + p.getInventory().setItemInMainHand(WandController.createWand(b[0], b[1])); p.updateInventory(); p.playSound(p.getLocation(), Sound.ENTITY_ITEMFRAME_ROTATE_ITEM, 1f, 0.55f); } - + if(args[0].equalsIgnoreCase("shift")) { int amt = Integer.valueOf(args[1]); - Location[] b = WandUtil.getCuboid(p.getInventory().getItemInMainHand()); + Location[] b = WandController.getCuboid(p.getInventory().getItemInMainHand()); Location a1 = b[0].clone(); Location a2 = b[1].clone(); Direction d = Direction.closest(p.getLocation().getDirection()).reverse(); @@ -205,14 +205,14 @@ public class CommandIsh implements CommandExecutor Cuboid cursor = new Cuboid(a1, a2); b[0] = cursor.getLowerNE(); b[1] = cursor.getUpperSW(); - p.getInventory().setItemInMainHand(WandUtil.createWand(b[0], b[1])); + p.getInventory().setItemInMainHand(WandController.createWand(b[0], b[1])); p.updateInventory(); p.playSound(p.getLocation(), Sound.ENTITY_ITEMFRAME_ROTATE_ITEM, 1f, 0.55f); } if(args[0].equalsIgnoreCase("xvert")) { - Location[] b = WandUtil.getCuboid(p.getInventory().getItemInMainHand()); + Location[] b = WandController.getCuboid(p.getInventory().getItemInMainHand()); Location a1 = b[0].clone(); Location a2 = b[1].clone(); Location a1x = b[0].clone(); @@ -226,17 +226,17 @@ public class CommandIsh implements CommandExecutor a2.add(new Vector(0, 1, 0)); cursor = new Cuboid(a1, a2); } - + a1.add(new Vector(0, -1, 0)); a2.add(new Vector(0, -1, 0)); - + while(!cursorx.containsOnly(Material.AIR)) { a1x.add(new Vector(0, -1, 0)); a2x.add(new Vector(0, -1, 0)); cursorx = new Cuboid(a1x, a2x); } - + a1x.add(new Vector(0, 1, 0)); a2x.add(new Vector(0, 1, 0)); b[0] = a1; @@ -248,7 +248,7 @@ public class CommandIsh implements CommandExecutor cursor = cursor.contract(CuboidDirection.West); b[0] = cursor.getLowerNE(); b[1] = cursor.getUpperSW(); - p.getInventory().setItemInMainHand(WandUtil.createWand(b[0], b[1])); + p.getInventory().setItemInMainHand(WandController.createWand(b[0], b[1])); p.updateInventory(); p.playSound(p.getLocation(), Sound.ENTITY_ITEMFRAME_ROTATE_ITEM, 1f, 0.55f); } diff --git a/src/main/java/ninja/bytecode/iris/Iris.java b/src/main/java/ninja/bytecode/iris/Iris.java index 7ea4decde..643082d02 100644 --- a/src/main/java/ninja/bytecode/iris/Iris.java +++ b/src/main/java/ninja/bytecode/iris/Iris.java @@ -1,223 +1,76 @@ 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.World; -import org.bukkit.WorldCreator; -import org.bukkit.entity.Player; import org.bukkit.event.HandlerList; import org.bukkit.event.Listener; import org.bukkit.generator.ChunkGenerator; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.java.JavaPlugin; -import org.bukkit.util.Vector; -import net.md_5.bungee.api.ChatColor; +import ninja.bytecode.iris.controller.PackController; import ninja.bytecode.iris.generator.IrisGenerator; -import ninja.bytecode.iris.schematic.Schematic; -import ninja.bytecode.iris.schematic.SchematicGroup; -import ninja.bytecode.iris.spec.IrisBiome; -import ninja.bytecode.iris.spec.IrisDimension; -import ninja.bytecode.iris.spec.IrisPack; import ninja.bytecode.iris.util.Direction; -import ninja.bytecode.shuriken.bench.PrecisionStopwatch; -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.execution.TaskExecutor.TaskGroup; -import ninja.bytecode.shuriken.format.F; -import ninja.bytecode.shuriken.io.IO; -import ninja.bytecode.shuriken.json.JSONException; -import ninja.bytecode.shuriken.json.JSONObject; +import ninja.bytecode.iris.util.IrisController; +import ninja.bytecode.iris.util.IrisControllerSet; import ninja.bytecode.shuriken.logging.L; public class Iris extends JavaPlugin implements Listener { - public static GSet refresh = new GSet<>(); - public static Profiler profiler; - public static TaskExecutor genPool; - public static IrisGenerator gen; + public IrisControllerSet controllerSet; + public static Settings settings; public static Iris instance; - public static GMap>> values; - public static GMap dimensions; - public static GMap biomes; - public static GMap schematics; public void onEnable() { - L.consoleConsumer = (s) -> Bukkit.getConsoleSender().sendMessage(s); - Direction.calculatePermutations(); - dimensions = new GMap<>(); - biomes = new GMap<>(); - schematics = new GMap<>(); - profiler = new Profiler(512); - values = new GMap<>(); instance = this; - settings = new Settings(); - J.attempt(() -> createTempCache()); - loadContent(); - 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(); - loadComplete(); - } - - public static void started(String obj) - { - profiler.start(obj); - } - - public static void stopped(String obj) - { - profiler.stop(obj); - } - - private void loadComplete() - { - if(settings.performance.loadonstart) - { - GSet ws = new GSet<>(); - - World w = createIrisWorld(); - for(Player i : Bukkit.getOnlinePlayers()) - { - Location m = i.getLocation(); - ws.add(i.getWorld().getName()); - i.teleport(new Location(w, m.getX(), m.getY(), m.getZ(), m.getYaw(), m.getPitch())); - i.setFlying(true); - i.setGameMode(GameMode.SPECTATOR); - } - - for(String i : ws) - { - Bukkit.unloadWorld(i, false); - } - } - } - - private static File internalResource(String resource) - { - if(new File(Iris.instance.getDataFolder(), "pack").exists()) - { - return new File(Iris.instance.getDataFolder(), 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() - { - PrecisionStopwatch p = PrecisionStopwatch.start(); - L.i("Loading Content"); + controllerSet = new IrisControllerSet(); + L.consoleConsumer = (s) -> Bukkit.getConsoleSender().sendMessage(s); try { - IrisPack master = new IrisPack(loadJSON("pack/manifest.json")); - master.load(); + controllerSet.startControllers(getFile()); } - catch(Throwable e) + catch(IOException e) { - e.printStackTrace(); + L.ex(e); } - int m = 0; + L.i("Controllers: " + controllerSet.size()); - for(SchematicGroup i : schematics.v()) + Direction.calculatePermutations(); + settings = new Settings(); + getServer().getPluginManager().registerEvents((Listener) this, this); + getCommand("iris").setExecutor(new CommandIris()); + getCommand("ish").setExecutor(new CommandIsh()); + getController(PackController.class).createTempCache(getFile()); + + if(!settings.performance.debugMode) { - m += i.size(); + getController(PackController.class).loadContent(); } - - L.v(ChatColor.LIGHT_PURPLE + "Processing Content"); - - TaskExecutor exf = new TaskExecutor(settings.performance.compilerThreads, settings.performance.compilerPriority, "Iris Compiler"); - TaskGroup gg = exf.startWork(); - for(SchematicGroup i : schematics.v()) - { - gg.queue(i::processVariants); - } - gg.execute(); - exf.close(); - L.i(ChatColor.LIGHT_PURPLE + "Dimensions: " + ChatColor.WHITE + dimensions.size()); - L.i(ChatColor.LIGHT_PURPLE + "Biomes: " + ChatColor.WHITE + biomes.size()); - L.i(ChatColor.LIGHT_PURPLE + "Object Groups: " + ChatColor.WHITE + F.f(schematics.size())); - L.i(ChatColor.LIGHT_PURPLE + "Objects: " + ChatColor.WHITE + F.f(m)); - L.i(ChatColor.LIGHT_PURPLE + "Compilation Time: " + ChatColor.WHITE + F.duration(p.getMilliseconds(), 2)); - L.i(ChatColor.GREEN + "Iris Dimensions Successfully Compiled!"); - L.flush(); - } - - private int getTC() - { - switch(settings.performance.performanceMode) - { - case HALF_CPU: - return Math.max(Runtime.getRuntime().availableProcessors() / 2, 1); - case MATCH_CPU: - return Runtime.getRuntime().availableProcessors(); - case SINGLE_THREADED: - return 1; - case DOUBLE_CPU: - return Runtime.getRuntime().availableProcessors() * 2; - case UNLIMITED: - return -1; - case EXPLICIT: - return settings.performance.threadCount; - default: - break; - } - - return Math.max(Runtime.getRuntime().availableProcessors() / 2, 1); } public void onDisable() { - genPool.close(); + controllerSet.stopControllers(); HandlerList.unregisterAll((Plugin) this); + Bukkit.getScheduler().cancelTasks(this); + } + + public void reload() + { + onDisable(); + onEnable(); + } + + @SuppressWarnings("unchecked") + public static T getController(Class c) + { + return (T) instance.controllerSet.get(c); } @Override @@ -225,90 +78,4 @@ public class Iris extends JavaPlugin implements Listener { return new IrisGenerator(); } - - public World createIrisWorld() - { - World ww = Bukkit.createWorld(new WorldCreator("iris-worlds/" + UUID.randomUUID().toString()).generator(new IrisGenerator()).seed(5944323)); - ww.setSpawnFlags(false, false); - ww.setAutoSave(false); - ww.setKeepSpawnInMemory(false); - ww.setSpawnLocation(0, 256, 0); - return ww; - } - - public static void v(String w, String t, Function d) - { - if(!values.containsKey(w)) - { - values.put(w, new GMap<>()); - } - - values.get(w).put(t, d); - } - - public static IrisDimension loadDimension(String s) throws JSONException, IOException - { - return new IrisDimension(loadJSON("pack/dimensions/" + s + ".json")); - } - - public static IrisBiome loadBiome(String s) throws JSONException, IOException - { - 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); - return g; - } - - L.i("Cannot load Object Group: " + s); - - return null; - } - - public static Schematic loadSchematic(String s) throws IOException - { - 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 File loadFolder(String string) - { - File internal = internalResource(string); - - if(internal.exists()) - { - return internal; - } - - L.f(ChatColor.RED + "Cannot find folder: " + internal.getAbsolutePath()); - return null; - } - - public static InputStream loadResource(String string) throws IOException - { - File internal = internalResource(string); - - if(internal.exists()) - { - L.v(ChatColor.DARK_PURPLE + "Loading Resource: " + ChatColor.GRAY + internal.getAbsolutePath()); - L.flush(); - return new FileInputStream(internal); - } - - else - { - L.f(ChatColor.RED + "Cannot find Resource: " + ChatColor.YELLOW + internal.getAbsolutePath()); - return null; - } - } } diff --git a/src/main/java/ninja/bytecode/iris/Settings.java b/src/main/java/ninja/bytecode/iris/Settings.java index e65fe2b53..10f737ab2 100644 --- a/src/main/java/ninja/bytecode/iris/Settings.java +++ b/src/main/java/ninja/bytecode/iris/Settings.java @@ -12,8 +12,8 @@ public class Settings public PerformanceMode performanceMode = PerformanceMode.DOUBLE_CPU; public int threadCount = 1; public int threadPriority = Thread.MIN_PRIORITY; - public boolean loadonstart = true; - public int compilerThreads = 4; + public boolean debugMode = true; + public int compilerThreads = 12; public int compilerPriority = Thread.MAX_PRIORITY; public int decorationAccuracy = 1; } diff --git a/src/main/java/ninja/bytecode/iris/WandManager.java b/src/main/java/ninja/bytecode/iris/WandManager.java deleted file mode 100644 index 84c5ef616..000000000 --- a/src/main/java/ninja/bytecode/iris/WandManager.java +++ /dev/null @@ -1,156 +0,0 @@ -package ninja.bytecode.iris; - -import java.awt.Color; - -import org.bukkit.Bukkit; -import org.bukkit.Chunk; -import org.bukkit.Location; -import org.bukkit.Sound; -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.block.Action; -import org.bukkit.event.player.PlayerInteractEvent; -import org.bukkit.inventory.EquipmentSlot; -import org.bukkit.util.Vector; - -import ninja.bytecode.iris.util.ParticleEffect; -import ninja.bytecode.iris.util.ParticleRedstone; -import ninja.bytecode.iris.util.WandUtil; - -public class WandManager implements Listener -{ - @SuppressWarnings("deprecation") - public WandManager() - { - Bukkit.getPluginManager().registerEvents(this, Iris.instance); - Bukkit.getScheduler().scheduleSyncRepeatingTask(Iris.instance, () -> - { - for(Player i : Bukkit.getOnlinePlayers()) - { - tick(i); - } - - for(Chunk i : Iris.refresh) - { - i.getWorld().refreshChunk(i.getX(), i.getZ()); - } - - Iris.refresh.clear(); - }, 0, 2); - } - - @EventHandler - public void tick(Player p) - { - try - { - if(WandUtil.isWand(p.getInventory().getItemInMainHand())) - { - Location[] d = WandUtil.getCuboid(p.getInventory().getItemInMainHand()); - ParticleEffect.CRIT_MAGIC.display(0.1f, 1, d[0].clone().add(0.5, 0.5, 0.5).clone().add(Vector.getRandom().subtract(Vector.getRandom()).normalize().clone().multiply(0.65)), p); - ParticleEffect.CRIT.display(0.1f, 1, d[1].clone().add(0.5, 0.5, 0.5).clone().add(Vector.getRandom().subtract(Vector.getRandom()).normalize().clone().multiply(0.65)), p); - - if(!d[0].getWorld().equals(d[1].getWorld())) - { - return; - } - - if(d[0].distanceSquared(d[1]) > 64 * 64) - { - return; - } - - int minx = Math.min(d[0].getBlockX(), d[1].getBlockX()); - int miny = Math.min(d[0].getBlockY(), d[1].getBlockY()); - int minz = Math.min(d[0].getBlockZ(), d[1].getBlockZ()); - int maxx = Math.max(d[0].getBlockX(), d[1].getBlockX()); - int maxy = Math.max(d[0].getBlockY(), d[1].getBlockY()); - int maxz = Math.max(d[0].getBlockZ(), d[1].getBlockZ()); - - for(double j = minx - 1; j < maxx + 1; j += 0.25) - { - for(double k = miny - 1; k < maxy + 1; k += 0.25) - { - for(double l = minz - 1; l < maxz + 1; l += 0.25) - { - boolean jj = j == minx || j == maxx; - boolean kk = k == miny || k == maxy; - boolean ll = l == minz || l == maxz; - double aa = j; - double bb = k; - double cc = l; - - if((jj && kk) || (jj && ll) || (ll && kk)) - { - Vector push = new Vector(0, 0, 0); - - if(j == minx) - { - push.add(new Vector(-0.55, 0, 0)); - } - - if(k == miny) - { - push.add(new Vector(0, -0.55, 0)); - } - - if(l == minz) - { - push.add(new Vector(0, 0, -0.55)); - } - - if(j == maxx) - { - push.add(new Vector(0.55, 0, 0)); - } - - if(k == maxy) - { - push.add(new Vector(0, 0.55, 0)); - } - - if(l == maxz) - { - push.add(new Vector(0, 0, 0.55)); - } - - Location lv = new Location(d[0].getWorld(), aa, bb, cc).clone().add(0.5, 0.5, 0.5).clone().add(push); - int color = Color.getHSBColor((float) (0.5f + (Math.sin((aa + bb + cc + (p.getTicksLived() / 2)) / 20f) / 2)), 1, 1).getRGB(); - new ParticleRedstone().setColor(new Color(color)).play(lv, p); - } - } - } - } - } - } - - catch(Throwable e) - { - - } - } - - @EventHandler - public void on(PlayerInteractEvent e) - { - if(e.getHand().equals(EquipmentSlot.HAND) && WandUtil.isWand(e.getPlayer().getInventory().getItemInMainHand())) - { - if(e.getAction().equals(Action.LEFT_CLICK_BLOCK)) - { - e.setCancelled(true); - e.getPlayer().getInventory().setItemInMainHand(WandUtil.update(true, e.getClickedBlock().getLocation(), e.getPlayer().getInventory().getItemInMainHand())); - e.getPlayer().playSound(e.getClickedBlock().getLocation(), Sound.BLOCK_END_PORTAL_FRAME_FILL, 1f, 0.67f); - e.getPlayer().updateInventory(); - } - - else if(e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) - { - e.setCancelled(true); - e.getPlayer().getInventory().setItemInMainHand(WandUtil.update(false, e.getClickedBlock().getLocation(), e.getPlayer().getInventory().getItemInMainHand())); - e.getPlayer().playSound(e.getClickedBlock().getLocation(), Sound.BLOCK_END_PORTAL_FRAME_FILL, 1f, 1.17f); - e.getPlayer().updateInventory(); - } - } - } -} diff --git a/src/main/java/ninja/bytecode/iris/command/CommandIris.java b/src/main/java/ninja/bytecode/iris/command/CommandIris.java new file mode 100644 index 000000000..ba0e61c63 --- /dev/null +++ b/src/main/java/ninja/bytecode/iris/command/CommandIris.java @@ -0,0 +1,132 @@ +package ninja.bytecode.iris.command; + +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.GameMode; +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import ninja.bytecode.iris.Iris; +import ninja.bytecode.iris.controller.TimingsController; +import ninja.bytecode.iris.controller.WorldController; +import ninja.bytecode.iris.generator.IrisGenerator; +import ninja.bytecode.iris.pack.IrisBiome; +import ninja.bytecode.shuriken.format.F; + +public class CommandIris implements CommandExecutor +{ + public void msg(CommandSender s, String msg) + { + s.sendMessage(ChatColor.DARK_PURPLE + "[" + ChatColor.GRAY + "Iris" + ChatColor.DARK_PURPLE + "]" + ChatColor.GRAY + ": " + msg); + } + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) + { + if(args.length == 0) + { + msg(sender, "/iris timings - Iris Timings"); + msg(sender, "/iris rtp [biome] - RTP to a biome"); + msg(sender, "/iris gen - Gen a new Iris World"); + msg(sender, "/ish - Iris Schematic Commands"); + } + + if(args.length > 0) + { + if(args[0].equalsIgnoreCase("timings")) + { + double t = Iris.getController(TimingsController.class).getResult("terrain"); + double d = Iris.getController(TimingsController.class).getResult("decor"); + msg(sender, "Generation: " + ChatColor.BOLD + ChatColor.WHITE + F.duration(t + d, 2)); + msg(sender, " \\Terrain: " + ChatColor.BOLD + ChatColor.WHITE + F.duration(t, 2)); + msg(sender, " \\Decor: " + ChatColor.BOLD + ChatColor.WHITE + F.duration(d, 2)); + } + + if(args[0].equalsIgnoreCase("rtp")) + { + if(sender instanceof Player) + { + Player p = (Player) sender; + World w = p.getWorld(); + + if(w.getGenerator() instanceof IrisGenerator) + { + if(args.length > 1) + { + IrisGenerator g = (IrisGenerator) w.getGenerator(); + IrisBiome b = null; + for(IrisBiome i : g.getLoadedBiomes()) + { + if(args[1].toLowerCase().equals(i.getName().toLowerCase().replaceAll("\\Q \\E", "_"))) + { + b = i; + break; + } + } + + if(b == null) + { + msg(sender, "Unknown Biome: " + args[1]); + } + + else + { + msg(sender, "Looking for " + b.getName() + "..."); + boolean f = false; + for(int i = 0; i < 10000; i++) + { + int x = (int) ((int) (29999983 / 1.2) * Math.random()); + int z = (int) ((int) (29999983 / 1.2) * Math.random()); + + if(g.getBiome(x, z).equals(b)) + { + f = true; + p.teleport(w.getHighestBlockAt(x, z).getLocation()); + break; + } + } + + if(!f) + { + msg(sender, "Looked for " + b.getName() + " in 10,000 different locations and could not find it. Try again!"); + } + } + } + + else + { + int x = (int) ((int) (29999983 / 1.2) * Math.random()); + int z = (int) ((int) (29999983 / 1.2) * Math.random()); + p.teleport(w.getHighestBlockAt(x, z).getLocation()); + } + } + } + } + + if(args[0].equalsIgnoreCase("gen")) + { + if(sender instanceof Player) + { + World wold = ((Player) sender).getWorld(); + World w = Iris.getController(WorldController.class).createIrisWorld(null, 0, true); + ((Player) sender).teleport(new Location(w, 0, 256, 0)); + ((Player) sender).setFlying(true); + ((Player) sender).setGameMode(GameMode.CREATIVE); + wold.setAutoSave(false); + Bukkit.unloadWorld(wold, false); + } + + else + { + Iris.getController(WorldController.class).createIrisWorld(null, 0, true); + } + } + } + + return false; + } +} diff --git a/src/main/java/ninja/bytecode/iris/command/CommandIsh.java b/src/main/java/ninja/bytecode/iris/command/CommandIsh.java new file mode 100644 index 000000000..75eb72fd9 --- /dev/null +++ b/src/main/java/ninja/bytecode/iris/command/CommandIsh.java @@ -0,0 +1,261 @@ +package ninja.bytecode.iris.command; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; + +import org.bukkit.ChatColor; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.Sound; +import org.bukkit.block.Block; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.util.Vector; + +import ninja.bytecode.iris.Iris; +import ninja.bytecode.iris.controller.WandController; +import ninja.bytecode.iris.generator.genobject.GenObject; +import ninja.bytecode.iris.util.Cuboid; +import ninja.bytecode.iris.util.Cuboid.CuboidDirection; +import ninja.bytecode.iris.util.Direction; +import ninja.bytecode.shuriken.format.F; + +public class CommandIsh implements CommandExecutor +{ + public void msg(CommandSender s, String msg) + { + s.sendMessage(ChatColor.DARK_PURPLE + "[" + ChatColor.GRAY + "Iris" + ChatColor.DARK_PURPLE + "]" + ChatColor.GRAY + ": " + msg); + } + + @SuppressWarnings("deprecation") + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) + { + if(args.length == 0) + { + msg(sender, "/ish wand - Get an Iris Wand"); + msg(sender, "/ish save - Save Schematic"); + msg(sender, "/ish load [cursor] - Paste Schematic"); + msg(sender, "/ish expand - Expand Cuboid in direction"); + msg(sender, "/ish shift - Shift Cuboid in direction"); + msg(sender, "/ish shrinkwrap - Shrink to blocks"); + msg(sender, "/ish xup - Shift up, Expand up, Contract in."); + msg(sender, "/ish xvert - Expand up, Expand down, Contract in."); + msg(sender, "/ish id - What id am i looking at"); + } + + if(args.length > 0) + { + if(sender instanceof Player) + { + Player p = (Player) sender; + if(args[0].equalsIgnoreCase("wand")) + { + p.getInventory().addItem(WandController.createWand()); + p.playSound(p.getLocation(), Sound.ITEM_ARMOR_EQUIP_DIAMOND, 1f, 1.55f); + } + + if(args[0].equalsIgnoreCase("id")) + { + + Block b = p.getTargetBlock(null, 64); + msg(p, b.getType().getId() + ":" + b.getData() + " (" + b.getType().toString() + ":" + b.getData() + ")"); + } + + if(args[0].equalsIgnoreCase("save")) + { + GenObject s = WandController.createSchematic(p.getInventory().getItemInMainHand(), p.getLocation()); + File f = new File(Iris.instance.getDataFolder(), "schematics/" + args[1] + ".ish"); + f.getParentFile().mkdirs(); + try + { + FileOutputStream fos = new FileOutputStream(f); + s.write(fos); + msg(p, "Saved " + args[1] + " (" + F.f(s.getSchematic().size()) + " Entries)"); + p.playSound(p.getLocation(), Sound.BLOCK_ENCHANTMENT_TABLE_USE, 1f, 0.45f); + } + + catch(Throwable e1) + { + e1.printStackTrace(); + } + } + + if(args[0].equalsIgnoreCase("load")) + { + GenObject s = new GenObject(1, 1, 1); + File f = new File(Iris.instance.getDataFolder(), "schematics/" + args[1] + ".ish"); + if(!f.exists()) + { + msg(p, "Not Found"); + return true; + } + + try + { + FileInputStream fin = new FileInputStream(f); + s.read(fin); + + boolean cursor = false; + for(String i : args) + { + if(i.equalsIgnoreCase("cursor")) + { + cursor = true; + break; + } + } + + Location at = p.getLocation(); + + if(cursor) + { + at = p.getTargetBlock(null, 64).getLocation(); + } + + WandController.pasteSchematic(s, at); + p.playSound(p.getLocation(), Sound.BLOCK_ENCHANTMENT_TABLE_USE, 1f, 1.25f); + msg(p, "Pasted " + args[1] + " (" + F.f(s.getSchematic().size()) + " Blocks Modified)"); + } + + catch(Throwable e1) + { + e1.printStackTrace(); + } + } + + if(args[0].equalsIgnoreCase("xup")) + { + Location[] b = WandController.getCuboid(p.getInventory().getItemInMainHand()); + b[0].add(new Vector(0, 1, 0)); + b[1].add(new Vector(0, 1, 0)); + Location a1 = b[0].clone(); + Location a2 = b[1].clone(); + Cuboid cursor = new Cuboid(a1, a2); + + while(!cursor.containsOnly(Material.AIR)) + { + a1.add(new Vector(0, 1, 0)); + a2.add(new Vector(0, 1, 0)); + cursor = new Cuboid(a1, a2); + } + + a1.add(new Vector(0, -1, 0)); + a2.add(new Vector(0, -1, 0)); + b[0] = a1; + a2 = b[1]; + cursor = new Cuboid(a1, a2); + cursor = cursor.contract(CuboidDirection.North); + cursor = cursor.contract(CuboidDirection.South); + cursor = cursor.contract(CuboidDirection.East); + cursor = cursor.contract(CuboidDirection.West); + b[0] = cursor.getLowerNE(); + b[1] = cursor.getUpperSW(); + p.getInventory().setItemInMainHand(WandController.createWand(b[0], b[1])); + p.updateInventory(); + p.playSound(p.getLocation(), Sound.ENTITY_ITEMFRAME_ROTATE_ITEM, 1f, 0.55f); + } + + if(args[0].equalsIgnoreCase("shrinkwrap") || args[0].equalsIgnoreCase("shrink")) + { + Location[] b = WandController.getCuboid(p.getInventory().getItemInMainHand()); + Location a1 = b[0].clone(); + Location a2 = b[1].clone(); + Cuboid cursor = new Cuboid(a1, a2); + cursor = cursor.contract(CuboidDirection.North); + cursor = cursor.contract(CuboidDirection.South); + cursor = cursor.contract(CuboidDirection.East); + cursor = cursor.contract(CuboidDirection.West); + cursor = cursor.contract(CuboidDirection.Up); + cursor = cursor.contract(CuboidDirection.Down); + b[0] = cursor.getLowerNE(); + b[1] = cursor.getUpperSW(); + p.getInventory().setItemInMainHand(WandController.createWand(b[0], b[1])); + p.updateInventory(); + p.playSound(p.getLocation(), Sound.ENTITY_ITEMFRAME_ROTATE_ITEM, 1f, 0.55f); + } + + if(args[0].equalsIgnoreCase("expand")) + { + int amt = Integer.valueOf(args[1]); + Location[] b = WandController.getCuboid(p.getInventory().getItemInMainHand()); + Location a1 = b[0].clone(); + Location a2 = b[1].clone(); + Cuboid cursor = new Cuboid(a1, a2); + Direction d = Direction.closest(p.getLocation().getDirection()).reverse(); + cursor = cursor.expand(d, amt); + b[0] = cursor.getLowerNE(); + b[1] = cursor.getUpperSW(); + p.getInventory().setItemInMainHand(WandController.createWand(b[0], b[1])); + p.updateInventory(); + p.playSound(p.getLocation(), Sound.ENTITY_ITEMFRAME_ROTATE_ITEM, 1f, 0.55f); + } + + if(args[0].equalsIgnoreCase("shift")) + { + int amt = Integer.valueOf(args[1]); + Location[] b = WandController.getCuboid(p.getInventory().getItemInMainHand()); + Location a1 = b[0].clone(); + Location a2 = b[1].clone(); + Direction d = Direction.closest(p.getLocation().getDirection()).reverse(); + a1.add(d.toVector().multiply(amt)); + a2.add(d.toVector().multiply(amt)); + Cuboid cursor = new Cuboid(a1, a2); + b[0] = cursor.getLowerNE(); + b[1] = cursor.getUpperSW(); + p.getInventory().setItemInMainHand(WandController.createWand(b[0], b[1])); + p.updateInventory(); + p.playSound(p.getLocation(), Sound.ENTITY_ITEMFRAME_ROTATE_ITEM, 1f, 0.55f); + } + + if(args[0].equalsIgnoreCase("xvert")) + { + Location[] b = WandController.getCuboid(p.getInventory().getItemInMainHand()); + Location a1 = b[0].clone(); + Location a2 = b[1].clone(); + Location a1x = b[0].clone(); + Location a2x = b[1].clone(); + Cuboid cursor = new Cuboid(a1, a2); + Cuboid cursorx = new Cuboid(a1, a2); + + while(!cursor.containsOnly(Material.AIR)) + { + a1.add(new Vector(0, 1, 0)); + a2.add(new Vector(0, 1, 0)); + cursor = new Cuboid(a1, a2); + } + + a1.add(new Vector(0, -1, 0)); + a2.add(new Vector(0, -1, 0)); + + while(!cursorx.containsOnly(Material.AIR)) + { + a1x.add(new Vector(0, -1, 0)); + a2x.add(new Vector(0, -1, 0)); + cursorx = new Cuboid(a1x, a2x); + } + + a1x.add(new Vector(0, 1, 0)); + a2x.add(new Vector(0, 1, 0)); + b[0] = a1; + b[1] = a2x; + cursor = new Cuboid(b[0], b[1]); + cursor = cursor.contract(CuboidDirection.North); + cursor = cursor.contract(CuboidDirection.South); + cursor = cursor.contract(CuboidDirection.East); + cursor = cursor.contract(CuboidDirection.West); + b[0] = cursor.getLowerNE(); + b[1] = cursor.getUpperSW(); + p.getInventory().setItemInMainHand(WandController.createWand(b[0], b[1])); + p.updateInventory(); + p.playSound(p.getLocation(), Sound.ENTITY_ITEMFRAME_ROTATE_ITEM, 1f, 0.55f); + } + } + } + + return false; + } +} diff --git a/src/main/java/ninja/bytecode/iris/controller/DebugController.java b/src/main/java/ninja/bytecode/iris/controller/DebugController.java new file mode 100644 index 000000000..d20af96da --- /dev/null +++ b/src/main/java/ninja/bytecode/iris/controller/DebugController.java @@ -0,0 +1,67 @@ +package ninja.bytecode.iris.controller; + +import org.bukkit.Bukkit; +import org.bukkit.GameMode; +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.entity.Player; + +import ninja.bytecode.iris.Iris; +import ninja.bytecode.iris.util.IrisController; +import ninja.bytecode.shuriken.collections.GSet; +import ninja.bytecode.shuriken.execution.J; + +public class DebugController implements IrisController +{ + @Override + public void onStart() + { + Bukkit.getScheduler().scheduleSyncDelayedTask(Iris.instance, () -> + { + if(Iris.settings.performance.debugMode) + { + J.a(() -> + { + J.a(() -> + { + J.sleep(1000); + while(!Iris.getController(PackController.class).isReady()) + { + J.sleep(250); + } + + Bukkit.getScheduler().scheduleSyncDelayedTask(Iris.instance, () -> + { + if(Iris.settings.performance.debugMode) + { + GSet ws = new GSet<>(); + + World w = Iris.getController(WorldController.class).createIrisWorld(null, 0, true); + for(Player i : Bukkit.getOnlinePlayers()) + { + Location m = i.getLocation(); + ws.add(i.getWorld().getName()); + i.teleport(new Location(w, m.getX(), m.getY(), m.getZ(), m.getYaw(), m.getPitch())); + i.setFlying(true); + i.setGameMode(GameMode.SPECTATOR); + } + + for(String i : ws) + { + Bukkit.unloadWorld(i, false); + } + } + }, 1); + }); + Iris.getController(PackController.class).loadContent(); + }); + } + }, 1); + } + + @Override + public void onStop() + { + + } +} diff --git a/src/main/java/ninja/bytecode/iris/controller/ExecutionController.java b/src/main/java/ninja/bytecode/iris/controller/ExecutionController.java new file mode 100644 index 000000000..ee9b9918f --- /dev/null +++ b/src/main/java/ninja/bytecode/iris/controller/ExecutionController.java @@ -0,0 +1,55 @@ +package ninja.bytecode.iris.controller; + +import org.bukkit.World; + +import ninja.bytecode.iris.Iris; +import ninja.bytecode.iris.util.IrisController; +import ninja.bytecode.shuriken.collections.GMap; +import ninja.bytecode.shuriken.execution.TaskExecutor; + +public class ExecutionController implements IrisController +{ + GMap executors; + + @Override + public void onStart() + { + executors = new GMap<>(); + } + + @Override + public void onStop() + { + + } + + public TaskExecutor getExecutor(World world) + { + TaskExecutor x = new TaskExecutor(getTC(), Iris.settings.performance.threadPriority, "Iris Generator (" + world.getName() + ")"); + executors.put(world.getWorldFolder().getAbsolutePath() + " (" + world + ")", x); + return x; + } + + private int getTC() + { + switch(Iris.settings.performance.performanceMode) + { + case HALF_CPU: + return Math.max(Runtime.getRuntime().availableProcessors() / 2, 1); + case MATCH_CPU: + return Runtime.getRuntime().availableProcessors(); + case SINGLE_THREADED: + return 1; + case DOUBLE_CPU: + return Runtime.getRuntime().availableProcessors() * 2; + case UNLIMITED: + return -1; + case EXPLICIT: + return Iris.settings.performance.threadCount; + default: + break; + } + + return Math.max(Runtime.getRuntime().availableProcessors() / 2, 1); + } +} diff --git a/src/main/java/ninja/bytecode/iris/controller/PackController.java b/src/main/java/ninja/bytecode/iris/controller/PackController.java new file mode 100644 index 000000000..3ef10af2a --- /dev/null +++ b/src/main/java/ninja/bytecode/iris/controller/PackController.java @@ -0,0 +1,230 @@ +package ninja.bytecode.iris.controller; + +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.zip.ZipEntry; +import java.util.zip.ZipFile; + +import net.md_5.bungee.api.ChatColor; +import ninja.bytecode.iris.Iris; +import ninja.bytecode.iris.generator.genobject.GenObject; +import ninja.bytecode.iris.generator.genobject.GenObjectGroup; +import ninja.bytecode.iris.pack.IrisBiome; +import ninja.bytecode.iris.pack.IrisDimension; +import ninja.bytecode.iris.pack.IrisPack; +import ninja.bytecode.iris.util.IrisController; +import ninja.bytecode.shuriken.bench.PrecisionStopwatch; +import ninja.bytecode.shuriken.collections.GMap; +import ninja.bytecode.shuriken.execution.TaskExecutor; +import ninja.bytecode.shuriken.execution.TaskExecutor.TaskGroup; +import ninja.bytecode.shuriken.format.F; +import ninja.bytecode.shuriken.io.IO; +import ninja.bytecode.shuriken.json.JSONException; +import ninja.bytecode.shuriken.json.JSONObject; +import ninja.bytecode.shuriken.logging.L; + +public class PackController implements IrisController +{ + private GMap dimensions; + private GMap biomes; + private GMap genObjectGroups; + private boolean ready; + + @Override + public void onStart() + { + dimensions = new GMap<>(); + biomes = new GMap<>(); + genObjectGroups = new GMap<>(); + ready = false; + } + + @Override + public void onStop() + { + + } + + public boolean isReady() + { + return ready; + } + + public void createTempCache(File jar) + { + try + { + File temp = new File(System.getProperty("java.io.tmpdir") + "/Iris/"); + temp.mkdirs(); + L.i("Iris Cache: " + temp.getAbsolutePath()); + ZipFile zipFile = new ZipFile(jar); + 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(); + } + + catch(Throwable e) + { + L.w(ChatColor.YELLOW + "Failed to cache internal resources. Did you reload Iris externally?"); + } + } + + public void loadContent() + { + dimensions = new GMap<>(); + biomes = new GMap<>(); + genObjectGroups = new GMap<>(); + ready = false; + PrecisionStopwatch p = PrecisionStopwatch.start(); + L.i("Loading Content"); + + try + { + IrisPack master = new IrisPack(loadJSON("pack/manifest.json")); + master.load(); + } + + catch(Throwable e) + { + e.printStackTrace(); + } + + L.v(ChatColor.LIGHT_PURPLE + "Processing Content"); + + TaskExecutor exf = new TaskExecutor(Iris.settings.performance.compilerThreads, Iris.settings.performance.compilerPriority, "Iris Compiler"); + TaskGroup gg = exf.startWork(); + for(GenObjectGroup i : getGenObjectGroups().v()) + { + gg.queue(i::processVariants); + } + + gg.execute(); + exf.close(); + int m = 0; + + for(GenObjectGroup i : getGenObjectGroups().v()) + { + m += i.size(); + } + + L.i(ChatColor.LIGHT_PURPLE + "Dimensions: " + ChatColor.WHITE + getDimensions().size()); + L.i(ChatColor.LIGHT_PURPLE + "Biomes: " + ChatColor.WHITE + getBiomes().size()); + L.i(ChatColor.LIGHT_PURPLE + "Object Groups: " + ChatColor.WHITE + F.f(getGenObjectGroups().size())); + L.i(ChatColor.LIGHT_PURPLE + "Objects: " + ChatColor.WHITE + F.f(m)); + L.i(ChatColor.LIGHT_PURPLE + "Compilation Time: " + ChatColor.WHITE + F.duration(p.getMilliseconds(), 2)); + L.flush(); + L.i(ChatColor.GREEN + "Iris Dimensions Successfully Compiled!"); + ready = true; + } + + public IrisDimension loadDimension(String s) throws JSONException, IOException + { + L.v(ChatColor.GOLD + "Loading Dimension: " + ChatColor.GRAY + "pack/dimensions/" + s + ".json"); + return new IrisDimension(loadJSON("pack/dimensions/" + s + ".json")); + } + + public IrisBiome loadBiome(String s) throws JSONException, IOException + { + L.v(ChatColor.DARK_GREEN + "Loading Biome: " + ChatColor.GRAY + "pack/biomes/" + s + ".json"); + return new IrisBiome(loadJSON("pack/biomes/" + s + ".json")); + } + + public GenObjectGroup loadSchematicGroup(String s) + { + GenObjectGroup g = GenObjectGroup.load("pack/objects/" + s); + L.v(ChatColor.DARK_AQUA + "Loading Objects: " + ChatColor.GRAY + "pack/objects/" + s + ".ish"); + + if(g != null) + { + Iris.getController(PackController.class).getGenObjectGroups().put(s, g); + return g; + } + + L.i("Cannot load Object Group: " + s); + + return null; + } + + public GenObject loadSchematic(String s) throws IOException + { + return GenObject.load(loadResource("pack/objects/" + s + ".ish")); + } + + public JSONObject loadJSON(String s) throws JSONException, IOException + { + return new JSONObject(IO.readAll(loadResource(s))); + } + + public File loadFolder(String string) + { + File internal = internalResource(string); + + if(internal.exists()) + { + return internal; + } + + L.f(ChatColor.RED + "Cannot find folder: " + internal.getAbsolutePath()); + return null; + } + + public InputStream loadResource(String string) throws IOException + { + File internal = internalResource(string); + + if(internal.exists()) + { + L.flush(); + return new FileInputStream(internal); + } + + else + { + L.f(ChatColor.RED + "Cannot find Resource: " + ChatColor.YELLOW + internal.getAbsolutePath()); + return null; + } + } + + private static File internalResource(String resource) + { + if(new File(Iris.instance.getDataFolder(), "pack").exists()) + { + return new File(Iris.instance.getDataFolder(), resource); + } + + return new File(System.getProperty("java.io.tmpdir") + "/Iris/" + resource); + } + + public GMap getDimensions() + { + return dimensions; + } + + public GMap getBiomes() + { + return biomes; + } + + public GMap getGenObjectGroups() + { + return genObjectGroups; + } +} diff --git a/src/main/java/ninja/bytecode/iris/controller/TimingsController.java b/src/main/java/ninja/bytecode/iris/controller/TimingsController.java new file mode 100644 index 000000000..9e2cdf356 --- /dev/null +++ b/src/main/java/ninja/bytecode/iris/controller/TimingsController.java @@ -0,0 +1,36 @@ +package ninja.bytecode.iris.controller; + +import ninja.bytecode.iris.util.IrisController; +import ninja.bytecode.shuriken.bench.Profiler; + +public class TimingsController implements IrisController +{ + public Profiler profiler; + + @Override + public void onStart() + { + profiler = new Profiler(768); + } + + @Override + public void onStop() + { + + } + + public void started(String obj) + { + profiler.start(obj); + } + + public void stopped(String obj) + { + profiler.stop(obj); + } + + public double getResult(String tag) + { + return profiler.getResult(tag).getAverage(); + } +} diff --git a/src/main/java/ninja/bytecode/iris/controller/WandController.java b/src/main/java/ninja/bytecode/iris/controller/WandController.java new file mode 100644 index 000000000..e09b22675 --- /dev/null +++ b/src/main/java/ninja/bytecode/iris/controller/WandController.java @@ -0,0 +1,304 @@ +package ninja.bytecode.iris.controller; + +import java.awt.Color; +import java.util.Iterator; + +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.Sound; +import org.bukkit.block.Block; +import org.bukkit.enchantments.Enchantment; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.block.Action; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.inventory.EquipmentSlot; +import org.bukkit.inventory.ItemFlag; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.util.BlockVector; +import org.bukkit.util.Vector; + +import ninja.bytecode.iris.Iris; +import ninja.bytecode.iris.generator.genobject.GenObject; +import ninja.bytecode.iris.util.Cuboid; +import ninja.bytecode.iris.util.IrisController; +import ninja.bytecode.iris.util.MB; +import ninja.bytecode.iris.util.ParticleEffect; +import ninja.bytecode.iris.util.ParticleRedstone; +import ninja.bytecode.shuriken.collections.GList; + +public class WandController implements IrisController +{ + @Override + public void onStart() + { + //TODO: Optimize + Bukkit.getScheduler().scheduleSyncRepeatingTask(Iris.instance, () -> + { + for(Player i : Bukkit.getOnlinePlayers()) + { + tick(i); + } + }, 0, 5); + } + + @Override + public void onStop() + { + + } + + @EventHandler + public void tick(Player p) + { + try + { + if(isWand(p.getInventory().getItemInMainHand())) + { + Location[] d = getCuboid(p.getInventory().getItemInMainHand()); + ParticleEffect.CRIT_MAGIC.display(0.1f, 1, d[0].clone().add(0.5, 0.5, 0.5).clone().add(Vector.getRandom().subtract(Vector.getRandom()).normalize().clone().multiply(0.65)), p); + ParticleEffect.CRIT.display(0.1f, 1, d[1].clone().add(0.5, 0.5, 0.5).clone().add(Vector.getRandom().subtract(Vector.getRandom()).normalize().clone().multiply(0.65)), p); + + if(!d[0].getWorld().equals(d[1].getWorld())) + { + return; + } + + if(d[0].distanceSquared(d[1]) > 64 * 64) + { + return; + } + + int minx = Math.min(d[0].getBlockX(), d[1].getBlockX()); + int miny = Math.min(d[0].getBlockY(), d[1].getBlockY()); + int minz = Math.min(d[0].getBlockZ(), d[1].getBlockZ()); + int maxx = Math.max(d[0].getBlockX(), d[1].getBlockX()); + int maxy = Math.max(d[0].getBlockY(), d[1].getBlockY()); + int maxz = Math.max(d[0].getBlockZ(), d[1].getBlockZ()); + + for(double j = minx - 1; j < maxx + 1; j += 0.25) + { + for(double k = miny - 1; k < maxy + 1; k += 0.25) + { + for(double l = minz - 1; l < maxz + 1; l += 0.25) + { + boolean jj = j == minx || j == maxx; + boolean kk = k == miny || k == maxy; + boolean ll = l == minz || l == maxz; + double aa = j; + double bb = k; + double cc = l; + + if((jj && kk) || (jj && ll) || (ll && kk)) + { + Vector push = new Vector(0, 0, 0); + + if(j == minx) + { + push.add(new Vector(-0.55, 0, 0)); + } + + if(k == miny) + { + push.add(new Vector(0, -0.55, 0)); + } + + if(l == minz) + { + push.add(new Vector(0, 0, -0.55)); + } + + if(j == maxx) + { + push.add(new Vector(0.55, 0, 0)); + } + + if(k == maxy) + { + push.add(new Vector(0, 0.55, 0)); + } + + if(l == maxz) + { + push.add(new Vector(0, 0, 0.55)); + } + + Location lv = new Location(d[0].getWorld(), aa, bb, cc).clone().add(0.5, 0.5, 0.5).clone().add(push); + int color = Color.getHSBColor((float) (0.5f + (Math.sin((aa + bb + cc + (p.getTicksLived() / 2)) / 20f) / 2)), 1, 1).getRGB(); + new ParticleRedstone().setColor(new Color(color)).play(lv, p); + } + } + } + } + } + } + + catch(Throwable e) + { + + } + } + + @EventHandler + public void on(PlayerInteractEvent e) + { + if(e.getHand().equals(EquipmentSlot.HAND) && isWand(e.getPlayer().getInventory().getItemInMainHand())) + { + if(e.getAction().equals(Action.LEFT_CLICK_BLOCK)) + { + e.setCancelled(true); + e.getPlayer().getInventory().setItemInMainHand(update(true, e.getClickedBlock().getLocation(), e.getPlayer().getInventory().getItemInMainHand())); + e.getPlayer().playSound(e.getClickedBlock().getLocation(), Sound.BLOCK_END_PORTAL_FRAME_FILL, 1f, 0.67f); + e.getPlayer().updateInventory(); + } + + else if(e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) + { + e.setCancelled(true); + e.getPlayer().getInventory().setItemInMainHand(update(false, e.getClickedBlock().getLocation(), e.getPlayer().getInventory().getItemInMainHand())); + e.getPlayer().playSound(e.getClickedBlock().getLocation(), Sound.BLOCK_END_PORTAL_FRAME_FILL, 1f, 1.17f); + e.getPlayer().updateInventory(); + } + } + } + + public static void pasteSchematic(GenObject s, Location at) + { + s.place(at.getWorld(), at.getBlockX(), at.getBlockY(), at.getBlockZ()); + } + + @SuppressWarnings("deprecation") + public static GenObject createSchematic(ItemStack wand, Location at) + { + if(!isWand(wand)) + { + return null; + } + + try + { + Location[] f = getCuboid(wand); + Cuboid c = new Cuboid(f[0], f[1]); + GenObject s = new GenObject(c.getSizeX(), c.getSizeY(), c.getSizeZ()); + Iterator bb = c.iterator(); + while(bb.hasNext()) + { + Block b = bb.next(); + + if(b.getType().equals(Material.AIR)) + { + continue; + } + + byte data = b.getData(); + + BlockVector bv = b.getLocation().subtract(c.getCenter()).toVector().toBlockVector(); + s.put(bv.getBlockX(), + bv.getBlockY(), + bv.getBlockZ(), + + new MB(b.getType(), data)); + } + + return s; + } + + catch(Throwable e) + { + e.printStackTrace(); + } + + return null; + } + + public static Location stringToLocation(String s) + { + try + { + String[] f = s.split("\\Q in \\E"); + String[] g = f[0].split("\\Q,\\E"); + return new Location(Bukkit.getWorld(f[1]), Integer.valueOf(g[0]), Integer.valueOf(g[1]), Integer.valueOf(g[2])); + } + + catch(Throwable e) + { + return null; + } + } + + public static String locationToString(Location s) + { + if(s == null) + { + return "<#>"; + } + + return s.getBlockX() + "," + s.getBlockY() + "," + s.getBlockZ() + " in " + s.getWorld().getName(); + } + + public static ItemStack createWand() + { + return createWand(null, null); + } + + public static ItemStack update(boolean left, Location a, ItemStack item) + { + if(!isWand(item)) + { + return item; + } + + Location[] f = getCuboid(item); + Location other = left ? f[1] : f[0]; + + if(other != null && !other.getWorld().getName().equals(a.getWorld().getName())) + { + other = null; + } + + return createWand(left ? a : other, left ? other : a); + } + + public static ItemStack createWand(Location a, Location b) + { + ItemStack is = new ItemStack(Material.BLAZE_ROD); + is.addUnsafeEnchantment(Enchantment.ARROW_INFINITE, 1); + ItemMeta im = is.getItemMeta(); + im.setDisplayName(ChatColor.BOLD + "" + ChatColor.GOLD + "Wand of Iris"); + im.setUnbreakable(true); + im.addItemFlags(ItemFlag.HIDE_ATTRIBUTES, ItemFlag.HIDE_PLACED_ON, ItemFlag.HIDE_POTION_EFFECTS, ItemFlag.HIDE_DESTROYS, ItemFlag.HIDE_ENCHANTS); + im.setLore(new GList().add(locationToString(a), locationToString(b))); + is.setItemMeta(im); + + return is; + } + + public static Location[] getCuboid(ItemStack is) + { + ItemMeta im = is.getItemMeta(); + return new Location[] {stringToLocation(im.getLore().get(0)), stringToLocation(im.getLore().get(1))}; + } + + public static boolean isWand(ItemStack item) + { + if(!item.getType().equals(createWand().getType())) + { + return false; + } + + if(!item.getItemMeta().getEnchants().equals(createWand().getItemMeta().getEnchants())) + { + return false; + } + + if(!item.getItemMeta().getDisplayName().equals(createWand().getItemMeta().getDisplayName())) + { + return false; + } + + return true; + } +} diff --git a/src/main/java/ninja/bytecode/iris/controller/WorldController.java b/src/main/java/ninja/bytecode/iris/controller/WorldController.java new file mode 100644 index 000000000..f285c2d66 --- /dev/null +++ b/src/main/java/ninja/bytecode/iris/controller/WorldController.java @@ -0,0 +1,65 @@ +package ninja.bytecode.iris.controller; + +import java.io.File; +import java.util.UUID; + +import org.bukkit.Bukkit; +import org.bukkit.World; +import org.bukkit.WorldCreator; + +import ninja.bytecode.iris.Iris; +import ninja.bytecode.iris.generator.IrisGenerator; +import ninja.bytecode.iris.pack.IrisDimension; +import ninja.bytecode.iris.util.IrisController; +import ninja.bytecode.shuriken.execution.J; +import ninja.bytecode.shuriken.io.IO; + +public class WorldController implements IrisController +{ + @Override + public void onStart() + { + J.attemptAsync(() -> + { + for(File i : new File(Iris.instance.getDataFolder().getParentFile().getParentFile(), "iris-worlds").listFiles()) + { + if(new File(i, ".garbage").exists()) + { + IO.delete(i); + } + } + }); + } + + @Override + public void onStop() + { + + } + + public World createIrisWorld(IrisDimension dimension, long seed, boolean temp) + { + if(dimension == null) + { + dimension = Iris.getController(PackController.class).getDimensions().get("overworld"); + } + + //@builder + World ww = Bukkit.createWorld(new WorldCreator("iris-worlds/" + UUID.randomUUID().toString()) + .generator(new IrisGenerator(dimension)) + .environment(dimension.getEnvironment()) + .seed(seed)); + //@done + ww.setSpawnFlags(false, false); + ww.setAutoSave(false); + ww.setKeepSpawnInMemory(false); + ww.setSpawnLocation(0, 256, 0); + + if(temp) + { + J.attempt(() -> new File(ww.getWorldFolder(), ".garbage").createNewFile()); + } + + return ww; + } +} diff --git a/src/main/java/ninja/bytecode/iris/generator/IrisGenerator.java b/src/main/java/ninja/bytecode/iris/generator/IrisGenerator.java index c0e2b7df7..43aa197a1 100644 --- a/src/main/java/ninja/bytecode/iris/generator/IrisGenerator.java +++ b/src/main/java/ninja/bytecode/iris/generator/IrisGenerator.java @@ -9,15 +9,16 @@ import org.bukkit.block.Biome; import org.bukkit.generator.BlockPopulator; import ninja.bytecode.iris.Iris; +import ninja.bytecode.iris.controller.PackController; +import ninja.bytecode.iris.generator.genobject.GenObjectDecorator; +import ninja.bytecode.iris.generator.genobject.GenObjectGroup; import ninja.bytecode.iris.generator.layer.GenLayerBase; import ninja.bytecode.iris.generator.layer.GenLayerBiome; import ninja.bytecode.iris.generator.layer.GenLayerCaves; import ninja.bytecode.iris.generator.layer.GenLayerLayeredNoise; import ninja.bytecode.iris.generator.layer.GenLayerRidge; -import ninja.bytecode.iris.generator.populator.ObjectPopulator; -import ninja.bytecode.iris.schematic.SchematicGroup; -import ninja.bytecode.iris.spec.IrisBiome; -import ninja.bytecode.iris.spec.IrisDimension; +import ninja.bytecode.iris.pack.IrisBiome; +import ninja.bytecode.iris.pack.IrisDimension; import ninja.bytecode.iris.util.AtomicChunkData; import ninja.bytecode.iris.util.ChunkPlan; import ninja.bytecode.iris.util.IrisInterpolation; @@ -61,11 +62,11 @@ public class IrisGenerator extends ParallelChunkGenerator private RNG rTerrain; private IrisDimension dim; private World world; - private GMap schematicCache = new GMap<>(); + private GMap schematicCache = new GMap<>(); public IrisGenerator() { - this(Iris.dimensions.get("overworld")); + this(Iris.getController(PackController.class).getDimensions().get("overworld")); } public GList getLoadedBiomes() @@ -230,15 +231,15 @@ public class IrisGenerator extends ParallelChunkGenerator if(Iris.settings.gen.doSchematics) { - p.add(new ObjectPopulator(this)); + p.add(new GenObjectDecorator(this)); } return p; } - public SchematicGroup loadSchematics(String folder) + public GenObjectGroup loadSchematics(String folder) { - return Iris.schematics.get(folder); + return Iris.getController(PackController.class).getGenObjectGroups().get(folder); } private double getBiomedHeight(int x, int z, ChunkPlan plan) @@ -260,12 +261,12 @@ public class IrisGenerator extends ParallelChunkGenerator return world; } - public GMap getSchematicCache() + public GMap getSchematicCache() { return schematicCache; } - public void setSchematicCache(GMap schematicCache) + public void setSchematicCache(GMap schematicCache) { this.schematicCache = schematicCache; } diff --git a/src/main/java/ninja/bytecode/iris/generator/genobject/GenObject.java b/src/main/java/ninja/bytecode/iris/generator/genobject/GenObject.java index 9ac5679e4..aab925206 100644 --- a/src/main/java/ninja/bytecode/iris/generator/genobject/GenObject.java +++ b/src/main/java/ninja/bytecode/iris/generator/genobject/GenObject.java @@ -1,4 +1,4 @@ -package ninja.bytecode.iris.schematic; +package ninja.bytecode.iris.generator.genobject; import java.io.DataInputStream; import java.io.DataOutputStream; @@ -26,7 +26,7 @@ import ninja.bytecode.shuriken.collections.GMap; import ninja.bytecode.shuriken.io.CustomOutputStream; import ninja.bytecode.shuriken.logging.L; -public class Schematic +public class GenObject { private boolean centeredHeight; private int w; @@ -38,7 +38,7 @@ public class Schematic private int mountHeight; private BlockVector shift; - public Schematic(int w, int h, int d) + public GenObject(int w, int h, int d) { this.w = w; this.h = h; @@ -180,9 +180,9 @@ public class Schematic s.put(new BlockVector(x, y, z), mb); } - public Schematic copy() + public GenObject copy() { - Schematic s = new Schematic(w, h, d); + GenObject s = new GenObject(w, h, d); s.fill(this.s); s.centeredHeight = centeredHeight; s.name = name; @@ -254,17 +254,17 @@ public class Schematic } } - public static Schematic load(InputStream in) throws IOException + public static GenObject load(InputStream in) throws IOException { - Schematic s = new Schematic(1, 1, 1); + GenObject s = new GenObject(1, 1, 1); s.read(in); return s; } - public static Schematic load(File f) throws IOException + public static GenObject load(File f) throws IOException { - Schematic s = new Schematic(1, 1, 1); + GenObject s = new GenObject(1, 1, 1); s.name = f.getName().replaceAll("\\Q.ish\\E", ""); FileInputStream fin = new FileInputStream(f); s.read(fin); diff --git a/src/main/java/ninja/bytecode/iris/generator/genobject/GenObjectDecorator.java b/src/main/java/ninja/bytecode/iris/generator/genobject/GenObjectDecorator.java index dc2095448..e13446fd0 100644 --- a/src/main/java/ninja/bytecode/iris/generator/genobject/GenObjectDecorator.java +++ b/src/main/java/ninja/bytecode/iris/generator/genobject/GenObjectDecorator.java @@ -1,4 +1,4 @@ -package ninja.bytecode.iris.generator.populator; +package ninja.bytecode.iris.generator.genobject; import java.util.Random; @@ -11,19 +11,20 @@ import org.bukkit.block.BlockFace; import org.bukkit.generator.BlockPopulator; import ninja.bytecode.iris.Iris; +import ninja.bytecode.iris.controller.PackController; +import ninja.bytecode.iris.controller.TimingsController; import ninja.bytecode.iris.generator.IrisGenerator; -import ninja.bytecode.iris.schematic.SchematicGroup; -import ninja.bytecode.iris.spec.IrisBiome; +import ninja.bytecode.iris.pack.IrisBiome; import ninja.bytecode.shuriken.collections.GMap; import ninja.bytecode.shuriken.collections.GSet; import ninja.bytecode.shuriken.math.M; -public class ObjectPopulator extends BlockPopulator +public class GenObjectDecorator extends BlockPopulator { private GMap biomeMap; - private GMap> populationCache; + private GMap> populationCache; - public ObjectPopulator(IrisGenerator generator) + public GenObjectDecorator(IrisGenerator generator) { biomeMap = new GMap<>(); populationCache = new GMap<>(); @@ -32,11 +33,11 @@ public class ObjectPopulator extends BlockPopulator { biomeMap.put(i.getRealBiome(), i); - GMap gk = new GMap<>(); + GMap gk = new GMap<>(); for(String j : i.getSchematicGroups().k()) { - gk.put(Iris.schematics.get(j), i.getSchematicGroups().get(j)); + gk.put(Iris.getController(PackController.class).getGenObjectGroups().get(j), i.getSchematicGroups().get(j)); } populationCache.put(i.getRealBiome(), gk); @@ -46,7 +47,7 @@ public class ObjectPopulator extends BlockPopulator @Override public void populate(World world, Random random, Chunk source) { - Iris.started("decor"); + Iris.getController(TimingsController.class).started("decor"); GSet hits = new GSet<>(); for(int i = 0; i < Iris.settings.performance.decorationAccuracy; i++) @@ -67,7 +68,7 @@ public class ObjectPopulator extends BlockPopulator continue; } - GMap objects = populationCache.get(biome); + GMap objects = populationCache.get(biome); if(objects == null) { @@ -78,12 +79,12 @@ public class ObjectPopulator extends BlockPopulator populate(world, random, source, biome, ibiome, objects); } - Iris.stopped("decor"); + Iris.getController(TimingsController.class).stopped("decor"); } - private void populate(World world, Random random, Chunk source, Biome biome, IrisBiome ibiome, GMap objects) + private void populate(World world, Random random, Chunk source, Biome biome, IrisBiome ibiome, GMap objects) { - for(SchematicGroup i : objects.k()) + for(GenObjectGroup i : objects.k()) { for(int j = 0; j < getTries(objects.get(i)); j++) { diff --git a/src/main/java/ninja/bytecode/iris/generator/genobject/GenObjectGroup.java b/src/main/java/ninja/bytecode/iris/generator/genobject/GenObjectGroup.java index d5e4a9269..106a318bf 100644 --- a/src/main/java/ninja/bytecode/iris/generator/genobject/GenObjectGroup.java +++ b/src/main/java/ninja/bytecode/iris/generator/genobject/GenObjectGroup.java @@ -1,4 +1,4 @@ -package ninja.bytecode.iris.schematic; +package ninja.bytecode.iris.generator.genobject; import java.io.File; import java.io.IOException; @@ -6,6 +6,7 @@ import java.util.concurrent.locks.ReentrantLock; import net.md_5.bungee.api.ChatColor; import ninja.bytecode.iris.Iris; +import ninja.bytecode.iris.controller.PackController; import ninja.bytecode.iris.util.Direction; import ninja.bytecode.shuriken.collections.GList; import ninja.bytecode.shuriken.execution.TaskExecutor; @@ -14,14 +15,14 @@ import ninja.bytecode.shuriken.format.F; import ninja.bytecode.shuriken.io.IO; import ninja.bytecode.shuriken.logging.L; -public class SchematicGroup +public class GenObjectGroup { - private GList schematics; + private GList schematics; private GList flags; private String name; private int priority; - public SchematicGroup(String name) + public GenObjectGroup(String name) { this.schematics = new GList<>(); this.flags = new GList<>(); @@ -39,12 +40,12 @@ public class SchematicGroup this.name = name; } - public GList getSchematics() + public GList getSchematics() { return schematics; } - public void setSchematics(GList schematics) + public void setSchematics(GList schematics) { this.schematics = schematics; } @@ -74,13 +75,13 @@ public class SchematicGroup return getSchematics().size(); } - public static SchematicGroup load(String string) + public static GenObjectGroup load(String string) { - File folder = Iris.loadFolder(string); + File folder = Iris.getController(PackController.class).loadFolder(string); if(folder != null) { - SchematicGroup g = new SchematicGroup(string); + GenObjectGroup g = new GenObjectGroup(string); for(File i : folder.listFiles()) { @@ -102,7 +103,7 @@ public class SchematicGroup { try { - Schematic s = Schematic.load(i); + GenObject s = GenObject.load(i); g.getSchematics().add(s); } @@ -122,19 +123,19 @@ public class SchematicGroup public void processVariants() { - GList inject = new GList<>(); + GList inject = new GList<>(); String x = Thread.currentThread().getName(); ReentrantLock rr = new ReentrantLock(); TaskExecutor ex = new TaskExecutor(Iris.settings.performance.compilerThreads, Iris.settings.performance.compilerPriority, x + "/Subroutine "); TaskGroup gg = ex.startWork(); - for(Schematic i : getSchematics()) + for(GenObject i : getSchematics()) { for(Direction j : new Direction[] {Direction.S, Direction.E, Direction.W}) { - Schematic cp = i.copy(); + GenObject cp = i.copy(); gg.queue(() -> { - Schematic f = cp; + GenObject f = cp; f.rotate(Direction.N, j); rr.lock(); inject.add(f); @@ -147,7 +148,7 @@ public class SchematicGroup gg = ex.startWork(); getSchematics().add(inject); - for(Schematic i : getSchematics()) + for(GenObject i : getSchematics()) { gg.queue(() -> { i.computeMountShift(); @@ -184,7 +185,7 @@ public class SchematicGroup return false; if(getClass() != obj.getClass()) return false; - SchematicGroup other = (SchematicGroup) obj; + GenObjectGroup other = (GenObjectGroup) obj; if(flags == null) { if(other.flags != null) diff --git a/src/main/java/ninja/bytecode/iris/generator/layer/GenLayerBiome.java b/src/main/java/ninja/bytecode/iris/generator/layer/GenLayerBiome.java index 5b03c140c..cf21eb9f0 100644 --- a/src/main/java/ninja/bytecode/iris/generator/layer/GenLayerBiome.java +++ b/src/main/java/ninja/bytecode/iris/generator/layer/GenLayerBiome.java @@ -8,7 +8,7 @@ import org.bukkit.World; import ninja.bytecode.iris.Iris; import ninja.bytecode.iris.generator.IrisGenerator; -import ninja.bytecode.iris.spec.IrisBiome; +import ninja.bytecode.iris.pack.IrisBiome; import ninja.bytecode.iris.util.GenLayer; import ninja.bytecode.iris.util.MaxingGenerator; import ninja.bytecode.iris.util.MaxingGenerator.EnumMaxingGenerator; diff --git a/src/main/java/ninja/bytecode/iris/pack/IrisBiome.java b/src/main/java/ninja/bytecode/iris/pack/IrisBiome.java index 4703bb95c..363fb53cb 100644 --- a/src/main/java/ninja/bytecode/iris/pack/IrisBiome.java +++ b/src/main/java/ninja/bytecode/iris/pack/IrisBiome.java @@ -1,4 +1,4 @@ -package ninja.bytecode.iris.spec; +package ninja.bytecode.iris.pack; import java.lang.reflect.Field; @@ -6,6 +6,7 @@ import org.bukkit.Material; import org.bukkit.block.Biome; import ninja.bytecode.iris.Iris; +import ninja.bytecode.iris.controller.PackController; import ninja.bytecode.iris.util.MB; import ninja.bytecode.iris.util.PolygonGenerator; import ninja.bytecode.shuriken.collections.GList; @@ -169,7 +170,7 @@ public class IrisBiome for(String i : schematicGroups.k()) { - Iris.loadSchematicGroup(i); + Iris.getController(PackController.class).loadSchematicGroup(i); } }); } diff --git a/src/main/java/ninja/bytecode/iris/pack/IrisDimension.java b/src/main/java/ninja/bytecode/iris/pack/IrisDimension.java index d441559ed..5227da0cc 100644 --- a/src/main/java/ninja/bytecode/iris/pack/IrisDimension.java +++ b/src/main/java/ninja/bytecode/iris/pack/IrisDimension.java @@ -1,4 +1,4 @@ -package ninja.bytecode.iris.spec; +package ninja.bytecode.iris.pack; import java.io.IOException; import java.util.concurrent.locks.ReentrantLock; @@ -6,6 +6,7 @@ import java.util.concurrent.locks.ReentrantLock; import org.bukkit.World.Environment; import ninja.bytecode.iris.Iris; +import ninja.bytecode.iris.controller.PackController; import ninja.bytecode.shuriken.collections.GList; import ninja.bytecode.shuriken.execution.J; import ninja.bytecode.shuriken.execution.TaskExecutor; @@ -75,7 +76,7 @@ public class IrisDimension private GList biomesFromArray(JSONArray a) throws JSONException, IOException { GList b = new GList<>(); - TaskExecutor ex= new TaskExecutor(Iris.settings.performance.compilerThreads, Iris.settings.performance.compilerPriority, "Iris Dim Compiler"); + TaskExecutor ex= new TaskExecutor(Iris.settings.performance.compilerThreads, Iris.settings.performance.compilerPriority, "Iris Loader"); TaskGroup g = ex.startWork(); ReentrantLock lock = new ReentrantLock(); @@ -83,9 +84,9 @@ public class IrisDimension { int ii = i; g.queue(() -> { - IrisBiome bb = Iris.loadBiome(a.getString(ii)); + IrisBiome bb = Iris.getController(PackController.class).loadBiome(a.getString(ii)); lock.lock(); - Iris.biomes.put(a.getString(ii), bb); + Iris.getController(PackController.class).getBiomes().put(a.getString(ii), bb); b.add(bb); lock.unlock(); }); @@ -118,4 +119,9 @@ public class IrisDimension { return name; } + + public Environment getEnvironment() + { + return environment; + } } diff --git a/src/main/java/ninja/bytecode/iris/pack/IrisPack.java b/src/main/java/ninja/bytecode/iris/pack/IrisPack.java index a91552db3..a9dd5df2b 100644 --- a/src/main/java/ninja/bytecode/iris/pack/IrisPack.java +++ b/src/main/java/ninja/bytecode/iris/pack/IrisPack.java @@ -1,8 +1,9 @@ -package ninja.bytecode.iris.spec; +package ninja.bytecode.iris.pack; import java.io.IOException; import ninja.bytecode.iris.Iris; +import ninja.bytecode.iris.controller.PackController; import ninja.bytecode.shuriken.collections.GList; import ninja.bytecode.shuriken.execution.J; import ninja.bytecode.shuriken.json.JSONArray; @@ -73,14 +74,14 @@ public class IrisPack { for(String i : dimensions) { - IrisDimension d = Iris.loadDimension(i); - Iris.dimensions.put(i, d); + IrisDimension d = Iris.getController(PackController.class).loadDimension(i); + Iris.getController(PackController.class).getDimensions().put(i, d); } } public void loadBiome(String s) throws JSONException, IOException { - IrisBiome b = Iris.loadBiome(s); - Iris.biomes.put(s, b); + IrisBiome b = Iris.getController(PackController.class).loadBiome(s); + Iris.getController(PackController.class).getBiomes().put(s, b); } } diff --git a/src/main/java/ninja/bytecode/iris/util/ChunkPlan.java b/src/main/java/ninja/bytecode/iris/util/ChunkPlan.java index 92f257858..398585004 100644 --- a/src/main/java/ninja/bytecode/iris/util/ChunkPlan.java +++ b/src/main/java/ninja/bytecode/iris/util/ChunkPlan.java @@ -4,15 +4,15 @@ import java.util.function.Supplier; import org.bukkit.util.BlockVector; -import ninja.bytecode.iris.schematic.Schematic; -import ninja.bytecode.iris.spec.IrisBiome; +import ninja.bytecode.iris.generator.genobject.GenObject; +import ninja.bytecode.iris.pack.IrisBiome; import ninja.bytecode.shuriken.collections.GMap; public class ChunkPlan { private final GMap heightCache; private final GMap biomeCache; - private final GMap schematics; + private final GMap schematics; public ChunkPlan() { @@ -21,7 +21,7 @@ public class ChunkPlan this.biomeCache = new GMap<>(); } - public void planSchematic(BlockVector b, Schematic s) + public void planSchematic(BlockVector b, GenObject s) { schematics.put(b, s); } diff --git a/src/main/java/ninja/bytecode/iris/util/IrisController.java b/src/main/java/ninja/bytecode/iris/util/IrisController.java new file mode 100644 index 000000000..9d3450a29 --- /dev/null +++ b/src/main/java/ninja/bytecode/iris/util/IrisController.java @@ -0,0 +1,10 @@ +package ninja.bytecode.iris.util; + +import org.bukkit.event.Listener; + +public interface IrisController extends Listener +{ + public void onStart(); + + public void onStop(); +} diff --git a/src/main/java/ninja/bytecode/iris/util/IrisControllerSet.java b/src/main/java/ninja/bytecode/iris/util/IrisControllerSet.java new file mode 100644 index 000000000..956e484ad --- /dev/null +++ b/src/main/java/ninja/bytecode/iris/util/IrisControllerSet.java @@ -0,0 +1,73 @@ +package ninja.bytecode.iris.util; + +import java.io.File; +import java.io.IOException; + +import org.bukkit.Bukkit; +import org.bukkit.event.HandlerList; + +import ninja.bytecode.iris.Iris; +import ninja.bytecode.shuriken.collections.GMap; +import ninja.bytecode.shuriken.logging.L; +import ninja.bytecode.shuriken.tools.JarScanner; + +public class IrisControllerSet +{ + private GMap, IrisController> controllers; + + public IrisControllerSet() + { + controllers = new GMap<>(); + } + + public void startControllers(File jar) throws IOException + { + JarScanner ja = new JarScanner(jar, "ninja.bytecode.iris.controller"); + ja.scan(); + for(Class i : ja.getClasses()) + { + try + { + IrisController c = (IrisController) i.getConstructor().newInstance(); + Bukkit.getPluginManager().registerEvents(c, Iris.instance); + c.onStart(); + controllers.put(i, c); + } + + catch(Throwable e) + { + L.ex(e); + } + } + } + + public void stopControllers() + { + for(Class i : controllers.k()) + { + try + { + IrisController c = controllers.get(i); + HandlerList.unregisterAll(); + c.onStop(); + } + + catch(Throwable e) + { + L.ex(e); + } + } + + controllers.clear(); + } + + public IrisController get(Class c) + { + return controllers.get(c); + } + + public int size() + { + return controllers.size(); + } +} diff --git a/src/main/java/ninja/bytecode/iris/util/ParallelChunkGenerator.java b/src/main/java/ninja/bytecode/iris/util/ParallelChunkGenerator.java index 164387f8a..7af5270b7 100644 --- a/src/main/java/ninja/bytecode/iris/util/ParallelChunkGenerator.java +++ b/src/main/java/ninja/bytecode/iris/util/ParallelChunkGenerator.java @@ -8,7 +8,10 @@ import org.bukkit.block.Biome; import org.bukkit.generator.ChunkGenerator; import ninja.bytecode.iris.Iris; +import ninja.bytecode.iris.controller.ExecutionController; +import ninja.bytecode.iris.controller.TimingsController; import ninja.bytecode.shuriken.execution.ChronoLatch; +import ninja.bytecode.shuriken.execution.TaskExecutor; import ninja.bytecode.shuriken.execution.TaskExecutor.TaskGroup; import ninja.bytecode.shuriken.execution.TaskExecutor.TaskResult; import ninja.bytecode.shuriken.math.RollingSequence; @@ -27,6 +30,7 @@ public abstract class ParallelChunkGenerator extends ChunkGenerator private ChronoLatch cl = new ChronoLatch(1000); private RollingSequence rs = new RollingSequence(512); private World world; + private TaskExecutor genPool; public World getWorld() { @@ -41,7 +45,11 @@ public abstract class ParallelChunkGenerator extends ChunkGenerator public ChunkData generateChunkData(World world, Random random, int x, int z, BiomeGrid biome) { - Iris.started("terrain"); + Iris.getController(TimingsController.class).started("terrain"); + if(genPool == null) + { + genPool = Iris.getController(ExecutionController.class).getExecutor(world); + } this.world = world; data = new AtomicChunkData(world); @@ -53,7 +61,7 @@ public abstract class ParallelChunkGenerator extends ChunkGenerator ready = true; } - tg = Iris.genPool.startWork(); + tg = genPool.startWork(); O plan = new O(); for(i = 0; i < 16; i++) { @@ -93,7 +101,7 @@ public abstract class ParallelChunkGenerator extends ChunkGenerator } } - Iris.stopped("terrain"); + Iris.getController(TimingsController.class).stopped("terrain"); return data.toChunkData(); } diff --git a/src/main/java/ninja/bytecode/iris/util/WandUtil.java b/src/main/java/ninja/bytecode/iris/util/WandUtil.java deleted file mode 100644 index 09cca3b29..000000000 --- a/src/main/java/ninja/bytecode/iris/util/WandUtil.java +++ /dev/null @@ -1,157 +0,0 @@ -package ninja.bytecode.iris.util; - -import java.util.Iterator; - -import org.bukkit.Bukkit; -import org.bukkit.ChatColor; -import org.bukkit.Location; -import org.bukkit.Material; -import org.bukkit.block.Block; -import org.bukkit.enchantments.Enchantment; -import org.bukkit.inventory.ItemFlag; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.ItemMeta; -import org.bukkit.util.BlockVector; - -import ninja.bytecode.iris.schematic.Schematic; -import ninja.bytecode.shuriken.collections.GList; - -public class WandUtil -{ - public static void pasteSchematic(Schematic s, Location at) - { - s.place(at.getWorld(), at.getBlockX(), at.getBlockY(), at.getBlockZ()); - } - - @SuppressWarnings("deprecation") - public static Schematic createSchematic(ItemStack wand, Location at) - { - if(!isWand(wand)) - { - return null; - } - - try - { - Location[] f = getCuboid(wand); - Cuboid c = new Cuboid(f[0], f[1]); - Schematic s = new Schematic(c.getSizeX(), c.getSizeY(), c.getSizeZ()); - Iterator bb = c.iterator(); - while(bb.hasNext()) - { - Block b = bb.next(); - - if(b.getType().equals(Material.AIR)) - { - continue; - } - - byte data = b.getData(); - - BlockVector bv = b.getLocation().subtract(c.getCenter()).toVector().toBlockVector(); - s.put(bv.getBlockX(), - bv.getBlockY(), - bv.getBlockZ(), - - new MB(b.getType(), data)); - } - - return s; - } - - catch(Throwable e) - { - e.printStackTrace(); - } - - return null; - } - - public static Location stringToLocation(String s) - { - try - { - String[] f = s.split("\\Q in \\E"); - String[] g = f[0].split("\\Q,\\E"); - return new Location(Bukkit.getWorld(f[1]), Integer.valueOf(g[0]), Integer.valueOf(g[1]), Integer.valueOf(g[2])); - } - - catch(Throwable e) - { - return null; - } - } - - public static String locationToString(Location s) - { - if(s == null) - { - return "<#>"; - } - - return s.getBlockX() + "," + s.getBlockY() + "," + s.getBlockZ() + " in " + s.getWorld().getName(); - } - - public static ItemStack createWand() - { - return createWand(null, null); - } - - public static ItemStack update(boolean left, Location a, ItemStack item) - { - if(!isWand(item)) - { - return item; - } - - Location[] f = getCuboid(item); - Location other = left ? f[1] : f[0]; - - if(other != null && !other.getWorld().getName().equals(a.getWorld().getName())) - { - other = null; - } - - return createWand(left ? a : other, left ? other : a); - } - - public static ItemStack createWand(Location a, Location b) - { - ItemStack is = new ItemStack(Material.BLAZE_ROD); - is.addUnsafeEnchantment(Enchantment.ARROW_INFINITE, 1); - ItemMeta im = is.getItemMeta(); - im.setDisplayName(ChatColor.BOLD + "" + ChatColor.GOLD + "Wand of Iris"); - im.setUnbreakable(true); - im.addItemFlags(ItemFlag.HIDE_ATTRIBUTES, ItemFlag.HIDE_PLACED_ON, ItemFlag.HIDE_POTION_EFFECTS, ItemFlag.HIDE_DESTROYS, ItemFlag.HIDE_ENCHANTS); - im.setLore(new GList().add(locationToString(a), locationToString(b))); - is.setItemMeta(im); - - return is; - } - - public static Location[] getCuboid(ItemStack is) - { - ItemMeta im = is.getItemMeta(); - return new Location[] {stringToLocation(im.getLore().get(0)), stringToLocation(im.getLore().get(1))}; - } - - public static boolean isWand(ItemStack item) - { - if(!item.getType().equals(createWand().getType())) - { - return false; - } - - if(!item.getItemMeta().getEnchants().equals(createWand().getItemMeta().getEnchants())) - { - return false; - } - - if(!item.getItemMeta().getDisplayName().equals(createWand().getItemMeta().getDisplayName())) - { - return false; - } - - return true; - } -}