From d106262454f4b470ad3c6d0ffad3302e131a6052 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20Kriv=C3=A1cs=20Schr=C3=B8der?= Date: Fri, 31 May 2019 15:32:24 +0200 Subject: [PATCH] Have PlotAreas create their own PlotManager Leads to cleaner code, less need for instanceof and casting, as a PlotArea knows which kind of PlotManager it needs to make --- .../bukkit/generator/DelegatePlotGenerator.java | 4 ---- .../plotsquared/plot/PlotSquared.java | 2 +- .../plotsquared/plot/generator/ClassicPlotManager.java | 10 +++------- .../plotsquared/plot/generator/HybridGen.java | 4 ---- .../plotsquared/plot/generator/HybridPlotManager.java | 10 +++------- .../plotsquared/plot/generator/HybridPlotWorld.java | 6 ++++++ .../plot/generator/IndependentPlotGenerator.java | 8 -------- .../plotsquared/plot/generator/SquarePlotManager.java | 10 +++------- .../plotsquared/plot/object/PlotArea.java | 4 +++- .../plotsquared/plot/object/PlotManager.java | 1 - .../plotsquared/plot/object/worlds/SinglePlotArea.java | 5 +++++ .../plot/object/worlds/SingleWorldGenerator.java | 4 ---- 12 files changed, 24 insertions(+), 44 deletions(-) diff --git a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/generator/DelegatePlotGenerator.java b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/generator/DelegatePlotGenerator.java index f07c65cbf..c8b57dc78 100644 --- a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/generator/DelegatePlotGenerator.java +++ b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/generator/DelegatePlotGenerator.java @@ -30,10 +30,6 @@ import java.util.Random; @Override public void initialize(PlotArea area) { } - @Override public PlotManager getNewPlotManager(PlotArea plotArea) { - return PlotSquared.get().IMP.getDefaultGenerator().getNewPlotManager(plotArea); - } - @Override public String getName() { return this.chunkGenerator.getClass().getName(); } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/PlotSquared.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/PlotSquared.java index e3164427c..589309385 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/PlotSquared.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/PlotSquared.java @@ -1086,7 +1086,7 @@ import java.util.zip.ZipInputStream; } // Conventional plot generator PlotArea plotArea = plotGenerator.getNewPlotArea(world, null, null, null); - PlotManager plotManager = plotGenerator.getNewPlotManager(plotArea); + PlotManager plotManager = plotArea.getPlotManager(); PlotSquared.log(Captions.PREFIX + "&aDetected world load for '" + world + "'"); PlotSquared .log(Captions.PREFIX + "&3 - generator: &7" + baseGenerator + ">" + plotGenerator); diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/ClassicPlotManager.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/ClassicPlotManager.java index a822c3243..6d5e159f8 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/ClassicPlotManager.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/ClassicPlotManager.java @@ -15,13 +15,9 @@ public class ClassicPlotManager extends SquarePlotManager { private final ClassicPlotWorld classicPlotWorld; - public ClassicPlotManager(PlotArea plotArea) { - super(plotArea); - if (plotArea instanceof ClassicPlotWorld){ - classicPlotWorld = (ClassicPlotWorld)plotArea; - } else { - throw new RuntimeException("ClassicPlotManager requires plotArea to be an instance of ClassicPlotWorld"); - } + public ClassicPlotManager(ClassicPlotWorld classicPlotWorld) { + super(classicPlotWorld); + this.classicPlotWorld = classicPlotWorld; } @Override public boolean setComponent(PlotId plotId, String component, diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/HybridGen.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/HybridGen.java index 8d34f5d25..81d76b929 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/HybridGen.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/HybridGen.java @@ -253,10 +253,6 @@ public class HybridGen extends IndependentPlotGenerator { return new HybridPlotWorld(world, id, this, min, max); } - @Override public PlotManager getNewPlotManager(PlotArea plotArea) { - return new HybridPlotManager(plotArea); - } - @Override public void initialize(PlotArea area) { // All initialization is done in the PlotArea class } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/HybridPlotManager.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/HybridPlotManager.java index 0bbf67557..ac10f7aab 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/HybridPlotManager.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/HybridPlotManager.java @@ -23,13 +23,9 @@ public class HybridPlotManager extends ClassicPlotManager { private final HybridPlotWorld hybridPlotWorld; - public HybridPlotManager(PlotArea plotArea) { - super(plotArea); - if (plotArea instanceof HybridPlotWorld){ - hybridPlotWorld = (HybridPlotWorld)plotArea; - } else { - throw new RuntimeException("HybridPlotManager requires plotArea to be an instance of HybridPlotWorld"); - } + public HybridPlotManager(HybridPlotWorld hybridPlotWorld) { + super(hybridPlotWorld); + this.hybridPlotWorld = hybridPlotWorld; } @Override public void exportTemplate() throws IOException { diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/HybridPlotWorld.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/HybridPlotWorld.java index e8f1ed06a..101b6fcb3 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/HybridPlotWorld.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/HybridPlotWorld.java @@ -7,6 +7,7 @@ import com.github.intellectualsites.plotsquared.plot.object.Location; import com.github.intellectualsites.plotsquared.plot.object.Plot; import com.github.intellectualsites.plotsquared.plot.object.PlotArea; import com.github.intellectualsites.plotsquared.plot.object.PlotId; +import com.github.intellectualsites.plotsquared.plot.object.PlotManager; import com.github.intellectualsites.plotsquared.plot.object.schematic.Schematic; import com.github.intellectualsites.plotsquared.plot.util.MainUtil; import com.github.intellectualsites.plotsquared.plot.util.MathMan; @@ -43,6 +44,11 @@ public class HybridPlotWorld extends ClassicPlotWorld { super(worldName, id, generator, min, max); } + @Override + protected PlotManager createManager() { + return new HybridPlotManager(this); + } + public static byte wrap(byte data, int start) { if ((data >= start) && (data < (start + 4))) { data = (byte) ((((data - start) + 2) & 3) + start); diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/IndependentPlotGenerator.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/IndependentPlotGenerator.java index 44703be52..459aee272 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/IndependentPlotGenerator.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/IndependentPlotGenerator.java @@ -49,14 +49,6 @@ public abstract class IndependentPlotGenerator { */ public abstract PlotArea getNewPlotArea(String world, String id, PlotId min, PlotId max); - /** - * Return a new PlotManager object. - * - * @return - * @param plotArea - */ - public abstract PlotManager getNewPlotManager(PlotArea plotArea); - /** * If any additional setup options need to be changed before world creation. * - e.g. If setup doesn't support some standard options diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/SquarePlotManager.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/SquarePlotManager.java index 967802b24..be19930fd 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/SquarePlotManager.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/SquarePlotManager.java @@ -16,13 +16,9 @@ public abstract class SquarePlotManager extends GridPlotManager { private final SquarePlotWorld squarePlotWorld; - public SquarePlotManager(PlotArea plotArea) { - super(plotArea); - if (plotArea instanceof SquarePlotWorld){ - squarePlotWorld = (SquarePlotWorld)plotArea; - } else { - throw new RuntimeException("SquarePlotManager requires plotArea to be an instance of SquarePlotWorld"); - } + public SquarePlotManager(SquarePlotWorld squarePlotWorld) { + super(squarePlotWorld); + this.squarePlotWorld = squarePlotWorld; } @Override diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotArea.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotArea.java index 885371c0e..3ed1e2c95 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotArea.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotArea.java @@ -81,7 +81,7 @@ public abstract class PlotArea { @Nullable final PlotId max) { this.worldname = worldName; this.id = id; - this.manager = generator.getNewPlotManager(this); + this.manager = createManager(); this.generator = generator; if (min == null || max == null) { if (min != max) { @@ -102,6 +102,8 @@ public abstract class PlotArea { } } + protected abstract PlotManager createManager(); + public LocalBlockQueue getQueue(final boolean autoQueue) { return GlobalBlockQueue.IMP.getNewQueue(worldname, autoQueue); } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotManager.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotManager.java index f00fb40db..ac2a57058 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotManager.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotManager.java @@ -13,7 +13,6 @@ public abstract class PlotManager { private final PlotArea plotArea; public PlotManager(PlotArea plotArea) { - this.plotArea = plotArea; } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/worlds/SinglePlotArea.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/worlds/SinglePlotArea.java index e1f6505f6..41801c087 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/worlds/SinglePlotArea.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/worlds/SinglePlotArea.java @@ -26,6 +26,11 @@ public class SinglePlotArea extends GridPlotWorld { this.DEFAULT_HOME = new PlotLoc(Integer.MAX_VALUE, Integer.MAX_VALUE); } + @Override + protected PlotManager createManager() { + return new SinglePlotManager(this); + } + @Override public void loadConfiguration(ConfigurationSection config) { VOID = config.getBoolean("void", false); } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/worlds/SingleWorldGenerator.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/worlds/SingleWorldGenerator.java index a821c03a4..9ba3df278 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/worlds/SingleWorldGenerator.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/worlds/SingleWorldGenerator.java @@ -81,10 +81,6 @@ public class SingleWorldGenerator extends IndependentPlotGenerator { return ((SinglePlotAreaManager) PlotSquared.get().getPlotAreaManager()).getArea(); } - @Override public PlotManager getNewPlotManager(PlotArea plotArea) { - return new SinglePlotManager(plotArea); - } - @Override public void initialize(PlotArea area) { }