-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
483 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package dev.teraprath.lobby; | ||
|
||
import dev.teraprath.lobby.module.ModuleManager; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.event.Listener; | ||
import org.bukkit.plugin.java.JavaPlugin; | ||
|
||
public final class Lobby extends JavaPlugin { | ||
|
||
private static Lobby instance; | ||
private static ModuleManager manager; | ||
|
||
@Override | ||
public void onLoad() { | ||
|
||
instance = this; | ||
manager = new ModuleManager(); | ||
|
||
} | ||
|
||
public static void registerListener(Listener listener){ | ||
if (instance != null) { | ||
Bukkit.getPluginManager().registerEvents(listener,getInstance()); | ||
} | ||
} | ||
|
||
|
||
@Override | ||
public void onEnable() { | ||
} | ||
|
||
@Override | ||
public void onDisable() { | ||
manager.disableModules(); | ||
} | ||
|
||
public static Lobby getInstance() { | ||
return instance; | ||
} | ||
|
||
public static ModuleManager getManager() { | ||
return manager; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package dev.teraprath.lobby.module; | ||
|
||
public interface IModule { | ||
|
||
void onLoad(); | ||
|
||
void onEnable(); | ||
|
||
void onDisable(); | ||
|
||
boolean getEnabled(); | ||
} |
120 changes: 120 additions & 0 deletions
120
src/main/java/dev/teraprath/lobby/module/ModuleClassLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package dev.teraprath.lobby.module; | ||
|
||
import dev.teraprath.lobby.Lobby; | ||
import org.bukkit.Bukkit; | ||
|
||
import java.io.File; | ||
import java.lang.reflect.Method; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.net.URLClassLoader; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.logging.Level; | ||
|
||
public class ModuleClassLoader extends URLClassLoader { | ||
|
||
private static List<String> blacklistPackageName = Arrays.asList("org.bukkit.", "net.minecraft."); | ||
|
||
static { | ||
try { | ||
Method method = ClassLoader.class.getDeclaredMethod("registerAsParallelCapable"); | ||
if (method != null) { | ||
boolean oldAccessible = method.isAccessible(); | ||
method.setAccessible(true); | ||
method.invoke(null); | ||
method.setAccessible(oldAccessible); | ||
Bukkit.getLogger().log(Level.INFO, "Set QuestClassLoader as parallel capable"); | ||
} | ||
} catch (NoSuchMethodException ignored) { | ||
} catch (Exception var3) { | ||
Bukkit.getLogger().log(Level.WARNING, "Error setting QuestClassLoader as parallel capable", var3); | ||
} | ||
} | ||
|
||
private final ConcurrentHashMap<String, Class<?>> classes = new ConcurrentHashMap<>(); | ||
private Class<? extends IModule> moduleMainClass; | ||
private File file; | ||
private ModuleDescriptionFile description; | ||
private ModuleLoader loader; | ||
private boolean enabled; | ||
|
||
@SuppressWarnings("unchecked") | ||
ModuleClassLoader(ModuleLoader loader, ClassLoader parent, ModuleDescriptionFile description, File file) throws MalformedURLException, MalformedURLException { | ||
super(new URL[]{file.toURI().toURL()}, parent); | ||
// Validate.notNull(loader, "Loader can not be null"); | ||
this.file = file; | ||
this.description = description; | ||
this.loader = loader; | ||
|
||
Class mainClass; | ||
|
||
try { | ||
mainClass = Class.forName(description.getMain(), true, this); | ||
} catch (ClassNotFoundException e) { | ||
Lobby.getInstance().getLogger().severe("Cannot find main class `" + description.getMain() + "`"); | ||
return; | ||
} | ||
|
||
if (IModule.class.isAssignableFrom(mainClass)) { | ||
this.moduleMainClass = (Class<? extends IModule>) mainClass; | ||
} else { | ||
Lobby.getInstance().getLogger().severe("Declared main class `" + description.getMain() + "` does not implement from class IModule"); | ||
} | ||
} | ||
|
||
@Override | ||
protected Class<?> findClass(String name) throws ClassNotFoundException { | ||
return findClass(name, true); | ||
} | ||
|
||
private boolean isClassNameBlacklist(String name) { | ||
for (String str : blacklistPackageName) { | ||
if (name.startsWith(str)) | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
Class<?> findClass(String name, boolean checkGlobal) throws ClassNotFoundException { | ||
if (!isClassNameBlacklist(name)) { | ||
Class<?> result = this.classes.get(name); | ||
if (result == null) { | ||
if (checkGlobal) { | ||
result = this.loader.getClassByName(name); | ||
} | ||
|
||
if (result == null) { | ||
result = super.findClass(name); | ||
if (result != null) { | ||
this.loader.setClass(name, result); | ||
} | ||
} | ||
this.classes.put(name, result); | ||
} | ||
|
||
return result; | ||
} else { | ||
throw new ClassNotFoundException(name); | ||
} | ||
} | ||
|
||
public void clearClasses() { | ||
classes.clear(); | ||
} | ||
|
||
public Set<String> getClasses() { | ||
return classes.keySet(); | ||
} | ||
|
||
public IModule getModule() { | ||
try { | ||
return moduleMainClass.newInstance(); | ||
} catch (InstantiationException | IllegalAccessException e) { | ||
e.printStackTrace(); | ||
} | ||
return null; | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
src/main/java/dev/teraprath/lobby/module/ModuleDescriptionFile.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package dev.teraprath.lobby.module; | ||
|
||
import org.bukkit.plugin.InvalidDescriptionException; | ||
import org.bukkit.plugin.PluginAwareness; | ||
import org.yaml.snakeyaml.Yaml; | ||
import org.yaml.snakeyaml.constructor.AbstractConstruct; | ||
import org.yaml.snakeyaml.constructor.SafeConstructor; | ||
import org.yaml.snakeyaml.nodes.Node; | ||
import org.yaml.snakeyaml.nodes.Tag; | ||
|
||
import java.io.InputStream; | ||
import java.util.Map; | ||
|
||
public class ModuleDescriptionFile { | ||
|
||
private static final ThreadLocal<Yaml> YAML = new ThreadLocal<Yaml>() { | ||
protected Yaml initialValue() { | ||
return new Yaml(new SafeConstructor() { | ||
{ | ||
this.yamlConstructors.put(null, new AbstractConstruct() { | ||
public Object construct(final Node node) { | ||
return !node.getTag().startsWith("!@") ? SafeConstructor.undefinedConstructor.construct(node) : new PluginAwareness() { | ||
public String toString() { | ||
return node.toString(); | ||
} | ||
}; | ||
} | ||
}); | ||
PluginAwareness.Flags[] var2; | ||
int var3 = (var2 = PluginAwareness.Flags.values()).length; | ||
|
||
for (int var4 = 0; var4 < var3; ++var4) { | ||
final PluginAwareness.Flags flag = var2[var4]; | ||
this.yamlConstructors.put(new Tag("!@" + flag.name()), new AbstractConstruct() { | ||
public PluginAwareness.Flags construct(Node node) { | ||
return flag; | ||
} | ||
}); | ||
} | ||
} | ||
}); | ||
} | ||
}; | ||
|
||
private String main; | ||
private String author; | ||
private String name; | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getAuthor() { | ||
return author; | ||
} | ||
|
||
public String getMain() { | ||
return main; | ||
} | ||
|
||
public ModuleDescriptionFile(InputStream stream) throws InvalidDescriptionException { | ||
this.loadMap(this.asMap(YAML.get().load(stream))); | ||
} | ||
|
||
private void loadMap(Map<?, ?> map) throws InvalidDescriptionException { | ||
|
||
try { | ||
this.main = map.get("main").toString(); | ||
} catch (NullPointerException e) { | ||
throw new InvalidDescriptionException("main is not defined"); | ||
} catch (ClassCastException var18) { | ||
throw new InvalidDescriptionException(var18, "main is of wrong type"); | ||
} | ||
|
||
try { | ||
this.author = map.get("author").toString(); | ||
} catch (NullPointerException e) { | ||
throw new InvalidDescriptionException("author is not defined"); | ||
} catch (ClassCastException var18) { | ||
throw new InvalidDescriptionException(var18, "author is of wrong type"); | ||
} | ||
|
||
try { | ||
this.name = map.get("name").toString(); | ||
} catch (NullPointerException e) { | ||
throw new InvalidDescriptionException("name is not defined"); | ||
} catch (ClassCastException var18) { | ||
throw new InvalidDescriptionException(var18, "name is of wrong type"); | ||
} | ||
} | ||
|
||
private Map<?, ?> asMap(Object object) throws InvalidDescriptionException { | ||
if (object instanceof Map) { | ||
return (Map) object; | ||
} else { | ||
throw new InvalidDescriptionException(object + " is not properly structured."); | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.