mirror of
https://github.com/IntellectualSites/PlotSquared.git
synced 2024-11-25 22:56:45 +01:00
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:
parent
32654e1f25
commit
a601739fbd
@ -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);
|
||||
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 {
|
||||
MainUtil.sendMessage(player, Captions.FLAG_NOT_REMOVED);
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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() {
|
||||
|
Loading…
Reference in New Issue
Block a user