From 446c47d14882e5f8e683f0b3d6298ae5e8850990 Mon Sep 17 00:00:00 2001 From: dordsor21 Date: Thu, 10 Jan 2019 17:29:25 +0000 Subject: [PATCH] Fix PlotDeleteEvent and PlotMergeEvent not being called. Replaced current PlotMergeEvent with PlotAutoMergeEvent (called when a plot is merged on /plot auto). And made PlotMergeEvent be called for /plot merge command. --- .../bukkit/events/PlotAutoMergeEvent.java | 62 +++++++++++++++++ .../bukkit/events/PlotDeleteEvent.java | 12 +++- .../bukkit/events/PlotMergeEvent.java | 24 ++++--- .../bukkit/util/BukkitEventUtil.java | 13 ++-- .../plot/object/Plot.java | 14 +++- .../plot/object/PlotArea.java | 2 +- .../plot/util/EventUtil.java | 6 +- .../plot/util/EventUtilTest.java | 10 ++- .../nukkit/events/PlotAutoMergeEvent.java | 61 +++++++++++++++++ .../nukkit/events/PlotMergeEvent.java | 30 ++++---- .../nukkit/util/NukkitEventUtil.java | 13 ++-- .../sponge/events/PlotAutoMergeEvent.java | 68 +++++++++++++++++++ .../sponge/events/PlotDeleteEvent.java | 16 ++++- .../sponge/events/PlotMergeEvent.java | 29 ++++---- .../sponge/util/SpongeEventUtil.java | 13 ++-- 15 files changed, 313 insertions(+), 60 deletions(-) create mode 100644 Bukkit/src/main/java/com/plotsquared/bukkit/events/PlotAutoMergeEvent.java create mode 100644 Nukkit/src/main/java/com/plotsquared/nukkit/events/PlotAutoMergeEvent.java create mode 100644 Sponge/src/main/java/com/plotsquared/sponge/events/PlotAutoMergeEvent.java diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/events/PlotAutoMergeEvent.java b/Bukkit/src/main/java/com/plotsquared/bukkit/events/PlotAutoMergeEvent.java new file mode 100644 index 000000000..1f46a61da --- /dev/null +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/events/PlotAutoMergeEvent.java @@ -0,0 +1,62 @@ +package com.plotsquared.bukkit.events; + +import com.intellectualcrafters.plot.object.Plot; +import com.intellectualcrafters.plot.object.PlotId; +import org.bukkit.World; +import org.bukkit.event.Cancellable; +import org.bukkit.event.HandlerList; + +import java.util.ArrayList; + +public class PlotAutoMergeEvent extends PlotEvent implements Cancellable { + + private static final HandlerList handlers = new HandlerList(); + private final ArrayList plots; + private final World world; + private boolean cancelled; + + /** + * PlotMergeEvent: Called when plots are merged + * + * @param world World in which the event occurred + * @param plot Plot that was merged + * @param plots A list of plots involved in the event + */ + public PlotAutoMergeEvent(World world, Plot plot, ArrayList plots) { + super(plot); + this.world = world; + this.plots = plots; + } + + public static HandlerList getHandlerList() { + return handlers; + } + + /** + * Get the plots being added. + * + * @return Plot + */ + public ArrayList getPlots() { + return this.plots; + } + + public World getWorld() { + return this.world; + } + + @Override + public HandlerList getHandlers() { + return handlers; + } + + @Override + public boolean isCancelled() { + return this.cancelled; + } + + @Override + public void setCancelled(boolean b) { + this.cancelled = b; + } +} diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/events/PlotDeleteEvent.java b/Bukkit/src/main/java/com/plotsquared/bukkit/events/PlotDeleteEvent.java index b762519d6..2febbdd47 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/events/PlotDeleteEvent.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/events/PlotDeleteEvent.java @@ -2,15 +2,17 @@ package com.plotsquared.bukkit.events; import com.intellectualcrafters.plot.object.Plot; import com.intellectualcrafters.plot.object.PlotId; +import org.bukkit.event.Cancellable; import org.bukkit.event.HandlerList; /** * Called when a plot is deleted * */ -public class PlotDeleteEvent extends PlotEvent { +public class PlotDeleteEvent extends PlotEvent implements Cancellable { private static final HandlerList handlers = new HandlerList(); + private boolean cancelled; public PlotDeleteEvent(Plot plot) { super(plot); @@ -42,4 +44,12 @@ public class PlotDeleteEvent extends PlotEvent { public HandlerList getHandlers() { return handlers; } + + @Override public boolean isCancelled() { + return this.cancelled; + } + + @Override public void setCancelled(boolean b) { + this.cancelled = b; + } } diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/events/PlotMergeEvent.java b/Bukkit/src/main/java/com/plotsquared/bukkit/events/PlotMergeEvent.java index 09411a77e..bdf35fa9e 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/events/PlotMergeEvent.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/events/PlotMergeEvent.java @@ -11,7 +11,8 @@ import java.util.ArrayList; public class PlotMergeEvent extends PlotEvent implements Cancellable { private static final HandlerList handlers = new HandlerList(); - private final ArrayList plots; + private final int dir; + private final int max; private final World world; private boolean cancelled; @@ -20,25 +21,26 @@ public class PlotMergeEvent extends PlotEvent implements Cancellable { * * @param world World in which the event occurred * @param plot Plot that was merged - * @param plots A list of plots involved in the event + * @param dir The direction of the merge + * @param max Max merge size */ - public PlotMergeEvent(World world, Plot plot, ArrayList plots) { + public PlotMergeEvent(World world, Plot plot, final int dir, final int max) { super(plot); this.world = world; - this.plots = plots; + this.dir = dir; + this.max = max; } public static HandlerList getHandlerList() { return handlers; } - /** - * Get the plots being added. - * - * @return Plot - */ - public ArrayList getPlots() { - return this.plots; + public int getDir() { + return this.dir; + } + + public int getMax() { + return this.max; } public World getWorld() { diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitEventUtil.java b/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitEventUtil.java index 6564c6dc7..ce92a3982 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitEventUtil.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitEventUtil.java @@ -55,8 +55,8 @@ public class BukkitEventUtil extends EventUtil { } @Override - public void callDelete(Plot plot) { - callEvent(new PlotDeleteEvent(plot)); + public boolean callDelete(Plot plot) { + return callEvent(new PlotDeleteEvent(plot)); } @Override @@ -70,8 +70,13 @@ public class BukkitEventUtil extends EventUtil { } @Override - public boolean callMerge(Plot plot, ArrayList plots) { - return callEvent(new PlotMergeEvent(BukkitUtil.getWorld(plot.getWorldName()), plot, plots)); + public boolean callMerge(Plot plot, int dir, int max) { + return callEvent(new PlotMergeEvent(BukkitUtil.getWorld(plot.getWorldName()), plot, dir, max)); + } + + @Override + public boolean callAutoMerge(Plot plot, ArrayList plots) { + return callEvent(new PlotAutoMergeEvent(BukkitUtil.getWorld(plot.getWorldName()), plot, plots)); } @Override diff --git a/Core/src/main/java/com/intellectualcrafters/plot/object/Plot.java b/Core/src/main/java/com/intellectualcrafters/plot/object/Plot.java index cccae20c1..6f5be4b6f 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/object/Plot.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/object/Plot.java @@ -788,9 +788,18 @@ public class Plot { } public boolean clear(boolean checkRunning, final boolean isDelete, final Runnable whenDone) { - if (checkRunning && this.getRunning() != 0 || !EventUtil.manager.callClear(this)) { + if (checkRunning && this.getRunning() != 0) { return false; } + if (isDelete) { + if(!EventUtil.manager.callDelete(this)) { + return false; + } + } else { + if(!EventUtil.manager.callClear(this)) { + return false; + } + } final HashSet regions = this.getRegions(); final Set plots = this.getConnectedPlots(); final ArrayDeque queue = new ArrayDeque<>(plots); @@ -2157,6 +2166,9 @@ public class Plot { if (this.owner == null) { return false; } + if(!EventUtil.manager.callMerge(this, dir, max)) { + return false; + } HashSet visited = new HashSet<>(); HashSet merged = new HashSet<>(); Set connected = this.getConnectedPlots(); diff --git a/Core/src/main/java/com/intellectualcrafters/plot/object/PlotArea.java b/Core/src/main/java/com/intellectualcrafters/plot/object/PlotArea.java index 94472ed59..7d83c9df1 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/object/PlotArea.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/object/PlotArea.java @@ -817,7 +817,7 @@ public abstract class PlotArea { PlotId pos2 = plotIds.get(plotIds.size() - 1); PlotManager manager = getPlotManager(); - boolean result = EventUtil.manager.callMerge(getPlotAbs(pos1), plotIds); + boolean result = EventUtil.manager.callAutoMerge(getPlotAbs(pos1), plotIds); if (!result) { return false; } diff --git a/Core/src/main/java/com/intellectualcrafters/plot/util/EventUtil.java b/Core/src/main/java/com/intellectualcrafters/plot/util/EventUtil.java index b832e3f82..ac95dc16e 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/util/EventUtil.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/util/EventUtil.java @@ -45,7 +45,7 @@ public abstract class EventUtil { public abstract boolean callClear(Plot plot); - public abstract void callDelete(Plot plot); + public abstract boolean callDelete(Plot plot); public abstract boolean callFlagAdd(Flag flag, Plot plot); @@ -53,7 +53,9 @@ public abstract class EventUtil { public abstract boolean callFlagRemove(Flag flag, Object value, PlotCluster cluster); - public abstract boolean callMerge(Plot plot, ArrayList plots); + public abstract boolean callMerge(Plot plot, int dir, int max); + + public abstract boolean callAutoMerge(Plot plot, ArrayList plots); public abstract boolean callUnlink(PlotArea area, ArrayList plots); diff --git a/Core/src/test/java/com/intellectualcrafters/plot/util/EventUtilTest.java b/Core/src/test/java/com/intellectualcrafters/plot/util/EventUtilTest.java index a8b83c2b5..54b33c1df 100644 --- a/Core/src/test/java/com/intellectualcrafters/plot/util/EventUtilTest.java +++ b/Core/src/test/java/com/intellectualcrafters/plot/util/EventUtilTest.java @@ -32,7 +32,9 @@ public class EventUtilTest extends EventUtil { return false; } - @Override public void callDelete(Plot plot) {} + @Override public boolean callDelete(Plot plot) { + return false; + } @Override public boolean callFlagAdd(Flag flag, Plot plot) { return true; @@ -46,7 +48,11 @@ public class EventUtilTest extends EventUtil { return true; } - @Override public boolean callMerge(Plot plot, ArrayList plots) { + @Override public boolean callMerge(Plot plot, int dir, int max) { + return false; + } + + @Override public boolean callAutoMerge(Plot plot, ArrayList plots) { return false; } diff --git a/Nukkit/src/main/java/com/plotsquared/nukkit/events/PlotAutoMergeEvent.java b/Nukkit/src/main/java/com/plotsquared/nukkit/events/PlotAutoMergeEvent.java new file mode 100644 index 000000000..713958e59 --- /dev/null +++ b/Nukkit/src/main/java/com/plotsquared/nukkit/events/PlotAutoMergeEvent.java @@ -0,0 +1,61 @@ +package com.plotsquared.nukkit.events; + +import cn.nukkit.event.Cancellable; +import com.intellectualcrafters.plot.object.Plot; +import cn.nukkit.event.HandlerList; +import cn.nukkit.level.Level; +import com.intellectualcrafters.plot.object.PlotId; + +import java.util.ArrayList; + +public class PlotAutoMergeEvent extends PlotEvent implements Cancellable { + + private static final HandlerList handlers = new HandlerList(); + private final ArrayList plots; + private final Level world; + private boolean cancelled; + + /** + * PlotMergeEvent: Called when plots are merged + * + * @param world World in which the event occurred + * @param plot Plot that was merged + * @param plots A list of plots involved in the event + */ + public PlotAutoMergeEvent(Level world, Plot plot, ArrayList plots) { + super(plot); + this.world = world; + this.plots = plots; + } + + public static HandlerList getHandlerList() { + return handlers; + } + + /** + * Get the plots being added. + * + * @return Plot + */ + public ArrayList getPlots() { + return this.plots; + } + + public Level getWorld() { + return this.world; + } + + public HandlerList getHandlers() { + return handlers; + } + + @Override + public boolean isCancelled() { + return this.cancelled; + } + + @Override + public void setCancelled(boolean b) { + this.cancelled = b; + } +} diff --git a/Nukkit/src/main/java/com/plotsquared/nukkit/events/PlotMergeEvent.java b/Nukkit/src/main/java/com/plotsquared/nukkit/events/PlotMergeEvent.java index 960eb4c03..ba74bbb78 100644 --- a/Nukkit/src/main/java/com/plotsquared/nukkit/events/PlotMergeEvent.java +++ b/Nukkit/src/main/java/com/plotsquared/nukkit/events/PlotMergeEvent.java @@ -4,13 +4,12 @@ import cn.nukkit.event.Cancellable; import cn.nukkit.event.HandlerList; import cn.nukkit.level.Level; import com.intellectualcrafters.plot.object.Plot; -import com.intellectualcrafters.plot.object.PlotId; -import java.util.ArrayList; public class PlotMergeEvent extends PlotEvent implements Cancellable { private static final HandlerList handlers = new HandlerList(); - private final ArrayList plots; + private final int dir; + private final int max; private final Level world; private boolean cancelled; @@ -19,31 +18,32 @@ public class PlotMergeEvent extends PlotEvent implements Cancellable { * * @param world World in which the event occurred * @param plot Plot that was merged - * @param plots A list of plots involved in the event + * @param dir The direction of the merge + * @param max Max merge size */ - public PlotMergeEvent(Level world, Plot plot, ArrayList plots) { + public PlotMergeEvent(Level world, Plot plot, final int dir, final int max) { super(plot); this.world = world; - this.plots = plots; + this.dir = dir; + this.max = max; } public static HandlerList getHandlers() { return handlers; } - /** - * Get the plots being added. - * - * @return Plot - */ - public ArrayList getPlots() { - return this.plots; - } - public Level getLevel() { return this.world; } + public int getDir() { + return this.dir; + } + + public int getMax() { + return this.max; + } + @Override public boolean isCancelled() { return this.cancelled; diff --git a/Nukkit/src/main/java/com/plotsquared/nukkit/util/NukkitEventUtil.java b/Nukkit/src/main/java/com/plotsquared/nukkit/util/NukkitEventUtil.java index c8be83cef..d8e647abb 100644 --- a/Nukkit/src/main/java/com/plotsquared/nukkit/util/NukkitEventUtil.java +++ b/Nukkit/src/main/java/com/plotsquared/nukkit/util/NukkitEventUtil.java @@ -61,8 +61,8 @@ public class NukkitEventUtil extends EventUtil { } @Override - public void callDelete(Plot plot) { - callEvent(new PlotDeleteEvent(plot)); + public boolean callDelete(Plot plot) { + return callEvent(new PlotDeleteEvent(plot)); } @Override @@ -76,8 +76,13 @@ public class NukkitEventUtil extends EventUtil { } @Override - public boolean callMerge(Plot plot, ArrayList plots) { - return callEvent(new PlotMergeEvent(NukkitUtil.getWorld(plot.getWorldName()), plot, plots)); + public boolean callMerge(Plot plot, int dir, int max) { + return callEvent(new PlotMergeEvent(NukkitUtil.getWorld(plot.getWorldName()), plot, dir, max)); + } + + @Override + public boolean callAutoMerge(Plot plot, ArrayList plots) { + return callEvent(new PlotAutoMergeEvent(NukkitUtil.getWorld(plot.getWorldName()), plot, plots)); } @Override diff --git a/Sponge/src/main/java/com/plotsquared/sponge/events/PlotAutoMergeEvent.java b/Sponge/src/main/java/com/plotsquared/sponge/events/PlotAutoMergeEvent.java new file mode 100644 index 000000000..8a0e32d8c --- /dev/null +++ b/Sponge/src/main/java/com/plotsquared/sponge/events/PlotAutoMergeEvent.java @@ -0,0 +1,68 @@ +package com.plotsquared.sponge.events; + +import com.intellectualcrafters.plot.object.Plot; +import com.intellectualcrafters.plot.object.PlotId; +import org.spongepowered.api.event.Cancellable; +import org.spongepowered.api.event.cause.Cause; +import org.spongepowered.api.event.impl.AbstractEvent; +import org.spongepowered.api.world.World; + +import java.util.ArrayList; + +public class PlotAutoMergeEvent extends AbstractEvent implements Cancellable { + + private final ArrayList plots; + private final World world; + private Plot plot; + private boolean cancelled; + + /** + * PlotMergeEvent: Called when plots are merged + * + * @param world World in which the event occurred + * @param plot Plot that was merged + * @param plots A list of plots involved in the event + */ + public PlotAutoMergeEvent(World world, Plot plot, ArrayList plots) { + this.world = world; + this.plots = plots; + this.plot = plot; + } + + /** + * Get the plots being added. + * + * @return Plot + */ + public ArrayList getPlots() { + return this.plots; + } + + /** + * Get the main plot + * + * @return Plot + */ + public Plot getPlot() { + return plot; + } + + public World getWorld() { + return this.world; + } + + @Override + public boolean isCancelled() { + return this.cancelled; + } + + @Override + public void setCancelled(boolean b) { + this.cancelled = b; + } + + @Override + public Cause getCause() { + return null; + } +} diff --git a/Sponge/src/main/java/com/plotsquared/sponge/events/PlotDeleteEvent.java b/Sponge/src/main/java/com/plotsquared/sponge/events/PlotDeleteEvent.java index 72efad2f7..b81ec766d 100644 --- a/Sponge/src/main/java/com/plotsquared/sponge/events/PlotDeleteEvent.java +++ b/Sponge/src/main/java/com/plotsquared/sponge/events/PlotDeleteEvent.java @@ -1,13 +1,15 @@ package com.plotsquared.sponge.events; import com.intellectualcrafters.plot.object.Plot; +import org.spongepowered.api.event.Cancellable; import org.spongepowered.api.event.cause.Cause; import org.spongepowered.api.event.impl.AbstractEvent; import com.intellectualcrafters.plot.object.PlotId; -public class PlotDeleteEvent extends AbstractEvent { +public class PlotDeleteEvent extends AbstractEvent implements Cancellable { private final Plot plot; + private boolean cancelled; /** * PlotDeleteEvent: Called when a plot is deleted @@ -35,7 +37,17 @@ public class PlotDeleteEvent extends AbstractEvent { public String getWorld() { return plot.getWorldName(); } - + + @Override + public boolean isCancelled() { + return cancelled; + } + + @Override + public void setCancelled(final boolean cancel) { + cancelled = cancel; + } + @Override public Cause getCause() { return null; diff --git a/Sponge/src/main/java/com/plotsquared/sponge/events/PlotMergeEvent.java b/Sponge/src/main/java/com/plotsquared/sponge/events/PlotMergeEvent.java index e23dadfa4..776e3a7ae 100644 --- a/Sponge/src/main/java/com/plotsquared/sponge/events/PlotMergeEvent.java +++ b/Sponge/src/main/java/com/plotsquared/sponge/events/PlotMergeEvent.java @@ -11,29 +11,32 @@ import com.intellectualcrafters.plot.object.Plot; import com.intellectualcrafters.plot.object.PlotId; public class PlotMergeEvent extends AbstractEvent implements Cancellable { - private final ArrayList plots; private boolean cancelled; private Plot plot; + private final int dir; + private final int max; private World world; /** * PlotMergeEvent: Called when plots are merged * * @param world World in which the event occurred - * @param plot Plot that was merged - * @param plots A list of plots involved in the event + * @param dir The direction of the merge + * @param max Max merge size */ - public PlotMergeEvent(final World world, final Plot plot, final ArrayList plots) { - this.plots = plots; + public PlotMergeEvent(World world, Plot plot, final int dir, final int max) { + this.world = world; + this.dir = dir; + this.max = max; + this.plot = plot; } - - /** - * Get the plots being added; - * - * @return Plot - */ - public ArrayList getPlots() { - return plots; + + public int getDir() { + return this.dir; + } + + public int getMax() { + return this.max; } /** diff --git a/Sponge/src/main/java/com/plotsquared/sponge/util/SpongeEventUtil.java b/Sponge/src/main/java/com/plotsquared/sponge/util/SpongeEventUtil.java index a6b13e85f..e9922c3e5 100644 --- a/Sponge/src/main/java/com/plotsquared/sponge/util/SpongeEventUtil.java +++ b/Sponge/src/main/java/com/plotsquared/sponge/util/SpongeEventUtil.java @@ -51,8 +51,8 @@ public class SpongeEventUtil extends EventUtil { } @Override - public void callDelete(Plot plot) { - callEvent(new PlotDeleteEvent(plot)); + public boolean callDelete(Plot plot) { + return callEvent(new PlotDeleteEvent(plot)); } @Override @@ -66,8 +66,13 @@ public class SpongeEventUtil extends EventUtil { } @Override - public boolean callMerge(Plot plot, ArrayList plots) { - return callEvent(new PlotMergeEvent(SpongeUtil.getWorld(plot.getWorldName()), plot, plots)); + public boolean callMerge(Plot plot, int dir, int max) { + return callEvent(new PlotMergeEvent(SpongeUtil.getWorld(plot.getWorldName()), plot, dir, max)); + } + + @Override + public boolean callAutoMerge(Plot plot, ArrayList plots) { + return callEvent(new PlotAutoMergeEvent(SpongeUtil.getWorld(plot.getWorldName()), plot, plots)); } @Override