Adding more flag functionality

This commit is contained in:
boy0001 2015-01-29 20:59:32 +11:00
parent 7143da0ebf
commit 84ab0d9bb6
4 changed files with 237 additions and 4 deletions

View File

@ -36,6 +36,7 @@ public enum Command {
// (Rating system) (ratings can be stored as the average, and number of
// ratings)
// - /plot rate <number out of 10>
FLAG("flag", "f"),
CLUSTER("cluster", "clusters"),
BUY("buy","b"),
CREATEROADSCHEMATIC("createroadschematic"),

View File

@ -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 <set|remove|add|list|info>");
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 <flag> <value>");
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 <flag> [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 <flag> <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;
}
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<String, ArrayList<String>> 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<String>());
}
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 <set|remove|add|list|info>");
return false;
}
}

View File

@ -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());
@ -57,6 +57,10 @@ public class AbstractFlag {
}
}
public boolean isList() {
return this.value instanceof FlagValue.ListValue;
}
public Object parseValueRaw(final String value) {
return this.value.parse(value);
}

View File

@ -288,8 +288,14 @@ public abstract class FlagValue<T> {
}
}
public static class PlotBlockListValue extends FlagValue<HashSet<PlotBlock>> {
public interface ListValue {
public void add(Object t, String value);
public void remove(Object t, String value);
}
public static class PlotBlockListValue extends FlagValue<HashSet<PlotBlock>> implements ListValue {
@SuppressWarnings("unchecked")
@Override
public String toString(Object t) {
return StringUtils.join((HashSet<PlotBlock>) t, ",");
@ -324,6 +330,18 @@ public abstract class FlagValue<T> {
public String getDescription() {
return "Flag value must be a block list";
}
@Override
public void add(Object t, String value) {
((HashSet<PlotBlock>)t).addAll(parse(value));
}
@Override
public void remove(Object t, String value) {
for (PlotBlock item : parse(value)) {
((HashSet<PlotBlock>)t).remove(item);
}
}
}
public static class StringValue extends FlagValue<String> {