Add an annotation for registration order with INotifyReload.

SetupOrder allows to define a priority, so you can register
 ActionFactoryFactory instances before any checks get them.
 
 Default priorities are NCP core at -100, DataManager -80, rest at 0.
This commit is contained in:
asofold 2013-07-18 01:22:59 +02:00
parent 2913baa6c7
commit 13cb783c72
8 changed files with 89 additions and 16 deletions

View File

@ -2,6 +2,7 @@ package fr.neatmonster.nocheatplus.command;
import java.util.Collection; import java.util.Collection;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set; import java.util.Set;
import org.bukkit.command.Command; import org.bukkit.command.Command;
@ -90,7 +91,7 @@ public class NoCheatPlusCommand extends BaseCommand{
* @param plugin * @param plugin
* the instance of NoCheatPlus * the instance of NoCheatPlus
*/ */
public NoCheatPlusCommand(final JavaPlugin plugin, final Collection<INotifyReload> notifyReload) { public NoCheatPlusCommand(final JavaPlugin plugin, final List<INotifyReload> notifyReload) {
super(plugin, "nocheatplus", null, new String[]{"ncp"}); super(plugin, "nocheatplus", null, new String[]{"ncp"});
// Register sub commands: // Register sub commands:
for (BaseCommand cmd : new BaseCommand[]{ for (BaseCommand cmd : new BaseCommand[]{

View File

@ -1,7 +1,8 @@
package fr.neatmonster.nocheatplus.command.admin; package fr.neatmonster.nocheatplus.command.admin;
import java.io.File; import java.io.File;
import java.util.Collection; import java.util.Collections;
import java.util.List;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.Command; import org.bukkit.command.Command;
@ -10,9 +11,10 @@ import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import fr.neatmonster.nocheatplus.checks.CheckType; import fr.neatmonster.nocheatplus.checks.CheckType;
import fr.neatmonster.nocheatplus.command.NoCheatPlusCommand.NCPReloadEvent;
import fr.neatmonster.nocheatplus.command.BaseCommand; import fr.neatmonster.nocheatplus.command.BaseCommand;
import fr.neatmonster.nocheatplus.command.NoCheatPlusCommand.NCPReloadEvent;
import fr.neatmonster.nocheatplus.components.INotifyReload; import fr.neatmonster.nocheatplus.components.INotifyReload;
import fr.neatmonster.nocheatplus.components.order.Order;
import fr.neatmonster.nocheatplus.config.ConfPaths; import fr.neatmonster.nocheatplus.config.ConfPaths;
import fr.neatmonster.nocheatplus.config.ConfigFile; import fr.neatmonster.nocheatplus.config.ConfigFile;
import fr.neatmonster.nocheatplus.config.ConfigManager; import fr.neatmonster.nocheatplus.config.ConfigManager;
@ -23,9 +25,9 @@ import fr.neatmonster.nocheatplus.players.DataManager;
public class ReloadCommand extends BaseCommand { public class ReloadCommand extends BaseCommand {
/** Components that need to be notified on reload */ /** Components that need to be notified on reload */
private final Collection<INotifyReload> notifyReload; private final List<INotifyReload> notifyReload;
public ReloadCommand(JavaPlugin plugin, Collection<INotifyReload> notifyReload) { public ReloadCommand(JavaPlugin plugin, List<INotifyReload> notifyReload) {
super(plugin, "reload", Permissions.ADMINISTRATION_RELOAD); super(plugin, "reload", Permissions.ADMINISTRATION_RELOAD);
this.notifyReload = notifyReload; this.notifyReload = notifyReload;
} }
@ -62,7 +64,8 @@ public class ReloadCommand extends BaseCommand {
DataManager.clearData(checkType); DataManager.clearData(checkType);
} }
// Tell the plugin to adapt to new config. // Tell the registered listeners to adapt to new config, first sort them (!).
Collections.sort(notifyReload, Order.cmpSetupOrder);
for (final INotifyReload component : notifyReload){ for (final INotifyReload component : notifyReload){
component.onReload(); component.onReload();
} }

View File

@ -1,7 +1,12 @@
package fr.neatmonster.nocheatplus.components; package fr.neatmonster.nocheatplus.components;
/** /**
* Interface for a component that needs to be notified about a reload. * Interface for a component that needs to be notified about a reload. Use the annotation SetupOrder to influence when to get executed.
* <hr>
* Priorities used by NCP with SetupOrder:
* <li>Core: -100</li>
* <li>DataManager: -80</li>
* <li>Rest (checks): 0</li>
* @author mc_dev * @author mc_dev
* *
*/ */

View File

@ -0,0 +1,39 @@
package fr.neatmonster.nocheatplus.components.order;
import java.util.Comparator;
/**
* Utilities for sorting out order.
* @author mc_dev
*
*/
public class Order {
/**
* Comparator for sorting SetupOrder.
*/
public static Comparator<Object> cmpSetupOrder = new Comparator<Object>() {
@Override
public int compare(final Object obj1, final Object obj2) {
int prio1 = 0;
int prio2 = 0;
final SetupOrder order1 = obj1.getClass().getAnnotation(SetupOrder.class);
if (order1 != null) {
prio1 = order1.priority();
}
final SetupOrder order2 = obj2.getClass().getAnnotation(SetupOrder.class);
if (order2 != null) {
prio2 = order2.priority();
}
if (prio1 < prio2) {
return -1;
}
else if (prio1 == prio2){
return 0;
}
else {
return 1;
}
}
};
}

View File

@ -0,0 +1,19 @@
package fr.neatmonster.nocheatplus.components.order;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Priority for order during setup/initialization of something.
* @author mc_dev
*
*/
@Documented
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface SetupOrder {
public int priority() default 0;
}

View File

@ -77,7 +77,7 @@ public class ConfigManager {
* To be on the safe side also call DataManager.clearConfigs(). * To be on the safe side also call DataManager.clearConfigs().
* <hr> * <hr>
* To Hook into NCP for setting the factories, you should register a INotifyReload instance * 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). * with the NoCheatPlusAPI using the annotation SetupOrder with a higher negative value (-1000, see INotifyReload javadoc).
* @param factory * @param factory
*/ */
public static void setActionFactoryFactory(ActionFactoryFactory factory){ public static void setActionFactoryFactory(ActionFactoryFactory factory){

View File

@ -40,6 +40,7 @@ import fr.neatmonster.nocheatplus.components.IHaveCheckType;
import fr.neatmonster.nocheatplus.components.INeedConfig; import fr.neatmonster.nocheatplus.components.INeedConfig;
import fr.neatmonster.nocheatplus.components.INotifyReload; import fr.neatmonster.nocheatplus.components.INotifyReload;
import fr.neatmonster.nocheatplus.components.IRemoveData; import fr.neatmonster.nocheatplus.components.IRemoveData;
import fr.neatmonster.nocheatplus.components.order.SetupOrder;
import fr.neatmonster.nocheatplus.config.ConfPaths; import fr.neatmonster.nocheatplus.config.ConfPaths;
import fr.neatmonster.nocheatplus.config.ConfigFile; import fr.neatmonster.nocheatplus.config.ConfigFile;
import fr.neatmonster.nocheatplus.config.ConfigManager; import fr.neatmonster.nocheatplus.config.ConfigManager;
@ -58,6 +59,7 @@ import fr.neatmonster.nocheatplus.utilities.StringUtil;
* @author mc_dev * @author mc_dev
* *
*/ */
@SetupOrder(priority = -80)
public class DataManager implements Listener, INotifyReload, INeedConfig, ComponentRegistry<IRemoveData>, ComponentWithName, ConsistencyChecker{ public class DataManager implements Listener, INotifyReload, INeedConfig, ComponentRegistry<IRemoveData>, ComponentWithName, ConsistencyChecker{
protected static DataManager instance = null; protected static DataManager instance = null;

View File

@ -62,6 +62,7 @@ import fr.neatmonster.nocheatplus.components.NameSetPermState;
import fr.neatmonster.nocheatplus.components.NoCheatPlusAPI; import fr.neatmonster.nocheatplus.components.NoCheatPlusAPI;
import fr.neatmonster.nocheatplus.components.PermStateReceiver; import fr.neatmonster.nocheatplus.components.PermStateReceiver;
import fr.neatmonster.nocheatplus.components.TickListener; import fr.neatmonster.nocheatplus.components.TickListener;
import fr.neatmonster.nocheatplus.components.order.SetupOrder;
import fr.neatmonster.nocheatplus.config.ConfPaths; import fr.neatmonster.nocheatplus.config.ConfPaths;
import fr.neatmonster.nocheatplus.config.ConfigFile; import fr.neatmonster.nocheatplus.config.ConfigFile;
import fr.neatmonster.nocheatplus.config.ConfigManager; import fr.neatmonster.nocheatplus.config.ConfigManager;
@ -101,7 +102,7 @@ import fr.neatmonster.nocheatplus.utilities.TickTask;
public class NoCheatPlus extends JavaPlugin implements NoCheatPlusAPI { public class NoCheatPlus extends JavaPlugin implements NoCheatPlusAPI {
private static final String MSG_NOTIFY_OFF = ChatColor.RED + "NCP: " + ChatColor.WHITE + "Notifications are turned " + ChatColor.RED + "OFF" + ChatColor.WHITE + "."; private static final String MSG_NOTIFY_OFF = ChatColor.RED + "NCP: " + ChatColor.WHITE + "Notifications are turned " + ChatColor.RED + "OFF" + ChatColor.WHITE + ".";
////////////////// //////////////////
// Static API // Static API
////////////////// //////////////////
@ -720,18 +721,21 @@ public class NoCheatPlus extends JavaPlugin implements NoCheatPlusAPI {
listenerManager.clear(); listenerManager.clear();
} }
@SetupOrder(priority = - 100)
class ReloadHook implements INotifyReload{
@Override
public void onReload() {
// Only for reloading, not INeedConfig.
processReload();
}
}
// Add the "low level" system components first. // Add the "low level" system components first.
for (final Object obj : new Object[]{ for (final Object obj : new Object[]{
nameSetPerms, nameSetPerms,
getCoreListener(), getCoreListener(),
// Put ReloadListener first, because Checks could also listen to it. // Put ReloadListener first, because Checks could also listen to it.
new INotifyReload() { new ReloadHook(),
// Only for reloading, not INeedConfig.
@Override
public void onReload() {
processReload();
}
},
NCPExemptionManager.getListener(), NCPExemptionManager.getListener(),
new ConsistencyChecker() { new ConsistencyChecker() {
@Override @Override