Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Essentials/src/main/java/com/earth2me/essentials/IUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
10 changes: 10 additions & 0 deletions Essentials/src/main/java/com/earth2me/essentials/UserData.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,16 @@ public void delHome(final String name) throws Exception {
}
}

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();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
1 change: 1 addition & 0 deletions Essentials/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ player-commands:
- protect
- r
- rules
- renamehome
- realname
- seen
- sell
Expand Down
7 changes: 7 additions & 0 deletions Essentials/src/main/resources/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions Essentials/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down