From 042a295a55371325b9df65620fad9168f30dc9e2 Mon Sep 17 00:00:00 2001 From: boy0001 Date: Mon, 27 Jul 2015 03:00:09 +1000 Subject: [PATCH] Restructure generators --- .../intellectualcrafters/plot/IPlotMain.java | 3 +- .../com/intellectualcrafters/plot/PS.java | 4 +- .../plot/generator/PlotGenerator2.java | 8 +- .../com/plotsquared/bukkit/BukkitMain.java | 11 ++- .../bukkit/generator/AugmentedPopulator.java | 5 +- .../generator/BukkitGeneratorWrapper.java | 84 +++++++++++++++++++ .../bukkit/generator/BukkitPlotGenerator.java | 6 +- .../bukkit/listeners/WorldEvents.java | 16 ++-- .../bukkit/util/bukkit/BukkitSetupUtils.java | 6 +- 9 files changed, 120 insertions(+), 23 deletions(-) create mode 100644 src/main/java/com/plotsquared/bukkit/generator/BukkitGeneratorWrapper.java diff --git a/src/main/java/com/intellectualcrafters/plot/IPlotMain.java b/src/main/java/com/intellectualcrafters/plot/IPlotMain.java index 58a526f25..aba30386f 100644 --- a/src/main/java/com/intellectualcrafters/plot/IPlotMain.java +++ b/src/main/java/com/intellectualcrafters/plot/IPlotMain.java @@ -7,6 +7,7 @@ import org.bukkit.generator.ChunkGenerator; import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.generator.HybridUtils; +import com.intellectualcrafters.plot.generator.PlotGenerator2; import com.plotsquared.bukkit.listeners.APlotListener; import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.util.BlockManager; @@ -70,7 +71,7 @@ public interface IPlotMain { public void unregister(PlotPlayer player); - public ChunkGenerator getGenerator(String world, String name); + public PlotGenerator2 getGenerator(String world, String name); public APlotListener initPlotListener(); diff --git a/src/main/java/com/intellectualcrafters/plot/PS.java b/src/main/java/com/intellectualcrafters/plot/PS.java index 1f8bf4b3d..782d0dcc4 100644 --- a/src/main/java/com/intellectualcrafters/plot/PS.java +++ b/src/main/java/com/intellectualcrafters/plot/PS.java @@ -844,12 +844,12 @@ public class PS { if (ClusterManager.getClusters(world).size() > 0) { for (final PlotCluster cluster : ClusterManager.getClusters(world)) { log(C.PREFIX.s() + "&3 - &7| cluster: " + cluster); - generator.augment(generator.getName(), cluster, plotWorld); + generator.augment(cluster, plotWorld); // new AugmentedPopulator(world, generator, cluster, plotWorld.TERRAIN == 2, plotWorld.TERRAIN != 2); } } } else if (plotWorld.TYPE == 1) { - generator.augment(generator.getName(), null, plotWorld); + generator.augment(null, plotWorld); // new AugmentedPopulator(world, gen_class, null, plotWorld.TERRAIN == 2, plotWorld.TERRAIN != 2); } generator.initialize(plotWorld); diff --git a/src/main/java/com/intellectualcrafters/plot/generator/PlotGenerator2.java b/src/main/java/com/intellectualcrafters/plot/generator/PlotGenerator2.java index ef0579b9d..b93cc5250 100644 --- a/src/main/java/com/intellectualcrafters/plot/generator/PlotGenerator2.java +++ b/src/main/java/com/intellectualcrafters/plot/generator/PlotGenerator2.java @@ -4,11 +4,13 @@ import com.intellectualcrafters.plot.object.PlotCluster; import com.intellectualcrafters.plot.object.PlotManager; import com.intellectualcrafters.plot.object.PlotWorld; -public abstract class PlotGenerator2 { +public abstract class PlotGenerator2 { public final String world; + public T generator; - public PlotGenerator2(String world) { + public PlotGenerator2(String world, T generator) { this.world = world; + this.generator = generator; } public abstract void initialize(PlotWorld plotworld); @@ -22,7 +24,7 @@ public abstract class PlotGenerator2 { * @param cluster Will be the cluster, or null * @param plotworld */ - public abstract void augment(String generator, PlotCluster cluster, PlotWorld plotworld); + public abstract void augment(PlotCluster cluster, PlotWorld plotworld); /** diff --git a/src/main/java/com/plotsquared/bukkit/BukkitMain.java b/src/main/java/com/plotsquared/bukkit/BukkitMain.java index b6ceda979..883f84e6a 100644 --- a/src/main/java/com/plotsquared/bukkit/BukkitMain.java +++ b/src/main/java/com/plotsquared/bukkit/BukkitMain.java @@ -10,6 +10,9 @@ import com.plotsquared.bukkit.database.plotme.LikePlotMeConverter; import com.plotsquared.bukkit.database.plotme.PlotMeConnector_017; import com.intellectualcrafters.plot.flag.FlagManager; import com.intellectualcrafters.plot.generator.BukkitHybridUtils; +import com.intellectualcrafters.plot.generator.PlotGenerator2; +import com.plotsquared.bukkit.generator.BukkitGeneratorWrapper; +import com.plotsquared.bukkit.generator.BukkitPlotGenerator; import com.plotsquared.bukkit.generator.HybridGen; import com.intellectualcrafters.plot.generator.HybridUtils; import com.plotsquared.bukkit.listeners.*; @@ -472,13 +475,15 @@ public class BukkitMain extends JavaPlugin implements Listener, IPlotMain { } @Override - public ChunkGenerator getGenerator(final String world, final String name) { + public BukkitGeneratorWrapper getGenerator(final String world, final String name) { final Plugin gen_plugin = Bukkit.getPluginManager().getPlugin(name); + ChunkGenerator gen; if ((gen_plugin != null) && gen_plugin.isEnabled()) { - return gen_plugin.getDefaultWorldGenerator(world, ""); + gen = gen_plugin.getDefaultWorldGenerator(world, ""); } else { - return new HybridGen(world); + gen = new HybridGen(world); } + return new BukkitGeneratorWrapper(world, gen); } @Override diff --git a/src/main/java/com/plotsquared/bukkit/generator/AugmentedPopulator.java b/src/main/java/com/plotsquared/bukkit/generator/AugmentedPopulator.java index 8a8efba8e..e9d2f0801 100644 --- a/src/main/java/com/plotsquared/bukkit/generator/AugmentedPopulator.java +++ b/src/main/java/com/plotsquared/bukkit/generator/AugmentedPopulator.java @@ -15,7 +15,6 @@ import com.intellectualcrafters.plot.PS; import com.plotsquared.bukkit.object.BlockWrapper; import com.intellectualcrafters.plot.object.Location; import com.intellectualcrafters.plot.object.PlotCluster; -import com.plotsquared.bukkit.object.PlotGenerator; import com.intellectualcrafters.plot.object.PlotId; import com.intellectualcrafters.plot.object.PlotLoc; import com.intellectualcrafters.plot.object.PlotManager; @@ -32,7 +31,7 @@ public class AugmentedPopulator extends BlockPopulator { public static short[][] z_loc; public final PlotWorld plotworld; public final PlotManager manager; - public final PlotGenerator generator; + public final BukkitPlotGenerator generator; public final PlotCluster cluster; public final Random r = new Random(); public final boolean p; @@ -43,7 +42,7 @@ public class AugmentedPopulator extends BlockPopulator { private final int tx; private final int tz; - public AugmentedPopulator(final String world, final PlotGenerator generator, final PlotCluster cluster, final boolean p, final boolean b) { + public AugmentedPopulator(final String world, final BukkitPlotGenerator generator, final PlotCluster cluster, final boolean p, final boolean b) { initCache(); this.cluster = cluster; this.generator = generator; diff --git a/src/main/java/com/plotsquared/bukkit/generator/BukkitGeneratorWrapper.java b/src/main/java/com/plotsquared/bukkit/generator/BukkitGeneratorWrapper.java new file mode 100644 index 000000000..4b42a1976 --- /dev/null +++ b/src/main/java/com/plotsquared/bukkit/generator/BukkitGeneratorWrapper.java @@ -0,0 +1,84 @@ +package com.plotsquared.bukkit.generator; + +import org.bukkit.generator.ChunkGenerator; + +import com.intellectualcrafters.plot.PS; +import com.intellectualcrafters.plot.generator.PlotGenerator2; +import com.intellectualcrafters.plot.object.PlotCluster; +import com.intellectualcrafters.plot.object.PlotManager; +import com.intellectualcrafters.plot.object.PlotWorld; + +public class BukkitGeneratorWrapper extends PlotGenerator2 { + + public final boolean full; + + public BukkitGeneratorWrapper(String world, ChunkGenerator generator) { + super(world, generator); + full = generator != null; + } + + @Override + public void initialize(PlotWorld plotworld) { + if (generator instanceof BukkitPlotGenerator) { + ((BukkitPlotGenerator) generator).init(plotworld); + } + } + + @Override + public void augment(PlotCluster cluster, PlotWorld plotworld) { + if (generator instanceof BukkitPlotGenerator) { + BukkitPlotGenerator plotgen = (BukkitPlotGenerator) generator; + if (cluster != null) { + new AugmentedPopulator(world, plotgen, cluster, plotworld.TERRAIN == 2, plotworld.TERRAIN != 2); + } + else { + new AugmentedPopulator(world, plotgen, null, plotworld.TERRAIN == 2, plotworld.TERRAIN != 2); + } + } + } + + @Override + public void setGenerator(String gen_string) { + if (gen_string == null) { + generator = new HybridGen(world); + } else { + PlotGenerator2 gen_wrapper = (PlotGenerator2) PS.get().IMP.getGenerator(world, gen_string); + if (gen_wrapper != null) { + generator = gen_wrapper.generator; + } + else { + System.out.print("INVALID GENERATOR: " + gen_string); + } + } + } + + @Override + public PlotWorld getNewPlotWorld(String world) { + if (!(generator instanceof BukkitPlotGenerator)) { + return null; + } + return ((BukkitPlotGenerator) generator).getNewPlotWorld(world); + } + + @Override + public PlotManager getPlotManager() { + if (!(generator instanceof BukkitPlotGenerator)) { + return null; + } + return ((BukkitPlotGenerator) generator).getPlotManager(); + } + + @Override + public boolean isFull() { + return full; + } + + @Override + public String getName() { + if (generator == null) { + return "Null"; + } + return generator.getClass().getName(); + } + +} diff --git a/src/main/java/com/plotsquared/bukkit/generator/BukkitPlotGenerator.java b/src/main/java/com/plotsquared/bukkit/generator/BukkitPlotGenerator.java index fc62eceab..82a09b596 100644 --- a/src/main/java/com/plotsquared/bukkit/generator/BukkitPlotGenerator.java +++ b/src/main/java/com/plotsquared/bukkit/generator/BukkitPlotGenerator.java @@ -75,7 +75,8 @@ public abstract class BukkitPlotGenerator extends ChunkGenerator { public List getDefaultPopulators(World world) { try { if (!loaded) { - PS.get().loadWorld(WorldEvents.getName(world), this); + String name = WorldEvents.getName(world); + PS.get().loadWorld(name, new BukkitGeneratorWrapper(name, this)); PlotWorld plotworld = PS.get().getPlotWorld(WorldEvents.getName(world)); if (!plotworld.MOB_SPAWNING) { if (!plotworld.SPAWN_EGGS) { @@ -115,7 +116,8 @@ public abstract class BukkitPlotGenerator extends ChunkGenerator { public short[][] generateExtBlockSections(World world, Random r, int cx, int cz, BiomeGrid biomes) { try { if (!loaded) { - PS.get().loadWorld(WorldEvents.getName(world), this); + String name = WorldEvents.getName(world); + PS.get().loadWorld(name, new BukkitGeneratorWrapper(name, this)); loaded = true; } final int prime = 13; diff --git a/src/main/java/com/plotsquared/bukkit/listeners/WorldEvents.java b/src/main/java/com/plotsquared/bukkit/listeners/WorldEvents.java index d5b95ce3e..70d2d3bc5 100644 --- a/src/main/java/com/plotsquared/bukkit/listeners/WorldEvents.java +++ b/src/main/java/com/plotsquared/bukkit/listeners/WorldEvents.java @@ -9,7 +9,8 @@ import org.bukkit.event.world.WorldLoadEvent; import org.bukkit.generator.ChunkGenerator; import com.intellectualcrafters.plot.PS; -import com.plotsquared.bukkit.object.PlotGenerator; +import com.plotsquared.bukkit.generator.BukkitGeneratorWrapper; +import com.plotsquared.bukkit.generator.BukkitPlotGenerator; import com.plotsquared.bukkit.util.UUIDHandler; public class WorldEvents implements Listener { @@ -30,12 +31,15 @@ public class WorldEvents implements Listener { final World world = event.getWorld(); String name = getName(world); final ChunkGenerator gen = world.getGenerator(); - if (gen instanceof PlotGenerator) { - // - PS.get().loadWorld(name, (PlotGenerator) gen); - } else { + if (gen instanceof BukkitPlotGenerator) { + PS.get().loadWorld(name, new BukkitGeneratorWrapper(name, (BukkitPlotGenerator) gen)); + } + else { if (PS.get().config.contains("worlds." + name)) { - PS.get().loadWorld(name, null); + PS.get().loadWorld(name, new BukkitGeneratorWrapper(name, null)); + } + else if (gen != null) { + System.out.print("NOT INSTANCE OF BukkitGeneratorWrapper: " + gen.getClass().getName()); } } lastWorld = null; diff --git a/src/main/java/com/plotsquared/bukkit/util/bukkit/BukkitSetupUtils.java b/src/main/java/com/plotsquared/bukkit/util/bukkit/BukkitSetupUtils.java index 641fd4fb0..46043ca1b 100644 --- a/src/main/java/com/plotsquared/bukkit/util/bukkit/BukkitSetupUtils.java +++ b/src/main/java/com/plotsquared/bukkit/util/bukkit/BukkitSetupUtils.java @@ -12,7 +12,7 @@ import org.bukkit.plugin.Plugin; import com.intellectualcrafters.plot.PS; import com.intellectualcrafters.plot.config.ConfigurationNode; -import com.plotsquared.bukkit.object.PlotGenerator; +import com.plotsquared.bukkit.generator.BukkitPlotGenerator; import com.intellectualcrafters.plot.object.PlotWorld; import com.intellectualcrafters.plot.object.SetupObject; import com.plotsquared.bukkit.util.SetupUtils; @@ -57,7 +57,7 @@ public class BukkitSetupUtils extends SetupUtils { PS.get().config.set("worlds." + world + "." + "generator.init", object.setupGenerator); } ChunkGenerator gen = generators.get(object.setupGenerator); - if (gen instanceof PlotGenerator) { + if (gen instanceof BukkitPlotGenerator) { object.setupGenerator = null; } } @@ -103,7 +103,7 @@ public class BukkitSetupUtils extends SetupUtils { return null; } ChunkGenerator generator = world.getGenerator(); - if (!(generator instanceof PlotGenerator)) { + if (!(generator instanceof BukkitPlotGenerator)) { return null; } for (Entry entry : generators.entrySet()) {