Add HomeModifyEvent for home create/delete/rename/set (#5216)

Implements #5213.
This commit is contained in:
Josh Roy 2023-05-02 15:48:28 -04:00 committed by GitHub
parent e5b0c4c855
commit 3d23916ad5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 183 additions and 5 deletions

View File

@ -3,6 +3,8 @@ package com.earth2me.essentials.commands;
import com.earth2me.essentials.CommandSource;
import com.earth2me.essentials.IUser;
import com.earth2me.essentials.User;
import net.essentialsx.api.v2.events.HomeModifyEvent;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import java.util.ArrayList;
@ -37,18 +39,27 @@ public class Commanddelhome extends EssentialsCommand {
if (expandedArg.length > 1 && (user == null || user.isAuthorized("essentials.delhome.others"))) {
user = getPlayer(server, expandedArg, 0, true, true);
name = expandedArg[1];
name = expandedArg[1].toLowerCase(Locale.ENGLISH);
} else if (user == null) {
throw new NotEnoughArgumentsException();
} else {
name = expandedArg[0];
name = expandedArg[0].toLowerCase(Locale.ENGLISH);
}
if (name.equalsIgnoreCase("bed")) {
if (name.equals("bed")) {
throw new Exception(tl("invalidHomeName"));
}
user.delHome(name.toLowerCase(Locale.ENGLISH));
final HomeModifyEvent event = new HomeModifyEvent(sender.getUser(ess), user, name, user.getHome(name), false);
Bukkit.getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) {
if (ess.getSettings().isDebug()) {
ess.getLogger().info("HomeModifyEvent canceled for /delhome execution by " + sender.getDisplayName());
}
return;
}
user.delHome(name);
sender.sendMessage(tl("deleteHome", name));
}

View File

@ -4,6 +4,8 @@ import com.earth2me.essentials.CommandSource;
import com.earth2me.essentials.User;
import com.earth2me.essentials.utils.NumberUtil;
import net.ess3.api.IUser;
import net.essentialsx.api.v2.events.HomeModifyEvent;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import java.util.ArrayList;
@ -60,6 +62,15 @@ public class Commandrenamehome extends EssentialsCommand {
throw new NoSuchFieldException(tl("invalidHomeName"));
}
final HomeModifyEvent event = new HomeModifyEvent(user, usersHome, oldName, newName, usersHome.getHome(oldName));
Bukkit.getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) {
if (ess.getSettings().isDebug()) {
ess.getLogger().info("HomeModifyEvent canceled for /renamehome execution by " + user.getDisplayName());
}
return;
}
usersHome.renameHome(oldName, newName);
user.sendMessage(tl("homeRenamed", oldName, newName));
usersHome.setLastHomeConfirmation(null);

View File

@ -4,6 +4,8 @@ import com.earth2me.essentials.CommandSource;
import com.earth2me.essentials.User;
import com.earth2me.essentials.utils.LocationUtil;
import com.earth2me.essentials.utils.NumberUtil;
import net.essentialsx.api.v2.events.HomeModifyEvent;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Server;
@ -62,8 +64,25 @@ public class Commandsethome extends EssentialsCommand {
return;
}
final Location prevHomeLoc = usersHome.getHome(name);
final HomeModifyEvent event;
if (prevHomeLoc == null) {
event = new HomeModifyEvent(user, usersHome, name, location, true);
} else {
event = new HomeModifyEvent(user, usersHome, name, prevHomeLoc, location);
}
Bukkit.getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) {
if (ess.getSettings().isDebug()) {
ess.getLogger().info("HomeModifyEvent canceled for /sethome execution by " + user.getDisplayName());
}
return;
}
usersHome.setHome(name, location);
user.sendMessage(tl("homeSet", user.getLocation().getWorld().getName(), user.getLocation().getBlockX(), user.getLocation().getBlockY(), user.getLocation().getBlockZ(), name));
user.sendMessage(tl("homeSet", location.getWorld().getName(), location.getBlockX(), location.getBlockY(), location.getBlockZ(), name));
usersHome.setLastHomeConfirmation(null);
}

View File

@ -0,0 +1,137 @@
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 home is about to be modified.
*/
public class HomeModifyEvent extends Event implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private final IUser user;
private final IUser homeOwner;
private final Location newLocation;
private final Location oldLocation;
private final String newName;
private final String oldName;
private final HomeModifyCause cause;
private boolean canceled = false;
public HomeModifyEvent(IUser user, IUser homeOwner, String name, Location location, boolean create) {
this(user, homeOwner,
create ? location : null, // newLocation
create ? null : location, // oldLocation
create ? name : null, // newName
create ? null : name, // oldName
create ? HomeModifyCause.CREATE : HomeModifyCause.DELETE);
}
public HomeModifyEvent(IUser user, IUser homeOwner, String oldName, String newName, Location location) {
this(user, homeOwner, location, location, newName, oldName, HomeModifyCause.RENAME);
}
public HomeModifyEvent(IUser user, IUser homeOwner, String name, Location oldLocation, Location newLocation) {
this(user, homeOwner, newLocation, oldLocation, name, name, HomeModifyCause.UPDATE);
}
public HomeModifyEvent(IUser user, IUser homeOwner, Location newLocation, Location oldLocation, String newName, String oldName, HomeModifyCause cause) {
this.user = user;
this.homeOwner = homeOwner;
this.newLocation = newLocation;
this.oldLocation = oldLocation;
this.newName = newName;
this.oldName = oldName;
this.cause = cause;
}
/**
* Gets the user who modified the home or null if the console modified the home.
* @return The user who modified the home or null.
*/
public IUser getUser() {
return user;
}
/**
* Gets the owner of the home being modified.
* @return The user who owns the home.
*/
public IUser getHomeOwner() {
return homeOwner;
}
/**
* Returns the location of the home when {@link #getCause()} returns {@link HomeModifyCause#CREATE} or {@link HomeModifyCause#RENAME},
* returns the updated location of the home if it returns {@link HomeModifyCause#UPDATE}, or returns null if it returns {@link HomeModifyCause#DELETE}.
* @return The location of the home or null.
*/
public Location getNewLocation() {
return newLocation;
}
/**
* Returns the location of the home when {@link #getCause()} returns {@link HomeModifyCause#RENAME} or {@link HomeModifyCause#DELETE},
* returns the previous location of the home if it returns {@link HomeModifyCause#UPDATE}, or returns null if it returns {@link HomeModifyCause#CREATE}.
* @return The location of the home or null.
*/
public Location getOldLocation() {
return oldLocation;
}
/**
* Returns the name of the home when {@link #getCause()} returns {@link HomeModifyCause#CREATE} or {@link HomeModifyCause#UPDATE},
* returns the updated name if it returns {@link HomeModifyCause#RENAME}, or returns null if it returns {@link HomeModifyCause#DELETE}.
* @return The name of the home or null.
*/
public String getNewName() {
return newName;
}
/**
* Returns the name of the home when {@link #getCause()} returns {@link HomeModifyCause#UPDATE} or {@link HomeModifyCause#DELETE},
* returns the previous name if it returns {@link HomeModifyCause#RENAME}, or returns null if it returns {@link HomeModifyCause#CREATE}.
* @return The name of the home or null.
*/
public String getOldName() {
return oldName;
}
/**
* Returns the underlying cause of this modification to a home.
* @return The cause.
*/
public HomeModifyCause getCause() {
return cause;
}
@Override
public void setCancelled(boolean cancel) {
this.canceled = cancel;
}
@Override
public boolean isCancelled() {
return canceled;
}
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
/**
* The cause of why a home was modified.
* Used by {@link HomeModifyEvent}.
*/
public enum HomeModifyCause {
CREATE, DELETE, RENAME, UPDATE
}
}