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.
This commit is contained in:
dordsor21 2019-01-10 17:10:25 +00:00
parent e47b4fef20
commit 3bbd97e7e1
8 changed files with 109 additions and 29 deletions

View File

@ -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<PlotId> 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<PlotId> 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<PlotId> getPlots() {
return Collections.unmodifiableList(this.plots);
}
@Override public HandlerList getHandlers() {
return handlers;
}
}

View File

@ -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;
}
}

View File

@ -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<PlotId> 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<PlotId> 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<PlotId> getPlots() {
return Collections.unmodifiableList(this.plots);
}
@Override public HandlerList getHandlers() {
return handlers;
}

View File

@ -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<PlotId> 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<PlotId> plots) {
return callEvent(new PlotAutoMergeEvent(BukkitUtil.getWorld(plot.getWorldName()), plot, plots));
}
@Override public boolean callUnlink(PlotArea area, List<PlotId> plots) {

View File

@ -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<RegionWrapper> regions = this.getRegions();
final Set<Plot> plots = this.getConnectedPlots();
final ArrayDeque<Plot> 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<Plot> visited = new HashSet<>();
HashSet<PlotId> merged = new HashSet<>();
Set<Plot> connected = this.getConnectedPlots();

View File

@ -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;
}

View File

@ -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<PlotId> plots);
public abstract boolean callMerge(Plot plot, int dir, int max);
public abstract boolean callAutoMerge(Plot plot, List<PlotId> plots);
public abstract boolean callUnlink(PlotArea area, List<PlotId> plots);

View File

@ -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<PlotId> plots) {
@Override public boolean callMerge(Plot plot, int dir, int max){
return false;
}
@Override public boolean callAutoMerge(Plot plot, List<PlotId> plots) {
return false;
}