diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/org/bukkit/Bukkit.java | 2 | ||||
-rw-r--r-- | src/main/java/org/bukkit/Server.java | 8 | ||||
-rw-r--r-- | src/main/java/org/bukkit/plugin/Plugin.java | 8 | ||||
-rw-r--r-- | src/main/java/org/bukkit/plugin/PluginDescriptionFile.java | 18 | ||||
-rw-r--r-- | src/main/java/org/bukkit/plugin/java/JavaPlugin.java | 42 |
5 files changed, 77 insertions, 1 deletions
diff --git a/src/main/java/org/bukkit/Bukkit.java b/src/main/java/org/bukkit/Bukkit.java index 18c03f36..1f2c42c9 100644 --- a/src/main/java/org/bukkit/Bukkit.java +++ b/src/main/java/org/bukkit/Bukkit.java @@ -31,7 +31,7 @@ public final class Bukkit { */ public static void setServer(Server server) { if (Bukkit.server != null) { - throw new UnsupportedOperationException("Cannot redelcare singleton Server"); + throw new UnsupportedOperationException("Cannot redefine singleton Server"); } Bukkit.server = server; diff --git a/src/main/java/org/bukkit/Server.java b/src/main/java/org/bukkit/Server.java index b9cdbd82..be0bd521 100644 --- a/src/main/java/org/bukkit/Server.java +++ b/src/main/java/org/bukkit/Server.java @@ -1,6 +1,7 @@ package org.bukkit; +import com.avaje.ebean.config.ServerConfig; import org.bukkit.entity.Player; import java.util.List; import java.util.logging.Logger; @@ -186,4 +187,11 @@ public interface Server { * @throws CommandException Thrown when the executor for the given command fails with an unhandled exception */ public boolean dispatchCommand(CommandSender sender, String commandLine); + + /** + * Populates a given {@link ServerConfig} with values attributes to this server + * + * @param config ServerConfig to populate + */ + public void configureDbConfig(ServerConfig config); } diff --git a/src/main/java/org/bukkit/plugin/Plugin.java b/src/main/java/org/bukkit/plugin/Plugin.java index 11c2aa84..6c392ef4 100644 --- a/src/main/java/org/bukkit/plugin/Plugin.java +++ b/src/main/java/org/bukkit/plugin/Plugin.java @@ -1,6 +1,7 @@ package org.bukkit.plugin; +import com.avaje.ebean.EbeanServer; import java.io.File; import org.bukkit.Server; import org.bukkit.command.CommandExecutor; @@ -80,4 +81,11 @@ public interface Plugin extends CommandExecutor { * @param canNag is this plugin still naggable? */ public void setNaggable(boolean canNag); + + /** + * Gets the {@link EbeanServer} tied to this plugin + * + * @return Ebean server instance + */ + public EbeanServer getDatabase(); } diff --git a/src/main/java/org/bukkit/plugin/PluginDescriptionFile.java b/src/main/java/org/bukkit/plugin/PluginDescriptionFile.java index 6f6d5c4a..cc6d893c 100644 --- a/src/main/java/org/bukkit/plugin/PluginDescriptionFile.java +++ b/src/main/java/org/bukkit/plugin/PluginDescriptionFile.java @@ -23,6 +23,7 @@ public final class PluginDescriptionFile { private String description = null; private ArrayList<String> authors = new ArrayList<String>(); private String website = null; + private boolean database = false; @SuppressWarnings("unchecked") public PluginDescriptionFile(final InputStream stream) throws InvalidDescriptionException { @@ -120,6 +121,14 @@ public final class PluginDescriptionFile { return website; } + public boolean isDatabaseEnabled() { + return database; + } + + public void setDatabaseEnabled(boolean database) { + this.database = database; + } + private void loadMap(Map<String, Object> map) throws InvalidDescriptionException { try { name = map.get("name").toString(); @@ -164,6 +173,14 @@ public final class PluginDescriptionFile { } } + if (map.containsKey("database")) { + try { + database = (Boolean)map.get("database"); + } catch (ClassCastException ex) { + throw new InvalidDescriptionException(ex, "database is of wrong type"); + } + } + if (map.containsKey("website")) { try { website = (String)map.get("website"); @@ -204,6 +221,7 @@ public final class PluginDescriptionFile { map.put("name", name); map.put("main", main); map.put("version", version); + map.put("database", database); if (commands != null) map.put("command", commands); if (depend != null) map.put("depend", depend); diff --git a/src/main/java/org/bukkit/plugin/java/JavaPlugin.java b/src/main/java/org/bukkit/plugin/java/JavaPlugin.java index f33ddb78..8f224a33 100644 --- a/src/main/java/org/bukkit/plugin/java/JavaPlugin.java +++ b/src/main/java/org/bukkit/plugin/java/JavaPlugin.java @@ -1,7 +1,13 @@ package org.bukkit.plugin.java; +import com.avaje.ebean.EbeanServer; +import com.avaje.ebean.EbeanServerFactory; +import com.avaje.ebean.config.DataSourceConfig; +import com.avaje.ebean.config.ServerConfig; import java.io.File; +import java.util.ArrayList; +import java.util.List; import org.bukkit.Server; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -25,6 +31,7 @@ public abstract class JavaPlugin implements Plugin { private ClassLoader classLoader = null; private Configuration config = null; private boolean naggable = true; + private EbeanServer ebean = null; public JavaPlugin() { } @@ -147,10 +154,42 @@ public abstract class JavaPlugin implements Plugin { this.classLoader = classLoader; this.config = new Configuration(new File(dataFolder, "config.yml")); this.config.load(); + + if (description.isDatabaseEnabled()) { + ServerConfig db = new ServerConfig(); + + db.setDefaultServer(false); + db.setRegister(false); + db.setClasses(getDatabaseClasses()); + db.setName(description.getName()); + server.configureDbConfig(db); + + DataSourceConfig ds = db.getDataSourceConfig(); + ds.setUrl(replaceDatabaseString(ds.getUrl())); + getDataFolder().mkdirs(); + + ClassLoader previous = Thread.currentThread().getContextClassLoader(); + Thread.currentThread().setContextClassLoader(classLoader); + ebean = EbeanServerFactory.create(db); + Thread.currentThread().setContextClassLoader(previous); + } } } /** + * Provides a list of all classes that should be persisted in the database + * + * @return List of Classes that are Ebeans + */ + public List<Class<?>> getDatabaseClasses() { + return new ArrayList<Class<?>>(); + } + + private String replaceDatabaseString(String input) { + return input.replaceAll("\\{DIR\\}", getDataFolder().getPath().replaceAll("\\\\", "/")); + } + + /** * Gets the initialization status of this plugin * * @return true if this plugin is initialized, otherwise false @@ -199,4 +238,7 @@ public abstract class JavaPlugin implements Plugin { this.naggable = canNag;; } + public EbeanServer getDatabase() { + return ebean; + } } |