From 7a137b7e347070d10a55f4b7fe5901f4db84960f Mon Sep 17 00:00:00 2001 From: md_5 Date: Sat, 12 Jan 2013 12:29:22 +1100 Subject: [PATCH] Add API to allow overriding of the configuration storage system. --- .../java/net/md_5/bungee/api/ProxyServer.java | 10 ++++ .../api/config/ConfigurationAdapter.java | 57 +++++++++++++++++++ .../md_5/bungee/api/config/ListenerInfo.java | 26 +++++++++ .../md_5/bungee/api/config/ServerInfo.java | 27 +++++++++ .../md_5/bungee/api/connection/Server.java | 14 ++++- .../net/md_5/bungee/api/plugin/Plugin.java | 10 ++++ .../md_5/bungee/api/plugin/PluginManager.java | 26 +++++++-- 7 files changed, 165 insertions(+), 5 deletions(-) create mode 100644 api/src/main/java/net/md_5/bungee/api/config/ConfigurationAdapter.java create mode 100644 api/src/main/java/net/md_5/bungee/api/config/ListenerInfo.java create mode 100644 api/src/main/java/net/md_5/bungee/api/config/ServerInfo.java diff --git a/api/src/main/java/net/md_5/bungee/api/ProxyServer.java b/api/src/main/java/net/md_5/bungee/api/ProxyServer.java index d6145f91..0f38f5b2 100644 --- a/api/src/main/java/net/md_5/bungee/api/ProxyServer.java +++ b/api/src/main/java/net/md_5/bungee/api/ProxyServer.java @@ -6,6 +6,8 @@ import com.google.common.base.Preconditions; import java.util.Collection; import java.util.logging.Logger; import lombok.Getter; +import net.md_5.bungee.api.config.ConfigurationAdapter; +import net.md_5.bungee.api.plugin.Plugin; public abstract class ProxyServer { @@ -63,4 +65,12 @@ public abstract class ProxyServer * @return the plugin manager */ public abstract PluginManager getPluginManager(); + + /** + * Set the configuration adapter to be used. Must be called from + * {@link Plugin#onLoad()}. + * + * @param adapter the adapter to use + */ + public abstract void setConfigurationAdapter(ConfigurationAdapter adapter); } diff --git a/api/src/main/java/net/md_5/bungee/api/config/ConfigurationAdapter.java b/api/src/main/java/net/md_5/bungee/api/config/ConfigurationAdapter.java new file mode 100644 index 00000000..dc5f6fd8 --- /dev/null +++ b/api/src/main/java/net/md_5/bungee/api/config/ConfigurationAdapter.java @@ -0,0 +1,57 @@ +package net.md_5.bungee.api.config; + +import java.util.List; +import net.md_5.bungee.api.CommandSender; + +/** + * This class allows plugins to set their own configuration adapter to load + * settings from a different place. + */ +public interface ConfigurationAdapter +{ + + /** + * Gets an integer from the specified path. + * + * @param path the path to retrieve the integer from + * @return the retrieved integer + */ + public int getInt(String path); + + /** + * Gets a string from the specified path. + * + * @param path the path to retrieve the string from. + * @return the retrieved string + */ + public String getString(String path); + + /** + * Get a string list from the specified path. + * + * @param path the path to retrieve the list from. + * @return the retrieved list. + */ + public List getStringList(String path); + + /** + * Get the configuration all servers which may be accessible via the proxy. + * + * @return all accessible servers + */ + public List getServers(); + + /** + * Get information about all hosts to bind the proxy to. + * + * @return a list of all hosts to bind to + */ + public List getListeners(); + + /** + * Set the permissions of the specified {@link CommandSender} + * + * @param sender the sender to set permissions on. + */ + public void setPermissions(CommandSender sender); +} diff --git a/api/src/main/java/net/md_5/bungee/api/config/ListenerInfo.java b/api/src/main/java/net/md_5/bungee/api/config/ListenerInfo.java new file mode 100644 index 00000000..47e29389 --- /dev/null +++ b/api/src/main/java/net/md_5/bungee/api/config/ListenerInfo.java @@ -0,0 +1,26 @@ +package net.md_5.bungee.api.config; + +import java.net.InetSocketAddress; +import lombok.Data; + +/** + * Class representing the configuration of a server listener. Used for allowing + * multiple listeners on different ports. + */ +@Data +public class ListenerInfo +{ + + /** + * Host to bind to. + */ + private final InetSocketAddress host; + /** + * Displayed motd. + */ + private final String motd; + /** + * Max amount of slots displayed on the ping page. + */ + private final int maxPlayers; +} diff --git a/api/src/main/java/net/md_5/bungee/api/config/ServerInfo.java b/api/src/main/java/net/md_5/bungee/api/config/ServerInfo.java new file mode 100644 index 00000000..65aee741 --- /dev/null +++ b/api/src/main/java/net/md_5/bungee/api/config/ServerInfo.java @@ -0,0 +1,27 @@ +package net.md_5.bungee.api.config; + +import java.net.InetSocketAddress; +import lombok.AllArgsConstructor; +import lombok.Data; + +/** + * Class used to represent a server to connect to. + */ +@Data +@AllArgsConstructor +public class ServerInfo +{ + + /** + * Name this server displays as. + */ + private final String name; + /** + * Connectable address of this server. + */ + private final InetSocketAddress address; + /** + * Permission node required to access this server. + */ + private String permission; +} diff --git a/api/src/main/java/net/md_5/bungee/api/connection/Server.java b/api/src/main/java/net/md_5/bungee/api/connection/Server.java index 6e746f04..268f63e8 100644 --- a/api/src/main/java/net/md_5/bungee/api/connection/Server.java +++ b/api/src/main/java/net/md_5/bungee/api/connection/Server.java @@ -1,8 +1,10 @@ package net.md_5.bungee.api.connection; +import java.net.InetSocketAddress; import lombok.Getter; import lombok.RequiredArgsConstructor; import net.md_5.bungee.api.Callback; +import net.md_5.bungee.api.config.ServerInfo; import net.md_5.bungee.api.ServerPing; /** @@ -12,8 +14,18 @@ import net.md_5.bungee.api.ServerPing; public abstract class Server implements Connection { + /** + * Information about the address, name and configuration regarding this + * server. + */ @Getter - private final String name; + private final ServerInfo info; + + @Override + public InetSocketAddress getAddress() + { + return info.getAddress(); + } /** * Send data by any available means to this server. diff --git a/api/src/main/java/net/md_5/bungee/api/plugin/Plugin.java b/api/src/main/java/net/md_5/bungee/api/plugin/Plugin.java index 5d50ef98..ce315855 100644 --- a/api/src/main/java/net/md_5/bungee/api/plugin/Plugin.java +++ b/api/src/main/java/net/md_5/bungee/api/plugin/Plugin.java @@ -1,6 +1,7 @@ package net.md_5.bungee.api.plugin; import lombok.Getter; +import net.md_5.bungee.api.config.ConfigurationAdapter; /** * Represents any Plugin that may be loaded at runtime to enhance existing @@ -12,6 +13,15 @@ public class Plugin @Getter private PluginDescription description; + /** + * Called when the plugin has just been loaded. Most of the proxy will not + * be initialized, so only use it for registering + * {@link ConfigurationAdapter}'s and other predefined behavior. + */ + public void onLoad() + { + } + /** * Called when this plugin is enabled. */ diff --git a/api/src/main/java/net/md_5/bungee/api/plugin/PluginManager.java b/api/src/main/java/net/md_5/bungee/api/plugin/PluginManager.java index 89ec5aa0..0f16347d 100644 --- a/api/src/main/java/net/md_5/bungee/api/plugin/PluginManager.java +++ b/api/src/main/java/net/md_5/bungee/api/plugin/PluginManager.java @@ -115,14 +115,32 @@ public class PluginManager return plugins.get(name); } + /** + * Enable all plugins by calling the {@link Plugin#onEnable()} method. + */ + public void enablePlugins() + { + for (Map.Entry plugin : plugins.entrySet()) + { + try + { + plugin.getValue().onEnable(); + } catch (Exception ex) + { + ProxyServer.getInstance().getLogger().log(Level.WARNING, "Exception encountered when loading plugin: " + plugin.getKey(), ex); + } + } + } + /** * Load a plugin from the specified file. This file must be in jar format. + * This will not enable plugins, {@link #enablePlugins()} must be called. * * @param file the file to load from * @throws Exception Any exceptions encountered when loading a plugin from * this file. */ - public void load(File file) throws Exception + public void loadPlugin(File file) throws Exception { Preconditions.checkNotNull(file, "file"); Preconditions.checkArgument(file.isFile(), "Must load from file"); @@ -142,7 +160,7 @@ public class PluginManager plugin.init(desc); plugins.put(pdf.getName(), plugin); - plugin.onEnable(); + plugin.onLoad(); } } } @@ -152,7 +170,7 @@ public class PluginManager * * @param folder the folder to search for plugins in */ - public void loadAll(File folder) + public void loadPlugins(File folder) { Preconditions.checkNotNull(folder, "folder"); Preconditions.checkArgument(folder.isDirectory(), "Must load from a directory"); @@ -163,7 +181,7 @@ public class PluginManager { try { - load(file); + loadPlugin(file); } catch (Exception ex) { ProxyServer.getInstance().getLogger().log(Level.WARNING, "Could not load plugin from file " + file, ex);