This commit is contained in:
LOOHP
2021-04-06 21:14:05 +08:00
parent 05cfdb73a9
commit 6d14314bd9
126 changed files with 191394 additions and 0 deletions
@@ -0,0 +1,54 @@
package com.loohp.limbo.plugins;
import java.io.File;
import com.loohp.limbo.Limbo;
import com.loohp.limbo.file.FileConfiguration;
public class LimboPlugin {
private String name;
private File dataFolder;
private PluginInfo info;
private File pluginJar;
protected final void setInfo(FileConfiguration file, File pluginJar) {
this.info = new PluginInfo(file);
this.name = info.getName();
this.dataFolder = new File(Limbo.getInstance().getPluginFolder(), name);
this.pluginJar = pluginJar;
}
protected final File getPluginJar() {
return pluginJar;
}
public void onLoad() {
}
public void onEnable() {
}
public void onDisable() {
}
public final String getName() {
return name;
}
public final File getDataFolder() {
return new File(dataFolder.getAbsolutePath());
}
public final PluginInfo getInfo() {
return info;
}
public final Limbo getServer() {
return Limbo.getInstance();
}
}
@@ -0,0 +1,41 @@
package com.loohp.limbo.plugins;
import com.loohp.limbo.file.FileConfiguration;
public class PluginInfo {
private String name;
private String description;
private String author;
private String version;
private String main;
public PluginInfo(FileConfiguration file) {
name = file.get("name", String.class);
description = file.get("description", String.class) == null ? "" : file.get("description", String.class);
author = file.get("author", String.class);
version = file.get("version", String.class);
main = file.get("main", String.class);
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
public String getAuthor() {
return author;
}
public String getVersion() {
return version;
}
public String getMainClass() {
return main;
}
}
@@ -0,0 +1,139 @@
package com.loohp.limbo.plugins;
import java.io.File;
import java.io.FileInputStream;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import com.loohp.limbo.Limbo;
import com.loohp.limbo.commands.CommandExecutor;
import com.loohp.limbo.commands.CommandSender;
import com.loohp.limbo.commands.TabCompletor;
import com.loohp.limbo.file.FileConfiguration;
public class PluginManager {
private Map<String, LimboPlugin> plugins;
private List<Executor> executors;
private File pluginFolder;
public PluginManager(File pluginFolder) {
this.pluginFolder = pluginFolder;
this.executors = new ArrayList<>();
this.plugins = new LinkedHashMap<>();
}
protected void loadPlugins() {
for (File file : pluginFolder.listFiles()) {
if (file.isFile() && file.getName().endsWith(".jar")) {
boolean found = false;
try (ZipInputStream zip = new ZipInputStream(new FileInputStream(file))) {
while (true) {
ZipEntry entry = zip.getNextEntry();
if (entry == null) {
break;
}
String name = entry.getName();
if (name.endsWith("plugin.yml") || name.endsWith("limbo.yml")) {
found = true;
FileConfiguration pluginYaml = new FileConfiguration(zip);
String main = pluginYaml.get("main", String.class);
String pluginName = pluginYaml.get("name", String.class);
if (plugins.containsKey(pluginName)) {
System.err.println("Ambiguous plugin name in " + file.getName() + " with the plugin \"" + plugins.get(pluginName).getClass().getName() + "\"");
break;
}
URLClassLoader child = new URLClassLoader(new URL[] {file.toURI().toURL()}, Limbo.getInstance().getClass().getClassLoader());
Class<?> clazz = Class.forName(main, true, child);
LimboPlugin plugin = (LimboPlugin) clazz.getDeclaredConstructor().newInstance();
plugin.setInfo(pluginYaml, file);
plugins.put(plugin.getName(), plugin);
plugin.onLoad();
Limbo.getInstance().getConsole().sendMessage("Loading plugin " + file.getName() + " " + plugin.getInfo().getVersion() + " by " + plugin.getInfo().getAuthor());
break;
}
}
} catch (Exception e) {
System.err.println("Unable to load plugin \"" + file.getName() + "\"");
e.printStackTrace();
}
if (!found) {
System.err.println("Jar file " + file.getName() + " has no plugin.yml!");
}
}
}
}
public List<LimboPlugin> getPlugins() {
return new ArrayList<>(plugins.values());
}
public LimboPlugin getPlugin(String name) {
return plugins.get(name);
}
public void fireExecutors(CommandSender sender, String[] args) throws Exception {
Limbo.getInstance().getConsole().sendMessage(sender.getName() + " executed server command: /" + String.join(" ", args));
for (Executor entry : executors) {
try {
entry.executor.execute(sender, args);
} catch (Exception e) {
System.err.println("Error while passing command \"" + args[0] + "\" to the plugin \"" + entry.plugin.getName() + "\"");
e.printStackTrace();
}
}
}
public List<String> getTabOptions(CommandSender sender, String[] args) {
List<String> options = new ArrayList<>();
for (Executor entry : executors) {
if (entry.tab.isPresent()) {
try {
options.addAll(entry.tab.get().tabComplete(sender, args));
} catch (Exception e) {
System.err.println("Error while passing tab completion to the plugin \"" + entry.plugin.getName() + "\"");
e.printStackTrace();
}
}
}
return options;
}
public void registerCommands(LimboPlugin plugin, CommandExecutor executor) {
executors.add(new Executor(plugin, executor));
}
public void unregsiterAllCommands(LimboPlugin plugin) {
executors.removeIf(each -> each.plugin.equals(plugin));
}
public File getPluginFolder() {
return new File(pluginFolder.getAbsolutePath());
}
protected static class Executor {
public LimboPlugin plugin;
public CommandExecutor executor;
public Optional<TabCompletor> tab;
public Executor(LimboPlugin plugin, CommandExecutor executor) {
this.plugin = plugin;
this.executor = executor;
if (executor instanceof TabCompletor) {
this.tab = Optional.of((TabCompletor) executor);
} else {
this.tab = Optional.empty();
}
}
}
}