Add renamehome command (#5121)

This commit is contained in:
Nassim Jahnke 2022-10-04 14:40:18 +02:00 committed by GitHub
parent 28d7abe642
commit f7cbc7b0d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 120 additions and 0 deletions

View File

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

View File

@ -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();
}

View File

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

View File

@ -283,6 +283,7 @@ player-commands:
- protect
- r
- rules
- renamehome
- realname
- seen
- sell

View File

@ -482,6 +482,7 @@ homeCommandUsage2=/<command> <player>:<name>
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=/<command> <mob type> <radius> [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=/<command> <[player:]name> <new name>
renamehomeCommandUsage1=/<command> <name> <new name>
renamehomeCommandUsage1Description=Renames your home to the given name
renamehomeCommandUsage2=/<command> <player>:<name> <new name>
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.

View File

@ -388,6 +388,10 @@ commands:
description: Removes entities in your world.
usage: /<command> <all|tamed|named|drops|arrows|boats|minecarts|xp|paintings|itemframes|endercrystals|monsters|animals|ambient|mobs|[mobType]> [radius|world]
aliases: [eremove,butcher,ebutcher,killall,ekillall,mobkill,emobkill]
renamehome:
description: Renames a home.
usage: /<command> <[player:]name> <new name>
aliases: [erenamehome]
repair:
description: Repairs the durability of one or all items.
usage: /<command> [hand|all]