[Bleeding] Change to lazy ActionFactory setting.

Allows to set a new ActionFactoryFactory without the ConfigManager.init 
already trying to parse actions, thus implementing special actions 
should be easier. Still three calls might be necessary to be on the 
safe side:
1. ConfigManager.setActionFactoryFactory(new factory)
2. ConfigManaher.setAllActionFactories()
3. DataManager.clearConfigs()

Not entirely convinced if concurrency issues might arise with reload or 
even on startup (chat).
This commit is contained in:
asofold 2013-07-18 00:21:42 +02:00
parent b49df04120
commit 2913baa6c7
4 changed files with 52 additions and 14 deletions

View File

@ -21,7 +21,7 @@ import fr.neatmonster.nocheatplus.checks.ViolationData;
public class ConfigFile extends ConfigFileWithActions<ViolationData, ActionList> { public class ConfigFile extends ConfigFileWithActions<ViolationData, ActionList> {
@Override @Override
public void regenerateActionLists() { public void setActionFactory() {
factory = ConfigManager.getActionFactory(((MemorySection) this.get(ConfPaths.STRINGS)).getValues(false)); factory = ConfigManager.getActionFactory(((MemorySection) this.get(ConfPaths.STRINGS)).getValues(false));
} }

View File

@ -7,16 +7,32 @@ import fr.neatmonster.nocheatplus.actions.ActionData;
public abstract class ConfigFileWithActions<D extends ActionData, L extends AbstractActionList<D, L>> extends RawConfigFile { public abstract class ConfigFileWithActions<D extends ActionData, L extends AbstractActionList<D, L>> extends RawConfigFile {
/**
* Do this after reading new data.<br>
* TODO: Specify what this actually does.
*/
public abstract void regenerateActionLists();
/** The factory. */ /** The factory. */
protected AbstractActionFactory<D, L> factory = null; protected AbstractActionFactory<D, L> factory = null;
// /**
// * @deprecated Use resetActionFactory.
// */
// public void regenerateActionLists(){
// resetActionFactory();
// }
/**
* This should set (override if necessary) a default ActionFactory. NCP will use ConfigManager.getActionsFactoryFactory. <br>
* Do this after reading new data or changing the AbstractActionFactory instance.<br>
* This must set or override the internal factory field to enable/update ActionList getting.<br>
* If factory is null on getting an ActionList, this will be called internally.
*/
public abstract void setActionFactory();
/**
* Explicitly set the ActionFactory, also allow setting to null for lazy reset/get.
* @param factory
*/
public void setActionFactory(final AbstractActionFactory<D, L> factory){
this.factory = factory;
}
/** /**
* A convenience method to get an optimized action list from the configuration. * A convenience method to get an optimized action list from the configuration.
* *
@ -28,6 +44,9 @@ public abstract class ConfigFileWithActions<D extends ActionData, L extends Abst
*/ */
public L getOptimizedActionList(final String path, final String permission) public L getOptimizedActionList(final String path, final String permission)
{ {
if (factory == null){
setActionFactory();
}
final String value = this.getString(path); final String value = this.getString(path);
return factory.createActionList(value, permission).getOptimizedCopy(this); return factory.createActionList(value, permission).getOptimizedCopy(this);
} }
@ -44,6 +63,9 @@ public abstract class ConfigFileWithActions<D extends ActionData, L extends Abst
*/ */
public L getDefaultActionList(final String path, final String permission) public L getDefaultActionList(final String path, final String permission)
{ {
if (factory == null){
setActionFactory();
}
final String value = this.getString(path); final String value = this.getString(path);
return factory.createActionList(value, permission); return factory.createActionList(value, permission);
} }

View File

@ -72,6 +72,12 @@ public class ConfigManager {
/** /**
* Set the factory to get actions from. * Set the factory to get actions from.
* This will reset all ActionFactories to null (lazy initialization),
* call setAllActionFactories to ensure action factories are ready.
* To be on the safe side also call DataManager.clearConfigs().
* <hr>
* To Hook into NCP for setting the factories, you should register a INotifyReload instance
* with the NoCheatPlusAPI using the annotation SetupOrder with a higher negative value (-1000).
* @param factory * @param factory
*/ */
public static void setActionFactoryFactory(ActionFactoryFactory factory){ public static void setActionFactoryFactory(ActionFactoryFactory factory){
@ -86,8 +92,9 @@ public class ConfigManager {
} }
}; };
} }
// Use lazy resetting.
for (final ConfigFile config : worldsMap.values()){ for (final ConfigFile config : worldsMap.values()){
config.regenerateActionLists(); config.setActionFactory(null);
} }
} }
@ -95,6 +102,15 @@ public class ConfigManager {
return actionFactoryFactory; return actionFactoryFactory;
} }
/**
* Force setting up all configs action factories.
*/
public static void setAllActionFactories(){
for (final ConfigFile config : worldsMap.values()){
config.setActionFactory();
}
}
/** /**
* Get the WorldConfigProvider in use. * Get the WorldConfigProvider in use.
* @return * @return
@ -209,7 +225,7 @@ public class ConfigManager {
LogUtil.logSevere(e); LogUtil.logSevere(e);
} }
} }
globalConfig.regenerateActionLists(); // globalConfig.setActionFactory();
worldsMap.put(null, globalConfig); worldsMap.put(null, globalConfig);
@ -249,7 +265,7 @@ public class ConfigManager {
} }
worldConfig.setDefaults(globalConfig); worldConfig.setDefaults(globalConfig);
worldConfig.options().copyDefaults(true); worldConfig.options().copyDefaults(true);
worldConfig.regenerateActionLists(); // worldConfig.setActionFactory();
} }
} }

View File

@ -561,7 +561,7 @@ public class DefaultConfig extends ConfigFile {
set(ConfPaths.COMPATIBILITY_BLOCKS + ConfPaths.SUB_ALLOWINSTANTBREAK, new LinkedList<String>()); set(ConfPaths.COMPATIBILITY_BLOCKS + ConfPaths.SUB_ALLOWINSTANTBREAK, new LinkedList<String>());
set(ConfPaths.COMPATIBILITY_BLOCKS + ConfPaths.SUB_OVERRIDEFLAGS + ".snow", "default"); set(ConfPaths.COMPATIBILITY_BLOCKS + ConfPaths.SUB_OVERRIDEFLAGS + ".snow", "default");
// Update internal factory based on all the new entries to the "actions" section. // // Update internal factory based on all the new entries to the "actions" section.
regenerateActionLists(); // setActionFactory();
} }
} }