|
| 1 | +package world.bentobox.bentobox.api.commands.admin.range; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Collection; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Optional; |
| 7 | + |
| 8 | +import org.bukkit.Bukkit; |
| 9 | +import org.eclipse.jdt.annotation.Nullable; |
| 10 | + |
| 11 | +import world.bentobox.bentobox.api.commands.CompositeCommand; |
| 12 | +import world.bentobox.bentobox.api.events.island.IslandEvent; |
| 13 | +import world.bentobox.bentobox.api.localization.TextVariables; |
| 14 | +import world.bentobox.bentobox.api.user.User; |
| 15 | +import world.bentobox.bentobox.database.objects.BonusRangeRecord; |
| 16 | +import world.bentobox.bentobox.database.objects.Island; |
| 17 | +import world.bentobox.bentobox.util.Util; |
| 18 | + |
| 19 | +/** |
| 20 | + * Admin command to remove every bonus range carrying a given id from <b>all</b> |
| 21 | + * islands in this gamemode's world. |
| 22 | + * <p> |
| 23 | + * Addons tag the bonus ranges they grant with their own name (the bonus |
| 24 | + * {@code uniqueId}); for example the Upgrades addon stores them under the id |
| 25 | + * {@code "Upgrades"}. When such an addon is removed, the bonus ranges it added |
| 26 | + * remain on every island it ever touched. This command purges them in one go. |
| 27 | + * <p> |
| 28 | + * Because a server can hold a large number of islands, the scan that finds the |
| 29 | + * affected islands runs asynchronously (off the main thread). The admin is then |
| 30 | + * shown the count and must re-run the command with {@code confirm} to apply it. |
| 31 | + * The actual mutation and event firing happen back on the main thread, on the |
| 32 | + * live cached island instances. |
| 33 | + * |
| 34 | + * @author tastybento |
| 35 | + * @since 3.17.1 |
| 36 | + */ |
| 37 | +public class AdminRangePurgeBonusCommand extends CompositeCommand { |
| 38 | + |
| 39 | + /** True while an async scan is running, to prevent overlapping runs. */ |
| 40 | + volatile boolean inPurge; |
| 41 | + /** True once a scan has found islands and is awaiting a {@code confirm}. */ |
| 42 | + boolean toBeConfirmed; |
| 43 | + /** The bonus id the pending confirmation is for. */ |
| 44 | + @Nullable |
| 45 | + String pendingId; |
| 46 | + /** The unique ids of the islands the pending confirmation will purge. */ |
| 47 | + List<String> pendingIslandIds = List.of(); |
| 48 | + |
| 49 | + /** |
| 50 | + * Admin command to remove a bonus range id from every island. |
| 51 | + * @param parent - parent range command |
| 52 | + */ |
| 53 | + public AdminRangePurgeBonusCommand(CompositeCommand parent) { |
| 54 | + super(parent, "purgebonus"); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public void setup() { |
| 59 | + setPermission("admin.range.purgebonus"); |
| 60 | + setOnlyPlayer(false); |
| 61 | + setParametersHelp("commands.admin.range.purgebonus.parameters"); |
| 62 | + setDescription("commands.admin.range.purgebonus.description"); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public boolean canExecute(User user, String label, List<String> args) { |
| 67 | + if (inPurge) { |
| 68 | + user.sendMessage("commands.admin.range.purgebonus.in-progress"); |
| 69 | + return false; |
| 70 | + } |
| 71 | + // Expect "<id>" or "<id> confirm" |
| 72 | + if (args.isEmpty() || args.size() > 2 |
| 73 | + || (args.size() == 2 && !args.get(1).equalsIgnoreCase("confirm"))) { |
| 74 | + showHelp(this, user); |
| 75 | + return false; |
| 76 | + } |
| 77 | + return true; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public boolean execute(User user, String label, List<String> args) { |
| 82 | + String id = args.get(0); |
| 83 | + boolean confirm = args.size() == 2 && args.get(1).equalsIgnoreCase("confirm"); |
| 84 | + // Apply a pending purge if this is the matching confirmation |
| 85 | + if (confirm && toBeConfirmed && id.equals(pendingId)) { |
| 86 | + List<String> ids = pendingIslandIds; |
| 87 | + toBeConfirmed = false; |
| 88 | + pendingId = null; |
| 89 | + pendingIslandIds = List.of(); |
| 90 | + applyPurge(user, id, ids); |
| 91 | + return true; |
| 92 | + } |
| 93 | + // Otherwise (re)scan asynchronously and prompt for confirmation |
| 94 | + inPurge = true; |
| 95 | + getPlugin().getIslands().getIslandsASync().thenAccept(all -> { |
| 96 | + List<String> ids = findIslandIds(all, id); |
| 97 | + Bukkit.getScheduler().runTask(getPlugin(), () -> { |
| 98 | + inPurge = false; |
| 99 | + if (ids.isEmpty()) { |
| 100 | + user.sendMessage("commands.admin.range.purgebonus.none", "[id]", id); |
| 101 | + return; |
| 102 | + } |
| 103 | + pendingId = id; |
| 104 | + pendingIslandIds = ids; |
| 105 | + toBeConfirmed = true; |
| 106 | + user.sendMessage("commands.admin.range.purgebonus.warning", "[id]", id, TextVariables.NUMBER, |
| 107 | + String.valueOf(ids.size())); |
| 108 | + user.sendMessage("commands.admin.range.purgebonus.confirm"); |
| 109 | + }); |
| 110 | + }).exceptionally(ex -> { |
| 111 | + getPlugin().logStacktrace(ex); |
| 112 | + Bukkit.getScheduler().runTask(getPlugin(), () -> { |
| 113 | + inPurge = false; |
| 114 | + user.sendMessage("commands.admin.range.purgebonus.failed"); |
| 115 | + }); |
| 116 | + return null; |
| 117 | + }); |
| 118 | + return true; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @return the unique ids of the islands in this world that carry a bonus range |
| 123 | + * with the given id. |
| 124 | + */ |
| 125 | + List<String> findIslandIds(Collection<Island> islands, String id) { |
| 126 | + return islands.stream().filter(i -> getWorld().equals(i.getWorld())) |
| 127 | + .filter(i -> i.getBonusRangeRecord(id).isPresent()).map(Island::getUniqueId).toList(); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Removes the bonus range id from every still-matching island, firing a range |
| 132 | + * change event per island whose effective protection range changed. Runs on the |
| 133 | + * main thread and operates on the live cached island instances, which are |
| 134 | + * persisted automatically via {@code setChanged()}. |
| 135 | + * |
| 136 | + * @param user the admin running the command |
| 137 | + * @param id the bonus range uniqueId to purge |
| 138 | + * @param ids the unique ids of the islands found during the async scan |
| 139 | + */ |
| 140 | + void applyPurge(User user, String id, List<String> ids) { |
| 141 | + int changed = 0; |
| 142 | + for (String uid : ids) { |
| 143 | + Island island = getIslands().getIslandById(uid).orElse(null); |
| 144 | + // Re-check on the live instance in case it changed since the scan |
| 145 | + if (island == null || island.getBonusRangeRecord(id).isEmpty()) { |
| 146 | + continue; |
| 147 | + } |
| 148 | + int oldRange = island.getProtectionRange(); |
| 149 | + island.clearBonusRange(id); |
| 150 | + int newRange = island.getProtectionRange(); |
| 151 | + if (oldRange != newRange) { |
| 152 | + IslandEvent.builder() |
| 153 | + .island(island) |
| 154 | + .location(island.getCenter()) |
| 155 | + .reason(IslandEvent.Reason.RANGE_CHANGE) |
| 156 | + .involvedPlayer(island.getOwner()) |
| 157 | + .admin(true) |
| 158 | + .protectionRange(newRange, oldRange) |
| 159 | + .build(); |
| 160 | + } |
| 161 | + changed++; |
| 162 | + } |
| 163 | + getPlugin().log("Purged bonus range '" + id + "' from " + changed + " island(s) in " |
| 164 | + + getWorld().getName()); |
| 165 | + user.sendMessage("commands.admin.range.purgebonus.success", "[id]", id, TextVariables.NUMBER, |
| 166 | + String.valueOf(changed)); |
| 167 | + } |
| 168 | + |
| 169 | + @Override |
| 170 | + public Optional<List<String>> tabComplete(User user, String alias, List<String> args) { |
| 171 | + if (args.size() <= 1) { |
| 172 | + String lastArg = !args.isEmpty() ? args.getLast() : ""; |
| 173 | + // Suggest the bonus ids that exist on currently-loaded islands in this world |
| 174 | + Collection<Island> cached = getIslands().getIslandCache().getCachedIslands(); |
| 175 | + List<String> ids = cached.stream().filter(i -> getWorld().equals(i.getWorld())) |
| 176 | + .flatMap(i -> i.getBonusRanges().stream()).map(BonusRangeRecord::getUniqueId).distinct().sorted() |
| 177 | + .toList(); |
| 178 | + return Optional.of(Util.tabLimit(new ArrayList<>(ids), lastArg)); |
| 179 | + } else if (args.size() == 2) { |
| 180 | + return Optional.of(Util.tabLimit(new ArrayList<>(List.of("confirm")), args.getLast())); |
| 181 | + } |
| 182 | + return Optional.empty(); |
| 183 | + } |
| 184 | +} |
0 commit comments