2011-03-19 22:39:51 +00:00
|
|
|
package com.earth2me.essentials.commands;
|
|
|
|
|
2011-06-13 13:05:11 +00:00
|
|
|
import com.earth2me.essentials.Trade;
|
2011-03-19 22:39:51 +00:00
|
|
|
import com.earth2me.essentials.User;
|
2013-06-08 21:31:19 +00:00
|
|
|
import com.earth2me.essentials.utils.StringUtil;
|
2020-06-24 08:52:25 +00:00
|
|
|
import io.papermc.lib.PaperLib;
|
2011-11-26 21:30:40 +00:00
|
|
|
import org.bukkit.Location;
|
2011-11-18 17:42:26 +00:00
|
|
|
import org.bukkit.Server;
|
2011-12-07 01:12:36 +00:00
|
|
|
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
|
2011-03-19 22:39:51 +00:00
|
|
|
|
2017-06-11 00:17:43 +00:00
|
|
|
import java.util.Collections;
|
2015-04-15 04:06:16 +00:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Locale;
|
2020-06-24 08:52:25 +00:00
|
|
|
import java.util.concurrent.CompletableFuture;
|
2015-04-15 04:06:16 +00:00
|
|
|
|
|
|
|
import static com.earth2me.essentials.I18n.tl;
|
|
|
|
|
This is a big refactoring of the user class and more.
Many commands have been cleaned.
File changes:
- all user data has been moved from users.yml to userdata folder
- all files in userdata folder are lower case
Both changes should be done automatically.
Class changes:
- Moved all user data functions to UserData class
- Moved all user teleport functions to Teleport class
- Moved the user list to Essentials class
- Less static functions for better testing
- EssentialsCommand now has ess Property (Essentials class)
- New NotEnoughArgumentsException, that will show command description and syntax
New commands:
- /seen, shows the last login or logout
- /tempban, temporarily ban someone
- /tjail and mute, temporarily option added
Other changes:
- ban reason is saved
- don't show "You have xxx mail" on login, if user doesn't have essentials.mail permission
- time will be parsed: years, months (mo), weeks, days, hours, minutes (m), seconds, these can be shortened and combined, example: 2 days 5h 30m
git-svn-id: https://svn.java.net/svn/essentials~svn/trunk@1300 e251c2fe-e539-e718-e476-b85c1f46cddb
2011-05-01 21:07:30 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
public class Commandhome extends EssentialsCommand {
|
|
|
|
public Commandhome() {
|
|
|
|
super("home");
|
|
|
|
}
|
2011-03-19 22:39:51 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
// This method contains an undocumented translation parameters #EasterEgg
|
|
|
|
@Override
|
|
|
|
public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception {
|
|
|
|
final Trade charge = new Trade(this.getName(), ess);
|
|
|
|
User player = user;
|
|
|
|
String homeName = "";
|
|
|
|
String[] nameParts;
|
|
|
|
if (args.length > 0) {
|
|
|
|
nameParts = args[0].split(":");
|
|
|
|
if (nameParts[0].length() == args[0].length() || !user.isAuthorized("essentials.home.others")) {
|
|
|
|
homeName = nameParts[0];
|
|
|
|
} else {
|
|
|
|
player = getPlayer(server, nameParts, 0, true, true);
|
|
|
|
if (nameParts.length > 1) {
|
|
|
|
homeName = nameParts[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
if ("bed".equalsIgnoreCase(homeName) && user.isAuthorized("essentials.home.bed")) {
|
2020-06-24 08:52:25 +00:00
|
|
|
if (!player.getBase().isOnline()) {
|
|
|
|
throw new Exception(tl("bedOffline"));
|
2015-04-15 04:06:16 +00:00
|
|
|
}
|
2020-06-24 08:52:25 +00:00
|
|
|
PaperLib.getBedSpawnLocationAsync(player.getBase(), true).thenAccept(location -> {
|
|
|
|
CompletableFuture<Boolean> future = getNewExceptionFuture(user.getSource(), commandLabel);
|
|
|
|
if (location != null) {
|
|
|
|
future.thenAccept(success -> {
|
|
|
|
if (success) {
|
|
|
|
user.sendMessage(tl("teleportHome", "bed"));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
user.getAsyncTeleport().teleport(location, charge, TeleportCause.COMMAND, future);
|
|
|
|
} else {
|
|
|
|
showError(user.getBase(), new Exception(tl("bedMissing")), commandLabel);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return;
|
2015-04-15 04:06:16 +00:00
|
|
|
}
|
2020-06-24 08:52:25 +00:00
|
|
|
goHome(user, player, homeName.toLowerCase(Locale.ENGLISH), charge, getNewExceptionFuture(user.getSource(), commandLabel));
|
2015-04-15 04:06:16 +00:00
|
|
|
} catch (NotEnoughArgumentsException e) {
|
2020-06-24 08:52:25 +00:00
|
|
|
final User finalPlayer = player;
|
|
|
|
PaperLib.getBedSpawnLocationAsync(player.getBase(), true).thenAccept(bed -> {
|
|
|
|
final List<String> homes = finalPlayer.getHomes();
|
|
|
|
if (homes.isEmpty() && finalPlayer.equals(user)) {
|
|
|
|
if (ess.getSettings().isSpawnIfNoHome()) {
|
|
|
|
user.getAsyncTeleport().respawn(charge, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel));
|
2015-04-15 04:06:16 +00:00
|
|
|
} else {
|
2020-06-24 08:52:25 +00:00
|
|
|
showError(user.getBase(), new Exception(tl("noHomeSetPlayer")), commandLabel);
|
2015-04-15 04:06:16 +00:00
|
|
|
}
|
2020-06-24 08:52:25 +00:00
|
|
|
} else if (homes.isEmpty()) {
|
|
|
|
showError(user.getBase(), new Exception(tl("noHomeSetPlayer")), commandLabel);
|
|
|
|
} else if (homes.size() == 1 && finalPlayer.equals(user)) {
|
|
|
|
try {
|
|
|
|
goHome(user, finalPlayer, homes.get(0), charge, getNewExceptionFuture(user.getSource(), commandLabel));
|
|
|
|
} catch (Exception exception) {
|
|
|
|
showError(user.getBase(), exception, commandLabel);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
final int count = homes.size();
|
|
|
|
if (user.isAuthorized("essentials.home.bed")) {
|
|
|
|
if (bed != null) {
|
|
|
|
homes.add(tl("bed"));
|
|
|
|
} else {
|
|
|
|
homes.add(tl("bedNull"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
user.sendMessage(tl("homes", StringUtil.joinList(homes), count, getHomeLimit(finalPlayer)));
|
2015-04-15 04:06:16 +00:00
|
|
|
}
|
2020-06-24 08:52:25 +00:00
|
|
|
});
|
2015-04-15 04:06:16 +00:00
|
|
|
}
|
|
|
|
}
|
2012-05-20 22:58:03 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
private String getHomeLimit(final User player) {
|
|
|
|
if (!player.getBase().isOnline()) {
|
|
|
|
return "?";
|
|
|
|
}
|
|
|
|
if (player.isAuthorized("essentials.sethome.multiple.unlimited")) {
|
|
|
|
return "*";
|
|
|
|
}
|
|
|
|
return Integer.toString(ess.getSettings().getHomeLimit(player));
|
|
|
|
}
|
2013-12-21 17:42:55 +00:00
|
|
|
|
2020-06-24 08:52:25 +00:00
|
|
|
private void goHome(final User user, final User player, final String home, final Trade charge, CompletableFuture<Boolean> future) throws Exception {
|
2015-04-15 04:06:16 +00:00
|
|
|
if (home.length() < 1) {
|
|
|
|
throw new NotEnoughArgumentsException();
|
|
|
|
}
|
|
|
|
final Location loc = player.getHome(home);
|
|
|
|
if (loc == null) {
|
|
|
|
throw new NotEnoughArgumentsException();
|
|
|
|
}
|
|
|
|
if (user.getWorld() != loc.getWorld() && ess.getSettings().isWorldHomePermissions() && !user.isAuthorized("essentials.worlds." + loc.getWorld().getName())) {
|
|
|
|
throw new Exception(tl("noPerm", "essentials.worlds." + loc.getWorld().getName()));
|
|
|
|
}
|
2020-06-24 08:52:25 +00:00
|
|
|
user.getAsyncTeleport().teleport(loc, charge, TeleportCause.COMMAND, future);
|
|
|
|
future.thenAccept(success -> {
|
|
|
|
if (success) {
|
|
|
|
user.sendMessage(tl("teleportHome", home));
|
|
|
|
}
|
|
|
|
});
|
2015-04-15 04:06:16 +00:00
|
|
|
}
|
2017-06-11 00:17:43 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected List<String> getTabCompleteOptions(final Server server, final User user, final String commandLabel, final String[] args) {
|
|
|
|
boolean canVisitOthers = user.isAuthorized("essentials.home.others");
|
2020-04-30 19:26:27 +00:00
|
|
|
boolean canVisitBed = user.isAuthorized("essentials.home.bed");
|
2017-06-11 00:17:43 +00:00
|
|
|
if (args.length == 1) {
|
2020-04-30 19:26:27 +00:00
|
|
|
List<String> homes = user.getHomes();
|
|
|
|
if (canVisitBed) {
|
|
|
|
homes.add("bed");
|
2017-06-11 00:17:43 +00:00
|
|
|
}
|
2020-04-30 19:26:27 +00:00
|
|
|
if (canVisitOthers) {
|
|
|
|
int sepIndex = args[0].indexOf(':');
|
|
|
|
if (sepIndex < 0) {
|
|
|
|
getPlayers(server, user).forEach(player -> homes.add(player + ":"));
|
|
|
|
} else {
|
|
|
|
String namePart = args[0].substring(0, sepIndex);
|
|
|
|
User otherUser;
|
|
|
|
try {
|
|
|
|
otherUser = getPlayer(server, new String[]{namePart}, 0, true, true);
|
|
|
|
} catch (Exception ex) {
|
|
|
|
return homes;
|
|
|
|
}
|
|
|
|
otherUser.getHomes().forEach(home -> homes.add(namePart + ":" + home));
|
|
|
|
if (canVisitBed) {
|
|
|
|
homes.add(namePart + ":bed");
|
|
|
|
}
|
2017-06-11 00:17:43 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-30 19:26:27 +00:00
|
|
|
return homes;
|
2017-06-11 00:17:43 +00:00
|
|
|
} else {
|
|
|
|
return Collections.emptyList();
|
|
|
|
}
|
|
|
|
}
|
2011-03-19 22:39:51 +00:00
|
|
|
}
|