TF-EssentialsX/Essentials/src/com/earth2me/essentials/commands/Commandsethome.java

85 lines
2.1 KiB
Java
Raw Normal View History

package com.earth2me.essentials.commands;
import static com.earth2me.essentials.I18n.tl;
import com.earth2me.essentials.User;
2013-06-08 21:31:19 +00:00
import com.earth2me.essentials.utils.NumberUtil;
import java.util.Locale;
import org.bukkit.Location;
2011-11-18 17:42:26 +00:00
import org.bukkit.Server;
public class Commandsethome extends EssentialsCommand
{
public Commandsethome()
{
super("sethome");
}
@Override
2011-12-01 13:42:39 +00:00
public void run(final Server server, final User user, final String commandLabel, String[] args) throws Exception
{
User usersHome = user;
String name = "home";
final Location location = user.getLocation();
if (args.length > 0)
{
//Allowing both formats /sethome khobbits house | /sethome khobbits:house
final String[] nameParts = args[0].split(":");
if (nameParts[0].length() != args[0].length())
{
args = nameParts;
}
if (args.length < 2)
{
name = args[0].toLowerCase(Locale.ENGLISH);
}
else
{
name = args[1].toLowerCase(Locale.ENGLISH);
if (user.isAuthorized("essentials.sethome.others"))
{
2014-05-17 01:44:37 +00:00
usersHome = getPlayer(server, args[0], true, true);
2011-08-11 02:33:45 +00:00
if (usersHome == null)
{
2013-06-22 15:33:22 +00:00
throw new PlayerNotFoundException();
}
}
}
}
if (checkHomeLimit(user, usersHome, name))
{
name = "home";
}
2013-06-08 21:31:19 +00:00
if ("bed".equals(name) || NumberUtil.isInt(name))
2011-06-28 09:13:18 +00:00
{
throw new NoSuchFieldException(tl("invalidHomeName"));
2011-06-28 09:13:18 +00:00
}
usersHome.setHome(name, location);
user.sendMessage(tl("homeSet", user.getLocation().getWorld().getName(), user.getLocation().getBlockX(), user.getLocation().getBlockY(), user.getLocation().getBlockZ(), name));
2011-06-28 09:13:18 +00:00
}
private boolean checkHomeLimit(final User user, final User usersHome, String name) throws Exception
{
if (!user.isAuthorized("essentials.sethome.multiple.unlimited"))
{
int limit = ess.getSettings().getHomeLimit(user);
if (usersHome.getHomes().size() == limit && usersHome.getHomes().contains(name))
{
return false;
}
if (usersHome.getHomes().size() >= limit)
{
throw new Exception(tl("maxHomes", ess.getSettings().getHomeLimit(user)));
}
if (limit == 1)
{
return true;
}
}
return false;
}
}