173 lines
6.1 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
2014-11-16 10:48:18 +01:00
import com.intellectualcrafters.plot.object.Plot;
2014-11-08 20:27:09 +01:00
import org.bukkit.entity.Player;
2014-09-23 19:02:17 +02:00
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
2014-11-16 09:56:42 +01:00
/**
* Flag Manager Utility
*
* @author Citymonstret
* @author Empire92
*/
@SuppressWarnings("unused")
2014-09-23 19:02:17 +02:00
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-11-16 09:56:42 +01:00
* @param flag Flag to register
* @return success?
2014-11-05 14:42:08 +11:00
*/
public static boolean addFlag(final AbstractFlag flag) {
2014-11-16 09:56:42 +01: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
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;
}
2014-11-05 14:42:08 +11:00
public static Flag[] removeFlag(final Set<Flag> flags, final String r) {
final Flag[] flagArray = new Flag[flags.size() - 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
flagArray[index++] = flag;
2014-11-05 14:42:08 +11:00
}
2014-10-19 10:19:38 +02:00
}
return flagArray;
}
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-11-08 20:27:09 +01: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-11-16 09:56:42 +01: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-11-16 09:56:42 +01:00
* @param string Flag Key
2014-11-08 20:27:09 +01:00
* @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-11-16 09:56:42 +01: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-11-08 20:27:09 +01:00
} else {
2014-11-05 14:42:08 +11:00
flags[i] = new Flag(getFlag(split[0], true), split[1]);
}
}
return flags;
}
2014-11-05 14:42:08 +11:00
/**
* Get the flags for a plot
*
2014-11-16 09:56:42 +01:00
* @param plot Plot to search in
2014-11-05 14:42:08 +11:00
* @return List (AbstractFlag)
*/
public static List<AbstractFlag> getPlotFlags(final Plot plot) {
final Set<Flag> plotFlags = plot.settings.getFlags();
final List<AbstractFlag> flags = new ArrayList<>();
for (final Flag flag : plotFlags) {
flags.add(flag.getAbstractFlag());
}
return flags;
}
2014-09-23 19:02:17 +02:00
}