Clean up some events and utility classes. Try to use as general type

declarations as possible. Add configurable override for the unsafe block checker.
This commit is contained in:
Sauilitired
2018-12-28 07:39:39 +01:00
parent 660754511b
commit 12b8ae3eed
16 changed files with 226 additions and 290 deletions

View File

@ -2,18 +2,26 @@ 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 java.util.ArrayList;
import javax.annotation.Nonnull;
import java.util.Collections;
import java.util.List;
public class PlotMergeEvent extends PlotEvent implements Cancellable {
/**
* Event called when several plots are merged
* {@inheritDoc}
*/
public final class PlotMergeEvent extends PlotEvent implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private final ArrayList<PlotId> plots;
private final World world;
private boolean cancelled;
private final List<PlotId> plots;
@Getter private final World world;
@Getter @Setter private boolean cancelled;
/**
* PlotMergeEvent: Called when plots are merged
@ -22,7 +30,8 @@ public class PlotMergeEvent extends PlotEvent implements Cancellable {
* @param plot Plot that was merged
* @param plots A list of plots involved in the event
*/
public PlotMergeEvent(World world, Plot plot, ArrayList<PlotId> plots) {
public PlotMergeEvent(@Nonnull final World world, @Nonnull final Plot plot,
@Nonnull final List<PlotId> plots) {
super(plot);
this.world = world;
this.plots = plots;
@ -35,25 +44,13 @@ public class PlotMergeEvent extends PlotEvent implements Cancellable {
/**
* Get the plots being added.
*
* @return Plot
* @return Unmodifiable list containing the merging plots
*/
public ArrayList<PlotId> getPlots() {
return this.plots;
}
public World getWorld() {
return this.world;
public List<PlotId> getPlots() {
return Collections.unmodifiableList(this.plots);
}
@Override public HandlerList getHandlers() {
return handlers;
}
@Override public boolean isCancelled() {
return this.cancelled;
}
@Override public void setCancelled(boolean b) {
this.cancelled = b;
}
}

View File

@ -2,20 +2,28 @@ package com.github.intellectualsites.plotsquared.bukkit.events;
import com.github.intellectualsites.plotsquared.plot.object.PlotArea;
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.Event;
import org.bukkit.event.HandlerList;
import java.util.ArrayList;
import javax.annotation.Nonnull;
import java.util.Collections;
import java.util.List;
public class PlotUnlinkEvent extends Event implements Cancellable {
/**
* Event called when several merged plots are unlinked
* {@inheritDoc}
*/
public final class PlotUnlinkEvent extends Event implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private final ArrayList<PlotId> plots;
private final World world;
private final PlotArea area;
private boolean cancelled;
private final List<PlotId> plots;
@Getter private final World world;
@Getter private final PlotArea area;
@Getter @Setter private boolean cancelled;
/**
* Called when a mega-plot is unlinked.
@ -23,7 +31,8 @@ public class PlotUnlinkEvent extends Event implements Cancellable {
* @param world World in which the event occurred
* @param plots Plots that are involved in the event
*/
public PlotUnlinkEvent(World world, PlotArea area, ArrayList<PlotId> plots) {
public PlotUnlinkEvent(@Nonnull final World world, @Nonnull final PlotArea area,
@Nonnull final List<PlotId> plots) {
this.plots = plots;
this.world = world;
this.area = area;
@ -36,29 +45,13 @@ public class PlotUnlinkEvent extends Event implements Cancellable {
/**
* Get the plots involved.
*
* @return The {@link PlotId}'s of the plots involved
* @return Unmodifiable list containing {@link PlotId PlotIds} of the plots involved
*/
public ArrayList<PlotId> getPlots() {
return this.plots;
}
public World getWorld() {
return this.world;
}
public PlotArea getArea() {
return this.area;
public List<PlotId> getPlots() {
return Collections.unmodifiableList(this.plots);
}
@Override public HandlerList getHandlers() {
return handlers;
}
@Override public boolean isCancelled() {
return this.cancelled;
}
@Override public void setCancelled(boolean b) {
this.cancelled = b;
}
}

View File

@ -10,20 +10,24 @@ import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class BukkitEventUtil extends EventUtil {
/**
* Utility class for handling Bukkit {@link Event events}
*/
public final class BukkitEventUtil extends EventUtil {
public Player getPlayer(PlotPlayer player) {
@Nullable public Player getPlayer(final PlotPlayer player) {
if (player instanceof BukkitPlayer) {
return ((BukkitPlayer) player).player;
}
return null;
}
public boolean callEvent(Event event) {
private boolean callEvent(@Nonnull final Event event) {
Bukkit.getServer().getPluginManager().callEvent(event);
return !(event instanceof Cancellable) || !((Cancellable) event).isCancelled();
}
@ -56,11 +60,11 @@ public class BukkitEventUtil extends EventUtil {
return callEvent(new PlotFlagRemoveEvent(flag, plot));
}
@Override public boolean callMerge(Plot plot, ArrayList<PlotId> plots) {
@Override public boolean callMerge(Plot plot, List<PlotId> plots) {
return callEvent(new PlotMergeEvent(BukkitUtil.getWorld(plot.getWorldName()), plot, plots));
}
@Override public boolean callUnlink(PlotArea area, ArrayList<PlotId> plots) {
@Override public boolean callUnlink(PlotArea area, List<PlotId> plots) {
return callEvent(new PlotUnlinkEvent(BukkitUtil.getWorld(area.worldname), area, plots));
}