diff --git a/NCPCompat/src/main/java/fr/neatmonster/nocheatplus/compat/BlockPropertiesSetup.java b/NCPCompat/src/main/java/fr/neatmonster/nocheatplus/compat/BlockPropertiesSetup.java index b2dab073..5eeb145a 100644 --- a/NCPCompat/src/main/java/fr/neatmonster/nocheatplus/compat/BlockPropertiesSetup.java +++ b/NCPCompat/src/main/java/fr/neatmonster/nocheatplus/compat/BlockPropertiesSetup.java @@ -1,5 +1,7 @@ package fr.neatmonster.nocheatplus.compat; +import fr.neatmonster.nocheatplus.config.WorldConfigProvider; + /** * Provide a setup method for additional BlockProperties initialization.
* Typically MCAccess can implement it. TODO: An extra factory for Bukkit level. @@ -9,6 +11,7 @@ package fr.neatmonster.nocheatplus.compat; public interface BlockPropertiesSetup { /** * Additional initialization. + * @param worldConfigProvider Configuration provider if needed. */ - public void setupBlockProperties(); + public void setupBlockProperties(WorldConfigProvider worldConfigProvider); } diff --git a/NCPCompat/src/main/java/fr/neatmonster/nocheatplus/compat/bukkit/MCAccessBukkit.java b/NCPCompat/src/main/java/fr/neatmonster/nocheatplus/compat/bukkit/MCAccessBukkit.java index 3c1961b7..0dd1501f 100644 --- a/NCPCompat/src/main/java/fr/neatmonster/nocheatplus/compat/bukkit/MCAccessBukkit.java +++ b/NCPCompat/src/main/java/fr/neatmonster/nocheatplus/compat/bukkit/MCAccessBukkit.java @@ -16,6 +16,7 @@ import org.bukkit.potion.PotionEffectType; import fr.neatmonster.nocheatplus.compat.AlmostBoolean; import fr.neatmonster.nocheatplus.compat.BlockPropertiesSetup; import fr.neatmonster.nocheatplus.compat.MCAccess; +import fr.neatmonster.nocheatplus.config.WorldConfigProvider; import fr.neatmonster.nocheatplus.utilities.BlockCache; import fr.neatmonster.nocheatplus.utilities.BlockProperties; import fr.neatmonster.nocheatplus.utilities.PotionUtil; @@ -154,7 +155,7 @@ public class MCAccessBukkit implements MCAccess, BlockPropertiesSetup{ } @Override - public void setupBlockProperties() { + public void setupBlockProperties(final WorldConfigProvider worldConfigProvider) { // TODO Set some generic properties matching what BlockCache.getShape returns. for (Material mat : Material.values()){ if (!mat.isBlock()) continue; diff --git a/NCPCompat/src/main/java/fr/neatmonster/nocheatplus/config/WorldConfigProvider.java b/NCPCompat/src/main/java/fr/neatmonster/nocheatplus/config/WorldConfigProvider.java new file mode 100644 index 00000000..69cf58d0 --- /dev/null +++ b/NCPCompat/src/main/java/fr/neatmonster/nocheatplus/config/WorldConfigProvider.java @@ -0,0 +1,35 @@ +package fr.neatmonster.nocheatplus.config; + +import java.util.Collection; + +/** + * This is to bridge the gap between ConfigFile which needs Action and RawConfigFile which has to be available in NCPCompat.
+ * Aim is not speed of execution but providing a way of accessing all configs to set properties from within compatibility modules. + *
+ * This might be seen as a refactoring/structuring stage, leading to putting actions to NCPCompat as well. + * @author mc_dev + * + */ +public interface WorldConfigProvider { + + /** + * Get the default configuration. + * @return + */ + public C getDefaultConfig(); + + /** + * Get the world configuration. + * @param worldName The default config has null as world. The default config is returned, if the world is not known. + * @return + */ + public C getConfig(String worldName); + + /** + * Get a Collection-view of all worlds config files, including the default configuration. + * @return + */ + public Collection getAllConfigs(); + + // TODO: Add operations for all configs, like setForAllConfigs, get(Max|min)NumberForAllConfigs, .... +} diff --git a/NCPCompat/src/main/java/fr/neatmonster/nocheatplus/utilities/BlockProperties.java b/NCPCompat/src/main/java/fr/neatmonster/nocheatplus/utilities/BlockProperties.java index db3fdadc..9c60315a 100644 --- a/NCPCompat/src/main/java/fr/neatmonster/nocheatplus/utilities/BlockProperties.java +++ b/NCPCompat/src/main/java/fr/neatmonster/nocheatplus/utilities/BlockProperties.java @@ -19,6 +19,7 @@ import fr.neatmonster.nocheatplus.compat.BlockPropertiesSetup; import fr.neatmonster.nocheatplus.compat.MCAccess; import fr.neatmonster.nocheatplus.config.RawConfigFile; import fr.neatmonster.nocheatplus.config.RootConfPaths; +import fr.neatmonster.nocheatplus.config.WorldConfigProvider; import fr.neatmonster.nocheatplus.logging.LogUtil; /** @@ -281,14 +282,14 @@ public class BlockProperties { /** Penalty factor for block break duration if not on ground. */ protected static float breakPenaltyOffGround = 4f; - public static void init(final MCAccess mcAccess) { + public static void init(final MCAccess mcAccess, final WorldConfigProvider worldConfigProvider) { blockCache = mcAccess.getBlockCache(null); pLoc = new PlayerLocation(mcAccess, null); try{ - initTools(mcAccess); - initBlocks(mcAccess); + initTools(mcAccess, worldConfigProvider); + initBlocks(mcAccess, worldConfigProvider); if (mcAccess instanceof BlockPropertiesSetup){ - ((BlockPropertiesSetup) mcAccess).setupBlockProperties(); + ((BlockPropertiesSetup) mcAccess).setupBlockProperties(worldConfigProvider); } } catch(Throwable t){ @@ -296,7 +297,7 @@ public class BlockProperties { } } - private static void initTools(final MCAccess mcAccess) { + private static void initTools(final MCAccess mcAccess, final WorldConfigProvider worldConfigProvider) { tools.clear(); tools.put(268, new ToolProps(ToolType.SWORD, MaterialBase.WOOD)); tools.put(269, new ToolProps(ToolType.SPADE, MaterialBase.WOOD)); @@ -326,7 +327,7 @@ public class BlockProperties { tools.put(359, new ToolProps(ToolType.SHEARS, MaterialBase.NONE)); } - private static void initBlocks(final MCAccess mcAccess) { + private static void initBlocks(final MCAccess mcAccess, final WorldConfigProvider worldConfigProvider) { Arrays.fill(blocks, null); /////////////////////////// // Initalize block flags diff --git a/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/NoCheatPlus.java b/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/NoCheatPlus.java index 6eed742a..3b45b4a2 100644 --- a/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/NoCheatPlus.java +++ b/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/NoCheatPlus.java @@ -650,7 +650,7 @@ public class NoCheatPlus extends JavaPlugin implements NoCheatPlusAPI { */ protected void initBlockProperties(ConfigFile config){ // Set up BlockProperties. - BlockProperties.init(getMCAccess()); + BlockProperties.init(getMCAccess(), ConfigManager.getWorldConfigProvider()); BlockProperties.applyConfig(config, ConfPaths.COMPATIBILITY_BLOCKS); } diff --git a/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/config/ConfigManager.java b/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/config/ConfigManager.java index 1777bf2d..d269e69f 100644 --- a/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/config/ConfigManager.java +++ b/NCPPlugin/src/main/java/fr/neatmonster/nocheatplus/config/ConfigManager.java @@ -1,6 +1,7 @@ package fr.neatmonster.nocheatplus.config; import java.io.File; +import java.util.Collection; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; @@ -26,9 +27,6 @@ import fr.neatmonster.nocheatplus.logging.LogUtil; * The synchronized methods are to ensure that changing the configurations won't lead to trouble for the asynchronous checks. */ public class ConfigManager { - - /** The map containing the configuration files per world. */ - private static final Map worldsMap = new HashMap(); public static interface ActionFactoryFactory{ public ActionFactory newActionFactory(Map library); @@ -41,6 +39,28 @@ public class ConfigManager { } }; + /** The map containing the configuration files per world. */ + private static final Map worldsMap = new HashMap(); + + private static final WorldConfigProvider worldConfigProvider = new WorldConfigProvider() { + + @Override + public ConfigFile getDefaultConfig() { + return ConfigManager.getConfigFile(); + } + + @Override + public ConfigFile getConfig(String worldName) { + return ConfigManager.getConfigFile(worldName); + } + + @Override + public Collection getAllConfigs() { + return ConfigManager.worldsMap.values(); + } + + }; + /** * Factory method. * @param library @@ -70,6 +90,14 @@ public class ConfigManager { public static ActionFactoryFactory getActionFactoryFactory(){ return actionFactoryFactory; } + + /** + * Get the WorldConfigProvider in use. + * @return + */ + public static WorldConfigProvider getWorldConfigProvider() { + return worldConfigProvider; + } /** * Cleanup.