From 3bbd97e7e1410b68f98f59ad4008820ddbf675b9 Mon Sep 17 00:00:00 2001 From: dordsor21 Date: Thu, 10 Jan 2019 17:10: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 | 56 +++++++++++++++++++ .../bukkit/events/PlotDeleteEvent.java | 16 +++++- .../bukkit/events/PlotMergeEvent.java | 23 +++----- .../bukkit/util/BukkitEventUtil.java | 12 ++-- .../plotsquared/plot/object/Plot.java | 14 ++++- .../plotsquared/plot/object/PlotArea.java | 2 +- .../plotsquared/plot/util/EventUtil.java | 6 +- .../plotsquared/plot/util/EventUtilTest.java | 9 ++- 8 files changed, 109 insertions(+), 29 deletions(-) create mode 100644 Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/events/PlotAutoMergeEvent.java diff --git a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/events/PlotAutoMergeEvent.java b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/events/PlotAutoMergeEvent.java new file mode 100644 index 000000000..88c9773c9 --- /dev/null +++ b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/events/PlotAutoMergeEvent.java @@ -0,0 +1,56 @@ +package com.github.intellectualsites.plotsquared.bukkit.events; + +import com.github.intellectualsites.plotsquared.plot.object.Plot; +import com.github.intellectualsites.plotsquared.plot.object.PlotId; +import lombok.Getter; +import lombok.Setter; +import org.bukkit.World; +import org.bukkit.event.Cancellable; +import org.bukkit.event.HandlerList; + +import javax.annotation.Nonnull; +import java.util.Collections; +import java.util.List; + +/** + * Event called when plots are automatically merged with /plot auto + * {@inheritDoc} + */ +public final class PlotAutoMergeEvent extends PlotEvent implements Cancellable { + + private static final HandlerList handlers = new HandlerList(); + private final List plots; + @Getter private final World world; + @Getter @Setter private boolean cancelled; + + /** + * PlotAutoMergeEvent: Called when plots are automatically merged with /plot auto + * + * @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(@Nonnull final World world, @Nonnull final Plot plot, + @Nonnull final List plots) { + super(plot); + this.world = world; + this.plots = plots; + } + + public static HandlerList getHandlerList() { + return handlers; + } + + /** + * Get the plots being added. + * + * @return Unmodifiable list containing the merging plots + */ + public List getPlots() { + return Collections.unmodifiableList(this.plots); + } + + @Override public HandlerList getHandlers() { + return handlers; + } +} diff --git a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/events/PlotDeleteEvent.java b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/events/PlotDeleteEvent.java index d8669279d..5199496ff 100644 --- a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/events/PlotDeleteEvent.java +++ b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/events/PlotDeleteEvent.java @@ -2,14 +2,16 @@ package com.github.intellectualsites.plotsquared.bukkit.events; import com.github.intellectualsites.plotsquared.plot.object.Plot; import com.github.intellectualsites.plotsquared.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); @@ -20,7 +22,7 @@ public class PlotDeleteEvent extends PlotEvent { } /** - * Get the PlotId + * Get the PlotId. * * @return PlotId */ @@ -29,7 +31,7 @@ public class PlotDeleteEvent extends PlotEvent { } /** - * Get the world name + * Get the world name. * * @return String */ @@ -40,4 +42,12 @@ public class PlotDeleteEvent extends PlotEvent { @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/github/intellectualsites/plotsquared/bukkit/events/PlotMergeEvent.java b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/events/PlotMergeEvent.java index b03729be0..493145f2a 100644 --- a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/events/PlotMergeEvent.java +++ b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/events/PlotMergeEvent.java @@ -1,7 +1,6 @@ package com.github.intellectualsites.plotsquared.bukkit.events; import com.github.intellectualsites.plotsquared.plot.object.Plot; -import com.github.intellectualsites.plotsquared.plot.object.PlotId; import lombok.Getter; import lombok.Setter; import org.bukkit.World; @@ -9,8 +8,6 @@ import org.bukkit.event.Cancellable; import org.bukkit.event.HandlerList; import javax.annotation.Nonnull; -import java.util.Collections; -import java.util.List; /** * Event called when several plots are merged @@ -19,7 +16,8 @@ import java.util.List; public final class PlotMergeEvent extends PlotEvent implements Cancellable { private static final HandlerList handlers = new HandlerList(); - private final List plots; + @Getter private final int dir; + @Getter private final int max; @Getter private final World world; @Getter @Setter private boolean cancelled; @@ -28,28 +26,21 @@ public final 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(@Nonnull final World world, @Nonnull final Plot plot, - @Nonnull final List plots) { + @Nonnull final int dir, @Nonnull 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 Unmodifiable list containing the merging plots - */ - public List getPlots() { - return Collections.unmodifiableList(this.plots); - } - @Override public HandlerList getHandlers() { return handlers; } diff --git a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/BukkitEventUtil.java b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/BukkitEventUtil.java index b198084d6..808dabb7a 100644 --- a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/BukkitEventUtil.java +++ b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/BukkitEventUtil.java @@ -48,8 +48,8 @@ public final class BukkitEventUtil extends EventUtil { return callEvent(new PlotClearEvent(plot)); } - @Override public void callDelete(Plot plot) { - callEvent(new PlotDeleteEvent(plot)); + @Override public boolean callDelete(Plot plot) { + return callEvent(new PlotDeleteEvent(plot)); } @Override public boolean callFlagAdd(Flag flag, Plot plot) { @@ -60,8 +60,12 @@ public final class BukkitEventUtil extends EventUtil { return callEvent(new PlotFlagRemoveEvent(flag, plot)); } - @Override public boolean callMerge(Plot plot, List plots) { - return callEvent(new PlotMergeEvent(BukkitUtil.getWorld(plot.getWorldName()), plot, plots)); + @Override 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, List plots) { + return callEvent(new PlotAutoMergeEvent(BukkitUtil.getWorld(plot.getWorldName()), plot, plots)); } @Override public boolean callUnlink(PlotArea area, List plots) { diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/Plot.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/Plot.java index ef5277bdc..545098b45 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/Plot.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/Plot.java @@ -791,9 +791,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); @@ -2156,6 +2165,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/github/intellectualsites/plotsquared/plot/object/PlotArea.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotArea.java index c8173ed15..4bd13d91a 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 @@ -808,7 +808,7 @@ public abstract class PlotArea { final PlotId pos2 = plotIds.get(plotIds.size() - 1); final PlotManager manager = getPlotManager(); - final boolean result = EventUtil.manager.callMerge(getPlotAbs(pos1), plotIds); + final boolean result = EventUtil.manager.callAutoMerge(getPlotAbs(pos1), plotIds); if (!result) { return false; } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/EventUtil.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/EventUtil.java index 4773d1696..b8037944e 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/EventUtil.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/EventUtil.java @@ -37,7 +37,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); @@ -45,7 +45,9 @@ public abstract class EventUtil { public abstract boolean callFlagRemove(Flag flag, Object value, PlotCluster cluster); - public abstract boolean callMerge(Plot plot, List plots); + public abstract boolean callMerge(Plot plot, int dir, int max); + + public abstract boolean callAutoMerge(Plot plot, List plots); public abstract boolean callUnlink(PlotArea area, List plots); diff --git a/Core/src/test/java/com/github/intellectualsites/plotsquared/plot/util/EventUtilTest.java b/Core/src/test/java/com/github/intellectualsites/plotsquared/plot/util/EventUtilTest.java index aee8176b9..011fbb7f3 100644 --- a/Core/src/test/java/com/github/intellectualsites/plotsquared/plot/util/EventUtilTest.java +++ b/Core/src/test/java/com/github/intellectualsites/plotsquared/plot/util/EventUtilTest.java @@ -28,7 +28,8 @@ 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) { @@ -43,7 +44,11 @@ public class EventUtilTest extends EventUtil { return true; } - @Override public boolean callMerge(Plot plot, List plots) { + @Override public boolean callMerge(Plot plot, int dir, int max){ + return false; + } + + @Override public boolean callAutoMerge(Plot plot, List plots) { return false; }