From a3eb7f576dc364f5bc0105461805049f3714b8a6 Mon Sep 17 00:00:00 2001 From: asofold Date: Sun, 21 Oct 2012 07:13:29 +0200 Subject: [PATCH] Bleeding: Add capability to replace ActionFactory and actions. --- .../nocheatplus/actions/ActionFactory.java | 6 +-- .../actions/types/ActionWithParameters.java | 4 +- .../actions/types/DummyAction.java | 2 +- .../nocheatplus/actions/types/LogAction.java | 7 ++-- .../nocheatplus/config/ConfigFile.java | 2 +- .../nocheatplus/config/ConfigManager.java | 39 +++++++++++++++++++ 6 files changed, 50 insertions(+), 10 deletions(-) diff --git a/src/fr/neatmonster/nocheatplus/actions/ActionFactory.java b/src/fr/neatmonster/nocheatplus/actions/ActionFactory.java index e0ebf289..7f174472 100644 --- a/src/fr/neatmonster/nocheatplus/actions/ActionFactory.java +++ b/src/fr/neatmonster/nocheatplus/actions/ActionFactory.java @@ -25,7 +25,7 @@ import fr.neatmonster.nocheatplus.utilities.CheckUtils; * Helps with creating Actions out of text string definitions. */ public class ActionFactory { - private static final Map lib = new HashMap(); + protected static final Map lib = new HashMap(); /** * Instantiates a new action factory. @@ -133,7 +133,7 @@ public class ActionFactory { * the definition * @return the action */ - private Action parseCmdAction(final String definition) { + protected Action parseCmdAction(final String definition) { final String[] parts = definition.split(":"); final String name = parts[0]; final Object command = lib.get(parts[0]); @@ -165,7 +165,7 @@ public class ActionFactory { * the definition * @return the action */ - private Action parseLogAction(final String definition) { + protected Action parseLogAction(final String definition) { final String[] parts = definition.split(":"); final String name = parts[0]; final Object message = lib.get(parts[0]); diff --git a/src/fr/neatmonster/nocheatplus/actions/types/ActionWithParameters.java b/src/fr/neatmonster/nocheatplus/actions/types/ActionWithParameters.java index 923977fa..02c7ddea 100644 --- a/src/fr/neatmonster/nocheatplus/actions/types/ActionWithParameters.java +++ b/src/fr/neatmonster/nocheatplus/actions/types/ActionWithParameters.java @@ -28,7 +28,7 @@ import fr.neatmonster.nocheatplus.checks.ViolationData; */ public abstract class ActionWithParameters extends Action { /** The parts of the message. */ - private final ArrayList messageParts; + protected final ArrayList messageParts; /** * Instantiates a new action with parameters. @@ -83,7 +83,7 @@ public abstract class ActionWithParameters extends Action { * @param message * the message */ - private void parseMessage(final String message) { + protected void parseMessage(final String message) { final String parts[] = message.split("\\[", 2); // No opening braces left. diff --git a/src/fr/neatmonster/nocheatplus/actions/types/DummyAction.java b/src/fr/neatmonster/nocheatplus/actions/types/DummyAction.java index a9abce78..dd17162a 100644 --- a/src/fr/neatmonster/nocheatplus/actions/types/DummyAction.java +++ b/src/fr/neatmonster/nocheatplus/actions/types/DummyAction.java @@ -19,7 +19,7 @@ import fr.neatmonster.nocheatplus.checks.ViolationData; */ public class DummyAction extends Action { /** The original string used for this action definition. */ - private final String definition; + protected final String definition; /** * Instantiates a new dummy. diff --git a/src/fr/neatmonster/nocheatplus/actions/types/LogAction.java b/src/fr/neatmonster/nocheatplus/actions/types/LogAction.java index d139378f..7ab5a514 100644 --- a/src/fr/neatmonster/nocheatplus/actions/types/LogAction.java +++ b/src/fr/neatmonster/nocheatplus/actions/types/LogAction.java @@ -27,13 +27,13 @@ import fr.neatmonster.nocheatplus.utilities.CheckUtils; public class LogAction extends ActionWithParameters { // Some flags to decide where the log message should show up, based on the configuration file. /** Log to chat? */ - private final boolean toChat; + public final boolean toChat; /** Log to console? */ - private final boolean toConsole; + public final boolean toConsole; /** Log to file? */ - private final boolean toFile; + public final boolean toFile; /** * Instantiates a new log action. @@ -59,6 +59,7 @@ public class LogAction extends ActionWithParameters { this.toChat = toChat; this.toConsole = toConsole; this.toFile = toFile; + // TODO: already use && flagfromconfig. } /* (non-Javadoc) diff --git a/src/fr/neatmonster/nocheatplus/config/ConfigFile.java b/src/fr/neatmonster/nocheatplus/config/ConfigFile.java index 444fe9c3..0a24f6b6 100644 --- a/src/fr/neatmonster/nocheatplus/config/ConfigFile.java +++ b/src/fr/neatmonster/nocheatplus/config/ConfigFile.java @@ -111,7 +111,7 @@ public class ConfigFile extends YamlConfiguration { * Do this after reading new data. */ public void regenerateActionLists() { - factory = new ActionFactory(((MemorySection) this.get(ConfPaths.STRINGS)).getValues(false)); + factory = ConfigManager.getActionFactory(((MemorySection) this.get(ConfPaths.STRINGS)).getValues(false)); } /* (non-Javadoc) diff --git a/src/fr/neatmonster/nocheatplus/config/ConfigManager.java b/src/fr/neatmonster/nocheatplus/config/ConfigManager.java index ff14a5b4..c20063f8 100644 --- a/src/fr/neatmonster/nocheatplus/config/ConfigManager.java +++ b/src/fr/neatmonster/nocheatplus/config/ConfigManager.java @@ -18,6 +18,7 @@ import org.bukkit.Bukkit; import org.bukkit.configuration.MemoryConfiguration; import fr.neatmonster.nocheatplus.NoCheatPlus; +import fr.neatmonster.nocheatplus.actions.ActionFactory; import fr.neatmonster.nocheatplus.utilities.CheckUtils; /* @@ -93,6 +94,43 @@ public class ConfigManager { /** The log file. */ public static File logFile = null; + + public static interface ActionFactoryFactory{ + public ActionFactory newActionFactory(Map library); + } + + private static ActionFactoryFactory actionFactoryFactory = new ActionFactoryFactory() { + @Override + public final ActionFactory newActionFactory(final Map library) { + return new ActionFactory(library); + } + }; + + /** + * Factory method. + * @param library + * @return + */ + public static ActionFactory getActionFactory(final Map library){ + return actionFactoryFactory.newActionFactory(library); + } + + /** + * Set the factory to get actions from. + * @param factory + */ + public static void setActionFactoryFactory(ActionFactoryFactory factory){ + if (factory != null) actionFactoryFactory = factory; + else actionFactoryFactory = new ActionFactoryFactory() { + @Override + public final ActionFactory newActionFactory(final Map library) { + return new ActionFactory(library); + } + }; + for (final ConfigFile config : worldsMap.values()){ + config.regenerateActionLists(); + } + } /** * Cleanup. @@ -103,6 +141,7 @@ public class ConfigManager { final Logger logger = Logger.getLogger("NoCheatPlus"); logger.removeHandler(fileHandler); fileHandler = null; + setActionFactoryFactory(null); } /**