diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/FlagCmd.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/FlagCmd.java index 2fc224cdc..c3cc7d628 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/FlagCmd.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/FlagCmd.java @@ -2,27 +2,22 @@ package com.github.intellectualsites.plotsquared.plot.commands; import com.github.intellectualsites.plotsquared.commands.CommandDeclaration; import com.github.intellectualsites.plotsquared.plot.PlotSquared; +import com.github.intellectualsites.plotsquared.plot.config.CaptionUtility; import com.github.intellectualsites.plotsquared.plot.config.Captions; import com.github.intellectualsites.plotsquared.plot.config.Settings; -import com.github.intellectualsites.plotsquared.plot.database.DBFunc; -import com.github.intellectualsites.plotsquared.plot.flag.BlockStateListFlag; -import com.github.intellectualsites.plotsquared.plot.flag.FlagManager; -import com.github.intellectualsites.plotsquared.plot.flag.Flags; +import com.github.intellectualsites.plotsquared.plot.flags.FlagParseException; import com.github.intellectualsites.plotsquared.plot.flags.GlobalFlagContainer; import com.github.intellectualsites.plotsquared.plot.flags.PlotFlag; -import com.github.intellectualsites.plotsquared.plot.flags.implementations.PlotWeatherFlag; -import com.github.intellectualsites.plotsquared.plot.flags.types.BlockTypeListFlag; import com.github.intellectualsites.plotsquared.plot.flags.types.IntegerFlag; +import com.github.intellectualsites.plotsquared.plot.flags.types.ListFlag; import com.github.intellectualsites.plotsquared.plot.object.Location; import com.github.intellectualsites.plotsquared.plot.object.Plot; import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer; import com.github.intellectualsites.plotsquared.plot.util.MainUtil; import com.github.intellectualsites.plotsquared.plot.util.MathMan; import com.github.intellectualsites.plotsquared.plot.util.Permissions; -import com.github.intellectualsites.plotsquared.plot.util.PlotWeather; import com.github.intellectualsites.plotsquared.plot.util.StringComparison; import com.github.intellectualsites.plotsquared.plot.util.StringMan; -import com.sk89q.worldedit.world.block.BlockType; import java.util.ArrayList; import java.util.Arrays; @@ -32,49 +27,51 @@ import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.Optional; -import java.util.Set; +import java.util.StringJoiner; -@CommandDeclaration(command = "setflag", aliases = {"f", "flag", - "setflag"}, usage = "/plot flag ", description = "Set plot flags", category = CommandCategory.SETTINGS, requiredType = RequiredType.NONE, permission = "plots.flag") +@CommandDeclaration(command = "flag", aliases = {"f", + "flag"}, usage = "/plot flag ", + description = "Set plot flags", + category = CommandCategory.SETTINGS, + requiredType = RequiredType.NONE, + permission = "plots.flag") public class FlagCmd extends SubCommand { - private boolean checkPermValue(PlotPlayer player, PlotFlag flag, String key, String value) { + private boolean checkPermValue(PlotPlayer player, PlotFlag flag, String key, String value) { key = key.toLowerCase(); value = value.toLowerCase(); - String perm = Captions - .format(Captions.PERMISSION_SET_FLAG_KEY_VALUE.getTranslated(), key.toLowerCase(), - value.toLowerCase()); + String perm = CaptionUtility + .format(Captions.PERMISSION_SET_FLAG_KEY_VALUE.getTranslated(), key.toLowerCase(), value.toLowerCase()); if (flag instanceof IntegerFlag && MathMan.isInteger(value)) { try { int numeric = Integer.parseInt(value); perm = perm.substring(0, perm.length() - value.length() - 1); if (numeric > 0) { - int checkRange = PlotSquared.get().getPlatform().equalsIgnoreCase("bukkit") ? - numeric : - Settings.Limit.MAX_PLOTS; + int checkRange = + PlotSquared.get().getPlatform().equalsIgnoreCase("bukkit") ? numeric : Settings.Limit.MAX_PLOTS; final boolean result = player.hasPermissionRange(perm, checkRange) >= numeric; if (!result) { - MainUtil.sendMessage(player, Captions.NO_PERMISSION, Captions - .format(Captions.PERMISSION_SET_FLAG_KEY_VALUE.getTranslated(), - key.toLowerCase(), value.toLowerCase())); + MainUtil.sendMessage(player, Captions.NO_PERMISSION, CaptionUtility + .format(Captions.PERMISSION_SET_FLAG_KEY_VALUE.getTranslated(), key.toLowerCase(), + value.toLowerCase())); } return result; } } catch (NumberFormatException ignore) { } - } else if (flag instanceof BlockTypeListFlag) { + // TODO is this required at all? + /* } else if (flag instanceof BlockTypeListFlag) { final BlockTypeListFlag blockListFlag = (BlockTypeListFlag) flag; try { - List parsedBlocks = blockListFlag.parse(value); - for (final BlockType block : parsedBlocks) { - final String permission = Captions + BlockTypeListFlag parsedFlag = blockListFlag.parse(value); + for (final BlockType block : parsedFlag.getValue()) { + final String permission = CaptionUtility .format(Captions.PERMISSION_SET_FLAG_KEY_VALUE.getTranslated(), key.toLowerCase(), block.toString().toLowerCase()); final boolean result = Permissions.hasPermission(player, permission); if (!result) { - MainUtil.sendMessage(player, Captions.NO_PERMISSION, Captions + MainUtil.sendMessage(player, Captions.NO_PERMISSION, CaptionUtility .format(Captions.PERMISSION_SET_FLAG_KEY_VALUE.getTranslated(), key.toLowerCase(), value.toLowerCase())); return false; @@ -83,11 +80,31 @@ public class FlagCmd extends SubCommand { } catch (final Exception e) { return false; } + return true;*/ + } else if (flag instanceof ListFlag) { + final ListFlag listFlag = (ListFlag) flag; + try { + PlotFlag, ?> parsedFlag = listFlag.parse(value); + for (final Object entry : parsedFlag.getValue()) { + final String permission = CaptionUtility + .format(Captions.PERMISSION_SET_FLAG_KEY_VALUE.getTranslated(), key.toLowerCase(), + entry.toString().toLowerCase()); + final boolean result = Permissions.hasPermission(player, permission); + if (!result) { + MainUtil.sendMessage(player, Captions.NO_PERMISSION, CaptionUtility + .format(Captions.PERMISSION_SET_FLAG_KEY_VALUE.getTranslated(), key.toLowerCase(), + value.toLowerCase())); + return false; + } + } + } catch (final Exception e) { + return false; + } return true; } final boolean result = Permissions.hasPermission(player, perm); if (!result) { - MainUtil.sendMessage(player, Captions.NO_PERMISSION, Captions + MainUtil.sendMessage(player, Captions.NO_PERMISSION, CaptionUtility .format(Captions.PERMISSION_SET_FLAG_KEY_VALUE.getTranslated(), key.toLowerCase(), value.toLowerCase())); } @@ -117,20 +134,18 @@ public class FlagCmd extends SubCommand { sendMessage(player, Captions.PLOT_NOT_CLAIMED); return false; } - if (!plot.isOwner(player.getUUID()) && !Permissions - .hasPermission(player, Captions.PERMISSION_SET_FLAG_OTHER)) { - MainUtil - .sendMessage(player, Captions.NO_PERMISSION, Captions.PERMISSION_SET_FLAG_OTHER); + if (!plot.isOwner(player.getUUID()) && !Permissions.hasPermission(player, Captions.PERMISSION_SET_FLAG_OTHER)) { + MainUtil.sendMessage(player, Captions.NO_PERMISSION, Captions.PERMISSION_SET_FLAG_OTHER); return false; } - PlotFlag flag = null; + PlotFlag flag = null; if (args.length > 1) { flag = GlobalFlagContainer.getInstance().getFlagFromString(args[1]); - if (flag == null || flag.isReserved()) { + if (flag == null) { boolean suggested = false; try { - StringComparison> stringComparison = - new StringComparison<>(args[1], Flags.getFlags()); + StringComparison> stringComparison = + new StringComparison<>(args[1], GlobalFlagContainer.getInstance().getFlagMap().values()); String best = stringComparison.getBestMatch(); if (best != null) { MainUtil.sendMessage(player, Captions.NOT_VALID_FLAG_SUGGESTED, best); @@ -145,106 +160,77 @@ public class FlagCmd extends SubCommand { } switch (args[0].toLowerCase()) { case "info": { - if (!Permissions.hasPermission(player, Captions.PERMISSION_SET_FLAG)) { - MainUtil.sendMessage(player, Captions.NO_PERMISSION, "plots.flag.info"); - return false; - } - if (args.length != 2) { - MainUtil.sendMessage(player, Captions.COMMAND_SYNTAX, "/plot flag info "); - return false; - } - // flag key - MainUtil.sendMessage(player, Captions.FLAG_KEY, flag.getName()); - // flag type - MainUtil.sendMessage(player, Captions.FLAG_TYPE, flag.getClass().getSimpleName()); - // Flag type description - MainUtil.sendMessage(player, Captions.FLAG_DESC, flag.getValueDescription()); - return true; + return info(player, args, flag); } case "set": { if (!Permissions.hasPermission(player, Captions.PERMISSION_SET_FLAG)) { - MainUtil - .sendMessage(player, Captions.NO_PERMISSION, Captions.PERMISSION_SET_FLAG); + MainUtil.sendMessage(player, Captions.NO_PERMISSION, Captions.PERMISSION_SET_FLAG); return false; } if (args.length < 3) { - MainUtil.sendMessage(player, Captions.COMMAND_SYNTAX, - "/plot flag set "); + MainUtil.sendMessage(player, Captions.COMMAND_SYNTAX, "/plot flag set "); return false; } String value = StringMan.join(Arrays.copyOfRange(args, 2, args.length), " "); if (!checkPermValue(player, flag, args[1], value)) { return false; } - Object parsed = flag.parseValue(value); - if (parsed == null) { - MainUtil.sendMessage(player, "&c" + flag.getValueDescription()); - return false; - } - if (flag instanceof ListFlag) { - if (!(parsed instanceof Collection) || ((Collection) parsed).isEmpty()) { - return !MainUtil.sendMessage(player, Captions.FLAG_NOT_ADDED); - } - } - boolean result = plot.setFlag(flag, parsed); - if (!result) { - MainUtil.sendMessage(player, Captions.FLAG_NOT_ADDED); + PlotFlag parsed; + try { + parsed = flag.parse(value); + } catch (FlagParseException e) { + MainUtil.sendMessage(player, "&c" + e.getErrorMessage()); return false; } + plot.setFlag(parsed); MainUtil.sendMessage(player, Captions.FLAG_ADDED); return true; } case "remove": { if (!Permissions.hasPermission(player, Captions.PERMISSION_FLAG_REMOVE)) { - MainUtil.sendMessage(player, Captions.NO_PERMISSION, - Captions.PERMISSION_FLAG_REMOVE); + MainUtil.sendMessage(player, Captions.NO_PERMISSION, Captions.PERMISSION_FLAG_REMOVE); return false; } if (args.length != 2 && args.length != 3) { - MainUtil.sendMessage(player, Captions.COMMAND_SYNTAX, - "/plot flag remove [values]"); + MainUtil.sendMessage(player, Captions.COMMAND_SYNTAX, "/plot flag remove [values]"); return false; } - if (!Permissions.hasPermission(player, Captions - .format(Captions.PERMISSION_SET_FLAG_KEY.getTranslated(), - args[1].toLowerCase()))) { + if (!Permissions.hasPermission(player, + CaptionUtility.format(Captions.PERMISSION_SET_FLAG_KEY.getTranslated(), args[1].toLowerCase()))) { if (args.length != 3) { - MainUtil.sendMessage(player, Captions.NO_PERMISSION, Captions - .format(Captions.PERMISSION_SET_FLAG_KEY.getTranslated(), - args[1].toLowerCase())); + MainUtil.sendMessage(player, Captions.NO_PERMISSION, CaptionUtility + .format(Captions.PERMISSION_SET_FLAG_KEY.getTranslated(), args[1].toLowerCase())); return false; } - for (String entry : args[2].split(",")) { - if (!checkPermValue(player, flag, args[1], entry)) { - return false; - } - } } if (args.length == 3 && flag instanceof ListFlag) { String value = StringMan.join(Arrays.copyOfRange(args, 2, args.length), " "); - final ListFlag listFlag = (ListFlag) flag; - final Optional 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); - } + final ListFlag listFlag = (ListFlag) flag; + final List list = plot.getFlag((Class>) listFlag.getClass()); + final PlotFlag, ?> parsedFlag; + try { + parsedFlag = listFlag.parse(value); + } catch (FlagParseException e) { + MainUtil.sendMessage(player, "&c" + e.getErrorMessage()); + return false; + } + if (parsedFlag.getValue().isEmpty()) { + return !MainUtil.sendMessage(player, Captions.FLAG_NOT_REMOVED); + } + if (list.removeAll(parsedFlag.getValue())) { + if (list.isEmpty()) { + if (plot.removeFlag(flag)) { + return MainUtil.sendMessage(player, Captions.FLAG_REMOVED); } else { - MainUtil.sendMessage(player, Captions.FLAG_REMOVED); + return !MainUtil.sendMessage(player, Captions.FLAG_NOT_REMOVED); } } else { - return !MainUtil.sendMessage(player, Captions.FLAG_NOT_REMOVED); + MainUtil.sendMessage(player, Captions.FLAG_REMOVED); } + } else { + return !MainUtil.sendMessage(player, Captions.FLAG_NOT_REMOVED); } - DBFunc.setFlags(plot, plot.getFlags()); + // TODO reimplement somewhere else: DBFunc.setFlags(plot, plot.getFlags()); return true; } else { boolean result = plot.removeFlag(flag); @@ -253,23 +239,22 @@ public class FlagCmd extends SubCommand { return false; } } - if (flag.) { + /* TODO reimplement, maybe handle it with events? + if (flag == Flags.TIME) { player.setTime(Long.MAX_VALUE); - } else if (flag instanceof PlotWeatherFlag) { + } else if (flag == Flags.WEATHER) { player.setWeather(PlotWeather.RESET); - } + }*/ MainUtil.sendMessage(player, Captions.FLAG_REMOVED); return true; } case "add": if (!Permissions.hasPermission(player, Captions.PERMISSION_FLAG_ADD)) { - MainUtil - .sendMessage(player, Captions.NO_PERMISSION, Captions.PERMISSION_FLAG_ADD); + MainUtil.sendMessage(player, Captions.NO_PERMISSION, Captions.PERMISSION_FLAG_ADD); return false; } if (args.length < 3) { - MainUtil.sendMessage(player, Captions.COMMAND_SYNTAX, - "/plot flag add "); + MainUtil.sendMessage(player, Captions.COMMAND_SYNTAX, "/plot flag add "); return false; } for (String entry : args[2].split(",")) { @@ -278,30 +263,14 @@ public class FlagCmd extends SubCommand { } } String value = StringMan.join(Arrays.copyOfRange(args, 2, args.length), " "); - Object parsed = flag.parseValue(value); - if (parsed == null) { - MainUtil.sendMessage(player, "&c" + flag.getValueDescription()); + PlotFlag parsed; + try { + parsed = flag.parse(value); + } catch (FlagParseException e) { + MainUtil.sendMessage(player, "&c" + flag.getFlagDescription().getTranslated()); return false; } - Object val = parsed; - if (flag instanceof ListFlag) { - final Collection parsedCollection = (Collection) parsed; - if (parsedCollection.isEmpty()) { - return !MainUtil.sendMessage(player, Captions.FLAG_NOT_ADDED); - } - final ListFlag listFlag = (ListFlag) flag; - final Optional collectionOptional = plot.getFlag(listFlag); - if (collectionOptional.isPresent()) { - final Collection flagCollection = collectionOptional.get(); - if (flagCollection.addAll(parsedCollection)) { - MainUtil.sendMessage(player, Captions.FLAG_ADDED); - val = flagCollection; - } else { - return !MainUtil.sendMessage(player, Captions.FLAG_NOT_ADDED); - } - } - } - boolean result = plot.setFlag(flag, val); + boolean result = plot.setFlag(flag.merge(parsed.getValue())); if (!result) { MainUtil.sendMessage(player, Captions.FLAG_NOT_ADDED); return false; @@ -310,8 +279,7 @@ public class FlagCmd extends SubCommand { return true; case "list": if (!Permissions.hasPermission(player, Captions.PERMISSION_FLAG_LIST)) { - MainUtil - .sendMessage(player, Captions.NO_PERMISSION, Captions.PERMISSION_FLAG_LIST); + MainUtil.sendMessage(player, Captions.NO_PERMISSION, Captions.PERMISSION_FLAG_LIST); return false; } if (args.length > 1) { @@ -319,31 +287,43 @@ public class FlagCmd extends SubCommand { return false; } final Map> flags = new HashMap<>(); - for (Flag flag1 : Flags.getFlags()) { - final String category = flag1.getCategoryCaption(); - final Collection flagList = - flags.computeIfAbsent(category, k -> new ArrayList<>()); - flagList.add(flag1.getName()); + for (PlotFlag plotFlag : GlobalFlagContainer.getInstance().getRecognizedPlotFlags()) { + final String category = plotFlag.getFlagCategory().getTranslated(); + final Collection flagList = flags.computeIfAbsent(category, k -> new ArrayList<>()); + flagList.add(plotFlag.getName()); } - final StringBuilder message = new StringBuilder(); - final Iterator>> iterator = - flags.entrySet().iterator(); + final Iterator>> iterator = flags.entrySet().iterator(); + final StringJoiner message = new StringJoiner("\n"); while (iterator.hasNext()) { final Map.Entry> flagsEntry = iterator.next(); final List flagNames = flagsEntry.getValue(); Collections.sort(flagNames); - message.append(String.format(Captions.FLAG_LIST_ENTRY.formatted(), - flagsEntry.getKey(), StringMan.join(flagNames, ", "))); - if (iterator.hasNext()) { - message.append("\n"); - } + message.add(String.format(Captions.FLAG_LIST_ENTRY.formatted(), flagsEntry.getKey(), + StringMan.join(flagNames, ", "))); } MainUtil.sendMessage(player, message.toString()); return true; } - MainUtil - .sendMessage(player, Captions.COMMAND_SYNTAX, getUsage()); + MainUtil.sendMessage(player, Captions.COMMAND_SYNTAX, getUsage()); return false; } + + private boolean info(PlotPlayer player, String[] args, PlotFlag flag) { + if (!Permissions.hasPermission(player, Captions.PERMISSION_SET_FLAG)) { + MainUtil.sendMessage(player, Captions.NO_PERMISSION, "plots.flag.info"); + return false; + } + if (args.length != 2) { + MainUtil.sendMessage(player, Captions.COMMAND_SYNTAX, "/plot flag info "); + return false; + } + // flag key + MainUtil.sendMessage(player, Captions.FLAG_KEY, flag.getName()); + // flag type + MainUtil.sendMessage(player, Captions.FLAG_TYPE, flag.getClass().getSimpleName()); + // Flag type description + MainUtil.sendMessage(player, Captions.FLAG_DESC, flag.getFlagDescription().getTranslated()); + return true; + } } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/Plot.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/Plot.java index 59f4979cb..e6018aa39 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/Plot.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/Plot.java @@ -3157,4 +3157,8 @@ public class Plot { return this.flagContainer.getFlag(flagClass).getValue(); } + public boolean setFlag(PlotFlag plotFlag) { + flagContainer.addFlag(plotFlag); + return true; + } }