From 17689433602089eb105eccd2fa95c866c96ddd76 Mon Sep 17 00:00:00 2001 From: MattBDev Date: Fri, 20 May 2016 20:29:16 -0400 Subject: [PATCH] Register flags --- .../plot/api/PlotAPI.java | 2 +- .../intellectualcrafters/plot/flag/Flags.java | 67 +++++++++++++++++-- 2 files changed, 64 insertions(+), 5 deletions(-) diff --git a/Bukkit/src/main/java/com/intellectualcrafters/plot/api/PlotAPI.java b/Bukkit/src/main/java/com/intellectualcrafters/plot/api/PlotAPI.java index 5105ae664..892847ea2 100644 --- a/Bukkit/src/main/java/com/intellectualcrafters/plot/api/PlotAPI.java +++ b/Bukkit/src/main/java/com/intellectualcrafters/plot/api/PlotAPI.java @@ -380,7 +380,7 @@ public class PlotAPI { * */ public void addFlag(Flag flag) { - Flags.getFlags().add(flag); + Flags.registerFlag(flag); } /** diff --git a/Core/src/main/java/com/intellectualcrafters/plot/flag/Flags.java b/Core/src/main/java/com/intellectualcrafters/plot/flag/Flags.java index bd64dd2de..a69b340b9 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/flag/Flags.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/flag/Flags.java @@ -1,10 +1,17 @@ package com.intellectualcrafters.plot.flag; import com.google.common.collect.Sets; +import com.intellectualcrafters.plot.PS; +import com.intellectualcrafters.plot.object.Plot; +import com.intellectualcrafters.plot.object.PlotArea; +import com.intellectualcrafters.plot.object.RunnableVal; import com.intellectualcrafters.plot.util.MainUtil; import com.intellectualcrafters.plot.util.MathMan; +import java.util.Collections; import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; public class Flags { @@ -73,6 +80,10 @@ public class Flags { public static final IntegerFlag HOSTILE_CAP = new IntegerFlag("hostile-cap"); public static final IntegerFlag VEHICLE_CAP = new IntegerFlag("vehicle-cap"); public static final Flag KEEP = new Flag("keep") { + @Override public String valueToString(Object value) { + return value.toString(); + } + @Override public Object parseValue(String value) { if (MathMan.isInteger(value)) { return Long.parseLong(value); @@ -86,6 +97,10 @@ public class Flags { return MainUtil.timeToSec(value) * 1000 + System.currentTimeMillis(); } } + + @Override public String getValueDescription() { + return "Flag value must a timestamp or a boolean"; + } }; private static final HashSet> flags = Sets.newHashSet(MUSIC, DESCRIPTION, ANALYSIS, GREETING, FAREWELL, FEED, HEAL, GAMEMODE, @@ -101,11 +116,55 @@ public class Flags { PVE, NO_WORLDEDIT, MISC_CAP, ENTITY_CAP, MOB_CAP, ANIMAL_CAP, HOSTILE_CAP, VEHICLE_CAP); /** - * Get a list of registered AbstractFlag objects + * Get an immutable set of registered flags. * - * @return List (AbstractFlag) + * @return a set of registered flags. */ - public static HashSet> getFlags() { - return flags; + public static Set> getFlags() { + return Collections.unmodifiableSet(flags); + } + + public static void registerFlag(final Flag flag) { + Iterator> iterator = flags.iterator(); + Flag duplicate = null; + while (iterator.hasNext()){ + duplicate = iterator.next(); + if (flag.getName().equalsIgnoreCase(duplicate.getName())) { + iterator.remove(); + flags.add(flag); + break; + } + } + final Flag dupFinal = duplicate; + PS.get().foreachPlotArea(new RunnableVal() { + @Override public void run(PlotArea value) { + if (dupFinal != null) { + Object remove = null; + if (value.DEFAULT_FLAGS.containsKey(dupFinal)) { + remove = value.DEFAULT_FLAGS.remove(dupFinal); + } + if (!(remove instanceof String)) { + //error message? maybe? + return; + } + value.DEFAULT_FLAGS.put(flag,flag.parseValue((String) remove)); + } + } + }); + PS.get().foreachPlotRaw(new RunnableVal() { + @Override public void run(Plot value) { + if (dupFinal != null) { + Object remove = null; + if (value.getFlags().containsKey(dupFinal)) { + remove = value.getFlags().remove(dupFinal); + } + if (!(remove instanceof String)) { + //error message? maybe? + return; + } + value.getFlags().put(flag,flag.parseValue((String) remove)); + } + } + }); } }