424 lines
14 KiB
Java
Raw Normal View History

2014-11-08 20:27:09 +01:00
////////////////////////////////////////////////////////////////////////////////////////////////////
// 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 /
////////////////////////////////////////////////////////////////////////////////////////////////////
2014-11-16 10:48:18 +01:00
package com.intellectualcrafters.plot.flag;
2014-09-23 19:02:17 +02:00
2015-07-06 01:44:10 +10:00
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;
2015-01-29 18:45:14 +11:00
import com.intellectualcrafters.plot.config.C;
2015-01-09 01:08:50 +11:00
import com.intellectualcrafters.plot.database.DBFunc;
2015-07-06 01:44:10 +10:00
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;
2015-02-23 16:29:45 +11:00
import com.intellectualcrafters.plot.util.EventUtil;
2014-09-23 19:02:17 +02:00
2014-11-16 09:56:42 +01:00
/**
* Flag Manager Utility
*
* @author Citymonstret
* @author Empire92
*/
2015-02-20 17:34:19 +11:00
@SuppressWarnings("unused")
public class FlagManager {
2014-11-05 14:42:08 +11:00
// TODO add some flags
// - Plot clear interval
// - Mob cap
// - customized plot composition
2015-06-25 05:06:47 +10:00
private final static HashSet<String> reserved = new HashSet<>();
private final static HashSet<AbstractFlag> flags = new HashSet<>();
2015-02-23 12:32:27 +11:00
2015-06-25 05:06:47 +10:00
/**
* 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);
}
2014-11-05 14:42:08 +11:00
/**
* Register an AbstractFlag with PlotSquared
*
2015-03-13 18:02:48 +11:00
* @param af Flag to register
2014-12-17 20:15:11 -06:00
*
2015-03-13 18:02:48 +11:00
* @return boolean success
2014-11-05 14:42:08 +11:00
*/
public static boolean addFlag(AbstractFlag af) {
2015-06-25 05:06:47 +10:00
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);
2015-01-29 18:45:14 +11:00
}
}
}
2015-06-25 05:06:47 +10:00
if ((getFlag(af.getKey()) == null) && flags.add(af)) {
if (reserved) reserveFlag(af.getKey());
return true;
}
return false;
2014-11-05 14:42:08 +11:00
}
2015-02-23 12:32:27 +11:00
public static Flag getSettingFlag(final String world, final PlotSettings settings, final String id) {
2015-07-17 14:54:07 +10:00
Flag flag;
if (settings.flags.size() == 0 || (flag = settings.flags.get(id)) == null) {
PlotWorld plotworld = PS.get().getPlotWorld(world);
if (plotworld == null) {
return null;
2015-01-09 01:08:50 +11:00
}
2015-07-17 14:54:07 +10:00
if (plotworld.DEFAULT_FLAGS.size() == 0) {
return null;
}
return ((HashMap<String, Flag>) plotworld.DEFAULT_FLAGS.clone()).get(id);
2015-01-09 01:08:50 +11:00
}
return flag;
2015-01-09 01:08:50 +11:00
}
2015-04-26 16:29:58 +10:00
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;
}
2015-02-23 12:32:27 +11:00
2015-01-25 21:16:10 -07:00
/**
* Get the value of a flag for a plot (respects flag defaults)
* @param plot
* @param flag
2015-03-13 18:02:48 +11:00
* @return Flag
2015-01-25 21:16:10 -07:00
*/
2015-02-20 17:34:19 +11:00
public static Flag getPlotFlag(final Plot plot, final String flag) {
2015-01-25 21:16:10 -07:00
return getSettingFlag(plot.world, plot.settings, flag);
}
2015-02-23 12:32:27 +11:00
2015-02-20 17:34:19 +11:00
public static boolean isPlotFlagTrue(final Plot plot, final String strFlag) {
if (plot.owner == null) {
return false;
}
2015-02-20 17:34:19 +11:00
final Flag flag = getPlotFlag(plot, strFlag);
2015-07-17 14:54:07 +10:00
if (flag == null || !((Boolean) flag.getValue())) {
2015-02-20 17:34:19 +11:00
return false;
}
2015-07-17 14:54:07 +10:00
return true;
2015-01-22 16:26:16 -08:00
}
2015-04-15 17:42:28 +10:00
public static boolean isPlotFlagFalse(final Plot plot, final String strFlag) {
if (plot.owner == null) {
return false;
}
2015-04-15 17:42:28 +10:00
final Flag flag = getPlotFlag(plot, strFlag);
2015-07-17 14:54:07 +10:00
if (flag == null || ((Boolean) flag.getValue())) {
2015-04-15 17:42:28 +10:00
return false;
}
return false;
}
2015-02-23 12:32:27 +11:00
2015-01-09 01:08:50 +11:00
/**
* Get the value of a flag for a plot (ignores flag defaults)
* @param plot
* @param flag
2015-03-13 18:02:48 +11:00
* @return Flag
2015-01-09 01:08:50 +11:00
*/
2015-02-20 17:34:19 +11:00
public static Flag getPlotFlagAbs(final Plot plot, final String flag) {
return getSettingFlagAbs(plot.settings, flag);
2015-01-25 21:16:10 -07:00
}
2015-02-23 12:32:27 +11:00
2015-02-20 17:34:19 +11:00
public static Flag getSettingFlagAbs(final PlotSettings settings, final String flag) {
if ((settings.flags == null) || (settings.flags.size() == 0)) {
return null;
2015-01-16 21:34:05 -08:00
}
return settings.flags.get(flag);
2015-01-09 01:08:50 +11:00
}
2015-02-23 12:32:27 +11:00
2015-01-09 01:08:50 +11:00
/**
* Add a flag to a plot
* @param plot
* @param flag
*/
2015-02-20 17:34:19 +11:00
public static boolean addPlotFlag(final Plot plot, final Flag flag) {
2015-02-23 16:29:45 +11:00
final boolean result = EventUtil.manager.callFlagAdd(flag, plot);
if (!result) {
return false;
}
plot.settings.flags.put(flag.getKey(), flag);
2015-07-04 16:37:33 +10:00
DBFunc.setFlags(plot, plot.settings.flags.values());
2015-01-09 01:08:50 +11:00
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;
}
2015-02-23 12:32:27 +11:00
2015-02-20 17:34:19 +11:00
public static boolean addClusterFlag(final PlotCluster cluster, final Flag flag) {
//TODO plot cluster flag event
2015-01-25 21:16:10 -07:00
final Flag hasFlag = getSettingFlag(cluster.world, cluster.settings, flag.getKey());
cluster.settings.flags.put(flag.getKey(), flag);
DBFunc.setFlags(cluster, cluster.settings.flags.values());
2015-01-25 21:16:10 -07:00
return true;
}
2015-02-23 12:32:27 +11:00
2015-01-09 01:08:50 +11:00
/**
2015-02-20 17:34:19 +11:00
*
2015-01-09 01:08:50 +11:00
* @param plot
2015-03-13 18:26:08 +11:00
* @return set of flags
2015-01-09 01:08:50 +11:00
*/
2015-07-15 02:16:32 +10:00
public static HashMap<String, Flag> getPlotFlags(final Plot plot) {
2015-01-25 21:16:10 -07:00
return getSettingFlags(plot.world, plot.settings);
}
2015-02-23 12:32:27 +11:00
2015-07-15 02:16:32 +10:00
public static HashMap<String, Flag> getSettingFlags(final String world, final PlotSettings settings) {
PlotWorld plotworld = PS.get().getPlotWorld(world);
2015-07-15 02:16:32 +10:00
if (plotworld == null || plotworld.DEFAULT_FLAGS.size() == 0) {
return settings.flags;
}
else {
2015-07-15 02:16:32 +10:00
HashMap<String, Flag> map = (HashMap<String, Flag>) plotworld.DEFAULT_FLAGS.clone();
map.putAll(settings.flags);
return map;
2015-01-14 03:38:15 +11:00
}
2015-01-09 01:08:50 +11:00
}
2015-02-23 12:32:27 +11:00
public static boolean removePlotFlag(final Plot plot, final String id) {
Flag flag = plot.settings.flags.remove(id);
if (flag == null) {
return false;
2015-01-09 01:08:50 +11:00
}
final boolean result = EventUtil.manager.callFlagRemove(flag, plot);
if (!result) {
plot.settings.flags.put(id, flag);
return false;
}
2015-07-04 16:37:33 +10:00
DBFunc.setFlags(plot, plot.settings.flags.values());
return true;
2015-01-09 01:08:50 +11:00
}
2015-02-23 12:32:27 +11:00
public static boolean removeClusterFlag(final PlotCluster cluster, final String id) {
Flag flag = cluster.settings.flags.remove(id);
if (flag == null) {
return false;
2015-01-25 21:16:10 -07:00
}
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;
2015-01-25 21:16:10 -07:00
}
2015-02-23 12:32:27 +11:00
2015-02-20 17:34:19 +11:00
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) {
2015-01-09 01:08:50 +11:00
return;
}
else {
plot.settings.flags.clear();
}
2015-07-04 16:37:33 +10:00
DBFunc.setFlags(plot, plot.settings.flags.values());
2015-01-09 01:08:50 +11:00
}
2015-02-23 12:32:27 +11:00
2015-02-20 17:34:19 +11:00
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) {
2015-01-25 21:16:10 -07:00
return;
}
else {
cluster.settings.flags.clear();
}
DBFunc.setFlags(cluster, cluster.settings.flags.values());
2015-01-25 21:16:10 -07:00
}
2015-02-23 12:32:27 +11:00
2014-11-05 14:42:08 +11:00
public static Flag[] removeFlag(final Flag[] flags, final String r) {
final Flag[] f = new Flag[flags.length - 1];
2014-10-19 10:19:38 +02:00
int index = 0;
2014-11-05 14:42:08 +11:00
for (final Flag flag : flags) {
if (!flag.getKey().equals(r)) {
2014-10-19 10:19:38 +02:00
f[index++] = flag;
2014-11-05 14:42:08 +11:00
}
2014-10-19 10:19:38 +02:00
}
return f;
}
2015-02-23 12:32:27 +11:00
2015-01-07 03:08:29 +11:00
public static Set<Flag> removeFlag(final Set<Flag> flags, final String r) {
final HashSet<Flag> newflags = new HashSet<>();
2014-11-05 14:42:08 +11:00
for (final Flag flag : flags) {
2015-01-07 03:08:29 +11:00
if (!flag.getKey().equalsIgnoreCase(r)) {
newflags.add(flag);
2014-11-05 14:42:08 +11:00
}
2014-10-19 10:19:38 +02:00
}
2015-01-07 03:08:29 +11:00
return newflags;
2014-10-19 10:19:38 +02:00
}
2015-02-23 12:32:27 +11:00
2014-11-05 14:42:08 +11:00
/**
* Get a list of registered AbstractFlag objects
*
* @return List (AbstractFlag)
*/
public static HashSet<AbstractFlag> getFlags() {
2014-11-05 14:42:08 +11:00
return flags;
}
2015-02-23 12:32:27 +11:00
/**
* Get a list of registerd AbstragFlag objects based on player permissions
2014-11-05 14:42:08 +11:00
*
2014-12-17 20:15:11 -06:00
* @param player with permissions
*
* @return List (AbstractFlag)
*/
2015-02-22 23:24:48 +11:00
public static List<AbstractFlag> getFlags(final PlotPlayer player) {
2014-11-05 14:42:08 +11:00
final List<AbstractFlag> returnFlags = new ArrayList<>();
for (final AbstractFlag flag : flags) {
2014-11-07 21:30:29 +11:00
if (player.hasPermission("plots.set.flag." + flag.getKey().toLowerCase())) {
returnFlags.add(flag);
2014-11-05 14:42:08 +11:00
}
}
return returnFlags;
}
2015-02-23 12:32:27 +11:00
2014-11-05 14:42:08 +11:00
/**
* Get an AbstractFlag by a string Returns null if flag does not exist
*
2014-12-17 20:15:11 -06:00
* @param string Flag Key
*
2014-11-05 14:42:08 +11:00
* @return AbstractFlag
*/
public static AbstractFlag getFlag(final String string) {
for (final AbstractFlag flag : flags) {
if (flag.getKey().equalsIgnoreCase(string)) {
return flag;
}
}
return null;
}
2015-02-23 12:32:27 +11:00
2014-11-05 14:42:08 +11:00
/**
* Get an AbstractFlag by a string
*
2014-12-17 20:15:11 -06:00
* @param string Flag Key
* @param create If to create the flag if it does not exist
*
2014-11-05 14:42:08 +11:00
* @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);
}
2015-02-23 12:32:27 +11:00
2014-11-05 14:42:08 +11:00
/**
* Remove a registered AbstractFlag
*
2014-12-17 20:15:11 -06:00
* @param flag Flag Key
*
2014-11-05 14:42:08 +11:00
* @return boolean Result of operation
*/
public static boolean removeFlag(final AbstractFlag flag) {
return flags.remove(flag);
}
2015-02-23 12:32:27 +11:00
public static HashMap<String, Flag> parseFlags(final List<String> flagstrings) {
HashMap<String, Flag> map = new HashMap<String, Flag>();
for (String key : flagstrings) {
2015-03-10 21:48:20 +11:00
final String[] split;
if (key.contains(";")) {
split = key.split(";");
2015-03-10 21:48:20 +11:00
}
else {
split = key.split(":");
2015-03-10 21:48:20 +11:00
}
Flag flag;
2014-11-05 14:42:08 +11:00
if (split.length == 1) {
flag = new Flag(getFlag(split[0], true), "");
2014-12-17 20:15:11 -06:00
} else {
flag = new Flag(getFlag(split[0], true), split[1]);
2014-11-05 14:42:08 +11:00
}
map.put(flag.getKey(), flag);
2014-11-05 14:42:08 +11:00
}
return map;
2014-11-05 14:42:08 +11:00
}
2014-09-23 19:02:17 +02:00
}