diff --git a/Essentials/src/main/java/com/earth2me/essentials/IUser.java b/Essentials/src/main/java/com/earth2me/essentials/IUser.java index 617b88565..3c4e8361f 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/IUser.java +++ b/Essentials/src/main/java/com/earth2me/essentials/IUser.java @@ -155,6 +155,8 @@ public interface IUser { void delHome(String name) throws Exception; + void renameHome(String name, String newName) throws Exception; + boolean hasHome(); Location getLastLocation(); diff --git a/Essentials/src/main/java/com/earth2me/essentials/UserData.java b/Essentials/src/main/java/com/earth2me/essentials/UserData.java index 693efea4d..5e1892387 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/UserData.java +++ b/Essentials/src/main/java/com/earth2me/essentials/UserData.java @@ -199,6 +199,16 @@ public abstract class UserData extends PlayerExtension implements IConf { } } + public void renameHome(final String name, final String newName) throws Exception { + final LazyLocation location = holder.homes().remove(name); + if (location != null) { + holder.homes().put(StringUtil.safeString(newName), location); + config.save(); + } else { + throw new Exception(tl("invalidHome", name)); + } + } + public boolean hasHome() { return !holder.homes().isEmpty(); } diff --git a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandrenamehome.java b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandrenamehome.java new file mode 100644 index 000000000..e96a99a9e --- /dev/null +++ b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandrenamehome.java @@ -0,0 +1,96 @@ +package com.earth2me.essentials.commands; + +import com.earth2me.essentials.CommandSource; +import com.earth2me.essentials.User; +import com.earth2me.essentials.utils.NumberUtil; +import net.ess3.api.IUser; +import org.bukkit.Server; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Locale; + +import static com.earth2me.essentials.I18n.tl; + +public class Commandrenamehome extends EssentialsCommand { + public Commandrenamehome() { + super("renamehome"); + } + + @Override + public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception { + User usersHome = user; + final String oldName; + final String newName; + + // Allowing both formats /renamehome jroy home1 home | /sethome jroy:home1 home + if (args.length == 2) { + final String[] nameParts = args[0].split(":", 2); + newName = args[1].toLowerCase(Locale.ENGLISH); + + if (nameParts.length == 2) { + oldName = nameParts[1].toLowerCase(Locale.ENGLISH); + if (user.isAuthorized("essentials.renamehome.others")) { + usersHome = getPlayer(server, nameParts[0], true, true); + if (usersHome == null) { + throw new PlayerNotFoundException(); + } + } + } else { + oldName = args[0].toLowerCase(Locale.ENGLISH); + } + } else if (args.length == 3) { + if (!user.isAuthorized("essentials.renamehome.others")) { + throw new NotEnoughArgumentsException(); + } + + usersHome = getPlayer(server, args[0], true, true); + if (usersHome == null) { + throw new PlayerNotFoundException(); + } + + oldName = args[1].toLowerCase(Locale.ENGLISH); + newName = args[2].toLowerCase(Locale.ENGLISH); + } else { + throw new NotEnoughArgumentsException(); + } + + if ("bed".equals(newName) || NumberUtil.isInt(newName) || "bed".equals(oldName) || NumberUtil.isInt(oldName)) { + throw new NoSuchFieldException(tl("invalidHomeName")); + } + + usersHome.renameHome(oldName, newName); + user.sendMessage(tl("homeRenamed", oldName, newName)); + usersHome.setLastHomeConfirmation(null); + + } + + @Override + protected List getTabCompleteOptions(final Server server, final CommandSource sender, final String commandLabel, final String[] args) { + final IUser user = sender.getUser(ess); + if (args.length != 1) { + return Collections.emptyList(); + } + + final List homes = user == null ? new ArrayList<>() : user.getHomes(); + final boolean canRenameOthers = sender.isAuthorized("essentials.renamehome.others", ess); + + if (canRenameOthers) { + final int sepIndex = args[0].indexOf(':'); + if (sepIndex < 0) { + getPlayers(server, sender).forEach(player -> homes.add(player + ":")); + } else { + final String namePart = args[0].substring(0, sepIndex); + final User otherUser; + try { + otherUser = getPlayer(server, new String[]{namePart}, 0, true, true); + } catch (final Exception ex) { + return homes; + } + otherUser.getHomes().forEach(home -> homes.add(namePart + ":" + home)); + } + } + return homes; + } +} diff --git a/Essentials/src/main/resources/config.yml b/Essentials/src/main/resources/config.yml index 9e1b62135..314c0b650 100644 --- a/Essentials/src/main/resources/config.yml +++ b/Essentials/src/main/resources/config.yml @@ -283,6 +283,7 @@ player-commands: - protect - r - rules + - renamehome - realname - seen - sell diff --git a/Essentials/src/main/resources/messages.properties b/Essentials/src/main/resources/messages.properties index 226c8484b..3a2637269 100644 --- a/Essentials/src/main/resources/messages.properties +++ b/Essentials/src/main/resources/messages.properties @@ -482,6 +482,7 @@ homeCommandUsage2=/ : homeCommandUsage2Description=Teleports you to the specified player's home with the given name homes=\u00a76Homes\:\u00a7r {0} homeConfirmation=\u00a76You already have a home named \u00a7c{0}\u00a76!\nTo overwrite your existing home, type the command again. +homeRenamed=\u00a76Home \u00a7c{0} \u00a76has been renamed to \u00a7c{1}\u00a76. homeSet=\u00a76Home set to current location. hour=hour hours=hours @@ -1010,6 +1011,12 @@ removeCommandUsage1Description=Removes all of the given mob type in the current removeCommandUsage2=/ [world] removeCommandUsage2Description=Removes the given mob type within the given radius in the current world or another one if specified removed=\u00a76Removed\u00a7c {0} \u00a76entities. +renamehomeCommandDescription=Renames a home. +renamehomeCommandUsage=/ <[player:]name> +renamehomeCommandUsage1=/ +renamehomeCommandUsage1Description=Renames your home to the given name +renamehomeCommandUsage2=/ : +renamehomeCommandUsage2Description=Renames the specified player's home to the given name repair=\u00a76You have successfully repaired your\: \u00a7c{0}\u00a76. repairAlreadyFixed=\u00a74This item does not need repairing. repairCommandDescription=Repairs the durability of one or all items. diff --git a/Essentials/src/main/resources/plugin.yml b/Essentials/src/main/resources/plugin.yml index 8e60aa7b8..b6640e1d3 100644 --- a/Essentials/src/main/resources/plugin.yml +++ b/Essentials/src/main/resources/plugin.yml @@ -388,6 +388,10 @@ commands: description: Removes entities in your world. usage: / [radius|world] aliases: [eremove,butcher,ebutcher,killall,ekillall,mobkill,emobkill] + renamehome: + description: Renames a home. + usage: / <[player:]name> + aliases: [erenamehome] repair: description: Repairs the durability of one or all items. usage: / [hand|all]