mirror of
https://github.com/simon987/Much-Assembly-Required.git
synced 2025-04-19 18:46:43 +00:00
Fixes #160
This commit is contained in:
parent
6c7a2f0a73
commit
de45eb1827
@ -34,6 +34,6 @@ public class CubotPlugin extends ServerPlugin {
|
|||||||
registry.registerHardware(CubotShield.class);
|
registry.registerHardware(CubotShield.class);
|
||||||
registry.registerHardware(CubotCore.class);
|
registry.registerHardware(CubotCore.class);
|
||||||
|
|
||||||
LogManager.LOGGER.info("Initialised Cubot plugin");
|
LogManager.LOGGER.info("(Cubot Plugin) Initialised Cubot plugin");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,6 @@ public class MiscHWPlugin extends ServerPlugin {
|
|||||||
registry.registerHardware(RandomNumberGenerator.class);
|
registry.registerHardware(RandomNumberGenerator.class);
|
||||||
registry.registerHardware(Clock.class);
|
registry.registerHardware(Clock.class);
|
||||||
|
|
||||||
LogManager.LOGGER.info("Initialised Misc Hardware Plugin");
|
LogManager.LOGGER.info("(Mist HW Plugin) Initialised Misc Hardware Plugin");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -44,7 +44,7 @@ public class NpcPlugin extends ServerPlugin {
|
|||||||
|
|
||||||
radioTowers = new ArrayList<>(32);
|
radioTowers = new ArrayList<>(32);
|
||||||
|
|
||||||
LogManager.LOGGER.info("Initialised NPC plugin");
|
LogManager.LOGGER.info("(NPC Plugin) Initialised NPC plugin");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ArrayList<RadioTower> getRadioTowers() {
|
public static ArrayList<RadioTower> getRadioTowers() {
|
||||||
|
@ -14,14 +14,10 @@ public class BiomassPlugin extends ServerPlugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init(ServerConfiguration config, GameRegistry registry) {
|
public void init(ServerConfiguration config, GameRegistry registry) {
|
||||||
|
|
||||||
listeners.add(new WorldCreationListener());
|
listeners.add(new WorldCreationListener());
|
||||||
listeners.add(new WorldUpdateListener(config));
|
listeners.add(new WorldUpdateListener(config));
|
||||||
|
|
||||||
if (registry.isGameObjectRegistered("net.simon987.npcplugin.HarvesterNPC")) {
|
|
||||||
listeners.add(new ObjectDeathListener(config));
|
listeners.add(new ObjectDeathListener(config));
|
||||||
} else {
|
|
||||||
LogManager.LOGGER.severe("(BiomassPlugin) NPC plugin is not loaded so biomass will not spawn on death of HarvesterNPC");
|
|
||||||
}
|
|
||||||
|
|
||||||
registry.registerGameObject(BiomassBlob.class);
|
registry.registerGameObject(BiomassBlob.class);
|
||||||
registry.registerItem(ItemBiomass.ID, ItemBiomass.class);
|
registry.registerItem(ItemBiomass.ID, ItemBiomass.class);
|
||||||
|
@ -19,7 +19,6 @@ public class ObjectDeathListener implements GameEventListener {
|
|||||||
|
|
||||||
public ObjectDeathListener(ServerConfiguration config) {
|
public ObjectDeathListener(ServerConfiguration config) {
|
||||||
biomassDropCount = config.getInt("harvester_biomass_drop_count");
|
biomassDropCount = config.getInt("harvester_biomass_drop_count");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
classpath=net.simon987.biomassplugin.BiomassPlugin
|
classpath=net.simon987.biomassplugin.BiomassPlugin
|
||||||
name=Biomass Plugin
|
name=Biomass Plugin
|
||||||
version=1.0
|
version=1.0
|
||||||
|
depend=NPC Plugin
|
@ -10,6 +10,6 @@ public class RadioactiveCloudPlugin extends ServerPlugin {
|
|||||||
@Override
|
@Override
|
||||||
public void init(ServerConfiguration config, GameRegistry registry) {
|
public void init(ServerConfiguration config, GameRegistry registry) {
|
||||||
|
|
||||||
LogManager.LOGGER.info("Initialised Radioactive cloud plugin.");
|
LogManager.LOGGER.info("(Radioactive cloud Plugin) Initialised Radioactive cloud plugin.");
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -24,7 +24,6 @@ import net.simon987.server.user.UserStatsHelper;
|
|||||||
import net.simon987.server.websocket.SocketServer;
|
import net.simon987.server.websocket.SocketServer;
|
||||||
import org.bson.Document;
|
import org.bson.Document;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public class GameServer implements Runnable {
|
public class GameServer implements Runnable {
|
||||||
@ -68,8 +67,8 @@ public class GameServer implements Runnable {
|
|||||||
|
|
||||||
gameUniverse = new GameUniverse(config);
|
gameUniverse = new GameUniverse(config);
|
||||||
gameUniverse.setMongo(mongo);
|
gameUniverse.setMongo(mongo);
|
||||||
pluginManager = new PluginManager();
|
|
||||||
gameRegistry = new GameRegistry();
|
gameRegistry = new GameRegistry();
|
||||||
|
pluginManager = new PluginManager(config, gameRegistry);
|
||||||
|
|
||||||
maxExecutionTime = config.getInt("user_timeout");
|
maxExecutionTime = config.getInt("user_timeout");
|
||||||
|
|
||||||
@ -84,21 +83,8 @@ public class GameServer implements Runnable {
|
|||||||
config.setString("secret_key", secretKey);
|
config.setString("secret_key", secretKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Load all plugins in plugins folder, if it doesn't exist, create it
|
if (!pluginManager.loadInFolder("plugins/")) {
|
||||||
File pluginDir = new File("plugins/");
|
System.exit(-1);
|
||||||
File[] pluginDirListing = pluginDir.listFiles();
|
|
||||||
|
|
||||||
if (pluginDirListing != null) {
|
|
||||||
for (File pluginFile : pluginDirListing) {
|
|
||||||
|
|
||||||
if (pluginFile.getName().endsWith(".jar")) {
|
|
||||||
pluginManager.load(pluginFile, config, gameRegistry);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (!pluginDir.mkdir()) {
|
|
||||||
LogManager.LOGGER.severe("Couldn't create plugin directory");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
eventDispatcher = new GameEventDispatcher(pluginManager);
|
eventDispatcher = new GameEventDispatcher(pluginManager);
|
||||||
|
@ -5,30 +5,40 @@ import net.simon987.server.game.objects.GameRegistry;
|
|||||||
import net.simon987.server.logging.LogManager;
|
import net.simon987.server.logging.LogManager;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.lang.reflect.Constructor;
|
import java.lang.reflect.Constructor;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLClassLoader;
|
import java.net.URLClassLoader;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
import java.util.zip.ZipEntry;
|
import java.util.zip.ZipEntry;
|
||||||
import java.util.zip.ZipFile;
|
import java.util.zip.ZipFile;
|
||||||
|
|
||||||
public class PluginManager {
|
public class PluginManager {
|
||||||
|
|
||||||
private ArrayList<ServerPlugin> plugins;
|
private ArrayList<ServerPlugin> loadedPlugins;
|
||||||
|
private ArrayList<ServerPlugin> toLoadPlugins;
|
||||||
|
private ServerConfiguration config;
|
||||||
|
private GameRegistry gameRegistry;
|
||||||
|
|
||||||
public PluginManager() {
|
public PluginManager(ServerConfiguration config, GameRegistry registry) {
|
||||||
this.plugins = new ArrayList<>(10);
|
this.config = config;
|
||||||
|
this.gameRegistry = registry;
|
||||||
|
this.toLoadPlugins = new ArrayList<>(10);
|
||||||
|
this.loadedPlugins = new ArrayList<>(10);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void load(File pluginFile, ServerConfiguration config, GameRegistry gameRegistry) {
|
private ServerPlugin load(File pluginFile) {
|
||||||
|
|
||||||
LogManager.LOGGER.info("Loading plugin file " + pluginFile.getName());
|
LogManager.LOGGER.info("Loading plugin file " + pluginFile.getName());
|
||||||
|
|
||||||
|
ZipFile zipFile = null;
|
||||||
try {
|
try {
|
||||||
//Get the plugin config file from the archive
|
//Get the plugin config file from the archive
|
||||||
ZipFile zipFile = new ZipFile(pluginFile);
|
zipFile = new ZipFile(pluginFile);
|
||||||
|
|
||||||
ZipEntry configEntry = zipFile.getEntry("plugin.properties");
|
ZipEntry configEntry = zipFile.getEntry("plugin.properties");
|
||||||
|
|
||||||
@ -38,7 +48,6 @@ public class PluginManager {
|
|||||||
Properties pluginConfig = new Properties();
|
Properties pluginConfig = new Properties();
|
||||||
pluginConfig.load(stream);
|
pluginConfig.load(stream);
|
||||||
|
|
||||||
//Load the plugin
|
|
||||||
ClassLoader loader = URLClassLoader.newInstance(new URL[]{pluginFile.toURI().toURL()});
|
ClassLoader loader = URLClassLoader.newInstance(new URL[]{pluginFile.toURI().toURL()});
|
||||||
Class<?> aClass = Class.forName(pluginConfig.getProperty("classpath"), true, loader);
|
Class<?> aClass = Class.forName(pluginConfig.getProperty("classpath"), true, loader);
|
||||||
Class<? extends ServerPlugin> pluginClass = aClass.asSubclass(ServerPlugin.class);
|
Class<? extends ServerPlugin> pluginClass = aClass.asSubclass(ServerPlugin.class);
|
||||||
@ -48,26 +57,115 @@ public class PluginManager {
|
|||||||
plugin.setName(pluginConfig.getProperty("name"));
|
plugin.setName(pluginConfig.getProperty("name"));
|
||||||
plugin.setVersion(pluginConfig.getProperty("version"));
|
plugin.setVersion(pluginConfig.getProperty("version"));
|
||||||
|
|
||||||
LogManager.LOGGER.info("Loaded " + plugin.name + " V" + plugin.version);
|
String dependStr = pluginConfig.getProperty("depend");
|
||||||
|
if (dependStr != null) {
|
||||||
|
for (String dep : dependStr.split(",")) {
|
||||||
|
plugin.dependencies.add(dep.trim());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//Add it to the list
|
return plugin;
|
||||||
plugins.add(plugin);
|
|
||||||
|
|
||||||
//Init the plugin
|
|
||||||
plugin.init(config, gameRegistry);
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
LogManager.LOGGER.severe("Couldn't find plugin.properties in " + pluginFile.getName());
|
LogManager.LOGGER.severe("Couldn't find plugin.properties in " + pluginFile.getName());
|
||||||
}
|
}
|
||||||
zipFile.close();
|
|
||||||
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
if (zipFile != null) {
|
||||||
|
zipFile.close();
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Load all plugins in plugins folder, if it doesn't exist, create it
|
||||||
|
*
|
||||||
|
* @return true if all the plugins could be loaded
|
||||||
|
*/
|
||||||
|
public boolean loadInFolder(String dir) {
|
||||||
|
|
||||||
|
File pluginDir = new File(dir);
|
||||||
|
File[] pluginDirListing = pluginDir.listFiles();
|
||||||
|
|
||||||
|
if (pluginDirListing == null) {
|
||||||
|
if (!pluginDir.mkdir()) {
|
||||||
|
LogManager.LOGGER.severe("Couldn't create plugin directory");
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (File pluginFile : pluginDirListing) {
|
||||||
|
if (pluginFile.getName().endsWith(".jar")) {
|
||||||
|
toLoadPlugins.add(load(pluginFile));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
while (toLoadPlugins.size() > 0) {
|
||||||
|
|
||||||
|
ServerPlugin plugin = toLoadPlugins.get(0);
|
||||||
|
|
||||||
|
if (!initWithDependencies(plugin)) {
|
||||||
|
LogManager.LOGGER.severe("Plugin " + plugin.name + " has unmet dependencies: " +
|
||||||
|
Arrays.toString(plugin.dependencies.toArray()));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
toLoadPlugins.clear();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean initWithDependencies(ServerPlugin plugin) {
|
||||||
|
|
||||||
|
for (String depName : plugin.dependencies) {
|
||||||
|
|
||||||
|
if (!isLoaded(depName)) {
|
||||||
|
ServerPlugin dep = getPluginByName(depName, toLoadPlugins);
|
||||||
|
|
||||||
|
if (dep != null) {
|
||||||
|
initWithDependencies(dep);
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
initPlugin(plugin);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ServerPlugin getPluginByName(String name, List<ServerPlugin> plugins) {
|
||||||
|
|
||||||
|
for (ServerPlugin p : plugins) {
|
||||||
|
if (p.name.equals(name)) {
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isLoaded(String name) {
|
||||||
|
return getPluginByName(name, loadedPlugins) != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initPlugin(ServerPlugin plugin) {
|
||||||
|
|
||||||
|
toLoadPlugins.remove(plugin);
|
||||||
|
loadedPlugins.add(plugin);
|
||||||
|
plugin.init(config, gameRegistry);
|
||||||
|
}
|
||||||
|
|
||||||
public ArrayList<ServerPlugin> getPlugins() {
|
public ArrayList<ServerPlugin> getPlugins() {
|
||||||
return plugins;
|
return loadedPlugins;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ import net.simon987.server.event.GameEventListener;
|
|||||||
import net.simon987.server.game.objects.GameRegistry;
|
import net.simon987.server.game.objects.GameRegistry;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public abstract class ServerPlugin {
|
public abstract class ServerPlugin {
|
||||||
|
|
||||||
@ -21,7 +22,9 @@ public abstract class ServerPlugin {
|
|||||||
/**
|
/**
|
||||||
* List of event listeners
|
* List of event listeners
|
||||||
*/
|
*/
|
||||||
protected ArrayList<GameEventListener> listeners = new ArrayList<>(5);
|
protected List<GameEventListener> listeners = new ArrayList<>(5);
|
||||||
|
|
||||||
|
protected List<String> dependencies = new ArrayList<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called when the plugin is loaded
|
* Called when the plugin is loaded
|
||||||
@ -44,7 +47,7 @@ public abstract class ServerPlugin {
|
|||||||
this.version = version;
|
this.version = version;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<GameEventListener> getListeners() {
|
public List<GameEventListener> getListeners() {
|
||||||
return listeners;
|
return listeners;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user