Skip to content

Commit a453f7e

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 a453f7e

File tree

1 file changed

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

1 file changed

+24
-1
lines changed

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
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;
2931

3032
/**
@@ -60,10 +62,31 @@ public Set<T> parseInput(FlagContext context) throws InvalidFlagFormat {
6062
return Sets.newHashSet();
6163
} else {
6264
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+
}
6380

6481
for (String str : input.split(",")) {
6582
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+
}
6790
}
6891

6992
return items;

0 commit comments

Comments
 (0)