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

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