334 lines
12 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-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;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
2015-01-09 01:08:50 +11:00
import com.intellectualcrafters.plot.PlotMain;
import com.intellectualcrafters.plot.database.DBFunc;
import com.intellectualcrafters.plot.events.PlotFlagAddEvent;
import com.intellectualcrafters.plot.events.PlotFlagRemoveEvent;
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;
import com.intellectualcrafters.plot.object.PlotSettings;
2015-01-09 01:08:50 +11:00
import com.intellectualcrafters.plot.object.PlotWorld;
2014-09-23 19:02:17 +02:00
2014-11-16 09:56:42 +01:00
/**
* Flag Manager Utility
*
* @author Citymonstret
* @author Empire92
*/
2014-12-17 20:15:11 -06: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-09-23 19:02:17 +02:00
2014-11-21 23:45:46 +01:00
private final static ArrayList<AbstractFlag> flags = new ArrayList<>();
2014-09-23 19:02:17 +02: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
*/
public static boolean addFlag(final AbstractFlag flag) {
2014-12-16 16:03:20 +11:00
return (getFlag(flag.getKey()) == null) && flags.add(flag);
2014-11-05 14:42:08 +11:00
}
2014-09-23 19:02:17 +02:00
2015-01-25 21:16:10 -07:00
public static Flag getSettingFlag(String world, PlotSettings settings, String flag) {
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-01-25 21:16:10 -07:00
PlotWorld plotworld = PlotMain.getWorldSettings(world);
2015-01-14 03:38:15 +11:00
if (plotworld != null && plotworld.DEFAULT_FLAGS != null && plotworld.DEFAULT_FLAGS.length > 0) {
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-01-25 21:16:10 -07:00
/**
* Get the value of a flag for a plot (respects flag defaults)
* @param plot
* @param flag
* @return
*/
public static Flag getPlotFlag(Plot plot, String flag) {
return getSettingFlag(plot.world, plot.settings, flag);
}
2015-01-22 16:26:16 -08:00
public static boolean isPlotFlagTrue(Plot plot, String strFlag) {
Flag flag = getPlotFlag(plot, strFlag);
if (flag == null) {
return false;
}
2015-01-25 21:16:10 -07:00
if (flag.getValue() instanceof Boolean) {
return (boolean) flag.getValue();
}
return false;
2015-01-22 16:26:16 -08: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
*/
public static Flag getPlotFlagAbs(Plot plot, String flag) {
2015-01-25 21:16:10 -07:00
return getSettingFlagAbs(plot.settings, flag);
}
public static Flag getSettingFlagAbs(PlotSettings settings, String flag) {
if (settings.flags == null || settings.flags.size() == 0) {
2015-01-16 21:34:05 -08:00
return null;
}
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;
}
/**
* Add a flag to a plot
* @param plot
* @param flag
*/
public static boolean addPlotFlag(Plot plot, final Flag flag) {
final PlotFlagAddEvent event = new PlotFlagAddEvent(flag, plot);
Bukkit.getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) {
return false;
}
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-01-25 21:16:10 -07:00
public static boolean addClusterFlag(PlotCluster cluster, final Flag flag) {
//TODO plot cluster flag event
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-01-09 01:08:50 +11:00
/**
*
* @param plot
* @return
*/
public static Set<Flag> getPlotFlags(Plot plot) {
2015-01-25 21:16:10 -07:00
return getSettingFlags(plot.world, plot.settings);
}
public static Set<Flag> getSettingFlags(String world, PlotSettings settings) {
Set<Flag> plotflags = settings.flags;
PlotWorld plotworld = PlotMain.getWorldSettings(world);
2015-01-14 03:38:15 +11:00
if (plotworld != null && plotworld.DEFAULT_FLAGS != null && plotworld.DEFAULT_FLAGS.length > 0) {
plotflags.addAll(Arrays.asList(plotworld.DEFAULT_FLAGS));
}
2015-01-09 01:08:50 +11:00
return plotflags;
}
public static boolean removePlotFlag(Plot plot, String flag) {
final Flag hasFlag = getPlotFlag(plot, flag);
if (hasFlag != null) {
Flag flagObj = FlagManager.getPlotFlagAbs(plot, flag);
if (flagObj != null) {
final PlotFlagRemoveEvent event = new PlotFlagRemoveEvent(flagObj, plot);
Bukkit.getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) {
return false;
}
plot.settings.flags.remove(hasFlag);
DBFunc.setFlags(plot.world, plot, plot.settings.flags);
return true;
}
}
return false;
}
2015-01-25 21:16:10 -07:00
public static boolean removeClusterFlag(PlotCluster cluster, String flag) {
final Flag hasFlag = getSettingFlag(cluster.world, cluster.settings, flag);
if (hasFlag != null) {
Flag flagObj = FlagManager.getSettingFlagAbs(cluster.settings, flag);
if (flagObj != null) {
//TODO cluster flag add event
cluster.settings.flags.remove(hasFlag);
DBFunc.setFlags(cluster, cluster.settings.flags);
return true;
}
}
return false;
}
2015-01-09 01:08:50 +11:00
public static void setPlotFlags(Plot plot, Set<Flag> flags) {
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-01-25 21:16:10 -07:00
public static void setClusterFlags(PlotCluster cluster, Set<Flag> flags) {
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-01-09 01:08:50 +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-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-10-19 10:19:38 +02:00
int index = 0;
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
}
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;
}
2014-09-23 19:02:17 +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
*
* @return List (AbstractFlag)
*/
2014-11-05 14:42:08 +11:00
public static List<AbstractFlag> getFlags(final Player player) {
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;
}
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;
}
2014-09-23 19:02:17 +02: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);
}
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);
}
2014-09-23 19:02:17 +02: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++) {
final String[] split = flagstrings.get(i).split(";");
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
}