Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow set flags to be manipulate with add and sub keywords
Browse files Browse the repository at this point in the history
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.
DarkArc committed Jan 15, 2020

Verified

This commit was signed with the committer’s verified signature.
agzam Ag Ibragimov
1 parent b835ee3 commit a453f7e
Showing 1 changed file with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -20,11 +20,13 @@
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;

/**
@@ -60,10 +62,31 @@ public Set<T> parseInput(FlagContext context) throws InvalidFlagFormat {
return Sets.newHashSet();
} else {
Set<T> items = Sets.newHashSet();
boolean subtractive = false;

// If the input starts with `add ` or `sub `, attempt to load the existing values,
// and make this a modification, instead of an overwrite.
if (input.startsWith("add ") || input.startsWith("sub ")) {
ProtectedRegion region = Objects.requireNonNull((ProtectedRegion) context.get("region"));

Set<T> existingValue = region.getFlag(this);
if (existingValue != null) {
items.addAll(existingValue);
}

subtractive = input.startsWith("sub ");
input = input.substring(4);
}

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;

0 comments on commit a453f7e

Please sign in to comment.