|
19 | 19 | import world.bentobox.bentobox.panels.IslandCreationPanel;
|
20 | 20 | import world.bentobox.bentobox.util.Util;
|
21 | 21 |
|
22 |
| - |
23 | 22 | /**
|
24 | 23 | * @author tastybento
|
25 | 24 | */
|
26 | 25 | public class IslandResetCommand extends ConfirmableCommand {
|
27 | 26 |
|
28 |
| - private boolean noPaste; |
29 |
| - |
30 |
| - public IslandResetCommand(CompositeCommand islandCommand) { |
31 |
| - super(islandCommand, "reset", "restart"); |
32 |
| - } |
33 |
| - |
34 |
| - /** |
35 |
| - * Creates the island reset command |
36 |
| - * @param islandCommand - parent command |
37 |
| - * @param noPaste - true if resetting should not paste a new island |
38 |
| - */ |
39 |
| - public IslandResetCommand(CompositeCommand islandCommand, boolean noPaste) { |
40 |
| - super(islandCommand, "reset", "restart"); |
41 |
| - this.noPaste = noPaste; |
42 |
| - } |
43 |
| - |
44 |
| - @Override |
45 |
| - public void setup() { |
46 |
| - setPermission("island.reset"); |
47 |
| - setOnlyPlayer(true); |
48 |
| - setParametersHelp("commands.island.reset.parameters"); |
49 |
| - setDescription("commands.island.reset.description"); |
50 |
| - } |
51 |
| - |
52 |
| - @Override |
53 |
| - public boolean canExecute(User user, String label, List<String> args) { |
54 |
| - // Check cooldown |
55 |
| - if (getSettings().getResetCooldown() > 0 && checkCooldown(user)) { |
56 |
| - return false; |
57 |
| - } |
58 |
| - if (!getIslands().hasIsland(getWorld(), user.getUniqueId())) { |
59 |
| - user.sendMessage("general.errors.no-island"); |
60 |
| - return false; |
61 |
| - } |
62 |
| - int resetsLeft = getPlayers().getResetsLeft(getWorld(), user.getUniqueId()); |
63 |
| - if (resetsLeft != -1) { |
64 |
| - // Resets are not unlimited here |
65 |
| - if (resetsLeft == 0) { |
66 |
| - // No resets allowed |
67 |
| - user.sendMessage("commands.island.reset.none-left"); |
68 |
| - return false; |
69 |
| - } else { |
70 |
| - // Still some resets left |
71 |
| - // Notify how many resets are left |
72 |
| - user.sendMessage("commands.island.reset.resets-left", TextVariables.NUMBER, String.valueOf(resetsLeft)); |
73 |
| - } |
74 |
| - } |
75 |
| - |
76 |
| - return true; |
77 |
| - } |
78 |
| - |
79 |
| - @Override |
80 |
| - public boolean execute(User user, String label, List<String> args) { |
81 |
| - // Permission check if the name is not the default one |
82 |
| - if (!args.isEmpty()) { |
83 |
| - String name = getPlugin().getBlueprintsManager().validate(getAddon(), Util.sanitizeInput(args.get(0))); |
84 |
| - if (name == null || name.isEmpty()) { |
85 |
| - // The blueprint name is not valid. |
86 |
| - user.sendMessage("commands.island.create.unknown-blueprint"); |
87 |
| - return false; |
88 |
| - } |
89 |
| - if (!getPlugin().getBlueprintsManager().checkPerm(getAddon(), user, Util.sanitizeInput(args.get(0)))) { |
90 |
| - return false; |
91 |
| - } |
92 |
| - return resetIsland(user, name); |
93 |
| - } else { |
94 |
| - // Show panel after confirmation |
95 |
| - if (getPlugin().getSettings().isResetConfirmation()) { |
96 |
| - this.askConfirmation(user, user.getTranslation("commands.island.reset.confirmation"), () -> selectBundle(user, label)); |
97 |
| - } else { |
98 |
| - selectBundle(user, label); |
99 |
| - } |
100 |
| - return true; |
101 |
| - } |
102 |
| - } |
103 |
| - |
104 |
| - /** |
105 |
| - * Either selects the bundle to use or asks the user to choose. |
106 |
| - * @since 1.5.1 |
107 |
| - */ |
108 |
| - private void selectBundle(@NonNull User user, @NonNull String label) { |
109 |
| - // Show panel only if there are multiple bundles available |
110 |
| - if (getPlugin().getBlueprintsManager().getBlueprintBundles(getAddon()).size() > 1) { |
111 |
| - // Show panel - once the player selected a bundle, this will re-run this command |
112 |
| - IslandCreationPanel.openPanel(this, user, label); |
113 |
| - } else { |
114 |
| - resetIsland(user, BlueprintsManager.DEFAULT_BUNDLE_NAME); |
115 |
| - } |
116 |
| - } |
117 |
| - |
118 |
| - /** |
119 |
| - * Reset island |
120 |
| - * @param user user |
121 |
| - * @param name name of Blueprint Bundle |
122 |
| - * @return true if successful |
123 |
| - */ |
124 |
| - private boolean resetIsland(User user, String name) { |
125 |
| - // Get the player's old island |
126 |
| - Island oldIsland = getIslands().getIsland(getWorld(), user); |
127 |
| - deleteOldIsland(user, oldIsland); |
128 |
| - |
129 |
| - user.sendMessage("commands.island.create.creating-island"); |
130 |
| - // Create new island and then delete the old one |
131 |
| - try { |
132 |
| - Builder builder = NewIsland.builder() |
133 |
| - .player(user) |
134 |
| - .reason(Reason.RESET) |
135 |
| - .addon(getAddon()) |
136 |
| - .oldIsland(oldIsland) |
137 |
| - .name(name); |
138 |
| - if (noPaste) builder.noPaste(); |
139 |
| - builder.build(); |
140 |
| - } catch (IOException e) { |
141 |
| - getPlugin().logError("Could not create island for player. " + e.getMessage()); |
142 |
| - user.sendMessage(e.getMessage()); |
143 |
| - return false; |
144 |
| - } |
145 |
| - setCooldown(user.getUniqueId(), getSettings().getResetCooldown()); |
146 |
| - return true; |
147 |
| - } |
148 |
| - |
149 |
| - private void deleteOldIsland(User user, Island oldIsland) { |
150 |
| - // Fire island preclear event |
151 |
| - IslandEvent.builder() |
152 |
| - .involvedPlayer(user.getUniqueId()) |
153 |
| - .reason(Reason.PRECLEAR) |
154 |
| - .island(oldIsland) |
155 |
| - .oldIsland(oldIsland) |
156 |
| - .location(oldIsland.getCenter()) |
157 |
| - .build(); |
158 |
| - |
159 |
| - // Reset the island |
160 |
| - |
161 |
| - // Kick all island members (including the owner) |
162 |
| - kickMembers(oldIsland); |
163 |
| - |
164 |
| - // Add a reset |
165 |
| - getPlayers().addReset(getWorld(), user.getUniqueId()); |
166 |
| - } |
167 |
| - |
168 |
| - /** |
169 |
| - * Kicks the members (incl. owner) of the island. |
170 |
| - * @since 1.7.0 |
171 |
| - */ |
172 |
| - private void kickMembers(Island island) { |
173 |
| - /* |
174 |
| - * We cannot assume the island owner can run /[cmd] team kick (it might be disabled, or there could be permission restrictions...) |
175 |
| - * Therefore, we need to do it manually. |
176 |
| - * Plus, a more specific team event (TeamDeleteEvent) is called by this method. |
177 |
| - */ |
178 |
| - island.getMemberSet().forEach(memberUUID -> { |
179 |
| - |
180 |
| - User member = User.getInstance(memberUUID); |
181 |
| - // Send a "you're kicked" message if the member is not the island owner (send before removing!) |
182 |
| - if (!memberUUID.equals(island.getOwner())) { |
183 |
| - member.sendMessage("commands.island.reset.kicked-from-island", TextVariables.GAMEMODE, getAddon().getDescription().getName()); |
184 |
| - } |
185 |
| - // Remove player |
186 |
| - getIslands().removePlayer(getWorld(), memberUUID); |
187 |
| - |
188 |
| - // Clean player |
189 |
| - getPlayers().cleanLeavingPlayer(getWorld(), member, false, island); |
190 |
| - |
191 |
| - // Fire event |
192 |
| - TeamEvent.builder() |
193 |
| - .island(island) |
194 |
| - .reason(TeamEvent.Reason.DELETE) |
195 |
| - .involvedPlayer(memberUUID) |
196 |
| - .build(); |
197 |
| - }); |
198 |
| - } |
| 27 | + private boolean noPaste; |
| 28 | + |
| 29 | + public IslandResetCommand(CompositeCommand islandCommand) { |
| 30 | + super(islandCommand, "reset", "restart"); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Creates the island reset command |
| 35 | + * |
| 36 | + * @param islandCommand - parent command |
| 37 | + * @param noPaste - true if resetting should not paste a new island |
| 38 | + */ |
| 39 | + public IslandResetCommand(CompositeCommand islandCommand, boolean noPaste) { |
| 40 | + super(islandCommand, "reset", "restart"); |
| 41 | + this.noPaste = noPaste; |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public void setup() { |
| 46 | + setPermission("island.reset"); |
| 47 | + setOnlyPlayer(true); |
| 48 | + setParametersHelp("commands.island.reset.parameters"); |
| 49 | + setDescription("commands.island.reset.description"); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public boolean canExecute(User user, String label, List<String> args) { |
| 54 | + // Check cooldown |
| 55 | + if (getSettings().getResetCooldown() > 0 && checkCooldown(user)) { |
| 56 | + return false; |
| 57 | + } |
| 58 | + if (!getIslands().hasIsland(getWorld(), user.getUniqueId())) { |
| 59 | + user.sendMessage("general.errors.no-island"); |
| 60 | + return false; |
| 61 | + } |
| 62 | + int resetsLeft = getPlayers().getResetsLeft(getWorld(), user.getUniqueId()); |
| 63 | + if (resetsLeft != -1) { |
| 64 | + // Resets are not unlimited here |
| 65 | + if (resetsLeft == 0) { |
| 66 | + // No resets allowed |
| 67 | + user.sendMessage("commands.island.reset.none-left"); |
| 68 | + return false; |
| 69 | + } else { |
| 70 | + // Still some resets left |
| 71 | + // Notify how many resets are left |
| 72 | + user.sendMessage("commands.island.reset.resets-left", TextVariables.NUMBER, String.valueOf(resetsLeft)); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return true; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public boolean execute(User user, String label, List<String> args) { |
| 81 | + // Permission check if the name is not the default one |
| 82 | + if (!args.isEmpty()) { |
| 83 | + String name = getPlugin().getBlueprintsManager().validate(getAddon(), Util.sanitizeInput(args.get(0))); |
| 84 | + if (name == null || name.isEmpty()) { |
| 85 | + // The blueprint name is not valid. |
| 86 | + user.sendMessage("commands.island.create.unknown-blueprint"); |
| 87 | + return false; |
| 88 | + } |
| 89 | + if (!getPlugin().getBlueprintsManager().checkPerm(getAddon(), user, Util.sanitizeInput(args.get(0)))) { |
| 90 | + return false; |
| 91 | + } |
| 92 | + return resetIsland(user, name); |
| 93 | + } else { |
| 94 | + // Show panel after confirmation |
| 95 | + if (getPlugin().getSettings().isResetConfirmation()) { |
| 96 | + this.askConfirmation(user, user.getTranslation("commands.island.reset.confirmation"), |
| 97 | + () -> selectBundle(user, label)); |
| 98 | + } else { |
| 99 | + selectBundle(user, label); |
| 100 | + } |
| 101 | + return true; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Either selects the bundle to use or asks the user to choose. |
| 107 | + * |
| 108 | + * @since 1.5.1 |
| 109 | + */ |
| 110 | + private void selectBundle(@NonNull User user, @NonNull String label) { |
| 111 | + // Show panel only if there are multiple bundles available |
| 112 | + if (getPlugin().getBlueprintsManager().getBlueprintBundles(getAddon()).size() > 1) { |
| 113 | + // Show panel - once the player selected a bundle, this will re-run this command |
| 114 | + IslandCreationPanel.openPanel(this, user, label); |
| 115 | + } else { |
| 116 | + resetIsland(user, BlueprintsManager.DEFAULT_BUNDLE_NAME); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Reset island |
| 122 | + * |
| 123 | + * @param user user |
| 124 | + * @param name name of Blueprint Bundle |
| 125 | + * @return true if successful |
| 126 | + */ |
| 127 | + private boolean resetIsland(User user, String name) { |
| 128 | + // Get the player's old island |
| 129 | + Island oldIsland = getIslands().getIsland(getWorld(), user); |
| 130 | + deleteOldIsland(user, oldIsland); |
| 131 | + |
| 132 | + user.sendMessage("commands.island.create.creating-island"); |
| 133 | + // Create new island and then delete the old one |
| 134 | + try { |
| 135 | + Builder builder = NewIsland.builder().player(user).reason(Reason.RESET).addon(getAddon()) |
| 136 | + .oldIsland(oldIsland).name(name); |
| 137 | + if (noPaste) |
| 138 | + builder.noPaste(); |
| 139 | + builder.build(); |
| 140 | + } catch (IOException e) { |
| 141 | + getPlugin().logError("Could not create island for player. " + e.getMessage()); |
| 142 | + user.sendMessage(e.getMessage()); |
| 143 | + return false; |
| 144 | + } |
| 145 | + setCooldown(user.getUniqueId(), getSettings().getResetCooldown()); |
| 146 | + return true; |
| 147 | + } |
| 148 | + |
| 149 | + private void deleteOldIsland(User user, Island oldIsland) { |
| 150 | + // Fire island preclear event |
| 151 | + IslandEvent.builder().involvedPlayer(user.getUniqueId()).reason(Reason.PRECLEAR).island(oldIsland) |
| 152 | + .oldIsland(oldIsland).location(oldIsland.getCenter()).build(); |
| 153 | + |
| 154 | + // Reset the island |
| 155 | + |
| 156 | + // Kick all island members (including the owner) |
| 157 | + kickMembers(oldIsland); |
| 158 | + |
| 159 | + // Add a reset |
| 160 | + getPlayers().addReset(getWorld(), user.getUniqueId()); |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Kicks the members (incl. owner) of the island. |
| 165 | + * |
| 166 | + * @since 1.7.0 |
| 167 | + */ |
| 168 | + private void kickMembers(Island island) { |
| 169 | + /* |
| 170 | + * We cannot assume the island owner can run /[cmd] team kick (it might be |
| 171 | + * disabled, or there could be permission restrictions...) Therefore, we need to |
| 172 | + * do it manually. Plus, a more specific team event (TeamDeleteEvent) is called |
| 173 | + * by this method. |
| 174 | + */ |
| 175 | + island.getMemberSet().forEach(memberUUID -> { |
| 176 | + |
| 177 | + User member = User.getInstance(memberUUID); |
| 178 | + // Send a "you're kicked" message if the member is not the island owner (send |
| 179 | + // before removing!) |
| 180 | + if (!memberUUID.equals(island.getOwner())) { |
| 181 | + member.sendMessage("commands.island.reset.kicked-from-island", TextVariables.GAMEMODE, |
| 182 | + getAddon().getDescription().getName()); |
| 183 | + } |
| 184 | + // Remove player |
| 185 | + getIslands().removePlayer(island, memberUUID); |
| 186 | + |
| 187 | + // Clean player |
| 188 | + getPlayers().cleanLeavingPlayer(getWorld(), member, false, island); |
| 189 | + |
| 190 | + // Fire event |
| 191 | + TeamEvent.builder().island(island).reason(TeamEvent.Reason.DELETE).involvedPlayer(memberUUID).build(); |
| 192 | + }); |
| 193 | + } |
199 | 194 | }
|
0 commit comments