Skip to content

Commit c95f52a

Browse files
committed
Allow set flags to be manipulate via keywords
This adds support for updating an existing region, so that if you have a long list of items in a set flag, such as `deny-spawn` you can add or remove items, rather than overwrite. Example: ``` /rg flag test deny-spawn add creeper /rg flag test deny-spawn remove zombie ```
1 parent b835ee3 commit c95f52a

File tree

1 file changed

+30
-1
lines changed
  • worldguard-core/src/main/java/com/sk89q/worldguard/protection/flags

1 file changed

+30
-1
lines changed

worldguard-core/src/main/java/com/sk89q/worldguard/protection/flags/SetFlag.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,25 @@
2020
package com.sk89q.worldguard.protection.flags;
2121

2222
import com.google.common.collect.Sets;
23+
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
2324

2425
import java.util.ArrayList;
2526
import java.util.Collection;
2627
import java.util.HashSet;
2728
import java.util.List;
29+
import java.util.Objects;
2830
import java.util.Set;
31+
import java.util.regex.Matcher;
32+
import java.util.regex.Pattern;
2933

3034
/**
3135
* Stores a set of types.
3236
*/
3337
public class SetFlag<T> extends Flag<Set<T>> {
3438

39+
private static final Pattern ANY_MODIFIER = Pattern.compile("^(add|sub|subtract|rem|remove) (.*)$");
40+
private static final Pattern REMOVE_MODIFIERS = Pattern.compile("^(sub|subtract|rem|remove) (.*)$");
41+
3542
private Flag<T> subFlag;
3643

3744
public SetFlag(String name, RegionGroup defaultGroup, Flag<T> subFlag) {
@@ -60,10 +67,32 @@ public Set<T> parseInput(FlagContext context) throws InvalidFlagFormat {
6067
return Sets.newHashSet();
6168
} else {
6269
Set<T> items = Sets.newHashSet();
70+
boolean subtractive = false;
71+
72+
// If the input starts with particular keywords, attempt to load the existing values,
73+
// and make this a modification, instead of an overwrite.
74+
Matcher keywordMatcher = ANY_MODIFIER.matcher(input);
75+
if (keywordMatcher.matches()) {
76+
ProtectedRegion region = Objects.requireNonNull((ProtectedRegion) context.get("region"));
77+
78+
Set<T> existingValue = region.getFlag(this);
79+
if (existingValue != null) {
80+
items.addAll(existingValue);
81+
}
82+
83+
subtractive = REMOVE_MODIFIERS.matcher(input).matches();
84+
input = keywordMatcher.group(2);
85+
}
6386

6487
for (String str : input.split(",")) {
6588
FlagContext copy = context.copyWith(null, str, null);
66-
items.add(subFlag.parseInput(copy));
89+
90+
T subFlagValue = subFlag.parseInput(copy);
91+
if (subtractive) {
92+
items.remove(subFlagValue);
93+
} else {
94+
items.add(subFlagValue);
95+
}
6796
}
6897

6998
return items;

0 commit comments

Comments
 (0)