Add #205 more methods in Plugin for getting resources / data folders.

This commit is contained in:
md_5 2013-03-16 11:53:25 +11:00
parent 5365e5fb92
commit f5b4e1242d
4 changed files with 49 additions and 6 deletions

View File

@ -2,6 +2,7 @@ package net.md_5.bungee.api;
import net.md_5.bungee.api.plugin.PluginManager; import net.md_5.bungee.api.plugin.PluginManager;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import java.io.File;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.util.Collection; import java.util.Collection;
import java.util.Map; import java.util.Map;
@ -221,4 +222,11 @@ public abstract class ProxyServer
* @return the console command sender of this proxy * @return the console command sender of this proxy
*/ */
public abstract CommandSender getConsole(); public abstract CommandSender getConsole();
/**
* Return the folder used to load plugins from.
*
* @return the folder used to load plugin
*/
public abstract File getPluginsFolder();
} }

View File

@ -1,6 +1,9 @@
package net.md_5.bungee.api.plugin; package net.md_5.bungee.api.plugin;
import java.io.File;
import java.io.InputStream;
import lombok.Getter; import lombok.Getter;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.config.ConfigurationAdapter; import net.md_5.bungee.api.config.ConfigurationAdapter;
/** /**
@ -12,6 +15,8 @@ public class Plugin
@Getter @Getter
private PluginDescription description; private PluginDescription description;
@Getter
private ProxyServer proxy;
/** /**
* Called when the plugin has just been loaded. Most of the proxy will not * Called when the plugin has just been loaded. Most of the proxy will not
@ -36,13 +41,38 @@ public class Plugin
{ {
} }
/**
* Gets the data folder where this plugin may store arbitrary data. It will
* be a child of {@link ProxyServer#getPluginsFolder()}.
*
* @return the data folder of this plugin
*/
public final File getDataFolder()
{
return new File( getProxy().getPluginsFolder(), getDescription().getName() );
}
/**
* Get a resource from within this plugins jar or container. Care must be
* taken to close the returned stream.
*
* @param name the full path name of this resource
* @return the stream for getting this resource, or null if it does not
* exist
*/
public final InputStream getResourceAsStream(String name)
{
return getClass().getClassLoader().getResourceAsStream( name );
}
/** /**
* Called by the loader to initialize the fields in this plugin. * Called by the loader to initialize the fields in this plugin.
* *
* @param description the description that describes this plugin * @param description the description that describes this plugin
*/ */
final void init(PluginDescription description) final void init(ProxyServer proxy, PluginDescription description)
{ {
this.proxy = proxy;
this.description = description; this.description = description;
} }
} }

View File

@ -15,6 +15,7 @@ import java.util.jar.JarEntry;
import java.util.jar.JarFile; import java.util.jar.JarFile;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import lombok.RequiredArgsConstructor;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.CommandSender; import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.ProxyServer;
@ -24,11 +25,14 @@ import org.yaml.snakeyaml.Yaml;
* Class to manage bridging between plugin duties and implementation duties, for * Class to manage bridging between plugin duties and implementation duties, for
* example event handling and plugin management. * example event handling and plugin management.
*/ */
@RequiredArgsConstructor
public class PluginManager public class PluginManager
{ {
private static final Pattern argsSplit = Pattern.compile( " " ); private static final Pattern argsSplit = Pattern.compile( " " );
/*========================================================================*/ /*========================================================================*/
private final ProxyServer proxy;
/*========================================================================*/
private final Yaml yaml = new Yaml(); private final Yaml yaml = new Yaml();
private final EventBus eventBus = new EventBus(); private final EventBus eventBus = new EventBus();
private final Map<String, Plugin> plugins = new HashMap<>(); private final Map<String, Plugin> plugins = new HashMap<>();
@ -176,7 +180,7 @@ public class PluginManager
Class<?> main = loader.loadClass( desc.getMain() ); Class<?> main = loader.loadClass( desc.getMain() );
Plugin plugin = (Plugin) main.getDeclaredConstructor().newInstance(); Plugin plugin = (Plugin) main.getDeclaredConstructor().newInstance();
plugin.init( desc ); plugin.init( proxy, desc );
plugins.put( desc.getName(), plugin ); plugins.put( desc.getName(), plugin );
plugin.onLoad(); plugin.onLoad();
ProxyServer.getInstance().getLogger().log( Level.INFO, "Loaded plugin {0} version {1} by {2}", new Object[] ProxyServer.getInstance().getLogger().log( Level.INFO, "Loaded plugin {0} version {1} by {2}", new Object[]

View File

@ -92,7 +92,7 @@ public class BungeeCord extends ProxyServer
* Plugin manager. * Plugin manager.
*/ */
@Getter @Getter
public final PluginManager pluginManager = new PluginManager(); public final PluginManager pluginManager = new PluginManager(this);
@Getter @Getter
@Setter @Setter
private ReconnectHandler reconnectHandler; private ReconnectHandler reconnectHandler;
@ -100,6 +100,8 @@ public class BungeeCord extends ProxyServer
@Setter @Setter
private ConfigurationAdapter configurationAdapter = new YamlConfig(); private ConfigurationAdapter configurationAdapter = new YamlConfig();
private final Collection<String> pluginChannels = new HashSet<>(); private final Collection<String> pluginChannels = new HashSet<>();
@Getter
private final File pluginsFolder = new File( "plugins" );
{ {
@ -168,9 +170,8 @@ public class BungeeCord extends ProxyServer
@Override @Override
public void start() throws IOException public void start() throws IOException
{ {
File plugins = new File( "plugins" ); pluginsFolder.mkdir();
plugins.mkdir(); pluginManager.loadPlugins( pluginsFolder );
pluginManager.loadPlugins( plugins );
config.load(); config.load();
if ( reconnectHandler == null ) if ( reconnectHandler == null )
{ {