Compare commits

..
Author SHA1 Message Date
Wyatt Childers c95f52a303 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
```
2020-01-14 22:56:04 -05:00
3 changed files with 32 additions and 4 deletions

No files matched your search

+1 -2
View File
@@ -11,7 +11,6 @@ applyPlatformAndCoreConfiguration()
applyShadowConfiguration()
repositories {
mavenLocal()
maven {
name = "paper"
url = uri("https://papermc.io/repo/repository/maven-public/")
@@ -28,7 +27,7 @@ dependencies {
"api"("com.destroystokyo.paper:paper-api:1.15-R0.1-SNAPSHOT")
"implementation"("io.papermc:paperlib:1.0.2")
"api"("com.sk89q.worldedit:worldedit-bukkit:${Versions.WORLDEDIT}") { isTransitive = false }
"implementation"("com.sk89q:commandbook:3.0-SNAPSHOT") { isTransitive = false }
"implementation"("com.sk89q:commandbook:2.3") { isTransitive = false }
"implementation"("org.bstats:bstats-bukkit:1.5")
}
@@ -19,7 +19,7 @@
package com.sk89q.worldguard.bukkit.listener;
import com.sk89q.commandbook.component.info.InfoComponent;
import com.sk89q.commandbook.InfoComponent;
import com.sk89q.worldguard.LocalPlayer;
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
@@ -20,18 +20,25 @@
package com.sk89q.worldguard.protection.flags;
import com.google.common.collect.Sets;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Stores a set of types.
*/
public class SetFlag<T> extends Flag<Set<T>> {
private static final Pattern ANY_MODIFIER = Pattern.compile("^(add|sub|subtract|rem|remove) (.*)$");
private static final Pattern REMOVE_MODIFIERS = Pattern.compile("^(sub|subtract|rem|remove) (.*)$");
private Flag<T> subFlag;
public SetFlag(String name, RegionGroup defaultGroup, Flag<T> subFlag) {
@@ -60,10 +67,32 @@ public class SetFlag<T> extends Flag<Set<T>> {
return Sets.newHashSet();
} else {
Set<T> items = Sets.newHashSet();
boolean subtractive = false;
// If the input starts with particular keywords, attempt to load the existing values,
// and make this a modification, instead of an overwrite.
Matcher keywordMatcher = ANY_MODIFIER.matcher(input);
if (keywordMatcher.matches()) {
ProtectedRegion region = Objects.requireNonNull((ProtectedRegion) context.get("region"));
Set<T> existingValue = region.getFlag(this);
if (existingValue != null) {
items.addAll(existingValue);
}
subtractive = REMOVE_MODIFIERS.matcher(input).matches();
input = keywordMatcher.group(2);
}
for (String str : input.split(",")) {
FlagContext copy = context.copyWith(null, str, null);
items.add(subFlag.parseInput(copy));
T subFlagValue = subFlag.parseInput(copy);
if (subtractive) {
items.remove(subFlagValue);
} else {
items.add(subFlagValue);
}
}
return items;