Started using databases, WIP

This commit is contained in:
Norbi Peti 2016-11-25 23:39:56 +01:00
parent d91da7f306
commit a9b0e98e07
5 changed files with 109 additions and 2 deletions

View file

@ -4,11 +4,16 @@ import java.util.Map.Entry;
import java.util.UUID; import java.util.UUID;
import java.util.logging.Logger; import java.util.logging.Logger;
import javax.persistence.PersistenceException;
import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import buttondevteam.lib.CPlayer;
import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.TBMCPlayer; import buttondevteam.lib.TBMCPlayer;
import buttondevteam.lib.db.CData;
import buttondevteam.lib.db.DataManager;
public class MainPlugin extends JavaPlugin { public class MainPlugin extends JavaPlugin {
public static MainPlugin Instance; public static MainPlugin Instance;
@ -23,10 +28,29 @@ public class MainPlugin extends JavaPlugin {
pdfFile = getDescription(); pdfFile = getDescription();
logger = getLogger(); 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() + ")."); logger.info(pdfFile.getName() + " has been Enabled (V." + pdfFile.getVersion() + ").");
TBMCCoreAPI.RegisterEventsForExceptions(new PlayerListener(), this); 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 @Override
public void onDisable() { public void onDisable() {
logger.info("Saving player data..."); logger.info("Saving player data...");
@ -35,5 +59,4 @@ public class MainPlugin extends JavaPlugin {
} }
logger.info("Player data saved."); logger.info("Player data saved.");
} }
} }

View file

@ -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;
}
}

View file

@ -0,0 +1,7 @@
package buttondevteam.lib.db;
import java.util.UUID;
public interface CData {
public UUID getChromaID();
}

View file

@ -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 extends CData> T load(Class<T> cl, UUID id) {
return database.find(cl, id);
}
public static <T extends CData> void save(T obj) {
database.save(obj);
}
}

View file

@ -2,3 +2,4 @@ name: ButtonCore
main: buttondevteam.core.MainPlugin main: buttondevteam.core.MainPlugin
version: 1.0 version: 1.0
author: TBMCPlugins author: TBMCPlugins
database: true