|
20 | 20 | package com.sk89q.worldguard.protection.flags; |
21 | 21 |
|
22 | 22 | import com.google.common.collect.Sets; |
| 23 | +import com.sk89q.worldguard.protection.regions.ProtectedRegion; |
23 | 24 |
|
24 | 25 | import java.util.ArrayList; |
25 | 26 | import java.util.Collection; |
26 | 27 | import java.util.HashSet; |
27 | 28 | import java.util.List; |
| 29 | +import java.util.Objects; |
28 | 30 | import java.util.Set; |
| 31 | +import java.util.regex.Matcher; |
| 32 | +import java.util.regex.Pattern; |
29 | 33 |
|
30 | 34 | /** |
31 | 35 | * Stores a set of types. |
32 | 36 | */ |
33 | 37 | public class SetFlag<T> extends Flag<Set<T>> { |
34 | 38 |
|
| 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 | + |
35 | 42 | private Flag<T> subFlag; |
36 | 43 |
|
37 | 44 | public SetFlag(String name, RegionGroup defaultGroup, Flag<T> subFlag) { |
@@ -60,10 +67,32 @@ public Set<T> parseInput(FlagContext context) throws InvalidFlagFormat { |
60 | 67 | return Sets.newHashSet(); |
61 | 68 | } else { |
62 | 69 | 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 | + } |
63 | 86 |
|
64 | 87 | for (String str : input.split(",")) { |
65 | 88 | 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 | + } |
67 | 96 | } |
68 | 97 |
|
69 | 98 | return items; |
|
0 commit comments