Skip to content

Commit b574703

Browse files
committed
Allow set flags to be manipulate with add and sub 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.
1 parent b835ee3 commit b574703

File tree

1 file changed

+24
-6
lines changed
  • worldguard-core/src/main/java/com/sk89q/worldguard/protection/flags

1 file changed

+24
-6
lines changed

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

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,9 @@
2020
package com.sk89q.worldguard.protection.flags;
2121

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

24-
import java.util.ArrayList;
25-
import java.util.Collection;
26-
import java.util.HashSet;
27-
import java.util.List;
28-
import java.util.Set;
25+
import java.util.*;
2926

3027
/**
3128
* Stores a set of types.
@@ -60,10 +57,31 @@ public Set<T> parseInput(FlagContext context) throws InvalidFlagFormat {
6057
return Sets.newHashSet();
6158
} else {
6259
Set<T> items = Sets.newHashSet();
60+
boolean subtractive = false;
61+
62+
// If the input starts with `add ` or `sub `, attempt to load the existing values,
63+
// and make this a modification, instead of an overwrite.
64+
if (input.startsWith("add ") || input.startsWith("sub ")) {
65+
ProtectedRegion region = Objects.requireNonNull((ProtectedRegion) context.get("region"));
66+
67+
Set<T> existingValue = region.getFlag(this);
68+
if (existingValue != null) {
69+
items.addAll(existingValue);
70+
}
71+
72+
subtractive = input.startsWith("sub ");
73+
input = input.substring(4);
74+
}
6375

6476
for (String str : input.split(",")) {
6577
FlagContext copy = context.copyWith(null, str, null);
66-
items.add(subFlag.parseInput(copy));
78+
79+
T subFlagValue = subFlag.parseInput(copy);
80+
if (subtractive) {
81+
items.remove(subFlagValue);
82+
} else {
83+
items.add(subFlagValue);
84+
}
6785
}
6886

6987
return items;

0 commit comments

Comments
 (0)