|
| 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 com.google.common.primitives.Ints; |
| 11 | + |
| 12 | +import world.bentobox.bentobox.api.commands.CompositeCommand; |
| 13 | +import world.bentobox.bentobox.api.commands.ConfirmableCommand; |
| 14 | +import world.bentobox.bentobox.api.localization.TextVariables; |
| 15 | +import world.bentobox.bentobox.api.user.User; |
| 16 | +import world.bentobox.bentobox.database.objects.Island; |
| 17 | +import world.bentobox.bentobox.util.Util; |
| 18 | + |
| 19 | +/** |
| 20 | + * Sets the maximum number of homes allowed on this island. |
| 21 | + * |
| 22 | + * Commands: |
| 23 | + * <ul> |
| 24 | + * <li><b>/bsb maxhomes <player> <number></b> - Sets the maximum number of homes for each island where the player is the owner. This could apply to multiple islands.</li> |
| 25 | + * <li><b>/bsb maxhomes <player> <number> [island name]</b> - Sets the maximum number of homes for a specific named island where the player is the owner.</li> |
| 26 | + * <li><b>/bsb maxhomes <number></b> - Sets the maximum number of homes for the island you are standing on (in-game only).</li> |
| 27 | + * </ul> |
| 28 | + * |
| 29 | + * @author tastybento |
| 30 | + * @since 2.6.0 |
| 31 | + */ |
| 32 | + |
| 33 | +public class AdminMaxHomesCommand extends ConfirmableCommand { |
| 34 | + |
| 35 | + Integer maxHomes; |
| 36 | + Map<String, Island> islands = new HashMap<>(); |
| 37 | + |
| 38 | + public AdminMaxHomesCommand(CompositeCommand parent) { |
| 39 | + super(parent, "setmaxhomes"); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public void setup() { |
| 44 | + setPermission("mod.maxhomes"); |
| 45 | + setOnlyPlayer(false); |
| 46 | + setParametersHelp("commands.admin.maxhomes.parameters"); |
| 47 | + setDescription("commands.admin.maxhomes.description"); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public boolean canExecute(User user, String label, List<String> args) { |
| 52 | + islands.clear(); |
| 53 | + if (args.isEmpty()) { |
| 54 | + showHelp(this, user); |
| 55 | + return false; |
| 56 | + } |
| 57 | + // Check arguments |
| 58 | + if (args.size() == 1) { |
| 59 | + // Player must be in game |
| 60 | + if (!user.isPlayer()) { |
| 61 | + user.sendMessage("general.errors.use-in-game"); |
| 62 | + return false; |
| 63 | + } |
| 64 | + // Check world |
| 65 | + if (user.getWorld() != getWorld()) { |
| 66 | + user.sendMessage("general.errors.wrong-world"); |
| 67 | + return false; |
| 68 | + } |
| 69 | + // Arg must be an integer to return true, otherwise false |
| 70 | + maxHomes = Ints.tryParse(args.get(0)); |
| 71 | + if (maxHomes == null || maxHomes < 1) { |
| 72 | + user.sendMessage("general.errors.must-be-positive-number", TextVariables.NUMBER, args.get(0)); |
| 73 | + return false; |
| 74 | + } |
| 75 | + // Get the island the user is standing on |
| 76 | + boolean onIsland = getIslands().getIslandAt(user.getLocation()).map(is -> { |
| 77 | + islands.put("", is); |
| 78 | + return true; |
| 79 | + }).orElse(false); |
| 80 | + if (!onIsland) { |
| 81 | + user.sendMessage("general.errors.not-on-island"); |
| 82 | + return false; |
| 83 | + } |
| 84 | + return true; |
| 85 | + } |
| 86 | + // More than one argument |
| 87 | + // First arg must be a valid player name |
| 88 | + UUID targetUUID = getPlayers().getUUID(args.get(0)); |
| 89 | + if (targetUUID == null) { |
| 90 | + user.sendMessage("general.errors.unknown-player", TextVariables.NAME, args.get(0)); |
| 91 | + return false; |
| 92 | + } |
| 93 | + // Second arg must be the max homes number |
| 94 | + maxHomes = Ints.tryParse(args.get(1)); |
| 95 | + if (maxHomes == null) { |
| 96 | + user.sendMessage("general.errors.must-be-positive-number", TextVariables.NUMBER, args.get(1)); |
| 97 | + return false; |
| 98 | + } |
| 99 | + // Get islands |
| 100 | + islands = this.getNameIslandMap(User.getInstance(targetUUID)); |
| 101 | + if (islands.isEmpty()) { |
| 102 | + user.sendMessage("general.errors.player-has-no-island"); |
| 103 | + return false; |
| 104 | + } |
| 105 | + if (args.size() > 2) { |
| 106 | + // A specific island is mentioned. Parse which one it is and remove the others |
| 107 | + final String name = String.join(" ", args.subList(2, args.size())); // Join all the args from here with spaces |
| 108 | + |
| 109 | + islands.keySet().removeIf(n -> !name.equalsIgnoreCase(n)); |
| 110 | + |
| 111 | + if (islands.isEmpty()) { |
| 112 | + // Failed name check - there are either |
| 113 | + user.sendMessage("commands.admin.maxhomes.errors.unknown-island", TextVariables.NAME, name); |
| 114 | + return false; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + return true; |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public boolean execute(User user, String label, List<String> args) { |
| 123 | + if (islands.isEmpty() || maxHomes < 1) { |
| 124 | + // Sanity check |
| 125 | + return false; |
| 126 | + } |
| 127 | + islands.forEach((name, island) -> { |
| 128 | + island.setMaxHomes(maxHomes); |
| 129 | + user.sendMessage("commands.admin.maxhomes.max-homes-set", TextVariables.NAME, name, TextVariables.NUMBER, |
| 130 | + String.valueOf(maxHomes)); |
| 131 | + }); |
| 132 | + return true; |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public Optional<List<String>> tabComplete(User user, String alias, List<String> args) { |
| 137 | + String lastArg = !args.isEmpty() ? args.get(args.size()-1) : ""; |
| 138 | + if (args.size() == 2) { |
| 139 | + // Suggest player names |
| 140 | + return Optional.of(Util.getOnlinePlayerList(user)); |
| 141 | + } |
| 142 | + if (args.size() > 3) { |
| 143 | + return Optional.of(Util.tabLimit(new ArrayList<>(getNameIslandMap(user).keySet()), lastArg)); |
| 144 | + } |
| 145 | + return Optional.of(List.of("1")); |
| 146 | + |
| 147 | + } |
| 148 | + |
| 149 | + Map<String, Island> getNameIslandMap(User user) { |
| 150 | + Map<String, Island> islandMap = new HashMap<>(); |
| 151 | + int index = 0; |
| 152 | + for (Island island : getIslands().getIslands(getWorld(), user.getUniqueId())) { |
| 153 | + index++; |
| 154 | + if (island.getName() != null && !island.getName().isBlank()) { |
| 155 | + // Name has been set |
| 156 | + islandMap.put(island.getName(), island); |
| 157 | + } else { |
| 158 | + // Name has not been set |
| 159 | + String text = user.getTranslation("protection.flags.ENTER_EXIT_MESSAGES.island", TextVariables.NAME, |
| 160 | + user.getName(), TextVariables.DISPLAY_NAME, user.getDisplayName()) + " " + index; |
| 161 | + islandMap.put(text, island); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + return islandMap; |
| 166 | + |
| 167 | + } |
| 168 | + |
| 169 | +} |
0 commit comments