forked from espidev/ProtectionStones
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArgFlag.java
More file actions
420 lines (351 loc) · 21 KB
/
ArgFlag.java
File metadata and controls
420 lines (351 loc) · 21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package dev.espi.protectionstones.commands;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.protection.flags.*;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import dev.espi.protectionstones.*;
import dev.espi.protectionstones.utils.MiscUtil;
import dev.espi.protectionstones.utils.WGUtils;
import net.md_5.bungee.api.chat.*;
import org.apache.commons.lang3.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.util.StringUtil;
import java.util.*;
public class ArgFlag implements PSCommandArg {
@Override
public List<String> getNames() {
return Collections.singletonList("flag");
}
@Override
public boolean allowNonPlayersToExecute() {
return false;
}
@Override
public List<String> getPermissionsToExecute() {
return Collections.singletonList("protectionstones.flags");
}
@Override
public HashMap<String, Boolean> getRegisteredFlags() {
HashMap<String, Boolean> m = new HashMap<>();
m.put("-g", true); // group
return m;
}
private static final int GUI_SIZE = 18;
private static final List<String> FLAG_GROUPS = FlagHandler.FLAG_GROUPS;
private static final int[] REGION_GROUP_KERNING_LENGTHS = {2, 17, 14, 26, 23};
private String getDots(int num) {
StringBuilder str = new StringBuilder(" " + ChatColor.DARK_GRAY);
for (int i = 0; i < num; i++) str.append(".");
return str.toString();
}
// flag gui that has ability to use pages
private boolean openFlagGUI(Player p, PSRegion r, int page) {
List<String> allowedFlags = new ArrayList<>(r.getTypeOptions().allowedFlags.keySet());
// ensure the page is valid and in range
if (page < 0 || (page * GUI_SIZE) > allowedFlags.size()) {
PSL.msg(p, PSL.PAGE_DOES_NOT_EXIST.msg());
return true;
}
// add blank space if gui not long enough
for (int i = 0; i < (GUI_SIZE * page + GUI_SIZE) - (Math.min(allowedFlags.size(), GUI_SIZE * page + GUI_SIZE) - GUI_SIZE * page); i++) {
PSL.msg(p, ChatColor.WHITE + "");
}
PSL.msg(p, PSL.FLAG_GUI_HEADER.msg());
// send actual flags
for (int i = GUI_SIZE * page; i < Math.min(allowedFlags.size(), GUI_SIZE * page + GUI_SIZE); i++) {
if (i >= allowedFlags.size()) {
PSL.msg(p, ChatColor.WHITE + "");
} else {
String flag = allowedFlags.get(i);
List<String> currentFlagGroups = r.getTypeOptions().allowedFlags.get(flag);
TextComponent flagLine = new TextComponent();
// calculate flag command
String suggestedCommand = "/" + ProtectionStones.getInstance().getConfigOptions().base_command + " flag ";
// match flag
Flag<?> f = Flags.fuzzyMatchFlag(WGUtils.getFlagRegistry(), flag);
if (f == null) continue;
Object fValue = r.getWGRegion().getFlag(f);
// check current flag's set group
String groupfValue = "all";
if (f.getRegionGroupFlag() != null && r.getWGRegion().getFlag(f.getRegionGroupFlag()) != null) {
groupfValue = r.getWGRegion().getFlag(f.getRegionGroupFlag()).toString()
.toLowerCase().replace("_", "");
}
// add flag group if there is one set for the flag (for use in click commands)
String flagGroup = "";
if (f.getRegionGroupFlag() != null && r.getWGRegion().getFlag(f.getRegionGroupFlag()) != null) {
flagGroup = "-g " + groupfValue + " ";
}
// replace § with & to prevent "illegal characters in chat" disconnection
if (fValue instanceof String) {
fValue = ((String) fValue).replace("§", "&");
}
// add line based on flag type
if (f instanceof StateFlag) { // allow/deny
boolean isGroupValueAll = groupfValue.equalsIgnoreCase("all") || groupfValue.isEmpty();
TextComponent allow = new TextComponent((fValue == StateFlag.State.ALLOW ? ChatColor.WHITE : ChatColor.DARK_GRAY) + "Allow"),
deny = new TextComponent((fValue == StateFlag.State.DENY ? ChatColor.WHITE : ChatColor.DARK_GRAY) + "Deny");
allow.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(PSL.FLAG_GUI_HOVER_SET.msg()).create()));
deny.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(PSL.FLAG_GUI_HOVER_SET.msg()).create()));
if (fValue == StateFlag.State.ALLOW) {
allow.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, suggestedCommand + flagGroup + page + ":" + flag + " none"));
deny.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, suggestedCommand + flagGroup + page + ":" + flag + " deny"));
} else if (fValue == StateFlag.State.DENY) {
allow.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, suggestedCommand + flagGroup + page + ":" + flag + " allow"));
deny.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, suggestedCommand + flagGroup + page + ":" + flag + " none"));
} else {
allow.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, suggestedCommand + flagGroup + page + ":" + flag + " allow"));
deny.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, suggestedCommand + flagGroup + page + ":" + flag + " deny"));
}
flagLine.addExtra(allow);
flagLine.addExtra(" ");
flagLine.addExtra(deny);
flagLine.addExtra(getDots(5));
} else if (f instanceof BooleanFlag) { // true/false
TextComponent allow = new TextComponent((fValue == Boolean.TRUE ? ChatColor.WHITE : ChatColor.DARK_GRAY) + "True"),
deny = new TextComponent((fValue == Boolean.FALSE ? ChatColor.WHITE : ChatColor.DARK_GRAY) + "False");
allow.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(PSL.FLAG_GUI_HOVER_SET.msg()).create()));
deny.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(PSL.FLAG_GUI_HOVER_SET.msg()).create()));
if (fValue == Boolean.TRUE) {
allow.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, suggestedCommand + flagGroup + page + ":" + flag + " none"));
deny.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, suggestedCommand + flagGroup + page + ":" + flag + " false"));
} else if (fValue == Boolean.FALSE) {
allow.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, suggestedCommand + flagGroup + page + ":" + flag + " true"));
deny.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, suggestedCommand + flagGroup + page + ":" + flag + " none"));
} else {
allow.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, suggestedCommand + flagGroup + page + ":" + flag + " true"));
deny.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, suggestedCommand + flagGroup + page + ":" + flag + " false"));
}
flagLine.addExtra(allow);
flagLine.addExtra(" ");
flagLine.addExtra(deny);
flagLine.addExtra(getDots(5));
} else { // text
TextComponent edit = new TextComponent(ChatColor.DARK_GRAY + "Edit");
edit.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(PSL.FLAG_GUI_HOVER_SET_TEXT.msg()
.replace("%value%", fValue == null ? "none" : fValue.toString())).create()));
edit.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, suggestedCommand + flagGroup + flag + " "));
flagLine.addExtra(edit);
flagLine.addExtra(getDots(22));
}
// put group it applies to
TextComponent groupChange = new TextComponent(ChatColor.DARK_GRAY + " [ " + ChatColor.WHITE + groupfValue + ChatColor.DARK_GRAY + " ]");
String nextGroup;
if (currentFlagGroups.contains(groupfValue)) { // if the current flag group is an allowed flag group
nextGroup = currentFlagGroups.get((currentFlagGroups.indexOf(groupfValue) + 1) % currentFlagGroups.size());
} else { // otherwise, just take the first allowed flag group
nextGroup = currentFlagGroups.get(0);
}
// set hover and click task for flag group
BaseComponent[] hover;
// HACK: Prevent pvp flag value from being changed to none/null
// Special handling for "pvp" flag with "all" group, disabling interaction.
if (flag.equalsIgnoreCase("pvp") && groupfValue.equalsIgnoreCase("all")) {
hover = new ComponentBuilder(PSL.FLAG_PREVENT_EXPLOIT_HOVER.msg()).create();
// Remove click action to fully disable changing this group.
groupChange.setClickEvent(null);
} else if (fValue == null) {
hover = new ComponentBuilder(PSL.FLAG_GUI_HOVER_CHANGE_GROUP_NULL.msg()).create();
} else {
hover = new ComponentBuilder(PSL.FLAG_GUI_HOVER_CHANGE_GROUP.msg().replace("%group%", nextGroup)).create();
}
// Always set hover if the flag is pvp and group is "all"
if (flag.equalsIgnoreCase("pvp") && groupfValue.equalsIgnoreCase("all")) {
hover = new ComponentBuilder(PSL.FLAG_PREVENT_EXPLOIT_HOVER.msg()).create();
groupChange.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, hover));
groupChange.setClickEvent(null); // Disable click event explicitly
} else if (!nextGroup.equals(groupfValue)) {
groupChange.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, hover));
groupChange.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, suggestedCommand + "-g " + nextGroup + " " + page + ":" + flag + " " + fValue));
}
flagLine.addExtra(groupChange);
// send message
flagLine.addExtra(getDots(40 - REGION_GROUP_KERNING_LENGTHS[FLAG_GROUPS.indexOf(groupfValue)]) + ChatColor.AQUA + " " + flag);
p.spigot().sendMessage(flagLine);
}
}
// create footer
TextComponent backPage = new TextComponent(ChatColor.AQUA + " <<"), nextPage = new TextComponent(ChatColor.AQUA + ">> ");
backPage.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(PSL.GO_BACK_PAGE.msg()).create()));
nextPage.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(PSL.GO_NEXT_PAGE.msg()).create()));
backPage.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/" + ProtectionStones.getInstance().getConfigOptions().base_command + " flag " + (page - 1)));
nextPage.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/" + ProtectionStones.getInstance().getConfigOptions().base_command + " flag " + (page + 1)));
TextComponent footer = new TextComponent(ChatColor.DARK_GRAY + "" + ChatColor.STRIKETHROUGH + "=====" + ChatColor.RESET);
// add back page button if the page isn't 0
if (page != 0) footer.addExtra(backPage);
// add page number
footer.addExtra(new TextComponent(ChatColor.WHITE + " " + (page + 1) + " "));
// add forward page button if the page isn't last
if (page * GUI_SIZE + GUI_SIZE < r.getTypeOptions().allowedFlags.size()) footer.addExtra(nextPage);
footer.addExtra(ChatColor.DARK_GRAY + "" + ChatColor.STRIKETHROUGH + "=====");
p.spigot().sendMessage(footer);
return true;
}
@Override
public boolean executeArgument(CommandSender s, String[] args, HashMap<String, String> flags) {
Player p = (Player) s;
PSRegion r = PSRegion.fromLocationGroup(p.getLocation());
if (!p.hasPermission("protectionstones.flags")) {
PSL.msg(p, PSL.NO_PERMISSION_FLAGS.msg());
return true;
}
if (r == null) {
PSL.msg(p, PSL.NOT_IN_REGION.msg());
return true;
}
if (WGUtils.hasNoAccess(r.getWGRegion(), p, WorldGuardPlugin.inst().wrapPlayer(p), false)) {
PSL.msg(p, PSL.NO_ACCESS.msg());
return true;
}
// /ps flag GUI
if (args.length == 1) return openFlagGUI(p, r, 0);
// go to GUI page
if (args.length == 2) {
if (MiscUtil.isValidInteger(args[1])) {
return openFlagGUI(p, r, Integer.parseInt(args[1]));
}
PSL.msg(p, PSL.FLAG_HELP.msg());
return true;
}
if (args.length < 3) {
PSL.msg(p, PSL.FLAG_HELP.msg());
return true;
}
// beyond 2 args (set flag)
try {
String flagName = args[1].toLowerCase();
String gui = "";
String[] flagSplit = flagName.split(":");
if (flagSplit.length == 2) { // check if there is a GUI that needs to be reshown
gui = flagSplit[0];
flagName = flagSplit[1];
}
LinkedHashMap<String, List<String>> allowedFlags = r.getTypeOptions().allowedFlags;
// check if flag is allowed and its group is also allowed
if (allowedFlags.keySet().contains(flagName) && allowedFlags.get(flagName).contains(flags.getOrDefault("-g", "all")) && p.hasPermission("protectionstones.flags.edit." + flagName)) {
StringBuilder value = new StringBuilder();
for (int i = 2; i < args.length; i++) value.append(args[i]).append(" ");
setFlag(r, p, args[1], value.toString().trim(), flags.getOrDefault("-g", ""));
// reshow GUI
if (!gui.equals("")) {
Bukkit.dispatchCommand(p, ProtectionStones.getInstance().getConfigOptions().base_command + " flag " + gui);
}
} else {
PSL.msg(p, PSL.NO_PERMISSION_PER_FLAG.msg());
}
} catch (ArrayIndexOutOfBoundsException e) {
PSL.msg(p, PSL.FLAG_HELP.msg());
}
return true;
}
// tab completion
@Override
public List<String> tabComplete(CommandSender sender, String alias, String[] args) {
if (sender instanceof Player) {
Player p = (Player) sender;
PSRegion r = PSRegion.fromLocationGroup(p.getLocation());
if (r == null) return null;
List<String> keywords = new ArrayList<>();
if (args.length == 2) { // -g, or allowed flag names
keywords.add("-g");
for (String f : r.getTypeOptions().allowedFlags.keySet()) { // must allow the "all" group
if (r.getTypeOptions().allowedFlags.get(f).contains("all")) {
keywords.add(f);
}
}
return StringUtil.copyPartialMatches(args[1], keywords, new ArrayList<>());
} else if (args.length == 3 && args[1].equals("-g")) { // -g options
keywords.addAll(FlagHandler.FLAG_GROUPS);
return StringUtil.copyPartialMatches(args[2], keywords, new ArrayList<>());
} else if (args.length == 3) { // flag options
keywords.addAll(Arrays.asList("null", "default"));
Flag<?> f = Flags.fuzzyMatchFlag(WGUtils.getFlagRegistry(), args[1]);
if (f instanceof StateFlag) {
keywords.addAll(Arrays.asList("allow", "deny"));
} else if (f instanceof BooleanFlag) {
keywords.addAll(Arrays.asList("true", "false"));
}
return StringUtil.copyPartialMatches(args[2], keywords, new ArrayList<>());
} else if (args.length == 4 && args[1].equals("-g")) { // -g option flag
for (String f : r.getTypeOptions().allowedFlags.keySet()) {
if (r.getTypeOptions().allowedFlags.get(f).contains(args[2])) { // if the flag is allowed for this group
keywords.add(f);
}
}
return StringUtil.copyPartialMatches(args[3], keywords, new ArrayList<>());
} else if (args.length == 5 && args[1].equals("-g")) { // -g option flag arg
keywords.addAll(Arrays.asList("null", "default"));
Flag<?> f = Flags.fuzzyMatchFlag(WGUtils.getFlagRegistry(), args[3]);
if (f instanceof StateFlag) {
keywords.addAll(Arrays.asList("allow", "deny"));
} else if (f instanceof BooleanFlag) {
keywords.addAll(Arrays.asList("true", "false"));
}
return StringUtil.copyPartialMatches(args[4], keywords, new ArrayList<>());
}
}
return null;
}
// /ps flag logic (utilizing WG internal /region flag logic)
static void setFlag(PSRegion r, CommandSender p, String flagName, String value, String groupValue) {
// correct the flag if gui flags are there
String[] flagSplit = flagName.split(":");
if (flagSplit.length == 2) flagName = flagSplit[1];
Flag flag = Flags.fuzzyMatchFlag(WGUtils.getFlagRegistry(), flagName);
ProtectedRegion region = r.getWGRegion();
try {
if (value.equalsIgnoreCase("default")) { // get default from config, or from WG
HashMap<Flag<?>, Object> flags = new HashMap<>(r.getTypeOptions().regionFlags);
FlagHandler.initDefaultFlagPlaceholders(flags, (Player) p);
if (flags.get(flag) != null) {
region.setFlag(flag, flags.get(flag));
} else {
region.setFlag(flag, flag.getDefault());
}
if (flag.getRegionGroupFlag() != null) {
region.setFlag(flag.getRegionGroupFlag(), null);
}
PSL.msg(p, PSL.FLAG_SET.msg().replace("%flag%", flagName));
} else if (value.equalsIgnoreCase("null") || value.equalsIgnoreCase("none")) { // null flag (remove)
// HACK: pvp flag should never be allowed to set null when the flag group is restricted to all, since
// the default is that nonmembers can be killed, but members cannot.
boolean isGroupValueAll = groupValue.equalsIgnoreCase("all") || groupValue.isEmpty();
if (r.getTypeOptions().regionFlags.get(flag) != null && isGroupValueAll && flagName.equalsIgnoreCase("pvp")) {
PSL.msg(p, PSL.FLAG_PREVENT_EXPLOIT.msg());
return;
}
region.setFlag(flag, null);
if (flag.getRegionGroupFlag() != null) {
region.setFlag(flag.getRegionGroupFlag(), null);
}
PSL.msg(p, PSL.FLAG_SET.msg().replace("%flag%", flagName));
} else { // custom set flag using WG internal
FlagContext fc = FlagContext.create().setInput(value).build();
region.setFlag(flag, flag.parseInput(fc));
if (!groupValue.equals("") && flag.getRegionGroupFlag() != null) {
region.setFlag(flag.getRegionGroupFlag(), flag.getRegionGroupFlag().detectValue(groupValue));
}
PSL.msg(p, PSL.FLAG_SET.msg().replace("%flag%", flagName));
}
} catch (InvalidFlagFormat invalidFlagFormat) {
//invalidFlagFormat.printStackTrace();
PSL.msg(p, PSL.FLAG_NOT_SET.msg().replace("%flag%", flagName));
}
}
}