Restructured database

This commit is contained in:
Poslovitch 2017-05-31 16:32:36 +02:00
parent aa93c11080
commit 77b3653547
20 changed files with 84 additions and 78 deletions

View File

@ -15,9 +15,9 @@ import us.tastybento.bskyblock.config.PluginConfig;
import us.tastybento.bskyblock.config.Settings;
import us.tastybento.bskyblock.database.BSBDatabase;
import us.tastybento.bskyblock.database.BSBDatabase.DatabaseType;
import us.tastybento.bskyblock.database.IslandsManager;
import us.tastybento.bskyblock.database.OfflineHistoryMessages;
import us.tastybento.bskyblock.database.PlayersManager;
import us.tastybento.bskyblock.database.managers.IslandsManager;
import us.tastybento.bskyblock.database.managers.OfflineHistoryMessages;
import us.tastybento.bskyblock.database.managers.PlayersManager;
import us.tastybento.bskyblock.database.objects.Island;
import us.tastybento.bskyblock.database.objects.Island.SettingsFlag;
import us.tastybento.bskyblock.util.VaultHelper;

View File

@ -9,7 +9,7 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffectType;
import us.tastybento.bskyblock.database.BSBDatabase.DatabaseType;
import us.tastybento.bskyblock.database.OfflineHistoryMessages.HistoryMessageType;
import us.tastybento.bskyblock.database.managers.OfflineHistoryMessages.HistoryMessageType;
/**
* All the plugin settings are here

View File

@ -3,6 +3,7 @@ package us.tastybento.bskyblock.database;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.config.Settings;
import us.tastybento.bskyblock.database.flatfile.FlatFileDatabase;
import us.tastybento.bskyblock.database.managers.AbstractDatabaseHandler;
import us.tastybento.bskyblock.database.mysql.MySQLDatabase;
import us.tastybento.bskyblock.database.sqlite.SQLiteDatabase;

View File

@ -48,7 +48,6 @@ public interface DatabaseConnecter {
* @param tableName - analogous to a table in a database
* @param fileName - the name of the record. Must be unique.
*/
public void saveYamlFile(YamlConfiguration yamlFile, String tableName,
String fileName);
public void saveYamlFile(YamlConfiguration yamlFile, String tableName, String fileName);
}

View File

@ -3,12 +3,12 @@ package us.tastybento.bskyblock.database;
import us.tastybento.bskyblock.config.Settings;
public class DatabaseConnectionSettingsImpl {
private String host;
private int port;
private String databaseName;
private String username;
private String password;
/**
* Hosts database settings
* @param host
@ -17,14 +17,14 @@ public class DatabaseConnectionSettingsImpl {
* @param username
* @param password
*/
public DatabaseConnectionSettingsImpl(String host, int port,
String databaseName, String username, String password) {
public DatabaseConnectionSettingsImpl(String host, int port, String databaseName, String username, String password) {
this.host = host;
this.port = port;
this.databaseName = databaseName;
this.username = username;
this.password = password;
}
public DatabaseConnectionSettingsImpl() {
this.host = Settings.dbHost;
this.port = Settings.dbPort;
@ -32,60 +32,70 @@ public class DatabaseConnectionSettingsImpl {
this.username = Settings.dbUsername;
this.password = Settings.dbPassword;
}
/**
* @return the host
*/
public String getHost() {
return host;
}
/**
* @param host the host to set
*/
public void setHost(String host) {
this.host = host;
}
/**
* @return the port
*/
public int getPort() {
return port;
}
/**
* @param port the port to set
*/
public void setPort(int port) {
this.port = port;
}
/**
* @return the databaseName
*/
public String getDatabaseName() {
return databaseName;
}
/**
* @param databaseName the databaseName to set
*/
public void setDatabaseName(String databaseName) {
this.databaseName = databaseName;
}
/**
* @return the username
*/
public String getUsername() {
return username;
}
/**
* @param username the username to set
*/
public void setUsername(String username) {
this.username = username;
}
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/

View File

@ -1,8 +1,8 @@
package us.tastybento.bskyblock.database.flatfile;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.database.AbstractDatabaseHandler;
import us.tastybento.bskyblock.database.BSBDatabase;
import us.tastybento.bskyblock.database.managers.AbstractDatabaseHandler;
import us.tastybento.bskyblock.database.objects.Island;
public class FlatFileDatabase extends BSBDatabase{

View File

@ -17,8 +17,8 @@ import java.util.UUID;
import org.bukkit.configuration.file.YamlConfiguration;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.database.AbstractDatabaseHandler;
import us.tastybento.bskyblock.database.DatabaseConnecter;
import us.tastybento.bskyblock.database.managers.AbstractDatabaseHandler;
/**

View File

@ -1,4 +1,4 @@
package us.tastybento.bskyblock.database;
package us.tastybento.bskyblock.database.managers;
import java.beans.IntrospectionException;
import java.lang.reflect.Field;
@ -7,6 +7,7 @@ import java.sql.SQLException;
import java.util.List;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.database.DatabaseConnecter;
/**
* An abstract class that handles insert/select-operations into/from a database

View File

@ -1,4 +1,4 @@
package us.tastybento.bskyblock.database;
package us.tastybento.bskyblock.database.managers;
import java.util.HashMap;
import java.util.HashSet;
@ -11,6 +11,7 @@ import org.bukkit.Location;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.config.Settings;
import us.tastybento.bskyblock.database.BSBDatabase;
import us.tastybento.bskyblock.database.objects.Island;
/**
@ -22,12 +23,12 @@ public class IslandsManager {
private BSkyBlock plugin;
private BSBDatabase database;
private HashMap<Location, Island> islands;
private HashMap<UUID, Island> islandsByUUID;
// 2D islandGrid of islands, x,z
private TreeMap<Integer, TreeMap<Integer, Island>> islandGrid = new TreeMap<Integer, TreeMap<Integer, Island>>();
/**
* One island can be spawn, this is the one - otherwise, this value is null
*/
@ -36,7 +37,7 @@ public class IslandsManager {
// Metrics data
private int metrics_createdcount = 0;
private AbstractDatabaseHandler<Island> handler;
@SuppressWarnings("unchecked")
public IslandsManager(BSkyBlock plugin){
this.plugin = plugin;
@ -48,17 +49,17 @@ public class IslandsManager {
}
public void load(){
try {
for (Object island : handler.selectObjects()) {
if (island instanceof Island) {
islands.put(((Island)island).getCenter(), (Island)island);
islandsByUUID.put(((Island)island).getOwner(), (Island)island);
}
try {
for (Object island : handler.selectObjects()) {
if (island instanceof Island) {
islands.put(((Island)island).getCenter(), (Island)island);
islandsByUUID.put(((Island)island).getOwner(), (Island)island);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void save(boolean async){
@ -135,7 +136,7 @@ public class IslandsManager {
islandsByUUID.put(owner, island);
return island;
}
public void deleteIsland(Location location){
//TODO
}
@ -188,9 +189,9 @@ public class IslandsManager {
// Player already on island
return true;
}
// TODO: Fire a join team event. If canceled, return false
if (!setLeaveTeam(playerUUID)) {
// Player not allowed to leave team
return false;
@ -202,7 +203,7 @@ public class IslandsManager {
/**
* Called when a player leaves a team
* @param playerUUID
* @param playerUUID
* @return true if successful, false if not
*/
public boolean setLeaveTeam(UUID playerUUID) {
@ -283,7 +284,7 @@ public class IslandsManager {
return null;
}
/**
* Determines if a location is in the island world or not or
* in the new nether if it is activated
@ -315,7 +316,7 @@ public class IslandsManager {
return getIsland(playerUUID).getCenter();
return null;
}
/**
* @param playerUUID
* @return ban list for player

View File

@ -1,4 +1,4 @@
package us.tastybento.bskyblock.database;
package us.tastybento.bskyblock.database.managers;
import java.util.ArrayList;
import java.util.Arrays;
@ -10,6 +10,7 @@ import org.bukkit.entity.Player;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.config.Settings;
import us.tastybento.bskyblock.database.BSBDatabase;
/**
* Handles offline messaging to players and teams

View File

@ -1,7 +1,6 @@
package us.tastybento.bskyblock.database;
package us.tastybento.bskyblock.database.managers;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.UUID;
import org.bukkit.Location;
@ -9,17 +8,18 @@ import org.bukkit.entity.Player;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.config.Settings;
import us.tastybento.bskyblock.database.BSBDatabase;
import us.tastybento.bskyblock.database.objects.Island;
import us.tastybento.bskyblock.database.objects.Players;
import us.tastybento.bskyblock.util.VaultHelper;
public class PlayersManager{
private BSkyBlock plugin;
private BSBDatabase database;
private HashMap<UUID, Players> playerCache;
/**
* Provides a memory cache of online player information
* This is the one-stop-shop of player info
@ -32,24 +32,24 @@ public class PlayersManager{
database = BSBDatabase.getDatabase();
playerCache = new HashMap<UUID, Players>();
}
public void load(){
//TODO
}
public void save(boolean async){
// TODO
}
public void shutdown(){
save(false);
playerCache.clear();
}
public Players getPlayer(UUID uuid){
return playerCache.get(uuid);
}
/*
* Cache control methods
*/
@ -59,14 +59,14 @@ public class PlayersManager{
return null;
//plugin.getLogger().info("DEBUG: added player " + playerUUID);
if (!playerCache.containsKey(playerUUID)) {
final Players player = new Players(playerUUID);
playerCache.put(playerUUID, player);
return player;
final Players player = new Players(playerUUID);
playerCache.put(playerUUID, player);
return player;
} else {
return playerCache.get(playerUUID);
}
}
/**
* Stores the player's info and removes the player from the cache
*

View File

@ -1,10 +1,12 @@
package us.tastybento.bskyblock.database;
package us.tastybento.bskyblock.database.managers;
import java.util.UUID;
import org.bukkit.Location;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.database.BSBDatabase;
import us.tastybento.bskyblock.database.DatabaseConnecter;
import us.tastybento.bskyblock.database.flatfile.FlatFileDatabaseConnecter;
import us.tastybento.bskyblock.database.objects.Island;
@ -14,7 +16,7 @@ public class RunTest {
try {
DatabaseConnecter connecter = new FlatFileDatabaseConnecter(plugin, null);
/*
new DatabaseConnectionSettingsImpl(
"127.0.0.1", 3306, "exampleDatabase","user", "pass"));
@ -35,12 +37,12 @@ public class RunTest {
items.add(new ItemStack(Material.ACTIVATOR_RAIL, 2));
items.add(new ItemStack(Material.FEATHER,5));
test.setInventory(items);
*/
*/
BSBDatabase database = BSBDatabase.getDatabase();
AbstractDatabaseHandler<Island> handler = (AbstractDatabaseHandler<Island>) database.getHandler(plugin, Island.class);
handler.insertObject(test);
plugin.getLogger().info("DEBUG: ALL WRITTEN! Now reading...");
test = handler.selectObject(test.getUniqueId());

View File

@ -1,4 +1,4 @@
package us.tastybento.bskyblock.database;
package us.tastybento.bskyblock.database.managers;
import java.util.HashMap;
import java.util.List;

View File

@ -1,20 +1,16 @@
package us.tastybento.bskyblock.database.mysql;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.database.AbstractDatabaseHandler;
import us.tastybento.bskyblock.database.BSBDatabase;
import us.tastybento.bskyblock.database.DatabaseConnectionSettingsImpl;
import us.tastybento.bskyblock.database.managers.AbstractDatabaseHandler;
import us.tastybento.bskyblock.database.objects.Island;
public class MySQLDatabase extends BSBDatabase{
@Override
public AbstractDatabaseHandler<?> getHandler(BSkyBlock plugin, Class<?> type) {
return new MySQLDatabaseHandler<Island>(plugin, Island.class,
new MySqlDatabaseConnecter(new DatabaseConnectionSettingsImpl()));
return new MySQLDatabaseHandler<Island>(plugin, Island.class, new MySQLDatabaseConnecter(new DatabaseConnectionSettingsImpl()));
}
}

View File

@ -30,8 +30,8 @@ import org.bukkit.Location;
import org.bukkit.World;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.database.AbstractDatabaseHandler;
import us.tastybento.bskyblock.database.DatabaseConnecter;
import us.tastybento.bskyblock.database.managers.AbstractDatabaseHandler;
import us.tastybento.bskyblock.util.Util;
/**

View File

@ -9,17 +9,17 @@ import org.bukkit.configuration.file.YamlConfiguration;
import us.tastybento.bskyblock.database.DatabaseConnecter;
import us.tastybento.bskyblock.database.DatabaseConnectionSettingsImpl;
public class MySqlDatabaseConnecter implements DatabaseConnecter {
public class MySQLDatabaseConnecter implements DatabaseConnecter {
private String connectionUrl;
private DatabaseConnectionSettingsImpl dbSettings;
private Connection connection = null;
/**
* Class for MySQL database connections using the settings provided
* @param dbSettings
*/
public MySqlDatabaseConnecter(
DatabaseConnectionSettingsImpl dbSettings) {
public MySQLDatabaseConnecter(DatabaseConnectionSettingsImpl dbSettings) {
this.dbSettings = dbSettings;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
@ -55,8 +55,7 @@ public class MySqlDatabaseConnecter implements DatabaseConnecter {
}
@Override
public void saveYamlFile(YamlConfiguration yamlFile, String tableName,
String fileName) {
public void saveYamlFile(YamlConfiguration yamlFile, String tableName, String fileName) {
// Not used
}

View File

@ -1,9 +1,5 @@
/**
*
*/
package us.tastybento.bskyblock.database.objects;
/**
* Contains fields that must be in any data object
* @author tastybento
@ -14,12 +10,12 @@ public abstract class DataObject {
/**
* @return the uniqueId
*/
abstract public String getUniqueId();
public abstract String getUniqueId();
/**
* @param uniqueId the uniqueId to set
*/
abstract public void setUniqueId(String uniqueId);
public abstract void setUniqueId(String uniqueId);
}

View File

@ -22,9 +22,9 @@ import us.tastybento.bskyblock.config.Settings;
* @author Poslovitch
*/
public class Island extends DataObject {
private String uniqueId = "";
@Override
public String getUniqueId() {
// Island's have UUID's that are randomly assigned if they do not exist
@ -37,9 +37,9 @@ public class Island extends DataObject {
@Override
public void setUniqueId(String uniqueId) {
this.uniqueId = uniqueId;
}
/**
* Island Guard Settings flags
* Covers island, spawn and system settings
@ -112,7 +112,7 @@ public class Island extends DataObject {
// Can open doors or trapdoors
DOOR,
// Can dye sheeps
DYEING,
@ -350,7 +350,7 @@ public class Island extends DataObject {
private HashMap<SettingsFlag, Boolean> flags = new HashMap<SettingsFlag, Boolean>();
public Island() {};
public Island(Location location, UUID owner, int protectionRange) {
this.members.add(owner);
this.owner = owner;

View File

@ -257,7 +257,7 @@ public class Players extends DataObject {
@Override
public void setUniqueId(String uniqueId) {
// TODO Auto-generated method stub
}
}

View File

@ -1,8 +1,8 @@
package us.tastybento.bskyblock.database.sqlite;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.database.AbstractDatabaseHandler;
import us.tastybento.bskyblock.database.BSBDatabase;
import us.tastybento.bskyblock.database.managers.AbstractDatabaseHandler;
public class SQLiteDatabase extends BSBDatabase{