2014-11-12 19:41:40 +00:00
|
|
|
package com.lenis0012.bukkit.marriage2.internal;
|
|
|
|
|
2016-02-23 16:22:31 +00:00
|
|
|
import java.io.File;
|
2014-11-12 19:41:40 +00:00
|
|
|
import java.lang.reflect.Method;
|
2016-03-19 16:18:43 +00:00
|
|
|
import java.util.LinkedHashMap;
|
2014-11-12 19:41:40 +00:00
|
|
|
import java.util.List;
|
2016-03-19 16:18:43 +00:00
|
|
|
import java.util.Map;
|
2014-11-12 19:41:40 +00:00
|
|
|
import java.util.logging.Level;
|
|
|
|
|
2015-05-26 21:58:01 +00:00
|
|
|
import com.google.common.collect.Lists;
|
2016-03-19 16:18:43 +00:00
|
|
|
import com.google.common.collect.Maps;
|
2016-03-06 03:17:13 +00:00
|
|
|
import com.lenis0012.pluginutils.PluginHolder;
|
|
|
|
import com.lenis0012.pluginutils.modules.configuration.ConfigurationModule;
|
|
|
|
import com.lenis0012.pluginutils.modules.packets.PacketModule;
|
2014-11-12 19:41:40 +00:00
|
|
|
|
2014-11-14 19:37:31 +00:00
|
|
|
import com.lenis0012.bukkit.marriage2.Marriage;
|
2016-03-19 16:18:43 +00:00
|
|
|
import org.yaml.snakeyaml.Yaml;
|
2014-11-14 19:37:31 +00:00
|
|
|
|
2016-03-06 03:17:13 +00:00
|
|
|
public class MarriagePlugin extends PluginHolder {
|
2014-11-12 21:27:22 +00:00
|
|
|
private static MarriageCore core;
|
|
|
|
|
2016-03-06 23:09:03 +00:00
|
|
|
public static Marriage getCore() {
|
2014-11-12 21:27:22 +00:00
|
|
|
return core;
|
|
|
|
}
|
|
|
|
|
2014-11-12 19:41:40 +00:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
private final List<Method>[] methods = new List[Register.Type.values().length];
|
|
|
|
|
|
|
|
public MarriagePlugin() {
|
2016-03-06 03:17:13 +00:00
|
|
|
super(PacketModule.class,
|
|
|
|
ConfigurationModule.class);
|
2014-11-12 21:27:22 +00:00
|
|
|
core = new MarriageCore(this);
|
2014-11-12 19:41:40 +00:00
|
|
|
|
|
|
|
//Scan methods
|
2015-05-26 21:58:01 +00:00
|
|
|
for(int i = 0; i < methods.length; i++) {
|
|
|
|
methods[i] = Lists.newArrayList();
|
|
|
|
}
|
2014-11-12 19:41:40 +00:00
|
|
|
scanMethods(core.getClass());
|
|
|
|
}
|
|
|
|
|
|
|
|
private void scanMethods(Class<?> clazz) {
|
|
|
|
if(clazz == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loop through all methods in class
|
|
|
|
for(Method method : clazz.getMethods()) {
|
|
|
|
Register register = method.getAnnotation(Register.class);
|
|
|
|
if(register != null) {
|
|
|
|
methods[register.type().ordinal()].add(method);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan methods in super class
|
|
|
|
scanMethods(clazz.getSuperclass());
|
|
|
|
}
|
2016-02-23 16:22:31 +00:00
|
|
|
|
|
|
|
protected File getPluginFile() {
|
|
|
|
return getFile();
|
|
|
|
}
|
2014-11-12 19:41:40 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLoad() {
|
|
|
|
executeMethods(Register.Type.LOAD);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-03-06 03:17:13 +00:00
|
|
|
public void enable() {
|
2014-11-12 19:41:40 +00:00
|
|
|
executeMethods(Register.Type.ENABLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-03-06 03:17:13 +00:00
|
|
|
public void disable() {
|
2014-11-12 19:41:40 +00:00
|
|
|
executeMethods(Register.Type.DISABLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void executeMethods(Register.Type type) {
|
2015-05-26 21:58:01 +00:00
|
|
|
List<Method> list = Lists.newArrayList(methods[type.ordinal()]);
|
2014-11-14 19:37:31 +00:00
|
|
|
while(!list.isEmpty()) {
|
|
|
|
Method method = null;
|
|
|
|
int lowestPriority = Integer.MAX_VALUE;
|
|
|
|
for(Method m : list) {
|
2015-05-26 21:58:01 +00:00
|
|
|
Register register = m.getAnnotation(Register.class);
|
2014-11-14 19:37:31 +00:00
|
|
|
if(register.priority() < lowestPriority) {
|
|
|
|
method = m;
|
|
|
|
lowestPriority = register.priority();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(method != null) {
|
|
|
|
list.remove(method);
|
|
|
|
Register register = method.getAnnotation(Register.class);
|
|
|
|
getLogger().log(Level.INFO, "Loading " + register.name() + "...");
|
|
|
|
try {
|
|
|
|
method.invoke(core);
|
|
|
|
} catch (Exception e) {
|
|
|
|
getLogger().log(Level.SEVERE, "Failed to load " + register.name(), e);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
list.clear();
|
2014-11-12 19:41:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getLogger().log(Level.INFO, type.getCompletionMessage());
|
|
|
|
}
|
|
|
|
}
|