Add WarpModifyEvent (#3875)

Co-authored-by: Josh Roy <10731363+JRoy@users.noreply.github.com>
This commit is contained in:
Joshua 2021-01-08 21:20:03 +01:00 committed by GitHub
parent cb7b2dabf5
commit 5af9536db1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 121 additions and 5 deletions

View File

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

View File

@ -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"));

View File

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