mirror of
https://github.com/IntellectualSites/PlotSquared.git
synced 2025-06-27 03:04:43 +02:00
Move all files! Hahah
This commit is contained in:
@ -0,0 +1,112 @@
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// 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.flag;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
/**
|
||||
* Created 2014-09-23 for PlotSquared
|
||||
*
|
||||
* @author Citymonstret
|
||||
* @author Empire92
|
||||
*/
|
||||
public class AbstractFlag {
|
||||
public final String key;
|
||||
public final FlagValue<?> value;
|
||||
|
||||
public AbstractFlag(final String key) {
|
||||
this(key, new FlagValue.StringValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* AbstractFlag is a parameter used in creating a new Flag<br>
|
||||
* The key must be alphabetical characters and <= 16 characters in length
|
||||
* @param key
|
||||
*/
|
||||
public AbstractFlag(final String key, final FlagValue<?> value) {
|
||||
if (!StringUtils.isAlpha(key.replaceAll("_", "").replaceAll("-", ""))) {
|
||||
throw new IllegalArgumentException("Flag must be alphabetic characters");
|
||||
}
|
||||
if (key.length() > 16) {
|
||||
throw new IllegalArgumentException("Key must be <= 16 characters");
|
||||
}
|
||||
this.key = key.toLowerCase();
|
||||
if (value == null) {
|
||||
this.value = new FlagValue.StringValue();
|
||||
} else {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isList() {
|
||||
return this.value instanceof FlagValue.ListValue;
|
||||
}
|
||||
|
||||
public Object parseValueRaw(final String value) {
|
||||
try {
|
||||
return this.value.parse(value);
|
||||
} catch (final Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public String toString(final Object t) {
|
||||
return this.value.toString(t);
|
||||
}
|
||||
|
||||
public String getValueDesc() {
|
||||
return this.value.getDescription();
|
||||
}
|
||||
|
||||
/**
|
||||
* AbstractFlag key
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
public String getKey() {
|
||||
return this.key;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.key;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return this.key.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object other) {
|
||||
if (other == null) {
|
||||
return false;
|
||||
}
|
||||
if (other == this) {
|
||||
return true;
|
||||
}
|
||||
if (!(other instanceof AbstractFlag)) {
|
||||
return false;
|
||||
}
|
||||
final AbstractFlag otherObj = (AbstractFlag) other;
|
||||
return (otherObj.key.equals(this.key));
|
||||
}
|
||||
}
|
125
src/main/java/com/intellectualcrafters/plot/flag/Flag.java
Normal file
125
src/main/java/com/intellectualcrafters/plot/flag/Flag.java
Normal file
@ -0,0 +1,125 @@
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// 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.flag;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
public class Flag {
|
||||
private AbstractFlag key;
|
||||
private Object value;
|
||||
|
||||
/**
|
||||
* Flag object used to store basic information for a Plot. Flags are a key/value pair. For a flag to be usable by a
|
||||
* player, you need to register it with PlotSquared.
|
||||
*
|
||||
* @param key AbstractFlag
|
||||
* @param value Value must be alphanumerical (can have spaces) and be <= 48 characters
|
||||
*
|
||||
* @throws IllegalArgumentException if you provide inadequate inputs
|
||||
*/
|
||||
public Flag(final AbstractFlag key, final String value) {
|
||||
if (!StringUtils.isAsciiPrintable(value)) {
|
||||
throw new IllegalArgumentException("Flag must be ascii");
|
||||
}
|
||||
if (value.length() > 128) {
|
||||
throw new IllegalArgumentException("Value must be <= 128 characters");
|
||||
}
|
||||
this.key = key;
|
||||
this.value = key.parseValueRaw(value);
|
||||
if (this.value == null) {
|
||||
throw new IllegalArgumentException(key.getValueDesc());
|
||||
}
|
||||
}
|
||||
|
||||
public void setKey(final AbstractFlag key) {
|
||||
this.key = key;
|
||||
if (this.value instanceof String) {
|
||||
this.value = key.parseValueRaw((String) this.value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Warning: Unchecked
|
||||
*/
|
||||
public Flag(final AbstractFlag key, final Object value) {
|
||||
this.key = key;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the AbstractFlag used in creating the flag
|
||||
*
|
||||
* @return AbstractFlag
|
||||
*/
|
||||
public AbstractFlag getAbstractFlag() {
|
||||
return this.key;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the key for the AbstractFlag
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
public String getKey() {
|
||||
return this.key.getKey();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
public Object getValue() {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
public String getValueString() {
|
||||
return this.key.toString(this.value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
if (this.value.equals("")) {
|
||||
return this.key.getKey();
|
||||
}
|
||||
return this.key + ":" + getValueString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
if (getClass() != obj.getClass()) {
|
||||
return false;
|
||||
}
|
||||
final Flag other = (Flag) obj;
|
||||
return (this.key.getKey().equals(other.key.getKey()) && this.value.equals(other.value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return this.key.getKey().hashCode();
|
||||
}
|
||||
}
|
@ -0,0 +1,426 @@
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// 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.flag;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import com.intellectualcrafters.plot.PS;
|
||||
import com.intellectualcrafters.plot.config.C;
|
||||
import com.intellectualcrafters.plot.database.DBFunc;
|
||||
import com.intellectualcrafters.plot.object.Plot;
|
||||
import com.intellectualcrafters.plot.object.PlotCluster;
|
||||
import com.intellectualcrafters.plot.object.PlotPlayer;
|
||||
import com.intellectualcrafters.plot.object.PlotSettings;
|
||||
import com.intellectualcrafters.plot.object.PlotWorld;
|
||||
import com.intellectualcrafters.plot.util.EventUtil;
|
||||
|
||||
/**
|
||||
* Flag Manager Utility
|
||||
*
|
||||
* @author Citymonstret
|
||||
* @author Empire92
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public class FlagManager {
|
||||
// TODO add some flags
|
||||
// - Plot clear interval
|
||||
// - Mob cap
|
||||
// - customized plot composition
|
||||
|
||||
private final static HashSet<String> reserved = new HashSet<>();
|
||||
|
||||
private final static HashSet<AbstractFlag> flags = new HashSet<>();
|
||||
|
||||
/**
|
||||
* Reserve a flag so that it cannot be set by players
|
||||
* @param flag
|
||||
*/
|
||||
public static void reserveFlag(String flag) {
|
||||
reserved.add(flag);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get if a flag is reserved
|
||||
* @param flag
|
||||
* @return
|
||||
*/
|
||||
public static boolean isReserved(String flag) {
|
||||
return reserved.contains(flag);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the reserved flags
|
||||
* @return
|
||||
*/
|
||||
public static HashSet<String> getReservedFlags() {
|
||||
return (HashSet<String>) reserved.clone();
|
||||
}
|
||||
|
||||
/**
|
||||
* Unreserve a flag
|
||||
* @param flag
|
||||
*/
|
||||
public static void unreserveFlag(String flag) {
|
||||
reserved.remove(flag);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register an AbstractFlag with PlotSquared
|
||||
*
|
||||
* @param af Flag to register
|
||||
*
|
||||
* @return boolean success
|
||||
*/
|
||||
public static boolean addFlag(AbstractFlag af) {
|
||||
return addFlag(af, false);
|
||||
}
|
||||
|
||||
public static boolean addFlag(AbstractFlag af, boolean reserved) {
|
||||
PS.log(C.PREFIX.s() + "&8 - Adding flag: &7" + af);
|
||||
for (PlotWorld plotworld : PS.get().getPlotWorldObjects()) {
|
||||
Flag flag = ((HashMap<String, Flag>) plotworld.DEFAULT_FLAGS.clone()).get(af.getKey());
|
||||
if (flag != null) {
|
||||
flag.setKey(af);
|
||||
}
|
||||
}
|
||||
if (PS.get().getAllPlotsRaw() != null) {
|
||||
for (final Plot plot : PS.get().getPlotsRaw()) {
|
||||
Flag flag = plot.settings.flags.get(af.getKey());
|
||||
if (flag != null) {
|
||||
flag.setKey(af);
|
||||
}
|
||||
}
|
||||
}
|
||||
if ((getFlag(af.getKey()) == null) && flags.add(af)) {
|
||||
if (reserved) reserveFlag(af.getKey());
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static Flag getSettingFlag(final String world, final PlotSettings settings, final String id) {
|
||||
Flag flag = settings.flags.get(id);
|
||||
if (flag == null) {
|
||||
PlotWorld plotworld = PS.get().getPlotWorld(world);
|
||||
if (plotworld == null) {
|
||||
return null;
|
||||
}
|
||||
return ((HashMap<String, Flag>) plotworld.DEFAULT_FLAGS.clone()).get(id);
|
||||
}
|
||||
return flag;
|
||||
}
|
||||
|
||||
public static boolean isBooleanFlag(final Plot plot, final String key, final boolean defaultValue) {
|
||||
final Flag flag = FlagManager.getPlotFlag(plot, key);
|
||||
if (flag == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
final Object value = flag.getValue();
|
||||
if (value instanceof Boolean) {
|
||||
return (boolean) value;
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of a flag for a plot (respects flag defaults)
|
||||
* @param plot
|
||||
* @param flag
|
||||
* @return Flag
|
||||
*/
|
||||
public static Flag getPlotFlag(final Plot plot, final String flag) {
|
||||
return getSettingFlag(plot.world, plot.settings, flag);
|
||||
}
|
||||
|
||||
public static boolean isPlotFlagTrue(final Plot plot, final String strFlag) {
|
||||
if (plot.owner == null) {
|
||||
return false;
|
||||
}
|
||||
final Flag flag = getPlotFlag(plot, strFlag);
|
||||
if (flag == null) {
|
||||
return false;
|
||||
}
|
||||
if (flag.getValue() instanceof Boolean) {
|
||||
return (boolean) flag.getValue();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean isPlotFlagFalse(final Plot plot, final String strFlag) {
|
||||
if (plot.owner == null) {
|
||||
return false;
|
||||
}
|
||||
final Flag flag = getPlotFlag(plot, strFlag);
|
||||
if (flag == null) {
|
||||
return false;
|
||||
}
|
||||
if (flag.getValue() instanceof Boolean) {
|
||||
return !(boolean) flag.getValue();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of a flag for a plot (ignores flag defaults)
|
||||
* @param plot
|
||||
* @param flag
|
||||
* @return Flag
|
||||
*/
|
||||
public static Flag getPlotFlagAbs(final Plot plot, final String flag) {
|
||||
return getSettingFlagAbs(plot.settings, flag);
|
||||
}
|
||||
|
||||
public static Flag getSettingFlagAbs(final PlotSettings settings, final String flag) {
|
||||
if ((settings.flags == null) || (settings.flags.size() == 0)) {
|
||||
return null;
|
||||
}
|
||||
return settings.flags.get(flag);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a flag to a plot
|
||||
* @param plot
|
||||
* @param flag
|
||||
*/
|
||||
public static boolean addPlotFlag(final Plot plot, final Flag flag) {
|
||||
final boolean result = EventUtil.manager.callFlagAdd(flag, plot);
|
||||
if (!result) {
|
||||
return false;
|
||||
}
|
||||
plot.settings.flags.put(flag.getKey(), flag);
|
||||
DBFunc.setFlags(plot, plot.settings.flags.values());
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean addPlotFlagAbs(final Plot plot, final Flag flag) {
|
||||
final boolean result = EventUtil.manager.callFlagAdd(flag, plot);
|
||||
if (!result) {
|
||||
return false;
|
||||
}
|
||||
plot.settings.flags.put(flag.getKey(), flag);
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean addClusterFlag(final PlotCluster cluster, final Flag flag) {
|
||||
//TODO plot cluster flag event
|
||||
final Flag hasFlag = getSettingFlag(cluster.world, cluster.settings, flag.getKey());
|
||||
cluster.settings.flags.put(flag.getKey(), flag);
|
||||
DBFunc.setFlags(cluster, cluster.settings.flags.values());
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param plot
|
||||
* @return set of flags
|
||||
*/
|
||||
public static HashMap<String, Flag> getPlotFlags(final Plot plot) {
|
||||
return getSettingFlags(plot.world, plot.settings);
|
||||
}
|
||||
|
||||
public static HashMap<String, Flag> getSettingFlags(final String world, final PlotSettings settings) {
|
||||
PlotWorld plotworld = PS.get().getPlotWorld(world);
|
||||
if (plotworld == null || plotworld.DEFAULT_FLAGS.size() == 0) {
|
||||
return settings.flags;
|
||||
}
|
||||
else {
|
||||
HashMap<String, Flag> map = (HashMap<String, Flag>) plotworld.DEFAULT_FLAGS.clone();
|
||||
map.putAll(settings.flags);
|
||||
return map;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean removePlotFlag(final Plot plot, final String id) {
|
||||
Flag flag = plot.settings.flags.remove(id);
|
||||
if (flag == null) {
|
||||
return false;
|
||||
}
|
||||
final boolean result = EventUtil.manager.callFlagRemove(flag, plot);
|
||||
if (!result) {
|
||||
plot.settings.flags.put(id, flag);
|
||||
return false;
|
||||
}
|
||||
DBFunc.setFlags(plot, plot.settings.flags.values());
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean removeClusterFlag(final PlotCluster cluster, final String id) {
|
||||
Flag flag = cluster.settings.flags.remove(id);
|
||||
if (flag == null) {
|
||||
return false;
|
||||
}
|
||||
final boolean result = EventUtil.manager.callFlagRemove(flag, cluster);
|
||||
if (!result) {
|
||||
cluster.settings.flags.put(id, flag);
|
||||
return false;
|
||||
}
|
||||
DBFunc.setFlags(cluster, cluster.settings.flags.values());
|
||||
return true;
|
||||
}
|
||||
|
||||
public static void setPlotFlags(final Plot plot, final Set<Flag> flags) {
|
||||
if (flags != null && flags.size() != 0) {
|
||||
plot.settings.flags.clear();
|
||||
for (Flag flag : flags) {
|
||||
plot.settings.flags.put(flag.getKey(), flag);
|
||||
}
|
||||
}
|
||||
else if (plot.settings.flags.size() == 0) {
|
||||
return;
|
||||
}
|
||||
else {
|
||||
plot.settings.flags.clear();
|
||||
}
|
||||
DBFunc.setFlags(plot, plot.settings.flags.values());
|
||||
}
|
||||
|
||||
public static void setClusterFlags(final PlotCluster cluster, final Set<Flag> flags) {
|
||||
if (flags != null && flags.size() != 0) {
|
||||
cluster.settings.flags.clear();
|
||||
for (Flag flag : flags) {
|
||||
cluster.settings.flags.put(flag.getKey(), flag);
|
||||
}
|
||||
}
|
||||
else if (cluster.settings.flags.size() == 0) {
|
||||
return;
|
||||
}
|
||||
else {
|
||||
cluster.settings.flags.clear();
|
||||
}
|
||||
DBFunc.setFlags(cluster, cluster.settings.flags.values());
|
||||
}
|
||||
|
||||
public static Flag[] removeFlag(final Flag[] flags, final String r) {
|
||||
final Flag[] f = new Flag[flags.length - 1];
|
||||
int index = 0;
|
||||
for (final Flag flag : flags) {
|
||||
if (!flag.getKey().equals(r)) {
|
||||
f[index++] = flag;
|
||||
}
|
||||
}
|
||||
return f;
|
||||
}
|
||||
|
||||
public static Set<Flag> removeFlag(final Set<Flag> flags, final String r) {
|
||||
final HashSet<Flag> newflags = new HashSet<>();
|
||||
for (final Flag flag : flags) {
|
||||
if (!flag.getKey().equalsIgnoreCase(r)) {
|
||||
newflags.add(flag);
|
||||
}
|
||||
}
|
||||
return newflags;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of registered AbstractFlag objects
|
||||
*
|
||||
* @return List (AbstractFlag)
|
||||
*/
|
||||
public static HashSet<AbstractFlag> getFlags() {
|
||||
return flags;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of registerd AbstragFlag objects based on player permissions
|
||||
*
|
||||
* @param player with permissions
|
||||
*
|
||||
* @return List (AbstractFlag)
|
||||
*/
|
||||
public static List<AbstractFlag> getFlags(final PlotPlayer player) {
|
||||
final List<AbstractFlag> returnFlags = new ArrayList<>();
|
||||
for (final AbstractFlag flag : flags) {
|
||||
if (player.hasPermission("plots.set.flag." + flag.getKey().toLowerCase())) {
|
||||
returnFlags.add(flag);
|
||||
}
|
||||
}
|
||||
return returnFlags;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an AbstractFlag by a string Returns null if flag does not exist
|
||||
*
|
||||
* @param string Flag Key
|
||||
*
|
||||
* @return AbstractFlag
|
||||
*/
|
||||
public static AbstractFlag getFlag(final String string) {
|
||||
for (final AbstractFlag flag : flags) {
|
||||
if (flag.getKey().equalsIgnoreCase(string)) {
|
||||
return flag;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an AbstractFlag by a string
|
||||
*
|
||||
* @param string Flag Key
|
||||
* @param create If to create the flag if it does not exist
|
||||
*
|
||||
* @return AbstractFlag
|
||||
*/
|
||||
public static AbstractFlag getFlag(final String string, final boolean create) {
|
||||
if ((getFlag(string) == null) && create) {
|
||||
final AbstractFlag flag = new AbstractFlag(string);
|
||||
return flag;
|
||||
}
|
||||
return getFlag(string);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a registered AbstractFlag
|
||||
*
|
||||
* @param flag Flag Key
|
||||
*
|
||||
* @return boolean Result of operation
|
||||
*/
|
||||
public static boolean removeFlag(final AbstractFlag flag) {
|
||||
return flags.remove(flag);
|
||||
}
|
||||
|
||||
public static HashMap<String, Flag> parseFlags(final List<String> flagstrings) {
|
||||
HashMap<String, Flag> map = new HashMap<String, Flag>();
|
||||
for (String key : flagstrings) {
|
||||
final String[] split;
|
||||
if (key.contains(";")) {
|
||||
split = key.split(";");
|
||||
}
|
||||
else {
|
||||
split = key.split(":");
|
||||
}
|
||||
Flag flag;
|
||||
if (split.length == 1) {
|
||||
flag = new Flag(getFlag(split[0], true), "");
|
||||
} else {
|
||||
flag = new Flag(getFlag(split[0], true), split[1]);
|
||||
}
|
||||
map.put(flag.getKey(), flag);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
}
|
465
src/main/java/com/intellectualcrafters/plot/flag/FlagValue.java
Normal file
465
src/main/java/com/intellectualcrafters/plot/flag/FlagValue.java
Normal file
@ -0,0 +1,465 @@
|
||||
package com.intellectualcrafters.plot.flag;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
import com.intellectualcrafters.plot.object.PlotBlock;
|
||||
|
||||
/**
|
||||
* Created 2014-11-17 for PlotSquared
|
||||
*
|
||||
* @author Citymonstret
|
||||
*/
|
||||
public abstract class FlagValue<T> {
|
||||
private final Class<T> clazz;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public FlagValue() {
|
||||
this.clazz = (Class<T>) getClass();
|
||||
}
|
||||
|
||||
public FlagValue(final Class<T> clazz) {
|
||||
if (clazz == null) {
|
||||
throw new NullPointerException();
|
||||
}
|
||||
this.clazz = clazz;
|
||||
}
|
||||
|
||||
public boolean validValue(final Object value) {
|
||||
return (value != null) && (value.getClass() == this.clazz);
|
||||
}
|
||||
|
||||
public String toString(final Object t) {
|
||||
return t.toString();
|
||||
}
|
||||
|
||||
public abstract T getValue(Object t);
|
||||
|
||||
public abstract T parse(String t);
|
||||
|
||||
public abstract String getDescription();
|
||||
|
||||
public static class BooleanValue extends FlagValue<Boolean> {
|
||||
@Override
|
||||
public Boolean getValue(final Object t) {
|
||||
return (Boolean) t;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean parse(final String t) {
|
||||
switch (t.toLowerCase()) {
|
||||
case "1":
|
||||
case "yes":
|
||||
case "allow":
|
||||
case "true": {
|
||||
return true;
|
||||
}
|
||||
case "0":
|
||||
case "no":
|
||||
case "deny":
|
||||
case "false": {
|
||||
return false;
|
||||
}
|
||||
default: {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "Flag value must be a boolean (true|false)";
|
||||
}
|
||||
}
|
||||
|
||||
public static class IntegerValue extends FlagValue<Integer> {
|
||||
@Override
|
||||
public Integer getValue(final Object t) {
|
||||
return (Integer) t;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer parse(final String t) {
|
||||
try {
|
||||
return Integer.parseInt(t);
|
||||
} catch (final IllegalArgumentException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "Flag value must be a whole number";
|
||||
}
|
||||
}
|
||||
|
||||
public static class IntervalValue extends FlagValue<Integer[]> {
|
||||
@Override
|
||||
public String toString(final Object t) {
|
||||
final Integer[] value = ((Integer[]) t);
|
||||
return value[0] + " " + value[1];
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer[] getValue(final Object t) {
|
||||
return (Integer[]) t;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer[] parse(final String t) {
|
||||
int seconds;
|
||||
int amount;
|
||||
final String[] values = t.split(" ");
|
||||
if (values.length < 2) {
|
||||
seconds = 1;
|
||||
try {
|
||||
amount = Integer.parseInt(values[0]);
|
||||
} catch (final Exception e) {
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
amount = Integer.parseInt(values[0]);
|
||||
seconds = Integer.parseInt(values[1]);
|
||||
} catch (final Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return new Integer[] { amount, seconds };
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "Value(s) must be numeric. /plot set flag <flag> <interval> [amount]";
|
||||
}
|
||||
}
|
||||
|
||||
public static class UnsignedIntegerValue extends FlagValue<Integer> {
|
||||
@Override
|
||||
public Integer getValue(final Object t) {
|
||||
return (Integer) t;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer parse(final String t) {
|
||||
try {
|
||||
final int value = Integer.parseInt(t);
|
||||
if (value < 0) {
|
||||
return null;
|
||||
}
|
||||
return value;
|
||||
} catch (final IllegalArgumentException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "Flag value must be a positive whole number (includes 0)";
|
||||
}
|
||||
}
|
||||
|
||||
public static class DoubleValue extends FlagValue<Double> {
|
||||
@Override
|
||||
public Double getValue(final Object t) {
|
||||
return (Double) t;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double parse(final String t) {
|
||||
try {
|
||||
return Double.parseDouble(t);
|
||||
} catch (final IllegalArgumentException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "Flag value must be a number (negative decimals are allowed)";
|
||||
}
|
||||
}
|
||||
|
||||
public static class LongValue extends FlagValue<Long> {
|
||||
@Override
|
||||
public Long getValue(final Object t) {
|
||||
return (Long) t;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long parse(final String t) {
|
||||
try {
|
||||
return Long.parseLong(t);
|
||||
} catch (final IllegalArgumentException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "Flag value must be a whole number (large numbers allowed)";
|
||||
}
|
||||
}
|
||||
|
||||
public static class UnsignedLongValue extends FlagValue<Long> {
|
||||
@Override
|
||||
public Long getValue(final Object t) {
|
||||
return (Long) t;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long parse(final String t) {
|
||||
try {
|
||||
final long value = Long.parseLong(t);
|
||||
if (value < 0) {
|
||||
return null;
|
||||
}
|
||||
return value;
|
||||
} catch (final IllegalArgumentException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "Flag value must be a positive whole number (large numbers allowed)";
|
||||
}
|
||||
}
|
||||
|
||||
public static class UnsignedDoubleValue extends FlagValue<Double> {
|
||||
@Override
|
||||
public Double getValue(final Object t) {
|
||||
return (Double) t;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double parse(final String t) {
|
||||
try {
|
||||
final double value = Double.parseDouble(t);
|
||||
if (value < 0) {
|
||||
return null;
|
||||
}
|
||||
return value;
|
||||
} catch (final IllegalArgumentException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "Flag value must be a positive number (decimals allowed)";
|
||||
}
|
||||
}
|
||||
|
||||
public static class PlotBlockValue extends FlagValue<PlotBlock> {
|
||||
@Override
|
||||
public PlotBlock getValue(final Object t) {
|
||||
return (PlotBlock) t;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PlotBlock parse(final String t) {
|
||||
try {
|
||||
final String[] split = t.split(":");
|
||||
byte data;
|
||||
if (split.length == 2) {
|
||||
if ("*".equals(split[1])) {
|
||||
data = -1;
|
||||
} else {
|
||||
data = Byte.parseByte(split[1]);
|
||||
}
|
||||
} else {
|
||||
data = -1;
|
||||
}
|
||||
final short id = Short.parseShort(split[0]);
|
||||
return new PlotBlock(id, data);
|
||||
} catch (final Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "Flag value must be a number (negative decimals are allowed)";
|
||||
}
|
||||
}
|
||||
|
||||
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(final Object t) {
|
||||
return StringUtils.join((HashSet<PlotBlock>) t, ",");
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public HashSet<PlotBlock> getValue(final Object t) {
|
||||
return (HashSet<PlotBlock>) t;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HashSet<PlotBlock> parse(final String t) {
|
||||
final HashSet<PlotBlock> list = new HashSet<PlotBlock>();
|
||||
for (final String item : t.split(",")) {
|
||||
final String[] split = item.split(":");
|
||||
byte data;
|
||||
if (split.length == 2) {
|
||||
if ("*".equals(split[1])) {
|
||||
data = -1;
|
||||
} else {
|
||||
data = Byte.parseByte(split[1]);
|
||||
}
|
||||
} else {
|
||||
data = -1;
|
||||
}
|
||||
final short id = Short.parseShort(split[0]);
|
||||
final PlotBlock block = new PlotBlock(id, data);
|
||||
list.add(block);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "Flag value must be a block list";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(final Object t, final String value) {
|
||||
try {
|
||||
((HashSet<PlotBlock>) t).addAll(parse(value));
|
||||
} catch (final Exception e) {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(final Object t, final String value) {
|
||||
try {
|
||||
for (final PlotBlock item : parse(value)) {
|
||||
((HashSet<PlotBlock>) t).remove(item);
|
||||
}
|
||||
} catch (final Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class IntegerListValue extends FlagValue<List<Integer>> implements ListValue {
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public String toString(final Object t) {
|
||||
return StringUtils.join((List<Integer>) t, ",");
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public List<Integer> getValue(final Object t) {
|
||||
return (List<Integer>) t;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Integer> parse(final String t) {
|
||||
String[] split = (t.split(","));
|
||||
ArrayList<Integer> numbers = new ArrayList<Integer>();
|
||||
for (String element : split) {
|
||||
numbers.add(Integer.parseInt(element));
|
||||
}
|
||||
return numbers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "Flag value must be a integer list";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(final Object t, final String value) {
|
||||
try {
|
||||
((List<Integer>) t).addAll(parse(value));
|
||||
} catch (final Exception e) {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(final Object t, final String value) {
|
||||
try {
|
||||
for (final Integer item : parse(value)) {
|
||||
((List<Integer>) t).remove(item);
|
||||
}
|
||||
} catch (final Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class DoubleListValue extends FlagValue<List<Double>> implements ListValue {
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public String toString(final Object t) {
|
||||
return StringUtils.join((List<Double>) t, ",");
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public List<Double> getValue(final Object t) {
|
||||
return (List<Double>) t;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Double> parse(final String t) {
|
||||
String[] split = (t.split(","));
|
||||
ArrayList<Double> numbers = new ArrayList<Double>();
|
||||
for (String element : split) {
|
||||
numbers.add(Double.parseDouble(element));
|
||||
}
|
||||
return numbers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "Flag value must be a integer list";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(final Object t, final String value) {
|
||||
try {
|
||||
((List<Double>) t).addAll(parse(value));
|
||||
} catch (final Exception e) {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(final Object t, final String value) {
|
||||
try {
|
||||
for (final Double item : parse(value)) {
|
||||
((List<Double>) t).remove(item);
|
||||
}
|
||||
} catch (final Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class StringValue extends FlagValue<String> {
|
||||
@Override
|
||||
public String parse(final String s) {
|
||||
return s;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "Flag value must be alphanumeric. Some special characters are allowed.";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getValue(final Object t) {
|
||||
return t.toString();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user