Improve handling of list flags, which in turn resolves issues where add and remove would allow players to add flags with unrecognized values.

This commit is contained in:
Alexander Söderberg 2019-10-27 11:25:33 +01:00
parent 32654e1f25
commit a601739fbd
2 changed files with 33 additions and 29 deletions

View File

@ -197,25 +197,26 @@ public class FlagCmd extends SubCommand {
}
if (args.length == 3 && flag instanceof ListFlag) {
String value = StringMan.join(Arrays.copyOfRange(args, 2, args.length), " ");
Optional<? extends Collection> flag1 =
plot.getFlag((Flag<? extends Collection<?>>) flag);
if (flag1.isPresent()) {
boolean o = flag1.get().removeAll((Collection) flag.parseValue(value));
if (o) {
if (flag1.get().isEmpty()) {
final boolean result = plot.removeFlag(flag);
if (result) {
MainUtil.sendMessage(player, Captions.FLAG_REMOVED);
} else {
MainUtil.sendMessage(player, Captions.FLAG_NOT_REMOVED);
final ListFlag<? extends Collection> listFlag = (ListFlag<? extends Collection>) flag;
final Optional<? extends Collection> collectionOptional = plot.getFlag(listFlag);
if (collectionOptional.isPresent()) {
final Collection parsedCollection = (Collection) flag.parseValue(value);
if (parsedCollection.isEmpty()) {
return !MainUtil.sendMessage(player, Captions.FLAG_NOT_REMOVED);
}
final Collection flagCollection = collectionOptional.get();
if (flagCollection.removeAll(parsedCollection)) {
if (flagCollection.isEmpty()) {
if (plot.removeFlag(flag)) {
return MainUtil.sendMessage(player, Captions.FLAG_REMOVED);
} else {
return !MainUtil.sendMessage(player, Captions.FLAG_NOT_REMOVED);
}
return true;
} else {
MainUtil.sendMessage(player, Captions.FLAG_REMOVED);
}
} else {
MainUtil.sendMessage(player, Captions.FLAG_NOT_REMOVED);
return false;
return !MainUtil.sendMessage(player, Captions.FLAG_NOT_REMOVED);
}
}
DBFunc.setFlags(plot, plot.getFlags());
@ -259,16 +260,19 @@ public class FlagCmd extends SubCommand {
}
Object val = parsed;
if (flag instanceof ListFlag) {
Optional<? extends Collection> flag1 =
plot.getFlag((Flag<? extends Collection<?>>) flag);
if (flag1.isPresent()) {
boolean o = flag1.get().addAll((Collection) parsed);
if (o) {
final Collection parsedCollection = (Collection<?>) parsed;
if (parsedCollection.isEmpty()) {
return !MainUtil.sendMessage(player, Captions.FLAG_NOT_ADDED);
}
final ListFlag<? extends Collection> listFlag = (ListFlag<? extends Collection>) flag;
final Optional<? extends Collection> collectionOptional = plot.getFlag(listFlag);
if (collectionOptional.isPresent()) {
final Collection flagCollection = collectionOptional.get();
if (flagCollection.addAll(parsedCollection)) {
MainUtil.sendMessage(player, Captions.FLAG_ADDED);
val = flag1.get();
val = flagCollection;
} else {
MainUtil.sendMessage(player, Captions.FLAG_NOT_ADDED);
return false;
return !MainUtil.sendMessage(player, Captions.FLAG_NOT_ADDED);
}
}
}

View File

@ -2,9 +2,14 @@ package com.github.intellectualsites.plotsquared.plot.flag;
import com.github.intellectualsites.plotsquared.plot.PlotSquared;
import com.github.intellectualsites.plotsquared.plot.object.PlotBlock;
import com.github.intellectualsites.plotsquared.plot.util.LegacyMappings;
import com.github.intellectualsites.plotsquared.plot.util.StringMan;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
public class PlotBlockListFlag extends ListFlag<HashSet<PlotBlock>> {
@ -17,14 +22,9 @@ public class PlotBlockListFlag extends ListFlag<HashSet<PlotBlock>> {
}
@Override public HashSet<PlotBlock> parseValue(final String value) {
final HashSet<PlotBlock> list = new HashSet<>();
for (final String item : value.split(",")) {
final PlotBlock block = PlotSquared.get().IMP.getLegacyMappings().fromAny(item);
if (block != null) {
list.add(block);
}
}
return list;
final LegacyMappings legacyMappings = PlotSquared.get().IMP.getLegacyMappings();
return Arrays.stream(value.split(",")).map(legacyMappings::fromAny).filter(Objects::nonNull)
.collect(Collectors.toCollection(HashSet::new));
}
@Override public String getValueDescription() {