From 84ab0d9bb60f24bb4deb87a63112ecd4a2c7497b Mon Sep 17 00:00:00 2001 From: boy0001 Date: Thu, 29 Jan 2015 20:59:32 +1100 Subject: [PATCH] Adding more flag functionality --- .../plot/commands/Command.java | 1 + .../plot/commands/FlagCmd.java | 210 ++++++++++++++++++ .../plot/flag/AbstractFlag.java | 8 +- .../plot/flag/FlagValue.java | 22 +- 4 files changed, 237 insertions(+), 4 deletions(-) create mode 100644 PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/FlagCmd.java diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Command.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Command.java index 0e0aa9501..2e008f74f 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Command.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Command.java @@ -36,6 +36,7 @@ public enum Command { // (Rating system) (ratings can be stored as the average, and number of // ratings) // - /plot rate + FLAG("flag", "f"), CLUSTER("cluster", "clusters"), BUY("buy","b"), CREATEROADSCHEMATIC("createroadschematic"), diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/FlagCmd.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/FlagCmd.java new file mode 100644 index 000000000..b76ede213 --- /dev/null +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/FlagCmd.java @@ -0,0 +1,210 @@ +//////////////////////////////////////////////////////////////////////////////////////////////////// +// PlotSquared - A plot manager and world generator for the Bukkit API / +// Copyright (c) 2014 IntellectualSites/IntellectualCrafters / +// / +// This program is free software; you can redistribute it and/or modify / +// it under the terms of the GNU General Public License as published by / +// the Free Software Foundation; either version 3 of the License, or / +// (at your option) any later version. / +// / +// This program is distributed in the hope that it will be useful, / +// but WITHOUT ANY WARRANTY; without even the implied warranty of / +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the / +// GNU General Public License for more details. / +// / +// You should have received a copy of the GNU General Public License / +// along with this program; if not, write to the Free Software Foundation, / +// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA / +// / +// You can contact us via: support@intellectualsites.com / +//////////////////////////////////////////////////////////////////////////////////////////////////// + +package com.intellectualcrafters.plot.commands; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; + +import org.apache.commons.lang.StringUtils; +import org.bukkit.entity.Player; + +import com.intellectualcrafters.plot.PlotMain; +import com.intellectualcrafters.plot.config.C; +import com.intellectualcrafters.plot.flag.AbstractFlag; +import com.intellectualcrafters.plot.flag.Flag; +import com.intellectualcrafters.plot.flag.FlagManager; +import com.intellectualcrafters.plot.flag.FlagValue; +import com.intellectualcrafters.plot.listeners.PlotListener; +import com.intellectualcrafters.plot.object.Plot; +import com.intellectualcrafters.plot.util.PlayerFunctions; + +public class FlagCmd extends SubCommand { + + public FlagCmd() { + super(Command.FLAG, "Manage plot flags", "f", CommandCategory.ACTIONS, true); + } + + @Override + public boolean execute(final Player player, final String... args) { + /* + * plot flag set fly true + * plot flag remove fly + * plot flag remove use 1,3 + * plot flag add use 2,4 + * plot flag list + */ + if (args.length == 0) { + PlayerFunctions.sendMessage(player, C.COMMAND_SYNTAX, "/plot flag "); + return false; + } + Plot plot = PlayerFunctions.getCurrentPlot(player); + if (plot == null) { + PlayerFunctions.sendMessage(player, C.NOT_IN_PLOT); + return false; + } + if (!plot.hasOwner()) { + sendMessage(player, C.PLOT_NOT_CLAIMED); + return false; + } + if (!plot.hasRights(player) && !PlotMain.hasPermission(player, "plots.set.flag.other")) { + PlayerFunctions.sendMessage(player, C.NO_PERMISSION, "plots.set.flag.other"); + return false; + } + switch (args[0].toLowerCase()) { + case "set": { + if (!PlotMain.hasPermission(player, "plots.set.flag")) { + PlayerFunctions.sendMessage(player, C.NO_PERMISSION, "plots.set.flag"); + return false; + } + if (args.length != 3) { + PlayerFunctions.sendMessage(player, C.COMMAND_SYNTAX, "/plot flag set "); + return false; + } + AbstractFlag af = FlagManager.getFlag(args[1].toLowerCase()); + if (af == null) { + PlayerFunctions.sendMessage(player, C.NOT_VALID_FLAG); + return false; + } + if (!PlotMain.hasPermission(player, "plots.set.flag." + args[1].toLowerCase())) { + PlayerFunctions.sendMessage(player, C.NO_PERMISSION, "plots.set.flag." + args[1].toLowerCase()); + return false; + } + String value = StringUtils.join(Arrays.copyOfRange(args, 2, args.length), " "); + Object parsed = af.parseValueRaw(value); + if (parsed == null) { + PlayerFunctions.sendMessage(player, "&c" + af.getValueDesc()); + return false; + } + final Flag flag = new Flag(FlagManager.getFlag(args[1].toLowerCase(), true), parsed); + boolean result = FlagManager.addPlotFlag(plot, flag); + if (!result) { + PlayerFunctions.sendMessage(player, C.FLAG_NOT_ADDED); + return false; + } + PlayerFunctions.sendMessage(player, C.FLAG_ADDED); + PlotListener.plotEntry(player, plot); + return true; + } + case "remove": { + if (!PlotMain.hasPermission(player, "plots.flag.remove")) { + PlayerFunctions.sendMessage(player, C.NO_PERMISSION, "plots.flag.remove"); + return false; + } + if (args.length != 3) { + PlayerFunctions.sendMessage(player, C.COMMAND_SYNTAX, "/plot flag remove [values]"); + return false; + } + AbstractFlag af = FlagManager.getFlag(args[1].toLowerCase()); + if (af == null) { + PlayerFunctions.sendMessage(player, C.NOT_VALID_FLAG); + return false; + } + if (!PlotMain.hasPermission(player, "plots.set.flag." + args[1].toLowerCase())) { + PlayerFunctions.sendMessage(player, C.NO_PERMISSION, "plots.set.flag." + args[1].toLowerCase()); + return false; + } + Flag flag = FlagManager.getPlotFlagAbs(plot, args[1].toLowerCase()); + if (flag == null) { + PlayerFunctions.sendMessage(player, C.FLAG_NOT_IN_PLOT); + return false; + } + boolean result = FlagManager.removePlotFlag(plot, flag.getKey()); + if (!result) { + PlayerFunctions.sendMessage(player, C.FLAG_NOT_REMOVED); + return false; + } + PlayerFunctions.sendMessage(player, C.FLAG_REMOVED); + PlotListener.plotEntry(player, plot); + return true; + } + case "add": { + if (!PlotMain.hasPermission(player, "plots.flag.add")) { + PlayerFunctions.sendMessage(player, C.NO_PERMISSION, "plots.flag.add"); + return false; + } + if (args.length != 3) { + PlayerFunctions.sendMessage(player, C.COMMAND_SYNTAX, "/plot flag add "); + return false; + } + AbstractFlag af = FlagManager.getFlag(args[1].toLowerCase()); + if (af == null) { + PlayerFunctions.sendMessage(player, C.NOT_VALID_FLAG); + return false; + } + if (!PlotMain.hasPermission(player, "plots.set.flag." + args[1].toLowerCase())) { + PlayerFunctions.sendMessage(player, C.NO_PERMISSION, "plots.set.flag." + args[1].toLowerCase()); + return false; + } + String value = StringUtils.join(Arrays.copyOfRange(args, 2, args.length), " "); + Object parsed = af.parseValueRaw(value); + if (parsed == null) { + PlayerFunctions.sendMessage(player, "&c" + af.getValueDesc()); + return false; + } + Flag flag = FlagManager.getPlotFlag(plot, args[1].toLowerCase()); + if (flag == null || !flag.getAbstractFlag().isList()) { + flag = new Flag(FlagManager.getFlag(args[1].toLowerCase(), true), parsed); + } + else { + ((FlagValue.ListValue) flag.getValue()).add(flag.getValue(), value); + } + boolean result = FlagManager.addPlotFlag(plot, flag); + if (!result) { + PlayerFunctions.sendMessage(player, C.FLAG_NOT_ADDED); + return false; + } + PlayerFunctions.sendMessage(player, C.FLAG_ADDED); + PlotListener.plotEntry(player, plot); + return true; + } + case "list": { + if (!PlotMain.hasPermission(player, "plots.flag.list")) { + PlayerFunctions.sendMessage(player, C.NO_PERMISSION, "plots.flag.list"); + return false; + } + if (args.length != 3) { + PlayerFunctions.sendMessage(player, C.COMMAND_SYNTAX, "/plot flag list"); + return false; + } + HashMap> flags = new HashMap<>(); + for (AbstractFlag af : FlagManager.getFlags()) { + String type = af.value.getClass().toString().replaceAll("Value", ""); + if (!flags.containsKey(type)) { + flags.put(type, new ArrayList()); + } + flags.get(type).add(af.getKey()); + } + String message = ""; + String prefix = ""; + for (String flag : flags.keySet()) { + message += prefix + "&6" + flag +": &7" + StringUtils.join(flags.get(flag), ", "); + prefix = "\n"; + } + PlayerFunctions.sendMessage(player, message); + return true; + } + } + PlayerFunctions.sendMessage(player, C.COMMAND_SYNTAX, "/plot flag "); + return false; + } +} diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/flag/AbstractFlag.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/flag/AbstractFlag.java index fc1608f98..93f44ee08 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/flag/AbstractFlag.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/flag/AbstractFlag.java @@ -31,8 +31,8 @@ import org.apache.commons.lang.StringUtils; */ public class AbstractFlag { - private final String key; - private final FlagValue value; + public final String key; + public final FlagValue value; public AbstractFlag(final String key) { this(key, new FlagValue.StringValue()); @@ -56,6 +56,10 @@ public class AbstractFlag { this.value = value; } } + + public boolean isList() { + return this.value instanceof FlagValue.ListValue; + } public Object parseValueRaw(final String value) { return this.value.parse(value); diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/flag/FlagValue.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/flag/FlagValue.java index 5a9a7c009..614390820 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/flag/FlagValue.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/flag/FlagValue.java @@ -288,9 +288,15 @@ public abstract class FlagValue { } } - public static class PlotBlockListValue extends FlagValue> { + public interface ListValue { + public void add(Object t, String value); + public void remove(Object t, String value); + } + + public static class PlotBlockListValue extends FlagValue> implements ListValue { - @Override + @SuppressWarnings("unchecked") + @Override public String toString(Object t) { return StringUtils.join((HashSet) t, ","); } @@ -324,6 +330,18 @@ public abstract class FlagValue { public String getDescription() { return "Flag value must be a block list"; } + + @Override + public void add(Object t, String value) { + ((HashSet)t).addAll(parse(value)); + } + + @Override + public void remove(Object t, String value) { + for (PlotBlock item : parse(value)) { + ((HashSet)t).remove(item); + } + } } public static class StringValue extends FlagValue {