From 5af9536db1ebed641aeaa7bd3243f69187e798a1 Mon Sep 17 00:00:00 2001 From: Joshua Date: Fri, 8 Jan 2021 21:20:03 +0100 Subject: [PATCH] Add WarpModifyEvent (#3875) Co-authored-by: Josh Roy <10731363+JRoy@users.noreply.github.com> --- .../essentials/commands/Commanddelwarp.java | 18 +++- .../essentials/commands/Commandsetwarp.java | 17 +++- .../api/v2/events/WarpModifyEvent.java | 91 +++++++++++++++++++ 3 files changed, 121 insertions(+), 5 deletions(-) create mode 100644 Essentials/src/main/java/net/essentialsx/api/v2/events/WarpModifyEvent.java diff --git a/Essentials/src/main/java/com/earth2me/essentials/commands/Commanddelwarp.java b/Essentials/src/main/java/com/earth2me/essentials/commands/Commanddelwarp.java index df938bd5a..654090702 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/commands/Commanddelwarp.java +++ b/Essentials/src/main/java/com/earth2me/essentials/commands/Commanddelwarp.java @@ -1,6 +1,9 @@ package com.earth2me.essentials.commands; import com.earth2me.essentials.CommandSource; +import net.essentialsx.api.v2.events.WarpModifyEvent; +import org.bukkit.Bukkit; +import org.bukkit.Location; import org.bukkit.Server; import java.util.ArrayList; @@ -19,9 +22,18 @@ public class Commanddelwarp extends EssentialsCommand { if (args.length == 0) { throw new NotEnoughArgumentsException(); } - - ess.getWarps().removeWarp(args[0]); - sender.sendMessage(tl("deleteWarp", args[0])); + //Check if warp exists before calling the event + final Location location = ess.getWarps().getWarp(args[0]); + if (location != null) { + final WarpModifyEvent event = new WarpModifyEvent(sender.getUser(this.ess), args[0], location, null, WarpModifyEvent.WarpModifyCause.DELETE); + Bukkit.getServer().getPluginManager().callEvent(event); + if (event.isCancelled()) { + return; + } + ess.getWarps().removeWarp(args[0]); + } else { + throw new Exception(tl("warpNotExist")); + } } @Override diff --git a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandsetwarp.java b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandsetwarp.java index 298205b9d..6019fc2c7 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandsetwarp.java +++ b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandsetwarp.java @@ -5,6 +5,8 @@ import com.earth2me.essentials.api.IWarps; import com.earth2me.essentials.utils.NumberUtil; import com.earth2me.essentials.utils.StringUtil; import net.ess3.api.InvalidWorldException; +import net.essentialsx.api.v2.events.WarpModifyEvent; +import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Server; @@ -32,8 +34,19 @@ public class Commandsetwarp extends EssentialsCommand { warpLoc = warps.getWarp(args[0]); } catch (final WarpNotFoundException | InvalidWorldException ignored) { } - - if (warpLoc == null || user.isAuthorized("essentials.warp.overwrite." + StringUtil.safeString(args[0]))) { + if (warpLoc == null) { + final WarpModifyEvent event = new WarpModifyEvent(user, args[0], null, user.getLocation(), WarpModifyEvent.WarpModifyCause.CREATE); + Bukkit.getServer().getPluginManager().callEvent(event); + if (event.isCancelled()) { + return; + } + warps.setWarp(user, args[0], user.getLocation()); + } else if (user.isAuthorized("essentials.warp.overwrite." + StringUtil.safeString(args[0]))) { + final WarpModifyEvent event = new WarpModifyEvent(user, args[0], warpLoc, user.getLocation(), WarpModifyEvent.WarpModifyCause.UPDATE); + Bukkit.getServer().getPluginManager().callEvent(event); + if (event.isCancelled()) { + return; + } warps.setWarp(user, args[0], user.getLocation()); } else { throw new Exception(tl("warpOverwrite")); diff --git a/Essentials/src/main/java/net/essentialsx/api/v2/events/WarpModifyEvent.java b/Essentials/src/main/java/net/essentialsx/api/v2/events/WarpModifyEvent.java new file mode 100644 index 000000000..64586008d --- /dev/null +++ b/Essentials/src/main/java/net/essentialsx/api/v2/events/WarpModifyEvent.java @@ -0,0 +1,91 @@ +package net.essentialsx.api.v2.events; + +import net.ess3.api.IUser; +import org.bukkit.Location; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +/** + * Called when a warp is about to be modified. + * Includes creation and deletion as described in {@link WarpModifyCause}. + */ +public class WarpModifyEvent extends Event implements Cancellable { + private static final HandlerList handlers = new HandlerList(); + + private final IUser user; + private final String warpName; + private final Location oldLocation; + private final Location newLocation; + private final WarpModifyCause cause; + private boolean cancelled; + + /** + * @param user the {@link IUser} who is modifing the warp. + * @param warpName the name of the warp that's being altered. + * @param oldLocation the old location before being modified. Null if {@link WarpModifyCause#CREATE}. + * @param newLocation the new location after being modified. Null if {@link WarpModifyCause#DELETE}. + * @param cause the cause of change. + */ + public WarpModifyEvent(IUser user, String warpName, Location oldLocation, Location newLocation, WarpModifyCause cause) { + this.user = user; + this.warpName = warpName; + this.oldLocation = oldLocation; + this.newLocation = newLocation; + this.cause = cause; + } + + @Override + public boolean isCancelled() { + return cancelled; + } + + @Override + public void setCancelled(boolean cancel) { + this.cancelled = cancel; + } + + public IUser getUser() { + return user; + } + + public WarpModifyCause getCause() { + return cause; + } + + public String getWarpName() { + return warpName; + } + + /** + * Gets the current location of the warp or null if it's being created. + * @return The warps new location or null. + */ + public Location getOldLocation() { + return oldLocation; + } + + /** + * Gets the new location this warp is being updated to, or null if it's being deleted. + * @return The warps new location or null. + */ + public Location getNewLocation() { + return newLocation; + } + + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } + + /** + * The cause of why a warp was modified. + * Used by {@link WarpModifyEvent}. + */ + public enum WarpModifyCause { + UPDATE, CREATE, DELETE + } +}