|
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; |
29 | 31 |
|
30 | 32 | /** |
@@ -60,10 +62,31 @@ public Set<T> parseInput(FlagContext context) throws InvalidFlagFormat { |
60 | 62 | return Sets.newHashSet(); |
61 | 63 | } else { |
62 | 64 | Set<T> items = Sets.newHashSet(); |
| 65 | + boolean subtractive = false; |
| 66 | + |
| 67 | + // If the input starts with `add ` or `sub `, attempt to load the existing values, |
| 68 | + // and make this a modification, instead of an overwrite. |
| 69 | + if (input.startsWith("add ") || input.startsWith("sub ")) { |
| 70 | + ProtectedRegion region = Objects.requireNonNull((ProtectedRegion) context.get("region")); |
| 71 | + |
| 72 | + Set<T> existingValue = region.getFlag(this); |
| 73 | + if (existingValue != null) { |
| 74 | + items.addAll(existingValue); |
| 75 | + } |
| 76 | + |
| 77 | + subtractive = input.startsWith("sub "); |
| 78 | + input = input.substring(4); |
| 79 | + } |
63 | 80 |
|
64 | 81 | for (String str : input.split(",")) { |
65 | 82 | FlagContext copy = context.copyWith(null, str, null); |
66 | | - items.add(subFlag.parseInput(copy)); |
| 83 | + |
| 84 | + T subFlagValue = subFlag.parseInput(copy); |
| 85 | + if (subtractive) { |
| 86 | + items.remove(subFlagValue); |
| 87 | + } else { |
| 88 | + items.add(subFlagValue); |
| 89 | + } |
67 | 90 | } |
68 | 91 |
|
69 | 92 | return items; |
|
0 commit comments