|
| 1 | +package world.bentobox.bentobox.api.commands.admin; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Map; |
| 7 | +import java.util.Optional; |
| 8 | +import java.util.UUID; |
| 9 | + |
| 10 | +import world.bentobox.bentobox.api.commands.CompositeCommand; |
| 11 | +import world.bentobox.bentobox.api.localization.TextVariables; |
| 12 | +import world.bentobox.bentobox.api.user.User; |
| 13 | +import world.bentobox.bentobox.database.objects.Island; |
| 14 | +import world.bentobox.bentobox.util.Util; |
| 15 | + |
| 16 | + |
| 17 | +/** |
| 18 | + * This command resets players island name. |
| 19 | + * @author BONNe |
| 20 | + */ |
| 21 | +public class AdminResetHomeCommand extends CompositeCommand |
| 22 | +{ |
| 23 | + Map<String, Island> islands = new HashMap<>(); |
| 24 | + |
| 25 | + /** |
| 26 | + * Default constructor. |
| 27 | + * @param command Parent command. |
| 28 | + */ |
| 29 | + public AdminResetHomeCommand(CompositeCommand command) |
| 30 | + { |
| 31 | + super(command, "resethome"); |
| 32 | + } |
| 33 | + |
| 34 | + |
| 35 | + /** |
| 36 | + * {@inheritDoc} |
| 37 | + */ |
| 38 | + @Override |
| 39 | + public void setup() |
| 40 | + { |
| 41 | + this.setPermission("mod.resethome"); |
| 42 | + this.setDescription("commands.admin.resethome.description"); |
| 43 | + this.setParametersHelp("commands.admin.resethome.parameters"); |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | + /** |
| 48 | + * @param user the {@link User} who is executing this command. |
| 49 | + * @param label the label which has been used to execute this command. |
| 50 | + * It can be {@link CompositeCommand#getLabel()} or an alias. |
| 51 | + * @param args the command arguments. |
| 52 | + * @return {@code true} if name can be reset, {@code false} otherwise. |
| 53 | + */ |
| 54 | + @Override |
| 55 | + public boolean canExecute(User user, String label, List<String> args) |
| 56 | + { |
| 57 | + islands.clear(); |
| 58 | + if (args.isEmpty()) { |
| 59 | + this.showHelp(this, user); |
| 60 | + return false; |
| 61 | + } |
| 62 | + // First arg must be a valid player name |
| 63 | + UUID targetUUID = getPlayers().getUUID(args.get(0)); |
| 64 | + if (targetUUID == null) { |
| 65 | + user.sendMessage("general.errors.unknown-player", TextVariables.NAME, args.get(0)); |
| 66 | + return false; |
| 67 | + } |
| 68 | + // Get islands |
| 69 | + islands = this.getNameIslandMap(User.getInstance(targetUUID)); |
| 70 | + if (islands.isEmpty()) { |
| 71 | + user.sendMessage("general.errors.player-has-no-island"); |
| 72 | + return false; |
| 73 | + } |
| 74 | + |
| 75 | + // Second optional arg must be the name of the island |
| 76 | + if (args.size() == 1) { |
| 77 | + return true; |
| 78 | + } |
| 79 | + |
| 80 | + // A specific island is mentioned. Parse which one it is and remove the others |
| 81 | + final String name = String.join(" ", args.subList(1, args.size())); // Join all the args from here with spaces |
| 82 | + |
| 83 | + islands.keySet().removeIf(n -> !name.equalsIgnoreCase(n)); |
| 84 | + |
| 85 | + if (islands.isEmpty()) { |
| 86 | + // Failed name check - there are either |
| 87 | + user.sendMessage("commands.admin.maxhomes.errors.unknown-island", TextVariables.NAME, name); |
| 88 | + return false; |
| 89 | + } |
| 90 | + |
| 91 | + return true; |
| 92 | + } |
| 93 | + |
| 94 | + |
| 95 | + /** |
| 96 | + * @param user the {@link User} who is executing this command. |
| 97 | + * @param label the label which has been used to execute this command. |
| 98 | + * It can be {@link CompositeCommand#getLabel()} or an alias. |
| 99 | + * @param args the command arguments. |
| 100 | + * @return {@code true} |
| 101 | + */ |
| 102 | + @Override |
| 103 | + public boolean execute(User user, String label, List<String> args) |
| 104 | + { |
| 105 | + if (islands.isEmpty()) { |
| 106 | + // Sanity check |
| 107 | + return false; |
| 108 | + } |
| 109 | + islands.forEach((name, island) -> { |
| 110 | + island.getHomes().keySet().removeIf(String::isEmpty); // Remove the default home |
| 111 | + user.sendMessage("commands.admin.resethome.cleared", TextVariables.NAME, name); |
| 112 | + }); |
| 113 | + |
| 114 | + user.sendMessage("general.success"); |
| 115 | + return true; |
| 116 | + } |
| 117 | + |
| 118 | + |
| 119 | + @Override |
| 120 | + public Optional<List<String>> tabComplete(User user, String alias, List<String> args) { |
| 121 | + String lastArg = !args.isEmpty() ? args.get(args.size() - 1) : ""; |
| 122 | + if (args.size() == 2) { |
| 123 | + // Suggest player names |
| 124 | + return Optional.of(Util.getOnlinePlayerList(user)); |
| 125 | + } |
| 126 | + if (args.size() > 2) { |
| 127 | + // Work out who is in arg 2 |
| 128 | + UUID targetUUID = getPlayers().getUUID(args.get(0)); |
| 129 | + if (targetUUID != null) { |
| 130 | + User target = User.getInstance(targetUUID); |
| 131 | + return Optional.of(Util.tabLimit(new ArrayList<>(getNameIslandMap(target).keySet()), lastArg)); |
| 132 | + } |
| 133 | + } |
| 134 | + return Optional.empty(); |
| 135 | + |
| 136 | + } |
| 137 | + |
| 138 | + Map<String, Island> getNameIslandMap(User user) { |
| 139 | + Map<String, Island> islandMap = new HashMap<>(); |
| 140 | + int index = 0; |
| 141 | + System.out.println("Getting for " + user.getName()); |
| 142 | + for (Island island : getIslands().getIslands(getWorld(), user.getUniqueId())) { |
| 143 | + System.out.println("Island - " + island); |
| 144 | + index++; |
| 145 | + if (island.getName() != null && !island.getName().isBlank()) { |
| 146 | + // Name has been set |
| 147 | + islandMap.put(island.getName(), island); |
| 148 | + } else { |
| 149 | + // Name has not been set |
| 150 | + String text = user.getTranslation("protection.flags.ENTER_EXIT_MESSAGES.island", TextVariables.NAME, |
| 151 | + user.getName(), TextVariables.DISPLAY_NAME, user.getDisplayName()) + " " + index; |
| 152 | + islandMap.put(text, island); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + return islandMap; |
| 157 | + |
| 158 | + } |
| 159 | + |
| 160 | +} |
0 commit comments