2011-03-19 22:39:51 +00:00
|
|
|
package com.earth2me.essentials.commands;
|
|
|
|
|
2020-04-30 19:26:27 +00:00
|
|
|
import com.earth2me.essentials.CommandSource;
|
2011-03-19 22:39:51 +00:00
|
|
|
import com.earth2me.essentials.User;
|
2014-07-12 15:34:18 +00:00
|
|
|
import com.earth2me.essentials.utils.LocationUtil;
|
2013-06-08 21:31:19 +00:00
|
|
|
import com.earth2me.essentials.utils.NumberUtil;
|
2012-09-15 18:55:12 +00:00
|
|
|
import org.bukkit.Location;
|
2011-11-18 17:42:26 +00:00
|
|
|
import org.bukkit.Server;
|
2011-03-19 22:39:51 +00:00
|
|
|
|
2020-04-30 19:26:27 +00:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
2015-04-15 04:06:16 +00:00
|
|
|
import java.util.Locale;
|
2020-07-01 21:03:22 +00:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2015-04-15 04:06:16 +00:00
|
|
|
|
|
|
|
import static com.earth2me.essentials.I18n.tl;
|
|
|
|
|
2011-03-19 22:39:51 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
public class Commandsethome extends EssentialsCommand {
|
|
|
|
public Commandsethome() {
|
|
|
|
super("sethome");
|
|
|
|
}
|
2011-06-28 09:10:29 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
@Override
|
|
|
|
public void run(final Server server, final User user, final String commandLabel, String[] args) throws Exception {
|
|
|
|
User usersHome = user;
|
|
|
|
String name = "home";
|
2012-09-15 18:55:12 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
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;
|
|
|
|
}
|
2011-08-24 01:48:38 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
if (args.length < 2) {
|
|
|
|
name = args[0].toLowerCase(Locale.ENGLISH);
|
|
|
|
} else {
|
|
|
|
name = args[1].toLowerCase(Locale.ENGLISH);
|
|
|
|
if (user.isAuthorized("essentials.sethome.others")) {
|
|
|
|
usersHome = getPlayer(server, args[0], true, true);
|
|
|
|
if (usersHome == null) {
|
|
|
|
throw new PlayerNotFoundException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (checkHomeLimit(user, usersHome, name)) {
|
|
|
|
name = "home";
|
|
|
|
}
|
|
|
|
if ("bed".equals(name) || NumberUtil.isInt(name)) {
|
|
|
|
throw new NoSuchFieldException(tl("invalidHomeName"));
|
|
|
|
}
|
2014-07-12 15:34:18 +00:00
|
|
|
|
2020-08-11 18:09:22 +00:00
|
|
|
final Location location = user.getLocation();
|
2015-04-15 04:06:16 +00:00
|
|
|
if (!ess.getSettings().isTeleportSafetyEnabled() && LocationUtil.isBlockUnsafeForUser(usersHome, location.getWorld(), location.getBlockX(), location.getBlockY(), location.getBlockZ())) {
|
|
|
|
throw new Exception(tl("unsafeTeleportDestination", location.getWorld().getName(), location.getBlockX(), location.getBlockY(), location.getBlockZ()));
|
|
|
|
}
|
2014-07-12 15:34:18 +00:00
|
|
|
|
2020-07-01 21:03:22 +00:00
|
|
|
if (ess.getSettings().isConfirmHomeOverwrite() && usersHome.hasHome(name) && (!name.equals(usersHome.getLastHomeConfirmation()) || name.equals(usersHome.getLastHomeConfirmation()) && System.currentTimeMillis() - usersHome.getLastHomeConfirmationTimestamp() > TimeUnit.MINUTES.toMillis(2))) {
|
|
|
|
usersHome.setLastHomeConfirmation(name);
|
|
|
|
usersHome.setLastHomeConfirmationTimestamp();
|
|
|
|
user.sendMessage(tl("homeConfirmation", name));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
usersHome.setHome(name, location);
|
|
|
|
user.sendMessage(tl("homeSet", user.getLocation().getWorld().getName(), user.getLocation().getBlockX(), user.getLocation().getBlockY(), user.getLocation().getBlockZ(), name));
|
2020-07-01 21:03:22 +00:00
|
|
|
usersHome.setLastHomeConfirmation(null);
|
2011-06-28 09:13:18 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
}
|
2012-09-15 18:55:12 +00:00
|
|
|
|
2015-04-15 04:06:16 +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)));
|
|
|
|
}
|
2020-04-25 12:08:57 +00:00
|
|
|
return limit == 1;
|
2015-04-15 04:06:16 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-30 19:26:27 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected List<String> getTabCompleteOptions(final Server server, final CommandSource sender, final String commandLabel, final String[] args) {
|
|
|
|
return Collections.emptyList();
|
|
|
|
}
|
2011-03-19 22:39:51 +00:00
|
|
|
}
|