summaryrefslogtreecommitdiff
path: root/src/com/redstoner/coremods/moduleLoader/ModuleLoader.java
blob: e5e3c62bc219c760b8e16ffcb4c5427383f9a137 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package com.redstoner.coremods.moduleLoader;

import java.util.HashMap;

import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;

import com.nemez.cmdmgr.Command;
import com.nemez.cmdmgr.Command.AsyncType;
import com.nemez.cmdmgr.CommandManager;
import com.redstoner.annotations.AutoRegisterListener;
import com.redstoner.annotations.Debugable;
import com.redstoner.annotations.Version;
import com.redstoner.coremods.debugger.Debugger;
import com.redstoner.exceptions.MissingVersionException;
import com.redstoner.misc.Main;
import com.redstoner.misc.Utils;
import com.redstoner.misc.VersionHelper;
import com.redstoner.modules.CoreModule;
import com.redstoner.modules.Module;

/** The module loader, mother of all modules. Responsible for loading and taking care of all modules.
 * 
 * @author Pepich */
@Version(major = 3, minor = 0, revision = 0, compatible = 2)
public final class ModuleLoader implements CoreModule
{
	private static ModuleLoader instance;
	private static final HashMap<Module, Boolean> modules = new HashMap<Module, Boolean>();
	
	private ModuleLoader()
	{}
	
	public static void init()
	{
		if (instance == null)
			instance = new ModuleLoader();
		CommandManager.registerCommand(instance.getCommandString(), instance, Main.plugin);
	}
	
	/** This method will add a module to the module list, without enabling it
	 * 
	 * @param clazz The class of the module to be added. */
	@Debugable
	public static final void addModule(Class<? extends Module> clazz)
	{
		Debugger.notifyMethod(clazz);
		try
		{
			Module module = clazz.newInstance();
			modules.put(module, false);
		}
		catch (InstantiationException | IllegalAccessException e)
		{
			Utils.error("Could not add " + clazz.getName() + " to the list, constructor not accessible.");
		}
	}
	
	/** Call this to enable all not-yet enabled modules that are known to the loader. */
	@Debugable
	public static final void enableModules()
	{
		Debugger.notifyMethod();
		for (Module module : modules.keySet())
		{
			if (modules.get(module))
				continue;
			try
			{
				if (module.onEnable())
				{
					if (VersionHelper.isCompatible(VersionHelper.create(2, 0, 0, -1), module.getClass()))
						CommandManager.registerCommand(module.getCommandString(), module, Main.plugin);
					modules.put(module, true);
					Utils.log("Loaded module " + module.getClass().getName());
				}
				else
					Utils.error("Failed to load module " + module.getClass().getName());
			}
			catch (Exception e)
			{
				Utils.error("Failed to load module " + module.getClass().getName());
				e.printStackTrace();
			}
		}
		Utils.log("Modules enabled, running post initialization.");
		for (Module module : modules.keySet())
		{
			if (modules.get(module))
			{
				try
				{
					if (VersionHelper.isCompatible(VersionHelper.create(3, 0, 0, 3), module.getClass()))
					{
						module.postEnable();
					}
				}
				catch (MissingVersionException e)
				{
					e.printStackTrace();
				}
				if (module.getClass().isAnnotationPresent(AutoRegisterListener.class) && (module instanceof Listener))
				{
					Bukkit.getPluginManager().registerEvents((Listener) module, Main.plugin);
				}
			}
		}
	}
	
	/** This method enables a specific module. If no module with that name is known to the loader yet it will be added to the list.
	 * 
	 * @param clazz The class of the module to be enabled.
	 * @return true, when the module was successfully enabled. */
	@Debugable
	public static final boolean enableModule(Class<? extends Module> clazz)
	{
		Debugger.notifyMethod(clazz);
		for (Module module : modules.keySet())
		{
			if (module.getClass().equals(clazz))
			{
				if (module.onEnable())
				{
					if (module.getClass().isAnnotationPresent(AutoRegisterListener.class)
							&& (module instanceof Listener))
					{
						Bukkit.getPluginManager().registerEvents((Listener) module, Main.plugin);
					}
					Utils.log("Loaded module " + module.getClass().getName());
					modules.put(module, true);
					return true;
				}
				else
				{
					Utils.error("Failed to load module " + module.getClass().getName());
					return false;
				}
			}
		}
		try
		{
			Module m = clazz.newInstance();
			modules.put(m, false);
			if (m.onEnable())
			{
				if (m.getClass().isAnnotationPresent(AutoRegisterListener.class) && (m instanceof Listener))
				{
					Bukkit.getPluginManager().registerEvents((Listener) m, Main.plugin);
				}
				Utils.log("Loaded module " + m.getClass().getName());
				return true;
			}
			else
			{
				Utils.error("Failed to load module " + m.getClass().getName());
				return false;
			}
		}
		catch (InstantiationException | IllegalAccessException e)
		{
			Utils.error("Could not add " + clazz.getName() + " to the list, constructor not accessible.");
			return false;
		}
	}
	
	// @noformat
	@Override
	public String getCommandString()
	{
		return "command modules {\n" + 
				"	list{\n" + 
				"		help Lists all modules. Color indicates status: §aENABLED §cDISABLED;\n" + 
				"		perm jutils.admin;\n" + 
				"		run list;\n" + 
				"	}\n" + 
				"}";
	}
	// @format
	
	/** This method lists all modules to the specified CommandSender. The modules will be color coded correspondingly to their enabled status.
	 * 
	 * @param sender The person to send the info to, usually the issuer of the command or the console sender.
	 * @return true. */
	@Command(hook = "list", async = AsyncType.ALWAYS)
	public boolean listModulesCommand(CommandSender sender)
	{
		Utils.sendModuleHeader(sender);
		StringBuilder sb = new StringBuilder("Modules:\n");
		for (Module module : modules.keySet())
		{
			String[] classPath = module.getClass().getName().split("\\.");
			String classname = classPath[classPath.length - 1];
			sb.append(modules.get(module) ? "&a" : "&c");
			sb.append(classname);
			sb.append(", ");
		}
		sb.delete(sb.length() - 2, sb.length());
		Utils.sendMessage(sender, " §e", sb.toString(), '&');
		Utils.sendMessage(sender, " §7", "For more detailed information, consult the debugger.");
		return true;
	}
	
	public static void disableModules()
	{
		for (Module module : modules.keySet())
		{
			if (modules.get(module))
			{
				module.onDisable();
				if (module.getClass().isAnnotationPresent(AutoRegisterListener.class) && (module instanceof Listener))
				{
					HandlerList.unregisterAll((Listener) module);
				}
			}
		}
	}
}