mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2025-01-29 11:31:53 +01:00
Added a concept of UUID to the class that is to be saved.
If the class to be saved does not define its own uuid, then one will be generated at random by the database manager. For flat file databases, the UUID is used for filenames. For other databases, it's an index record. If the class defines a UUID then the same record will be replaced again and again. i.e., it's like a config file.
This commit is contained in:
parent
38ea97718a
commit
20d74e19c9
@ -7,8 +7,6 @@ import us.tastybento.bskyblock.BSkyBlock;
|
||||
/**
|
||||
* An abstract class that handles insert/select-operations into/from a database
|
||||
*
|
||||
* @author Tino for http://www.java-blog.com
|
||||
*
|
||||
* @param <T>
|
||||
*/
|
||||
public abstract class AbstractDatabaseHandler<T> {
|
||||
|
@ -4,6 +4,7 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import us.tastybento.bskyblock.BSkyBlock;
|
||||
import us.tastybento.bskyblock.config.Settings;
|
||||
import us.tastybento.bskyblock.database.flatfile.FlatFileDatabase;
|
||||
import us.tastybento.bskyblock.database.mysql.MySQLDatabase;
|
||||
@ -20,11 +21,22 @@ public abstract class BSBDatabase {
|
||||
return DatabaseType.FLATFILE.database;
|
||||
}
|
||||
|
||||
public abstract boolean connect(BSkyBlock plugin);
|
||||
public abstract Players loadPlayerData(UUID uuid);
|
||||
public abstract void savePlayerData(Players player);
|
||||
|
||||
/**
|
||||
* Loads an island
|
||||
* @param location
|
||||
* @return
|
||||
*/
|
||||
public abstract Island loadIslandData(String location);
|
||||
public abstract void saveIslandData(Island island);
|
||||
/**
|
||||
* Saves an island to the database
|
||||
* @param island
|
||||
* @return
|
||||
*/
|
||||
public abstract boolean saveIslandData(Island island);
|
||||
|
||||
public abstract HashMap<UUID, List<String>> loadOfflineHistoryMessages();
|
||||
public abstract void saveOfflineHistoryMessages(HashMap<UUID, List<String>> messages);
|
||||
|
@ -2,6 +2,7 @@ package us.tastybento.bskyblock.database;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
@ -38,5 +39,11 @@ public interface DatabaseConnecter {
|
||||
* @param simpleName
|
||||
*/
|
||||
public void saveYamlFile(YamlConfiguration config, String simpleName);
|
||||
|
||||
/**
|
||||
* Looks through the database (or files) and returns a known unique key
|
||||
* @return a unique key for this record
|
||||
*/
|
||||
public UUID getUniqueId();
|
||||
}
|
||||
|
||||
|
@ -31,6 +31,7 @@ public class IslandsManager {
|
||||
public IslandsManager(BSkyBlock plugin){
|
||||
this.plugin = plugin;
|
||||
database = BSBDatabase.getDatabase();
|
||||
database.connect(plugin);
|
||||
islands = new HashMap<Location, Island>();
|
||||
islandsByUUID = new HashMap<UUID, Island>();
|
||||
spawn = null;
|
||||
|
@ -1,13 +1,8 @@
|
||||
package us.tastybento.bskyblock.database;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import us.tastybento.bskyblock.BSkyBlock;
|
||||
import us.tastybento.bskyblock.database.flatfile.FlatFileDatabaseConnecter;
|
||||
@ -42,7 +37,7 @@ public class RunTest {
|
||||
items.add(new ItemStack(Material.ACTIVATOR_RAIL, 2));
|
||||
items.add(new ItemStack(Material.FEATHER,5));
|
||||
test.setInventory(items);
|
||||
*/
|
||||
*/
|
||||
FlatFileDatabaseInserter<Island> inserter = new FlatFileDatabaseInserter<Island>(plugin, Island.class, connecter);
|
||||
|
||||
inserter.insertObject(test);
|
||||
@ -51,7 +46,7 @@ public class RunTest {
|
||||
|
||||
FlatFileDatabaseSelecter<Island> selecter = new FlatFileDatabaseSelecter<Island>(plugin, Island.class, connecter);
|
||||
|
||||
test = selecter.selectObject();
|
||||
test = selecter.selectObject(test.getUuid().toString());
|
||||
|
||||
plugin.getLogger().info("DEBUG: name = " + test.getName());
|
||||
plugin.getLogger().info("DEBUG: owner = " + test.getOwner());
|
||||
@ -60,7 +55,7 @@ public class RunTest {
|
||||
plugin.getLogger().info("DEBUG: homes = " + homes);
|
||||
items = test.getInventory();
|
||||
plugin.getLogger().info("DEBUG: items = " + items);
|
||||
*/
|
||||
*/
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
@ -6,11 +6,16 @@ import java.util.UUID;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import us.tastybento.bskyblock.BSkyBlock;
|
||||
import us.tastybento.bskyblock.database.BSBDatabase;
|
||||
import us.tastybento.bskyblock.database.DatabaseConnecter;
|
||||
import us.tastybento.bskyblock.database.objects.Island;
|
||||
import us.tastybento.bskyblock.database.objects.Players;
|
||||
|
||||
public class FlatFileDatabase extends BSBDatabase{
|
||||
|
||||
private BSkyBlock plugin;
|
||||
DatabaseConnecter connecter;
|
||||
|
||||
@Override
|
||||
public UUID getUUID(String name, boolean adminCheck) {
|
||||
@ -60,9 +65,14 @@ public class FlatFileDatabase extends BSBDatabase{
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveIslandData(Island island) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
public boolean saveIslandData(Island island) {
|
||||
FlatFileDatabaseInserter<Island> inserter = new FlatFileDatabaseInserter<Island>(plugin, Island.class, connecter);
|
||||
try {
|
||||
inserter.insertObject(island);
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -88,4 +98,11 @@ public class FlatFileDatabase extends BSBDatabase{
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean connect(BSkyBlock plugin) {
|
||||
this.plugin = plugin;
|
||||
this.connecter = new FlatFileDatabaseConnecter(plugin, null);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package us.tastybento.bskyblock.database.flatfile;
|
||||
import java.io.File;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
@ -12,10 +13,14 @@ import us.tastybento.bskyblock.database.DatabaseConnectionSettingsImpl;
|
||||
|
||||
public class FlatFileDatabaseConnecter implements DatabaseConnecter {
|
||||
|
||||
private static final int MAX_LOOPS = 100;
|
||||
private BSkyBlock plugin;
|
||||
private File dataFolder;
|
||||
|
||||
|
||||
public FlatFileDatabaseConnecter(BSkyBlock plugin, DatabaseConnectionSettingsImpl databaseConnectionSettingsImpl) {
|
||||
this.plugin = plugin;
|
||||
dataFolder = new File(plugin.getDataFolder(), "database");
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -38,7 +43,6 @@ public class FlatFileDatabaseConnecter implements DatabaseConnecter {
|
||||
*/
|
||||
@Override
|
||||
public YamlConfiguration loadYamlFile(String fileName) {
|
||||
File dataFolder = new File(plugin.getDataFolder(), "database");
|
||||
File yamlFile = new File(dataFolder, fileName + ".yml");
|
||||
|
||||
YamlConfiguration config = null;
|
||||
@ -77,7 +81,6 @@ public class FlatFileDatabaseConnecter implements DatabaseConnecter {
|
||||
*/
|
||||
@Override
|
||||
public void saveYamlFile(YamlConfiguration yamlFile, String fileName) {
|
||||
File dataFolder = new File(plugin.getDataFolder(), "database");
|
||||
File file = new File(dataFolder, fileName + ".yml");
|
||||
|
||||
try {
|
||||
@ -87,4 +90,16 @@ public class FlatFileDatabaseConnecter implements DatabaseConnecter {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID getUniqueId() {
|
||||
UUID uuid = UUID.randomUUID();
|
||||
File file = new File(dataFolder, uuid.toString() + ".yml");
|
||||
int limit = 0;
|
||||
while (file.exists() && limit++ < MAX_LOOPS) {
|
||||
uuid = UUID.randomUUID();
|
||||
file = new File(dataFolder, uuid.toString() + ".yml");
|
||||
}
|
||||
return uuid;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -7,7 +7,6 @@ import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@ -53,7 +52,8 @@ public class FlatFileDatabaseInserter<T> extends AbstractDatabaseHandler<T> {
|
||||
* @throws IntrospectionException
|
||||
*/
|
||||
public void insertObject(T instance) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException {
|
||||
YamlConfiguration config = databaseConnecter.loadYamlFile(type.getSimpleName());
|
||||
YamlConfiguration config = new YamlConfiguration();
|
||||
String filename = "";
|
||||
for (Field field : type.getDeclaredFields()) {
|
||||
PropertyDescriptor propertyDescriptor = new PropertyDescriptor(field.getName(), type);
|
||||
|
||||
@ -65,6 +65,15 @@ public class FlatFileDatabaseInserter<T> extends AbstractDatabaseHandler<T> {
|
||||
// TODO: depending on the type, it'll need serializing
|
||||
if (propertyDescriptor.getPropertyType().equals(UUID.class)) {
|
||||
plugin.getLogger().info("DEBUG: writing UUID for " + field.getName());
|
||||
// Check if this is the UUID
|
||||
if (method.getName().equals("getUuid")) {
|
||||
if (value == null) {
|
||||
value = databaseConnecter.getUniqueId();
|
||||
// Set it in the class
|
||||
propertyDescriptor.getWriteMethod().invoke(instance, value);
|
||||
}
|
||||
filename = value.toString();
|
||||
}
|
||||
if (value != null) {
|
||||
config.set(field.getName(), ((UUID)value).toString());
|
||||
} else {
|
||||
@ -72,7 +81,7 @@ public class FlatFileDatabaseInserter<T> extends AbstractDatabaseHandler<T> {
|
||||
}
|
||||
} else if (propertyDescriptor.getPropertyType().equals(Set.class)) {
|
||||
plugin.getLogger().info("DEBUG: Hashset for " + field.getName());
|
||||
|
||||
|
||||
List<Object> list = new ArrayList<Object>();
|
||||
for (Object object : (Set<Object>)value) {
|
||||
if (object instanceof UUID) {
|
||||
@ -83,10 +92,11 @@ public class FlatFileDatabaseInserter<T> extends AbstractDatabaseHandler<T> {
|
||||
} else {
|
||||
config.set(field.getName(), value);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
databaseConnecter.saveYamlFile(config, type.getSimpleName());
|
||||
if (filename.isEmpty()) {
|
||||
throw new IllegalArgumentException("No UUID in class");
|
||||
}
|
||||
databaseConnecter.saveYamlFile(config, filename);
|
||||
|
||||
}
|
||||
/**
|
||||
|
@ -50,8 +50,8 @@ public class FlatFileDatabaseSelecter<T> extends AbstractDatabaseHandler<T> {
|
||||
* @throws IllegalAccessException
|
||||
* @throws InstantiationException
|
||||
*/
|
||||
public T selectObject() throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException {
|
||||
YamlConfiguration config = databaseConnecter.loadYamlFile(type.getSimpleName());
|
||||
public T selectObject(String key) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException {
|
||||
YamlConfiguration config = databaseConnecter.loadYamlFile(key);
|
||||
return createObject(config);
|
||||
}
|
||||
|
||||
@ -104,7 +104,7 @@ public class FlatFileDatabaseSelecter<T> extends AbstractDatabaseHandler<T> {
|
||||
} else if (propertyDescriptor.getPropertyType().equals(UUID.class)) {
|
||||
plugin.getLogger().info("DEBUG: is UUID");
|
||||
String uuid = (String)config.get(field.getName());
|
||||
if (uuid.equals("null")) {
|
||||
if (uuid == null || uuid.equals("null")) {
|
||||
method.invoke(instance, (Object)null);
|
||||
} else {
|
||||
Object value = UUID.fromString(uuid);
|
||||
|
@ -4,6 +4,7 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import us.tastybento.bskyblock.BSkyBlock;
|
||||
import us.tastybento.bskyblock.database.BSBDatabase;
|
||||
import us.tastybento.bskyblock.database.objects.Island;
|
||||
import us.tastybento.bskyblock.database.objects.Players;
|
||||
@ -29,9 +30,9 @@ public class MySQLDatabase extends BSBDatabase{
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveIslandData(Island island) {
|
||||
public boolean saveIslandData(Island island) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -64,4 +65,10 @@ public class MySQLDatabase extends BSBDatabase{
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean connect(BSkyBlock plugin) {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package us.tastybento.bskyblock.database.mysql;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
@ -39,4 +40,10 @@ public class MySqlDatabaseConnecter implements DatabaseConnecter {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID getUniqueId() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -278,6 +278,11 @@ public class Island {
|
||||
}
|
||||
|
||||
//// Island ////
|
||||
/**
|
||||
* The unique ID for this island
|
||||
*/
|
||||
private UUID uuid;
|
||||
|
||||
// The center of the island itself
|
||||
private Location center;
|
||||
|
||||
@ -785,4 +790,18 @@ public class Island {
|
||||
flags.put(flag, (flags.get(flag)) ? false : true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the uuid
|
||||
*/
|
||||
public UUID getUuid() {
|
||||
return uuid;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param uuid the uuid to set
|
||||
*/
|
||||
public void setUuid(UUID uuid) {
|
||||
this.uuid = uuid;
|
||||
}
|
||||
}
|
@ -4,6 +4,7 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import us.tastybento.bskyblock.BSkyBlock;
|
||||
import us.tastybento.bskyblock.database.BSBDatabase;
|
||||
import us.tastybento.bskyblock.database.objects.Island;
|
||||
import us.tastybento.bskyblock.database.objects.Players;
|
||||
@ -29,9 +30,9 @@ public class SQLiteDatabase extends BSBDatabase{
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveIslandData(Island island) {
|
||||
public boolean saveIslandData(Island island) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -64,4 +65,10 @@ public class SQLiteDatabase extends BSBDatabase{
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean connect(BSkyBlock plugin) {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user