From a9b0e98e07788111dd7d7e423a99ff8eb93d3761 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Fri, 25 Nov 2016 23:39:56 +0100 Subject: [PATCH] Started using databases, WIP --- .../java/buttondevteam/core/MainPlugin.java | 25 ++++++++++- src/main/java/buttondevteam/lib/CPlayer.java | 42 +++++++++++++++++++ src/main/java/buttondevteam/lib/db/CData.java | 7 ++++ .../buttondevteam/lib/db/DataManager.java | 34 +++++++++++++++ src/main/resources/plugin.yml | 3 +- 5 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 src/main/java/buttondevteam/lib/CPlayer.java create mode 100644 src/main/java/buttondevteam/lib/db/CData.java create mode 100644 src/main/java/buttondevteam/lib/db/DataManager.java diff --git a/src/main/java/buttondevteam/core/MainPlugin.java b/src/main/java/buttondevteam/core/MainPlugin.java index c61bb6c..7de842c 100644 --- a/src/main/java/buttondevteam/core/MainPlugin.java +++ b/src/main/java/buttondevteam/core/MainPlugin.java @@ -4,11 +4,16 @@ import java.util.Map.Entry; import java.util.UUID; import java.util.logging.Logger; +import javax.persistence.PersistenceException; + import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.java.JavaPlugin; +import buttondevteam.lib.CPlayer; import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.TBMCPlayer; +import buttondevteam.lib.db.CData; +import buttondevteam.lib.db.DataManager; public class MainPlugin extends JavaPlugin { public static MainPlugin Instance; @@ -23,9 +28,28 @@ public class MainPlugin extends JavaPlugin { pdfFile = getDescription(); logger = getLogger(); + setupDatabase(); + DataManager.setDatabase(getDatabase()); + final UUID cid = UUID.randomUUID(); + final UUID mcid = UUID.randomUUID(); + System.out.println(cid); + System.out.println(mcid); + System.out.println("----"); + DataManager.save(new CPlayer(cid, mcid)); + System.out.println("----"); + System.out.println(DataManager.load(CPlayer.class, cid).getMinecraftID()); logger.info(pdfFile.getName() + " has been Enabled (V." + pdfFile.getVersion() + ")."); TBMCCoreAPI.RegisterEventsForExceptions(new PlayerListener(), this); } + + private void setupDatabase() { + try { + getDatabase().find(CPlayer.class).findRowCount(); + } catch (PersistenceException ex) { + System.out.println("Installing database for ButtonCore due to first time usage"); + installDDL(); + } + } @Override public void onDisable() { @@ -35,5 +59,4 @@ public class MainPlugin extends JavaPlugin { } logger.info("Player data saved."); } - } diff --git a/src/main/java/buttondevteam/lib/CPlayer.java b/src/main/java/buttondevteam/lib/CPlayer.java new file mode 100644 index 0000000..a0efd60 --- /dev/null +++ b/src/main/java/buttondevteam/lib/CPlayer.java @@ -0,0 +1,42 @@ +package buttondevteam.lib; + +import java.util.UUID; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import buttondevteam.lib.db.CData; + +@Entity +public class CPlayer implements CData { + public CPlayer() { + } + + public CPlayer(UUID chromaid, UUID mcid) { + id = chromaid; + this.mcid = mcid; + } + + @Id + private UUID id; + @Column(unique = true) + private UUID mcid; + + @Override + public UUID getChromaID() { + return id; + } + + public void setChromaID(UUID id) { + this.id = id; + } + + public UUID getMinecraftID() { + return mcid; + } + + public void setMinecraftID(UUID mcid) { + this.mcid = mcid; + } + +} diff --git a/src/main/java/buttondevteam/lib/db/CData.java b/src/main/java/buttondevteam/lib/db/CData.java new file mode 100644 index 0000000..53f1c1e --- /dev/null +++ b/src/main/java/buttondevteam/lib/db/CData.java @@ -0,0 +1,7 @@ +package buttondevteam.lib.db; + +import java.util.UUID; + +public interface CData { + public UUID getChromaID(); +} diff --git a/src/main/java/buttondevteam/lib/db/DataManager.java b/src/main/java/buttondevteam/lib/db/DataManager.java new file mode 100644 index 0000000..e40b7ec --- /dev/null +++ b/src/main/java/buttondevteam/lib/db/DataManager.java @@ -0,0 +1,34 @@ +package buttondevteam.lib.db; + +import java.util.UUID; + +import javax.persistence.PersistenceException; + +import org.bukkit.plugin.java.JavaPlugin; + +import com.avaje.ebean.EbeanServer; + +import buttondevteam.core.MainPlugin; + +public final class DataManager { + private static EbeanServer database; + + /** + * Can be only used once and is used by {@link MainPlugin} + * + * @param database + * The database to set + */ + public static void setDatabase(EbeanServer database) { + DataManager.database = database; + } + + public static T load(Class cl, UUID id) { + return database.find(cl, id); + } + + public static void save(T obj) { + database.save(obj); + } + +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index fff2733..ecee843 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,4 +1,5 @@ name: ButtonCore main: buttondevteam.core.MainPlugin version: 1.0 -author: TBMCPlugins \ No newline at end of file +author: TBMCPlugins +database: true \ No newline at end of file