[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> {
@Override
public void regenerateActionLists() {
public void setActionFactory() {
factory = ConfigManager.getActionFactory(((MemorySection) this.get(ConfPaths.STRINGS)).getValues(false));
}

View File

@ -7,15 +7,31 @@ import fr.neatmonster.nocheatplus.actions.ActionData;
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. */
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.
@ -28,6 +44,9 @@ public abstract class ConfigFileWithActions<D extends ActionData, L extends Abst
*/
public L getOptimizedActionList(final String path, final String permission)
{
if (factory == null){
setActionFactory();
}
final String value = this.getString(path);
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)
{
if (factory == null){
setActionFactory();
}
final String value = this.getString(path);
return factory.createActionList(value, permission);
}

View File

@ -72,6 +72,12 @@ public class ConfigManager {
/**
* 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
*/
public static void setActionFactoryFactory(ActionFactoryFactory factory){
@ -85,9 +91,10 @@ public class ConfigManager {
return new ActionFactory(library);
}
};
}
}
// Use lazy resetting.
for (final ConfigFile config : worldsMap.values()){
config.regenerateActionLists();
config.setActionFactory(null);
}
}
@ -95,6 +102,15 @@ public class ConfigManager {
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.
* @return
@ -209,7 +225,7 @@ public class ConfigManager {
LogUtil.logSevere(e);
}
}
globalConfig.regenerateActionLists();
// globalConfig.setActionFactory();
worldsMap.put(null, globalConfig);
@ -249,7 +265,7 @@ public class ConfigManager {
}
worldConfig.setDefaults(globalConfig);
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_OVERRIDEFLAGS + ".snow", "default");
// Update internal factory based on all the new entries to the "actions" section.
regenerateActionLists();
// // Update internal factory based on all the new entries to the "actions" section.
// setActionFactory();
}
}