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-01-14 03:38:15 +11:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Set;
|
|
|
|
|
2015-02-19 19:51:10 +11:00
|
|
|
import com.intellectualcrafters.plot.PlotSquared;
|
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;
|
2014-12-17 20:15:11 -06:00
|
|
|
import com.intellectualcrafters.plot.object.Plot;
|
2015-01-25 21:16:10 -07:00
|
|
|
import com.intellectualcrafters.plot.object.PlotCluster;
|
2015-02-22 23:24:48 +11:00
|
|
|
import com.intellectualcrafters.plot.object.PlotPlayer;
|
2015-01-25 21:16:10 -07:00
|
|
|
import com.intellectualcrafters.plot.object.PlotSettings;
|
2015-01-09 01:08:50 +11:00
|
|
|
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
|
2014-11-21 23:45:46 +01:00
|
|
|
private final static ArrayList<AbstractFlag> flags = new ArrayList<>();
|
2015-02-23 12:32:27 +11:00
|
|
|
|
2014-11-05 14:42:08 +11:00
|
|
|
/**
|
|
|
|
* Register an AbstractFlag with PlotSquared
|
|
|
|
*
|
2014-12-17 20:15:11 -06:00
|
|
|
* @param flag Flag to register
|
|
|
|
*
|
2014-11-16 09:56:42 +01:00
|
|
|
* @return success?
|
2014-11-05 14:42:08 +11:00
|
|
|
*/
|
2015-01-29 18:45:14 +11:00
|
|
|
public static boolean addFlag(final AbstractFlag af) {
|
2015-02-19 21:23:36 +11:00
|
|
|
PlotSquared.log(C.PREFIX.s() + "&8 - Adding flag: &7" + af);
|
2015-03-10 21:21:38 +11:00
|
|
|
for (PlotWorld plotworld : PlotSquared.getPlotWorldObjects()) {
|
|
|
|
for (final Flag flag : plotworld.DEFAULT_FLAGS) {
|
|
|
|
if (flag.getAbstractFlag().getKey().equals(af.getKey())) {
|
|
|
|
flag.setKey(af);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-19 19:51:10 +11:00
|
|
|
if (PlotSquared.getAllPlotsRaw() != null) {
|
2015-02-20 17:34:19 +11:00
|
|
|
for (final Plot plot : PlotSquared.getPlots()) {
|
|
|
|
for (final Flag flag : plot.settings.flags) {
|
2015-02-14 15:20:26 +11:00
|
|
|
if (flag.getAbstractFlag().getKey().equals(af.getKey())) {
|
2015-01-29 18:45:14 +11:00
|
|
|
flag.setKey(af);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (getFlag(af.getKey()) == null) && flags.add(af);
|
2014-11-05 14:42:08 +11:00
|
|
|
}
|
2015-02-23 12:32:27 +11:00
|
|
|
|
2015-02-20 17:34:19 +11:00
|
|
|
public static Flag getSettingFlag(final String world, final PlotSettings settings, final String flag) {
|
|
|
|
final ArrayList<Flag> flags = new ArrayList<>();
|
|
|
|
if ((settings.flags != null) && (settings.flags.size() > 0)) {
|
|
|
|
flags.addAll(settings.flags);
|
2015-01-16 21:34:05 -08:00
|
|
|
}
|
2015-02-20 19:55:04 +11:00
|
|
|
final PlotWorld plotworld = PlotSquared.getPlotWorld(world);
|
2015-02-20 17:34:19 +11:00
|
|
|
if ((plotworld != null) && (plotworld.DEFAULT_FLAGS != null) && (plotworld.DEFAULT_FLAGS.length > 0)) {
|
2015-01-12 16:18:27 +11:00
|
|
|
flags.addAll(Arrays.asList(plotworld.DEFAULT_FLAGS));
|
|
|
|
}
|
2015-01-09 01:08:50 +11:00
|
|
|
for (final Flag myflag : flags) {
|
|
|
|
if (myflag.getKey().equals(flag)) {
|
|
|
|
return myflag;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
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
|
|
|
|
* @return
|
|
|
|
*/
|
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) {
|
|
|
|
final Flag flag = getPlotFlag(plot, strFlag);
|
|
|
|
if (flag == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (flag.getValue() instanceof Boolean) {
|
|
|
|
return (boolean) flag.getValue();
|
|
|
|
}
|
|
|
|
return false;
|
2015-01-22 16:26:16 -08:00
|
|
|
}
|
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
|
|
|
|
* @return
|
|
|
|
*/
|
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
|
|
|
}
|
2015-01-25 21:16:10 -07:00
|
|
|
for (final Flag myflag : settings.flags) {
|
2015-01-09 01:08:50 +11:00
|
|
|
if (myflag.getKey().equals(flag)) {
|
|
|
|
return myflag;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
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;
|
|
|
|
}
|
2015-01-09 01:08:50 +11:00
|
|
|
final Flag hasFlag = getPlotFlag(plot, flag.getKey());
|
|
|
|
if (hasFlag != null) {
|
|
|
|
plot.settings.flags.remove(hasFlag);
|
|
|
|
}
|
|
|
|
plot.settings.flags.add(flag);
|
|
|
|
DBFunc.setFlags(plot.world, plot, plot.settings.flags);
|
|
|
|
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());
|
|
|
|
if (hasFlag != null) {
|
|
|
|
cluster.settings.flags.remove(hasFlag);
|
|
|
|
}
|
|
|
|
cluster.settings.flags.add(flag);
|
|
|
|
DBFunc.setFlags(cluster, cluster.settings.flags);
|
|
|
|
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
|
|
|
|
* @return
|
|
|
|
*/
|
2015-02-20 17:34:19 +11:00
|
|
|
public static Set<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-02-20 17:34:19 +11:00
|
|
|
public static Set<Flag> getSettingFlags(final String world, final PlotSettings settings) {
|
|
|
|
final Set<Flag> plotflags = settings.flags;
|
2015-02-20 19:55:04 +11:00
|
|
|
final PlotWorld plotworld = PlotSquared.getPlotWorld(world);
|
2015-02-20 17:34:19 +11:00
|
|
|
if ((plotworld != null) && (plotworld.DEFAULT_FLAGS != null) && (plotworld.DEFAULT_FLAGS.length > 0)) {
|
|
|
|
final HashSet<String> flagStrings = new HashSet<>();
|
|
|
|
for (final Flag flag : plotflags) {
|
2015-01-31 09:09:16 +11:00
|
|
|
flagStrings.add(flag.getKey());
|
|
|
|
}
|
2015-02-20 17:34:19 +11:00
|
|
|
for (final Flag newflag : plotworld.DEFAULT_FLAGS) {
|
2015-01-31 09:09:16 +11:00
|
|
|
if (!flagStrings.contains(newflag.getKey())) {
|
|
|
|
plotflags.add(newflag);
|
|
|
|
}
|
|
|
|
}
|
2015-01-14 03:38:15 +11:00
|
|
|
}
|
2015-01-09 01:08:50 +11:00
|
|
|
return plotflags;
|
|
|
|
}
|
2015-02-23 12:32:27 +11:00
|
|
|
|
2015-02-20 17:34:19 +11:00
|
|
|
public static boolean removePlotFlag(final Plot plot, final String flag) {
|
2015-01-09 01:08:50 +11:00
|
|
|
final Flag hasFlag = getPlotFlag(plot, flag);
|
|
|
|
if (hasFlag != null) {
|
2015-02-20 17:34:19 +11:00
|
|
|
final Flag flagObj = FlagManager.getPlotFlagAbs(plot, flag);
|
2015-01-09 01:08:50 +11:00
|
|
|
if (flagObj != null) {
|
2015-02-23 16:29:45 +11:00
|
|
|
final boolean result = EventUtil.manager.callFlagRemove(flagObj, plot);
|
|
|
|
if (!result) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-01-09 01:08:50 +11:00
|
|
|
plot.settings.flags.remove(hasFlag);
|
|
|
|
DBFunc.setFlags(plot.world, plot, plot.settings.flags);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2015-02-23 12:32:27 +11:00
|
|
|
|
2015-02-20 17:34:19 +11:00
|
|
|
public static boolean removeClusterFlag(final PlotCluster cluster, final String flag) {
|
2015-01-25 21:16:10 -07:00
|
|
|
final Flag hasFlag = getSettingFlag(cluster.world, cluster.settings, flag);
|
|
|
|
if (hasFlag != null) {
|
2015-02-20 17:34:19 +11:00
|
|
|
final Flag flagObj = FlagManager.getSettingFlagAbs(cluster.settings, flag);
|
2015-01-25 21:16:10 -07:00
|
|
|
if (flagObj != null) {
|
2015-02-20 17:34:19 +11:00
|
|
|
//TODO cluster flag add event
|
2015-01-25 21:16:10 -07:00
|
|
|
cluster.settings.flags.remove(hasFlag);
|
|
|
|
DBFunc.setFlags(cluster, cluster.settings.flags);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
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) {
|
2015-01-09 01:08:50 +11:00
|
|
|
if (flags == null) {
|
|
|
|
plot.settings.flags = new HashSet<>();
|
|
|
|
DBFunc.setFlags(plot.world, plot, plot.settings.flags);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
plot.settings.flags = flags;
|
|
|
|
DBFunc.setFlags(plot.world, plot, plot.settings.flags);
|
|
|
|
}
|
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) {
|
2015-01-25 21:16:10 -07:00
|
|
|
if (flags == null) {
|
|
|
|
cluster.settings.flags = new HashSet<>();
|
|
|
|
DBFunc.setFlags(cluster, cluster.settings.flags);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cluster.settings.flags = flags;
|
|
|
|
DBFunc.setFlags(cluster, cluster.settings.flags);
|
|
|
|
}
|
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 List<AbstractFlag> getFlags() {
|
|
|
|
return flags;
|
|
|
|
}
|
2015-02-23 12:32:27 +11:00
|
|
|
|
2014-10-22 19:28:02 +02: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
|
|
|
|
*
|
2014-10-22 19:28:02 +02:00
|
|
|
* @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())) {
|
2014-10-22 19:28:02 +02:00
|
|
|
returnFlags.add(flag);
|
2014-11-05 14:42:08 +11:00
|
|
|
}
|
2014-10-22 19:28:02 +02: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);
|
|
|
|
addFlag(flag);
|
|
|
|
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
|
|
|
|
2014-11-05 14:42:08 +11:00
|
|
|
public static Flag[] parseFlags(final List<String> flagstrings) {
|
|
|
|
final Flag[] flags = new Flag[flagstrings.size()];
|
|
|
|
for (int i = 0; i < flagstrings.size(); i++) {
|
2015-03-10 21:48:20 +11:00
|
|
|
final String[] split;
|
|
|
|
if (flagstrings.get(i).contains(";")) {
|
|
|
|
split = flagstrings.get(i).split(";");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
split = flagstrings.get(i).split(":");
|
|
|
|
}
|
2014-11-05 14:42:08 +11:00
|
|
|
if (split.length == 1) {
|
|
|
|
flags[i] = new Flag(getFlag(split[0], true), "");
|
2014-12-17 20:15:11 -06:00
|
|
|
} else {
|
2014-11-05 14:42:08 +11:00
|
|
|
flags[i] = new Flag(getFlag(split[0], true), split[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return flags;
|
|
|
|
}
|
2014-09-23 19:02:17 +02:00
|
|
|
}
|