mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2025-03-28 22:45:50 +01:00
Merge remote-tracking branch 'origin/database'
Conflicts: src/main/java/us/tastybento/bskyblock/BSkyBlock.java
This commit is contained in:
commit
b5430c0aab
src/main/java/us/tastybento/bskyblock
BSkyBlock.java
commands
config
database
AbstractDatabaseHandler.javaBSBDatabase.javaDatabaseConnecter.javaDatabaseConnectionSettingsImpl.javaIslandsManager.javaOfflineHistoryMessages.javaPlayersManager.javaRunTest.javaTest.java
flatfile
mysql
MySQLDatabase.javaMySQLDatabaseHandler.javaMySQLDatabaseResourceCloser.javaMySqlDatabaseConnecter.java
objects
sqlite
@ -37,12 +37,6 @@ public class BSkyBlock extends JavaPlugin{
|
||||
@Override
|
||||
public void onEnable(){
|
||||
plugin = this;
|
||||
|
||||
if (!VaultHelper.setupPermissions()) {
|
||||
getLogger().severe("Cannot link with Vault for permissions! Disabling plugin!");
|
||||
getServer().getPluginManager().disablePlugin(this);
|
||||
return;
|
||||
}
|
||||
|
||||
// Load configuration and locales. If there are no errors, load the plugin.
|
||||
if(PluginConfig.loadPluginConfig(this)){
|
||||
@ -86,8 +80,7 @@ public class BSkyBlock extends JavaPlugin{
|
||||
// Save data
|
||||
playersManager.shutdown();
|
||||
islandsManager.shutdown();
|
||||
offlineHistoryMessages.shutdown();
|
||||
|
||||
//offlineHistoryMessages.shutdown();
|
||||
plugin = null;
|
||||
}
|
||||
|
||||
|
@ -1012,7 +1012,7 @@ public class IslandCommand extends BSBCommand{
|
||||
Player player = (Player) sender;
|
||||
Island island = plugin.getIslands().getIsland(player.getUniqueId());
|
||||
|
||||
if(!island.isLocked()){
|
||||
if(!island.getLocked()){
|
||||
// TODO: Expel all visitors
|
||||
// TODO: send offline messages
|
||||
island.setLocked(true);
|
||||
|
@ -68,7 +68,6 @@ public class Settings {
|
||||
public static int spawnLimitMonsters;
|
||||
public static int spawnLimitAnimals;
|
||||
public static int spawnLimitWaterAnimals;
|
||||
public static int breedingLimit;
|
||||
public static HashMap<EntityType, Integer> entityLimits;
|
||||
public static HashMap<String, Integer> tileEntityLimits;
|
||||
|
||||
|
@ -0,0 +1,147 @@
|
||||
package us.tastybento.bskyblock.database;
|
||||
|
||||
import java.beans.IntrospectionException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
import us.tastybento.bskyblock.BSkyBlock;
|
||||
import us.tastybento.bskyblock.database.objects.Island;
|
||||
|
||||
/**
|
||||
* An abstract class that handles insert/select-operations into/from a database
|
||||
*
|
||||
* @param <T>
|
||||
*/
|
||||
/**
|
||||
* @author tastybento
|
||||
*
|
||||
* @param <T>
|
||||
*/
|
||||
/**
|
||||
* @author tastybento
|
||||
*
|
||||
* @param <T>
|
||||
*/
|
||||
public abstract class AbstractDatabaseHandler<T> {
|
||||
|
||||
/**
|
||||
* The type of the objects that should be created and filled with values
|
||||
* from the database or inserted into the database
|
||||
*/
|
||||
protected Class<T> type;
|
||||
|
||||
/**
|
||||
* Contains the settings to create a connection to the database like
|
||||
* host/port/database/user/password
|
||||
*/
|
||||
protected DatabaseConnecter databaseConnecter;
|
||||
|
||||
/** The SQL-select- and insert query */
|
||||
protected final String selectQuery;
|
||||
protected final String insertQuery;
|
||||
|
||||
|
||||
protected BSkyBlock plugin;
|
||||
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param type
|
||||
* The type of the objects that should be created and filled with
|
||||
* values from the database or inserted into the database
|
||||
* @param databaseConnecter
|
||||
* Contains the settings to create a connection to the database
|
||||
* like host/port/database/user/password
|
||||
*/
|
||||
protected AbstractDatabaseHandler(BSkyBlock plugin, Class<T> type,
|
||||
DatabaseConnecter databaseConnecter) {
|
||||
|
||||
this.plugin = plugin;
|
||||
this.databaseConnecter = databaseConnecter;
|
||||
this.type = type;
|
||||
this.selectQuery = createSelectQuery();
|
||||
this.insertQuery = createInsertQuery();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the SQL-String to insert into / select from the database
|
||||
* Not used in the flat file database
|
||||
* @return the SQL-String
|
||||
*/
|
||||
protected abstract String createSelectQuery();
|
||||
protected abstract String createInsertQuery();
|
||||
|
||||
/**
|
||||
*
|
||||
* Creates a comma-separated-String with the names of the variables in this
|
||||
* class
|
||||
* Not used in Flat File database.
|
||||
* @param usePlaceHolders
|
||||
* true, if PreparedStatement-placeholders ('?') should be used
|
||||
* instead of the names of the variables
|
||||
* @return
|
||||
*/
|
||||
protected String getColumns(boolean usePlaceHolders) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
boolean first = true;
|
||||
/* Iterate the column-names */
|
||||
for (Field f : type.getDeclaredFields()) {
|
||||
if (first)
|
||||
first = false;
|
||||
else
|
||||
sb.append(", ");
|
||||
|
||||
if (usePlaceHolders)
|
||||
sb.append("?");
|
||||
else
|
||||
sb.append(f.getName());
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads all the records in this table and returns a list of them
|
||||
* @return list of <T>
|
||||
* @throws InstantiationException
|
||||
* @throws IllegalAccessException
|
||||
* @throws IllegalArgumentException
|
||||
* @throws InvocationTargetException
|
||||
* @throws IntrospectionException
|
||||
* @throws SecurityException
|
||||
* @throws SQLException
|
||||
*/
|
||||
protected abstract List<T> selectObjects() throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException, SQLException, SecurityException;
|
||||
|
||||
/**
|
||||
* Creates a <T> filled with values from the corresponding
|
||||
* database file
|
||||
* @param uniqueId
|
||||
* @return <T>
|
||||
* @throws IntrospectionException
|
||||
* @throws InvocationTargetException
|
||||
* @throws IllegalArgumentException
|
||||
* @throws IllegalAccessException
|
||||
* @throws InstantiationException
|
||||
*/
|
||||
protected abstract T selectObject(String uniqueId) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException;
|
||||
|
||||
/**
|
||||
* Inserts T into the corresponding database-table
|
||||
*
|
||||
* @param instance that should be inserted into the database
|
||||
* @throws IllegalAccessException
|
||||
* @throws IllegalArgumentException
|
||||
* @throws InvocationTargetException
|
||||
* @throws IntrospectionException
|
||||
* @throws InstantiationException
|
||||
* @throws SecurityException
|
||||
* @throws SQLException
|
||||
*/
|
||||
protected abstract void insertObject(T instance) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException, SQLException, SecurityException, InstantiationException;
|
||||
|
||||
}
|
@ -1,14 +1,9 @@
|
||||
package us.tastybento.bskyblock.database;
|
||||
|
||||
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;
|
||||
import us.tastybento.bskyblock.database.objects.Island;
|
||||
import us.tastybento.bskyblock.database.objects.Players;
|
||||
import us.tastybento.bskyblock.database.sqlite.SQLiteDatabase;
|
||||
|
||||
public abstract class BSBDatabase {
|
||||
@ -20,15 +15,6 @@ public abstract class BSBDatabase {
|
||||
return DatabaseType.FLATFILE.database;
|
||||
}
|
||||
|
||||
public abstract Players loadPlayerData(UUID uuid);
|
||||
public abstract void savePlayerData(Players player);
|
||||
|
||||
public abstract Island loadIslandData(String location);
|
||||
public abstract void saveIslandData(Island island);
|
||||
|
||||
public abstract HashMap<UUID, List<String>> loadOfflineHistoryMessages();
|
||||
public abstract void saveOfflineHistoryMessages(HashMap<UUID, List<String>> messages);
|
||||
|
||||
public enum DatabaseType{
|
||||
FLATFILE(new FlatFileDatabase()),
|
||||
MYSQL(new MySQLDatabase()),
|
||||
@ -40,27 +26,16 @@ public abstract class BSBDatabase {
|
||||
this.database = database;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks in database whether the player is known by the plugin or not
|
||||
* @param uniqueID
|
||||
* @return true or false
|
||||
*/
|
||||
public abstract boolean isPlayerKnown(UUID uniqueID);
|
||||
|
||||
/**
|
||||
* Gets the UUID for player with name. If adminCheck is true, the search will be more extensive
|
||||
* @param name
|
||||
* @param adminCheck
|
||||
* @return UUID of player with name, or null if it cannot be found
|
||||
*/
|
||||
public abstract UUID getUUID(String name, boolean adminCheck);
|
||||
|
||||
/**
|
||||
* Associates this name with the UUID
|
||||
* @param name
|
||||
* @param uuid
|
||||
*/
|
||||
public abstract void savePlayerName(String name, UUID uuid);
|
||||
|
||||
/**
|
||||
* Gets a handler for this class type with this database connection
|
||||
* @param plugin
|
||||
* @param type
|
||||
* @param databaseConnecter
|
||||
* @return selector object
|
||||
*/
|
||||
public abstract AbstractDatabaseHandler<?> getHandler(BSkyBlock plugin, Class<?> type);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,42 @@
|
||||
package us.tastybento.bskyblock.database;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
/**
|
||||
*
|
||||
* Creates a connection to a database.
|
||||
*
|
||||
*/
|
||||
public interface DatabaseConnecter {
|
||||
|
||||
/**
|
||||
* Establishes a new connection to the database
|
||||
*
|
||||
* @return A new connection to the database
|
||||
* @throws SQLException
|
||||
*/
|
||||
public Connection createConnection() throws SQLException;
|
||||
|
||||
/**
|
||||
* Returns the connection url
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getConnectionUrl();
|
||||
|
||||
/**
|
||||
* Looks through the database (or files) and returns a known unique key
|
||||
* @param tableName
|
||||
* @return a unique key for this record
|
||||
*/
|
||||
public String getUniqueId(String tableName);
|
||||
|
||||
public YamlConfiguration loadYamlFile(String string, String key);
|
||||
|
||||
public void saveYamlFile(YamlConfiguration yamlFile, String tableName,
|
||||
String fileName);
|
||||
}
|
||||
|
@ -0,0 +1,10 @@
|
||||
package us.tastybento.bskyblock.database;
|
||||
|
||||
public class DatabaseConnectionSettingsImpl {
|
||||
|
||||
public DatabaseConnectionSettingsImpl(String string, int i, String string2,
|
||||
String string3, String string4) {
|
||||
// TODO Auto-generated constructor stub
|
||||
}
|
||||
|
||||
}
|
@ -27,17 +27,30 @@ public class IslandsManager {
|
||||
|
||||
// Metrics data
|
||||
private int metrics_createdcount = 0;
|
||||
private AbstractDatabaseHandler<Island> handler;
|
||||
|
||||
public IslandsManager(BSkyBlock plugin){
|
||||
this.plugin = plugin;
|
||||
database = BSBDatabase.getDatabase();
|
||||
handler = (AbstractDatabaseHandler<Island>) database.getHandler(plugin, Island.class);
|
||||
islands = new HashMap<Location, Island>();
|
||||
islandsByUUID = new HashMap<UUID, Island>();
|
||||
spawn = null;
|
||||
//load();
|
||||
}
|
||||
|
||||
public void load(){
|
||||
//TODO
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
public void save(boolean async){
|
||||
@ -46,14 +59,24 @@ public class IslandsManager {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
for(Entry<Location, Island> entry : islands.entrySet()){
|
||||
database.saveIslandData(entry.getValue());
|
||||
for(Island island : islands.values()){
|
||||
try {
|
||||
handler.insertObject(island);
|
||||
} catch (Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
for(Entry<Location, Island> entry : islands.entrySet()){
|
||||
database.saveIslandData(entry.getValue());
|
||||
for(Island island : islands.values()){
|
||||
try {
|
||||
handler.insertObject(island);
|
||||
} catch (Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -21,16 +21,15 @@ public class OfflineHistoryMessages {
|
||||
private BSBDatabase database;
|
||||
|
||||
// Offline Messages
|
||||
private HashMap<UUID, List<String>> messages;
|
||||
private HashMap<UUID, List<String>> messages = new HashMap<UUID, List<String>>();
|
||||
|
||||
public OfflineHistoryMessages(BSkyBlock plugin){
|
||||
this.plugin = plugin;
|
||||
database = BSBDatabase.getDatabase();
|
||||
messages = new HashMap<UUID, List<String>>();
|
||||
}
|
||||
|
||||
public void load(){
|
||||
messages = database.loadOfflineHistoryMessages();
|
||||
//messages = database.loadOfflineHistoryMessages();
|
||||
}
|
||||
|
||||
public void save(boolean async){
|
||||
@ -39,11 +38,11 @@ public class OfflineHistoryMessages {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
database.saveOfflineHistoryMessages(messages);
|
||||
//database.saveOfflineHistoryMessages(messages);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
database.saveOfflineHistoryMessages(messages);
|
||||
//database.saveOfflineHistoryMessages(messages);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -38,21 +38,7 @@ public class PlayersManager{
|
||||
}
|
||||
|
||||
public void save(boolean async){
|
||||
if(async){
|
||||
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
for(Entry<UUID, Players> entry : playerCache.entrySet()){
|
||||
database.savePlayerData(entry.getValue());
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
for(Entry<UUID, Players> entry : playerCache.entrySet()){
|
||||
database.savePlayerData(entry.getValue());
|
||||
}
|
||||
}
|
||||
// TODO
|
||||
}
|
||||
|
||||
public void shutdown(){
|
||||
@ -89,7 +75,7 @@ public class PlayersManager{
|
||||
*/
|
||||
public void removeOnlinePlayer(final UUID player) {
|
||||
if (playerCache.containsKey(player)) {
|
||||
database.savePlayerData(playerCache.get(player));
|
||||
//database.savePlayerData(playerCache.get(player));
|
||||
playerCache.remove(player);
|
||||
// plugin.getLogger().info("Removing player from cache: " + player);
|
||||
}
|
||||
@ -100,7 +86,7 @@ public class PlayersManager{
|
||||
*/
|
||||
public void removeAllPlayers() {
|
||||
for (UUID pl : playerCache.keySet()) {
|
||||
database.savePlayerData(playerCache.get(pl));
|
||||
//database.savePlayerData(playerCache.get(pl));
|
||||
}
|
||||
playerCache.clear();
|
||||
}
|
||||
@ -124,7 +110,9 @@ public class PlayersManager{
|
||||
return true;
|
||||
} else {
|
||||
// Get from the database - do not add to cache yet
|
||||
return database.isPlayerKnown(uniqueID) ? true: false;
|
||||
//return database.isPlayerKnown(uniqueID) ? true: false;
|
||||
// TODO
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@ -263,7 +251,8 @@ public class PlayersManager{
|
||||
@SuppressWarnings("deprecation")
|
||||
public UUID getUUID(String string, boolean adminCheck) {
|
||||
// Look in the database if it ready
|
||||
return database.getUUID(string, adminCheck);
|
||||
//return database.getUUID(string, adminCheck);
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -274,7 +263,7 @@ public class PlayersManager{
|
||||
public void setPlayerName(UUID uniqueId, String name) {
|
||||
addPlayer(uniqueId);
|
||||
playerCache.get(uniqueId).setPlayerN(name);
|
||||
database.savePlayerName(name, uniqueId);
|
||||
//database.savePlayerName(name, uniqueId);
|
||||
}
|
||||
|
||||
/**
|
||||
|
64
src/main/java/us/tastybento/bskyblock/database/RunTest.java
Normal file
64
src/main/java/us/tastybento/bskyblock/database/RunTest.java
Normal file
@ -0,0 +1,64 @@
|
||||
package us.tastybento.bskyblock.database;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.Location;
|
||||
|
||||
import us.tastybento.bskyblock.BSkyBlock;
|
||||
import us.tastybento.bskyblock.database.flatfile.FlatFileDatabaseConnecter;
|
||||
import us.tastybento.bskyblock.database.objects.Island;
|
||||
|
||||
public class RunTest {
|
||||
|
||||
public RunTest(BSkyBlock plugin) {
|
||||
try {
|
||||
|
||||
DatabaseConnecter connecter = new FlatFileDatabaseConnecter(plugin, null);
|
||||
|
||||
/*
|
||||
new DatabaseConnectionSettingsImpl(
|
||||
"127.0.0.1", 3306, "exampleDatabase","user", "pass"));
|
||||
*/
|
||||
Island test = new Island();
|
||||
test.setName("testname");
|
||||
test.setOwner(UUID.randomUUID());
|
||||
test.addMember(UUID.randomUUID());
|
||||
test.addToBanList(UUID.randomUUID());
|
||||
test.setCenter(new Location(plugin.getServer().getWorld("world"), 1, 2, 3, 1, 1));
|
||||
test.setLocked(true);
|
||||
/*
|
||||
HashMap<Integer, Location> homes = new HashMap<Integer, Location>();
|
||||
homes.put(1, new Location(plugin.getServer().getWorld("world"), 1, 2, 3, 1, 1));
|
||||
homes.put(2, new Location(plugin.getServer().getWorld("world"), 3, 3, 3, 3, 3));
|
||||
test.setHomeLocations(homes);
|
||||
List<ItemStack> items = new ArrayList<ItemStack>();
|
||||
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());
|
||||
|
||||
plugin.getLogger().info("DEBUG: name = " + test.getName());
|
||||
plugin.getLogger().info("DEBUG: owner = " + test.getOwner());
|
||||
/*
|
||||
homes = test.getHomeLocations();
|
||||
plugin.getLogger().info("DEBUG: homes = " + homes);
|
||||
items = test.getInventory();
|
||||
plugin.getLogger().info("DEBUG: items = " + items);
|
||||
*/
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
80
src/main/java/us/tastybento/bskyblock/database/Test.java
Normal file
80
src/main/java/us/tastybento/bskyblock/database/Test.java
Normal file
@ -0,0 +1,80 @@
|
||||
package us.tastybento.bskyblock.database;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
public class Test {
|
||||
private int id;
|
||||
private String name;
|
||||
private HashMap<Integer, Location> homeLocations;
|
||||
private List<ItemStack> inventory;
|
||||
|
||||
public Test() {}
|
||||
|
||||
public Test(int id, String name) {
|
||||
super();
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the homeLocations
|
||||
*/
|
||||
public HashMap<Integer, Location> getHomeLocations() {
|
||||
return homeLocations;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param homeLocations the homeLocations to set
|
||||
*/
|
||||
public void setHomeLocations(HashMap<Integer, Location> homeLocations) {
|
||||
this.homeLocations = homeLocations;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the inventory
|
||||
*/
|
||||
public List<ItemStack> getInventory() {
|
||||
return inventory;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param inventory the inventory to set
|
||||
*/
|
||||
public void setInventory(List<ItemStack> inventory) {
|
||||
this.inventory = inventory;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
final String TAB = " \n";
|
||||
|
||||
StringBuilder retValue = new StringBuilder();
|
||||
|
||||
retValue.append("Test (\n ")
|
||||
.append(super.toString()).append(TAB)
|
||||
.append(" id = ").append(this.id).append(TAB)
|
||||
.append(" name = ").append(this.name).append(TAB)
|
||||
.append(" )");
|
||||
|
||||
return retValue.toString();
|
||||
}
|
||||
}
|
@ -1,91 +1,16 @@
|
||||
package us.tastybento.bskyblock.database.flatfile;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import us.tastybento.bskyblock.BSkyBlock;
|
||||
import us.tastybento.bskyblock.database.AbstractDatabaseHandler;
|
||||
import us.tastybento.bskyblock.database.BSBDatabase;
|
||||
import us.tastybento.bskyblock.database.objects.Island;
|
||||
import us.tastybento.bskyblock.database.objects.Players;
|
||||
|
||||
public class FlatFileDatabase extends BSBDatabase{
|
||||
|
||||
@Override
|
||||
public UUID getUUID(String name, boolean adminCheck) {
|
||||
/*
|
||||
if (plugin.getTinyDB() != null && plugin.getTinyDB().isDbReady()) {
|
||||
UUID result = plugin.getTinyDB().getPlayerUUID(string);
|
||||
if (result != null) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
// This goes after the database because it is possible for islands that have a duplicate name to be in
|
||||
// the cache. For example, Bill had an island but left. Bill changes his name to Bob. Then Alice changes
|
||||
// her name to Bill and logs into the game. There are now two islands with owner names called "Bill"
|
||||
// The name database will ensure the names are updated.
|
||||
for (UUID id : playerCache.keySet()) {
|
||||
String name = playerCache.get(id).getPlayerName();
|
||||
//plugin.getLogger().info("DEBUG: Testing name " + name);
|
||||
if (name != null && name.equalsIgnoreCase(string)) {
|
||||
//plugin.getLogger().info("DEBUG: found it! " + id);
|
||||
return id;
|
||||
}
|
||||
}
|
||||
*/
|
||||
// Try the server
|
||||
if (adminCheck) {
|
||||
return Bukkit.getServer().getOfflinePlayer(name).getUniqueId();
|
||||
}
|
||||
return null;
|
||||
public AbstractDatabaseHandler<?> getHandler(BSkyBlock plugin, Class<?> type) {
|
||||
return new FlatFileDatabaseHandler<Island>(plugin, Island.class, new FlatFileDatabaseConnecter(plugin, null));
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Players loadPlayerData(UUID uuid) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void savePlayerData(Players player) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Island loadIslandData(String location) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveIslandData(Island island) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public HashMap<UUID, List<String>> loadOfflineHistoryMessages() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveOfflineHistoryMessages(HashMap<UUID, List<String>> messages) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPlayerKnown(UUID uniqueID) {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void savePlayerName(String name, UUID uuid) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,114 @@
|
||||
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;
|
||||
|
||||
import us.tastybento.bskyblock.BSkyBlock;
|
||||
import us.tastybento.bskyblock.database.DatabaseConnecter;
|
||||
import us.tastybento.bskyblock.database.DatabaseConnectionSettingsImpl;
|
||||
|
||||
public class FlatFileDatabaseConnecter implements DatabaseConnecter {
|
||||
|
||||
private static final int MAX_LOOPS = 100;
|
||||
private static final String DATABASE_FOLDER_NAME = "database";
|
||||
private BSkyBlock plugin;
|
||||
private File dataFolder;
|
||||
|
||||
|
||||
public FlatFileDatabaseConnecter(BSkyBlock plugin, DatabaseConnectionSettingsImpl databaseConnectionSettingsImpl) {
|
||||
this.plugin = plugin;
|
||||
dataFolder = new File(plugin.getDataFolder(), DATABASE_FOLDER_NAME);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Connection createConnection() throws SQLException {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getConnectionUrl() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads a YAML file and if it does not exist it is looked for in the JAR
|
||||
*
|
||||
* @param fileName
|
||||
* @return
|
||||
*/
|
||||
public YamlConfiguration loadYamlFile(String tableName, String fileName) {
|
||||
if (!fileName.endsWith(".yml")) {
|
||||
fileName = fileName + ".yml";
|
||||
}
|
||||
File yamlFile = new File(dataFolder, tableName + File.separator + fileName);
|
||||
|
||||
YamlConfiguration config = null;
|
||||
if (yamlFile.exists()) {
|
||||
try {
|
||||
config = new YamlConfiguration();
|
||||
config.load(yamlFile);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
// Create the missing file
|
||||
config = new YamlConfiguration();
|
||||
plugin.getLogger().info("No " + fileName + " found. Creating it...");
|
||||
try {
|
||||
if (plugin.getResource(fileName) != null) {
|
||||
plugin.getLogger().info("Using default found in jar file.");
|
||||
plugin.saveResource(fileName, false);
|
||||
config = new YamlConfiguration();
|
||||
config.load(yamlFile);
|
||||
} else {
|
||||
config.save(yamlFile);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
plugin.getLogger().severe("Could not create the " + fileName + " file!");
|
||||
}
|
||||
}
|
||||
return config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves a YAML file
|
||||
*
|
||||
* @param yamlFile
|
||||
* @param fileName
|
||||
*/
|
||||
@Override
|
||||
public void saveYamlFile(YamlConfiguration yamlFile, String tableName, String fileName) {
|
||||
if (!fileName.endsWith(".yml")) {
|
||||
fileName = fileName + ".yml";
|
||||
}
|
||||
File tableFolder = new File(dataFolder, tableName);
|
||||
File file = new File(tableFolder, fileName);
|
||||
if (!tableFolder.exists()) {
|
||||
tableFolder.mkdirs();
|
||||
}
|
||||
try {
|
||||
yamlFile.save(file);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUniqueId(String tableName) {
|
||||
UUID uuid = UUID.randomUUID();
|
||||
File file = new File(dataFolder, tableName + File.separator + uuid.toString() + ".yml");
|
||||
int limit = 0;
|
||||
while (file.exists() && limit++ < MAX_LOOPS) {
|
||||
uuid = UUID.randomUUID();
|
||||
file = new File(dataFolder, tableName + File.separator + uuid.toString() + ".yml");
|
||||
}
|
||||
return uuid.toString();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,243 @@
|
||||
package us.tastybento.bskyblock.database.flatfile;
|
||||
|
||||
import java.beans.IntrospectionException;
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.io.File;
|
||||
import java.io.FilenameFilter;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
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;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* Class that creates a list of <T>s filled with values from the corresponding
|
||||
* database-table.
|
||||
*
|
||||
* @author tastybento
|
||||
*
|
||||
* @param <T>
|
||||
*/
|
||||
public class FlatFileDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
|
||||
|
||||
private static final String DATABASE_FOLDER_NAME = "database";
|
||||
public FlatFileDatabaseHandler(BSkyBlock plugin, Class<T> type,
|
||||
DatabaseConnecter databaseConnecter) {
|
||||
super(plugin, type, databaseConnecter);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String createSelectQuery() {
|
||||
// not used
|
||||
return "";
|
||||
}
|
||||
@Override
|
||||
protected String createInsertQuery() {
|
||||
// not used
|
||||
return "";
|
||||
}
|
||||
/**
|
||||
* Creates a <T> filled with values from the corresponding
|
||||
* database file
|
||||
*
|
||||
* @return <T> filled with values from the corresponding database file
|
||||
* @throws IntrospectionException
|
||||
* @throws InvocationTargetException
|
||||
* @throws IllegalArgumentException
|
||||
* @throws IllegalAccessException
|
||||
* @throws InstantiationException
|
||||
*/
|
||||
@Override
|
||||
public T selectObject(String key) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException {
|
||||
YamlConfiguration config = databaseConnecter.loadYamlFile(type.getSimpleName(), key);
|
||||
return createObject(config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads all the records in this table and returns a list of them
|
||||
* @return list of <T>
|
||||
* @throws InstantiationException
|
||||
* @throws IllegalAccessException
|
||||
* @throws IllegalArgumentException
|
||||
* @throws InvocationTargetException
|
||||
* @throws IntrospectionException
|
||||
*/
|
||||
@Override
|
||||
public List<T> selectObjects() throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException {
|
||||
List<T> list = new ArrayList<T>();
|
||||
FilenameFilter ymlFilter = new FilenameFilter() {
|
||||
@Override
|
||||
public boolean accept(File dir, String name) {
|
||||
String lowercaseName = name.toLowerCase();
|
||||
if (lowercaseName.endsWith(".yml")) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
};
|
||||
File dataFolder = new File(plugin.getDataFolder(), DATABASE_FOLDER_NAME);
|
||||
File tableFolder = new File(dataFolder, type.getSimpleName());
|
||||
if (!tableFolder.exists()) {
|
||||
// Nothing there...
|
||||
tableFolder.mkdirs();
|
||||
}
|
||||
for (File file: tableFolder.listFiles(ymlFilter)) {
|
||||
YamlConfiguration config = databaseConnecter.loadYamlFile(type.getSimpleName(), file.getName());
|
||||
list.add(createObject(config));
|
||||
}
|
||||
return list;
|
||||
}
|
||||
/**
|
||||
*
|
||||
* Creates a list of <T>s filled with values from the provided ResultSet
|
||||
*
|
||||
* @param config - YAML config file
|
||||
*
|
||||
* @return <T> filled with values
|
||||
*
|
||||
* @throws InstantiationException
|
||||
* @throws IllegalAccessException
|
||||
* @throws IntrospectionException
|
||||
* @throws IllegalArgumentException
|
||||
* @throws InvocationTargetException
|
||||
*/
|
||||
private T createObject(YamlConfiguration config) throws InstantiationException, IllegalAccessException,
|
||||
IntrospectionException, IllegalArgumentException, InvocationTargetException
|
||||
{
|
||||
|
||||
T instance = type.newInstance();
|
||||
|
||||
for (Field field : type.getDeclaredFields()) {
|
||||
|
||||
/* We assume the table-column-names exactly match the variable-names of T */
|
||||
// TODO: depending on the data type, it'll need deserializing
|
||||
try {
|
||||
|
||||
PropertyDescriptor propertyDescriptor = new PropertyDescriptor(field.getName(), type);
|
||||
Method method = propertyDescriptor.getWriteMethod();
|
||||
plugin.getLogger().info("DEBUG: " + field.getName() + ": " + propertyDescriptor.getPropertyType().getTypeName());
|
||||
if (propertyDescriptor.getPropertyType().equals(HashMap.class)) {
|
||||
plugin.getLogger().info("DEBUG: is HashMap");
|
||||
// TODO: this may not work with all keys. Further serialization may be required.
|
||||
HashMap<Object,Object> value = new HashMap<Object, Object>();
|
||||
for (String key : config.getConfigurationSection(field.getName()).getKeys(false)) {
|
||||
value.put(key, config.get(field.getName() + "." + key));
|
||||
}
|
||||
method.invoke(instance, value);
|
||||
} else if (propertyDescriptor.getPropertyType().equals(Set.class)) {
|
||||
plugin.getLogger().info("DEBUG: is Set " + propertyDescriptor.getReadMethod().getGenericReturnType().getTypeName());
|
||||
|
||||
// TODO: this may not work with all keys. Further serialization may be required.
|
||||
Set<Object> value = new HashSet((List<Object>) config.getList(field.getName()));
|
||||
|
||||
method.invoke(instance, value);
|
||||
} else if (propertyDescriptor.getPropertyType().equals(UUID.class)) {
|
||||
plugin.getLogger().info("DEBUG: is UUID");
|
||||
String uuid = (String)config.get(field.getName());
|
||||
if (uuid == null || uuid.equals("null")) {
|
||||
method.invoke(instance, (Object)null);
|
||||
} else {
|
||||
Object value = UUID.fromString(uuid);
|
||||
method.invoke(instance, value);
|
||||
}
|
||||
} else {
|
||||
Object value = config.get(field.getName());
|
||||
method.invoke(instance, value);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts T into the corresponding database-table
|
||||
*
|
||||
* @param instance that should be inserted into the database
|
||||
* @throws IllegalAccessException
|
||||
* @throws IllegalArgumentException
|
||||
* @throws InvocationTargetException
|
||||
* @throws IntrospectionException
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public void insertObject(T instance) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException {
|
||||
// This is the Yaml Configuration that will be used and saved at the end
|
||||
YamlConfiguration config = new YamlConfiguration();
|
||||
// The file name of the Yaml file.
|
||||
String filename = "";
|
||||
// Run through all the fields in the class that is being stored. EVERY field must have a get and set method
|
||||
for (Field field : type.getDeclaredFields()) {
|
||||
// Get the property descriptor for this field
|
||||
PropertyDescriptor propertyDescriptor = new PropertyDescriptor(field.getName(), type);
|
||||
// Get the read method, i.e., getXXXX();
|
||||
Method method = propertyDescriptor.getReadMethod();
|
||||
// Invoke the read method to get the value. We have no idea what type of value it is.
|
||||
Object value = method.invoke(instance);
|
||||
plugin.getLogger().info("DEBUG: writing " + field.getName());
|
||||
plugin.getLogger().info("DEBUG: property desc = " + propertyDescriptor.getPropertyType().getTypeName());
|
||||
// Depending on the vale type, it'll need serializing differenty
|
||||
// Check if this field is the mandatory UniqueId field. This is used to identify this instantiation of the class
|
||||
if (method.getName().equals("getUniqueId")) {
|
||||
// If the object does not have a unique name assigned to it already, one is created at random
|
||||
plugin.getLogger().info("DEBUG: uniqueId = " + value);
|
||||
String id = (String)value;
|
||||
if (id.isEmpty()) {
|
||||
id = databaseConnecter.getUniqueId(type.getSimpleName());
|
||||
// Set it in the class so that it will be used next time
|
||||
propertyDescriptor.getWriteMethod().invoke(instance, id);
|
||||
}
|
||||
// Save the name for when the file is saved
|
||||
filename = id;
|
||||
}
|
||||
// UUID's need special serialization
|
||||
if (propertyDescriptor.getPropertyType().equals(UUID.class)) {
|
||||
plugin.getLogger().info("DEBUG: writing UUID for " + field.getName());
|
||||
if (value != null) {
|
||||
config.set(field.getName(), ((UUID)value).toString());
|
||||
} else {
|
||||
// UUID's can be null, so they need to be saved as the string "null"
|
||||
config.set(field.getName(), "null");
|
||||
}
|
||||
} else if (propertyDescriptor.getPropertyType().equals(Set.class)) {
|
||||
// Sets need to be serialized as string lists
|
||||
plugin.getLogger().info("DEBUG: Set for " + field.getName());
|
||||
|
||||
List<Object> list = new ArrayList<Object>();
|
||||
for (Object object : (Set<Object>)value) {
|
||||
if (object instanceof UUID) {
|
||||
list.add(((UUID)object).toString());
|
||||
}
|
||||
}
|
||||
// Save the list in the config file
|
||||
config.set(field.getName(), list);
|
||||
} else {
|
||||
// For all other data that doesn't need special serialization
|
||||
config.set(field.getName(), value);
|
||||
}
|
||||
}
|
||||
if (filename.isEmpty()) {
|
||||
throw new IllegalArgumentException("No UUID in class");
|
||||
}
|
||||
// Save the file in the right folder
|
||||
databaseConnecter.saveYamlFile(config, type.getSimpleName(), filename);
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,67 +1,18 @@
|
||||
package us.tastybento.bskyblock.database.mysql;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import us.tastybento.bskyblock.BSkyBlock;
|
||||
import us.tastybento.bskyblock.database.AbstractDatabaseHandler;
|
||||
import us.tastybento.bskyblock.database.BSBDatabase;
|
||||
import us.tastybento.bskyblock.database.flatfile.FlatFileDatabaseConnecter;
|
||||
import us.tastybento.bskyblock.database.objects.Island;
|
||||
import us.tastybento.bskyblock.database.objects.Players;
|
||||
|
||||
public class MySQLDatabase extends BSBDatabase{
|
||||
|
||||
@Override
|
||||
public Players loadPlayerData(UUID uuid) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
public AbstractDatabaseHandler<?> getHandler(BSkyBlock plugin, Class<?> type) {
|
||||
return new MySQLDatabaseHandler<Island>(plugin, Island.class, new FlatFileDatabaseConnecter(plugin, null));
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void savePlayerData(Players player) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Island loadIslandData(String location) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveIslandData(Island island) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public HashMap<UUID, List<String>> loadOfflineHistoryMessages() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveOfflineHistoryMessages(HashMap<UUID, List<String>> messages) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPlayerKnown(UUID uniqueID) {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID getUUID(String name, boolean adminCheck) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void savePlayerName(String name, UUID uuid) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,212 @@
|
||||
package us.tastybento.bskyblock.database.mysql;
|
||||
|
||||
import java.beans.IntrospectionException;
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import us.tastybento.bskyblock.BSkyBlock;
|
||||
import us.tastybento.bskyblock.database.AbstractDatabaseHandler;
|
||||
import us.tastybento.bskyblock.database.DatabaseConnecter;
|
||||
|
||||
/**
|
||||
*
|
||||
* Class that inserts a <T> into the corresponding database-table.
|
||||
*
|
||||
* @author tastybento
|
||||
*
|
||||
* @param <T>
|
||||
*/
|
||||
public class MySQLDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
|
||||
|
||||
public MySQLDatabaseHandler(BSkyBlock plugin, Class<T> type,
|
||||
DatabaseConnecter databaseConnecter) {
|
||||
super(plugin, type, databaseConnecter);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String createSelectQuery() {
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
sb.append("SELECT ");
|
||||
sb.append(super.getColumns(false));
|
||||
sb.append(" FROM ");
|
||||
|
||||
/* We assume the table-name exactly matches the simpleName of T */
|
||||
sb.append(type.getSimpleName());
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String createInsertQuery() {
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
sb.append("INSERT INTO ");
|
||||
sb.append(type.getSimpleName());
|
||||
sb.append("(");
|
||||
sb.append(super.getColumns(false));
|
||||
sb.append(")");
|
||||
sb.append(" VALUES (");
|
||||
sb.append(super.getColumns(true));
|
||||
sb.append(")");
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts a <T> into the corresponding database-table
|
||||
*
|
||||
* @param instance <T> that should be inserted into the corresponding database-table
|
||||
* @throws SQLException
|
||||
* @throws SecurityException
|
||||
* @throws IllegalArgumentException
|
||||
* @throws InstantiationException
|
||||
* @throws IllegalAccessException
|
||||
* @throws IntrospectionException
|
||||
* @throws InvocationTargetException
|
||||
*/
|
||||
@Override
|
||||
public void insertObject(T instance) throws SQLException,
|
||||
SecurityException, IllegalArgumentException,
|
||||
InstantiationException, IllegalAccessException,
|
||||
IntrospectionException, InvocationTargetException {
|
||||
|
||||
Connection connection = null;
|
||||
PreparedStatement preparedStatement = null;
|
||||
|
||||
try {
|
||||
connection = databaseConnecter.createConnection();
|
||||
preparedStatement = connection.prepareStatement(selectQuery);
|
||||
|
||||
int i = 0;
|
||||
|
||||
for (Field field : type.getDeclaredFields()) {
|
||||
PropertyDescriptor propertyDescriptor = new PropertyDescriptor(
|
||||
field.getName(), type);
|
||||
|
||||
Method method = propertyDescriptor
|
||||
.getReadMethod();
|
||||
|
||||
Object value = method.invoke(instance);
|
||||
|
||||
preparedStatement.setObject(++i, value);
|
||||
}
|
||||
|
||||
preparedStatement.addBatch();
|
||||
|
||||
preparedStatement.executeBatch();
|
||||
|
||||
} finally {
|
||||
MySQLDatabaseResourceCloser.close(preparedStatement);
|
||||
MySQLDatabaseResourceCloser.close(preparedStatement);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a list of <T>s filled with values from the corresponding
|
||||
* database-table
|
||||
*
|
||||
* @return List of <T>s filled with values from the corresponding
|
||||
* database-table
|
||||
*
|
||||
* @throws SQLException
|
||||
* @throws SecurityException
|
||||
* @throws IllegalArgumentException
|
||||
* @throws InstantiationException
|
||||
* @throws IllegalAccessException
|
||||
* @throws IntrospectionException
|
||||
* @throws InvocationTargetException
|
||||
*/
|
||||
@Override
|
||||
public List<T> selectObjects() throws SQLException,
|
||||
SecurityException, IllegalArgumentException,
|
||||
InstantiationException, IllegalAccessException,
|
||||
IntrospectionException, InvocationTargetException {
|
||||
|
||||
Connection connection = null;
|
||||
Statement statement = null;
|
||||
ResultSet resultSet = null;
|
||||
|
||||
try {
|
||||
connection = databaseConnecter.createConnection();
|
||||
statement = connection.createStatement();
|
||||
resultSet = statement.executeQuery(selectQuery);
|
||||
|
||||
return createObjects(resultSet);
|
||||
|
||||
} finally {
|
||||
MySQLDatabaseResourceCloser.close(resultSet);
|
||||
MySQLDatabaseResourceCloser.close(statement);
|
||||
MySQLDatabaseResourceCloser.close(connection);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected T selectObject(String uniqueId) throws InstantiationException,
|
||||
IllegalAccessException, IllegalArgumentException,
|
||||
InvocationTargetException, IntrospectionException {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* Creates a list of <T>s filled with values from the provided ResultSet
|
||||
*
|
||||
* @param resultSet
|
||||
* ResultSet that contains the result of the
|
||||
* database-select-query
|
||||
*
|
||||
* @return List of <T>s filled with values from the provided ResultSet
|
||||
*
|
||||
* @throws SecurityException
|
||||
* @throws IllegalArgumentException
|
||||
* @throws SQLException
|
||||
* @throws InstantiationException
|
||||
* @throws IllegalAccessException
|
||||
* @throws IntrospectionException
|
||||
* @throws InvocationTargetException
|
||||
*/
|
||||
private List<T> createObjects(ResultSet resultSet)
|
||||
throws SecurityException, IllegalArgumentException,
|
||||
SQLException, InstantiationException,
|
||||
IllegalAccessException, IntrospectionException,
|
||||
InvocationTargetException {
|
||||
|
||||
List<T> list = new ArrayList<T>();
|
||||
|
||||
while (resultSet.next()) {
|
||||
|
||||
T instance = type.newInstance();
|
||||
|
||||
for (Field field : type.getDeclaredFields()) {
|
||||
|
||||
/* We assume the table-column-names exactly match the variable-names of T */
|
||||
Object value = resultSet.getObject(field.getName());
|
||||
|
||||
PropertyDescriptor propertyDescriptor = new PropertyDescriptor(
|
||||
field.getName(), type);
|
||||
|
||||
Method method = propertyDescriptor.getWriteMethod();
|
||||
|
||||
method.invoke(instance, value);
|
||||
}
|
||||
|
||||
list.add(instance);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,77 @@
|
||||
package us.tastybento.bskyblock.database.mysql;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
public class MySQLDatabaseResourceCloser {
|
||||
|
||||
/**
|
||||
* Closes the provided ResultSets
|
||||
*
|
||||
* @param resultSets
|
||||
* ResultSets that should be closed
|
||||
*/
|
||||
public static void close(ResultSet... resultSets) {
|
||||
|
||||
if (resultSets == null)
|
||||
return;
|
||||
|
||||
for (ResultSet resultSet : resultSets)
|
||||
if (resultSet != null)
|
||||
try {
|
||||
resultSet.close();
|
||||
} catch (SQLException e) {
|
||||
/* Do some exception-logging here. */
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the provided Statements
|
||||
*
|
||||
* @param statements
|
||||
* Statements that should be closed
|
||||
*/
|
||||
public static void close(Statement... statements) {
|
||||
/*
|
||||
* No need to create methods for PreparedStatement and
|
||||
* CallableStatement, because they extend Statement.
|
||||
*/
|
||||
|
||||
if (statements == null)
|
||||
return;
|
||||
|
||||
for (Statement statement : statements)
|
||||
if (statement != null)
|
||||
try {
|
||||
statement.close();
|
||||
} catch (SQLException e) {
|
||||
/* Do some exception-logging here. */
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the provided Connections
|
||||
*
|
||||
* @param connections
|
||||
* Connections that should be closed
|
||||
*/
|
||||
public static void close(Connection... connections) {
|
||||
|
||||
if (connections == null)
|
||||
return;
|
||||
|
||||
for (Connection connection : connections)
|
||||
if (connection != null)
|
||||
try {
|
||||
connection.close();
|
||||
} catch (SQLException e) {
|
||||
/* Do some exception-logging here. */
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,49 @@
|
||||
package us.tastybento.bskyblock.database.mysql;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
import us.tastybento.bskyblock.database.DatabaseConnecter;
|
||||
import us.tastybento.bskyblock.database.DatabaseConnectionSettingsImpl;
|
||||
|
||||
public class MySqlDatabaseConnecter implements DatabaseConnecter {
|
||||
|
||||
public MySqlDatabaseConnecter(
|
||||
DatabaseConnectionSettingsImpl databaseConnectionSettingsImpl) {
|
||||
// TODO Auto-generated constructor stub
|
||||
}
|
||||
|
||||
@Override
|
||||
public Connection createConnection() throws SQLException {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getConnectionUrl() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUniqueId(String tableName) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public YamlConfiguration loadYamlFile(String string, String key) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveYamlFile(YamlConfiguration yamlFile, String tableName,
|
||||
String fileName) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package us.tastybento.bskyblock.database.objects;
|
||||
|
||||
|
||||
/**
|
||||
* Contains fields that must be in any data object
|
||||
* @author tastybento
|
||||
*
|
||||
*/
|
||||
public abstract class DataObject {
|
||||
|
||||
/**
|
||||
* @return the uniqueId
|
||||
*/
|
||||
abstract public String getUniqueId();
|
||||
|
||||
/**
|
||||
* @param uniqueId the uniqueId to set
|
||||
*/
|
||||
abstract public void setUniqueId(String uniqueId);
|
||||
|
||||
|
||||
}
|
@ -5,10 +5,10 @@ import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
|
||||
import us.tastybento.bskyblock.BSkyBlock;
|
||||
import us.tastybento.bskyblock.api.events.island.IslandLockEvent;
|
||||
import us.tastybento.bskyblock.api.events.island.IslandUnlockEvent;
|
||||
import us.tastybento.bskyblock.config.Settings;
|
||||
@ -21,274 +21,7 @@ import us.tastybento.bskyblock.config.Settings;
|
||||
* @author Tastybento
|
||||
* @author Poslovitch
|
||||
*/
|
||||
public class Island {
|
||||
private BSkyBlock plugin;
|
||||
|
||||
//// Island ////
|
||||
// The center of the island itself
|
||||
private Location center;
|
||||
// Island range
|
||||
private int range;
|
||||
|
||||
// Coordinates of the island area
|
||||
private int minX;
|
||||
private int minZ;
|
||||
// Coordinates of minimum protected area
|
||||
private int minProtectedX;
|
||||
private int minProtectedZ;
|
||||
// Protection size
|
||||
private int protectionRange;
|
||||
// World the island is in
|
||||
private World world;
|
||||
|
||||
// Display name
|
||||
private String name;
|
||||
|
||||
// Time parameters
|
||||
private long createdDate;
|
||||
private long updatedDate;
|
||||
|
||||
//// Team ////
|
||||
// Owner (Team Leader)
|
||||
private UUID owner;
|
||||
// Members (use set because each value must be unique)
|
||||
private Set<UUID> members;
|
||||
// Trustees
|
||||
private Set<UUID> trustees;
|
||||
// Coops
|
||||
private Set<UUID> coops;
|
||||
// Banned players
|
||||
private Set<UUID> banned;
|
||||
|
||||
//// State ////
|
||||
private boolean locked = false;
|
||||
private boolean isSpawn = false;
|
||||
private boolean purgeProtected = false;
|
||||
|
||||
//// Protection ////
|
||||
private HashMap<SettingsFlag, Boolean> flags = new HashMap<SettingsFlag, Boolean>();
|
||||
|
||||
public Island(Location location, UUID owner, int protectionRange) {
|
||||
this.members = new HashSet<UUID>();
|
||||
this.members.add(owner);
|
||||
this.owner = owner;
|
||||
this.coops = new HashSet<UUID>();
|
||||
this.trustees = new HashSet<UUID>();
|
||||
this.banned = new HashSet<UUID>();
|
||||
this.createdDate = System.currentTimeMillis();
|
||||
this.updatedDate = System.currentTimeMillis();
|
||||
this.name = "";
|
||||
this.world = location.getWorld();
|
||||
this.center = location;
|
||||
this.minX = center.getBlockX() - Settings.islandDistance;
|
||||
this.minZ = center.getBlockZ() - Settings.islandDistance;
|
||||
this.protectionRange = protectionRange;
|
||||
this.minProtectedX = center.getBlockX() - protectionRange;
|
||||
this.minProtectedZ = center.getBlockZ() - protectionRange;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the center Location
|
||||
*/
|
||||
public Location getCenter(){
|
||||
return center;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the x coordinate of the island center
|
||||
*/
|
||||
public int getX(){
|
||||
return center.getBlockX();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the y coordinate of the island center
|
||||
*/
|
||||
public int getY(){
|
||||
return center.getBlockY();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the z coordinate of the island center
|
||||
*/
|
||||
public int getZ(){
|
||||
return center.getBlockZ();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the island range
|
||||
*/
|
||||
public int getRange(){
|
||||
return range;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param range - the range to set
|
||||
*/
|
||||
public void setRange(int range){
|
||||
this.range = range;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the island display name or the owner's name if none is set
|
||||
*/
|
||||
public String getName(){
|
||||
return (name != null) ? name : plugin.getServer().getOfflinePlayer(owner).getName();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param name - the display name to set
|
||||
* Set to null to remove the display name
|
||||
*/
|
||||
public void setName(String name){
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the date when the island was created
|
||||
*/
|
||||
public long getCreatedDate(){
|
||||
return createdDate;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param createdDate - the createdDate to sets
|
||||
*/
|
||||
public void setCreatedDate(long createdDate){
|
||||
this.createdDate = createdDate;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the date when the island was updated (team member connection, etc...)
|
||||
*/
|
||||
public long getUpdatedDate(){
|
||||
return updatedDate;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param updatedDate - the updatedDate to sets
|
||||
*/
|
||||
public void setUpdatedDate(long updatedDate){
|
||||
this.updatedDate = updatedDate;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the owner (team leader)
|
||||
*/
|
||||
public UUID getOwner(){
|
||||
return owner;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the owner of the island. If the owner was previous banned, they are unbanned
|
||||
* @param owner - the owner/team leader to set
|
||||
*/
|
||||
public void setOwner(UUID owner){
|
||||
this.owner = owner;
|
||||
this.banned.remove(owner);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the members of the island (owner included)
|
||||
*/
|
||||
public Set<UUID> getMembers(){
|
||||
return members;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param members - the members to set
|
||||
*/
|
||||
public void setMembers(Set<UUID> members){
|
||||
this.members = members;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the trustees players of the island
|
||||
*/
|
||||
public Set<UUID> getTrustees(){
|
||||
return trustees;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param trustees - the trustees to set
|
||||
*/
|
||||
public void setTrustees(Set<UUID> trustees){
|
||||
this.trustees = trustees;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the coop players of the island
|
||||
*/
|
||||
public Set<UUID> getCoops(){
|
||||
return coops;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param coops - the coops to set
|
||||
*/
|
||||
public void setCoops(Set<UUID> coops){
|
||||
this.coops = coops;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return true if the island is locked, otherwise false
|
||||
*/
|
||||
public boolean isLocked(){
|
||||
return locked;
|
||||
}
|
||||
|
||||
/**
|
||||
* Locks/Unlocks the island. May be cancelled by
|
||||
* {@link IslandLockEvent} or {@link IslandUnlockEvent}.
|
||||
* @param locked - the lock state to set
|
||||
*/
|
||||
public void setLocked(boolean locked){
|
||||
if(locked){
|
||||
// Lock the island
|
||||
IslandLockEvent event = new IslandLockEvent(this);
|
||||
plugin.getServer().getPluginManager().callEvent(event);
|
||||
|
||||
if(!event.isCancelled()){
|
||||
this.locked = locked;
|
||||
}
|
||||
} else {
|
||||
// Unlock the island
|
||||
IslandUnlockEvent event = new IslandUnlockEvent(this);
|
||||
plugin.getServer().getPluginManager().callEvent(event);
|
||||
|
||||
if(!event.isCancelled()){
|
||||
this.locked = locked;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return true if the island is the spawn otherwise false
|
||||
*/
|
||||
public boolean isSpawn(){
|
||||
return isSpawn;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param isSpawn - if the island is the spawn
|
||||
*/
|
||||
public void setSpawn(boolean isSpawn){
|
||||
this.isSpawn = isSpawn;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return true if the island is protected from the Purge, otherwise false
|
||||
*/
|
||||
public boolean isPurgeProtected(){
|
||||
return purgeProtected;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param purgeProtected - if the island is protected from the Purge
|
||||
*/
|
||||
public void setPurgeProtected(boolean purgeProtected){
|
||||
this.purgeProtected = purgeProtected;
|
||||
}
|
||||
public class Island extends DataObject {
|
||||
|
||||
/**
|
||||
* Island Guard Settings flags
|
||||
@ -362,6 +95,9 @@ public class Island {
|
||||
|
||||
// Can open doors or trapdoors
|
||||
DOOR,
|
||||
|
||||
// Can dye sheeps
|
||||
DYEING,
|
||||
|
||||
// Can use Elytras
|
||||
ELYTRA,
|
||||
@ -544,49 +280,74 @@ public class Island {
|
||||
WITHER_BLOW_UP_SHULKER_BOX
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets the flags to their default as set in config.yml for this island
|
||||
*/
|
||||
public void setFlagsDefaults(){
|
||||
/*for(SettingsFlag flag : SettingsFlag.values()){
|
||||
this.flags.put(flag, Settings.defaultIslandSettings.get(flag));
|
||||
}*/ //TODO default flags
|
||||
}
|
||||
//// Island ////
|
||||
// The center of the island itself
|
||||
private Location center;
|
||||
|
||||
/**
|
||||
* Resets the flags to their default as set in config.yml for the spawn
|
||||
*/
|
||||
public void setSpawnFlagsDefaults(){
|
||||
/*for(SettingsFlag flag : SettingsFlag.values()){
|
||||
this.flags.put(flag, Settings.defaultSpawnSettings.get(flag));
|
||||
}*/ //TODO default flags
|
||||
}
|
||||
// Island range
|
||||
private int range;
|
||||
|
||||
/**
|
||||
* Get the Island Guard flag status
|
||||
* @param flag
|
||||
* @return true or false, or false if flag is not in the list
|
||||
*/
|
||||
public boolean getFlag(SettingsFlag flag){
|
||||
if(flags.containsKey(flag)) return flags.get(flag);
|
||||
else return false;
|
||||
}
|
||||
// Coordinates of the island area
|
||||
private int minX;
|
||||
|
||||
/**
|
||||
* Set the Island Guard flag status
|
||||
* @param flag
|
||||
* @param value
|
||||
*/
|
||||
public void setFlag(SettingsFlag flag, boolean value){
|
||||
flags.put(flag, value);
|
||||
}
|
||||
private int minZ;
|
||||
|
||||
/**
|
||||
* Toggles the Island Guard flag status if it is in the list
|
||||
* @param flag
|
||||
*/
|
||||
public void toggleFlag(SettingsFlag flag){
|
||||
if(flags.containsKey(flag)) flags.put(flag, (flags.get(flag)) ? false : true);
|
||||
// Coordinates of minimum protected area
|
||||
private int minProtectedX;
|
||||
|
||||
private int minProtectedZ;
|
||||
|
||||
// Protection size
|
||||
private int protectionRange;
|
||||
|
||||
// World the island is in
|
||||
private World world;
|
||||
|
||||
// Display name
|
||||
private String name;
|
||||
|
||||
// Time parameters
|
||||
private long createdDate;
|
||||
|
||||
private long updatedDate;
|
||||
|
||||
//// Team ////
|
||||
// Owner (Team Leader)
|
||||
private UUID owner;
|
||||
|
||||
// Members (use set because each value must be unique)
|
||||
private Set<UUID> members = new HashSet<UUID>();
|
||||
|
||||
// Trustees
|
||||
private Set<UUID> trustees = new HashSet<UUID>();
|
||||
// Coops
|
||||
private Set<UUID> coops = new HashSet<UUID>();
|
||||
|
||||
// Banned players
|
||||
private Set<UUID> banned = new HashSet<UUID>();
|
||||
//// State ////
|
||||
private boolean locked = false;
|
||||
private boolean spawn = false;
|
||||
private boolean purgeProtected = false;
|
||||
//// Protection ////
|
||||
private HashMap<SettingsFlag, Boolean> flags = new HashMap<SettingsFlag, Boolean>();
|
||||
|
||||
private String uniqueId = "";
|
||||
|
||||
public Island() {};
|
||||
|
||||
public Island(Location location, UUID owner, int protectionRange) {
|
||||
this.members.add(owner);
|
||||
this.owner = owner;
|
||||
this.createdDate = System.currentTimeMillis();
|
||||
this.updatedDate = System.currentTimeMillis();
|
||||
this.world = location.getWorld();
|
||||
this.center = location;
|
||||
this.minX = center.getBlockX() - Settings.islandDistance;
|
||||
this.minZ = center.getBlockZ() - Settings.islandDistance;
|
||||
this.protectionRange = protectionRange;
|
||||
this.minProtectedX = center.getBlockX() - protectionRange;
|
||||
this.minProtectedZ = center.getBlockZ() - protectionRange;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -598,32 +359,6 @@ public class Island {
|
||||
banned.remove(playerUUID);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a location is within this island's protected area
|
||||
*
|
||||
* @param target
|
||||
* @return true if it is, false if not
|
||||
*/
|
||||
public boolean onIsland(Location target) {
|
||||
if (center.getWorld() != null) {
|
||||
if (target.getBlockX() >= minProtectedX && target.getBlockX() < (minProtectedX + protectionRange)
|
||||
&& target.getBlockZ() >= minProtectedZ && target.getBlockZ() < (minProtectedZ + protectionRange)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean inIslandSpace(int x, int z) {
|
||||
if (x >= center.getBlockX() - Settings.islandDistance / 2 && x < center.getBlockX() + Settings.islandDistance / 2 && z >= center.getBlockZ() - Settings.islandDistance / 2
|
||||
&& z < center.getBlockZ() + Settings.islandDistance / 2) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds target to a list of banned players for this island. May be blocked by the event being cancelled.
|
||||
* If the player is a member, coop or trustee, they will be removed from those lists.
|
||||
@ -646,14 +381,158 @@ public class Island {
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes target from the banned list. May be cancelled by unban event.
|
||||
* @param targetUUID
|
||||
* @return true if successful, otherwise false.
|
||||
* @return the banned
|
||||
*/
|
||||
public boolean removeFromBanList(UUID targetUUID) {
|
||||
// TODO fire unban event
|
||||
banned.remove(targetUUID);
|
||||
return true;
|
||||
public Set<UUID> getBanned() {
|
||||
return banned;
|
||||
}
|
||||
/**
|
||||
* @return the center Location
|
||||
*/
|
||||
public Location getCenter(){
|
||||
return center;
|
||||
}
|
||||
/**
|
||||
* @return the coop players of the island
|
||||
*/
|
||||
public Set<UUID> getCoops(){
|
||||
return coops;
|
||||
}
|
||||
/**
|
||||
* @return the date when the island was created
|
||||
*/
|
||||
public long getCreatedDate(){
|
||||
return createdDate;
|
||||
}
|
||||
/**
|
||||
* Get the Island Guard flag status
|
||||
* @param flag
|
||||
* @return true or false, or false if flag is not in the list
|
||||
*/
|
||||
public boolean getFlag(SettingsFlag flag){
|
||||
if(flags.containsKey(flag)) {
|
||||
return flags.get(flag);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the flags
|
||||
*/
|
||||
public HashMap<SettingsFlag, Boolean> getFlags() {
|
||||
return flags;
|
||||
}
|
||||
/**
|
||||
* @return the members of the island (owner included)
|
||||
*/
|
||||
public Set<UUID> getMembers(){
|
||||
return members;
|
||||
}
|
||||
/**
|
||||
* @return the minProtectedX
|
||||
*/
|
||||
public int getMinProtectedX() {
|
||||
return minProtectedX;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the minProtectedZ
|
||||
*/
|
||||
public int getMinProtectedZ() {
|
||||
return minProtectedZ;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the minX
|
||||
*/
|
||||
public int getMinX() {
|
||||
return minX;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the minZ
|
||||
*/
|
||||
public int getMinZ() {
|
||||
return minZ;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the island display name or the owner's name if none is set
|
||||
*/
|
||||
public String getName(){
|
||||
return (name != null) ? name : Bukkit.getServer().getOfflinePlayer(owner).getName();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the owner (team leader)
|
||||
*/
|
||||
public UUID getOwner(){
|
||||
return owner;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the protectionRange
|
||||
*/
|
||||
public int getProtectionRange() {
|
||||
return protectionRange;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the island range
|
||||
*/
|
||||
public int getRange(){
|
||||
return range;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the trustees players of the island
|
||||
*/
|
||||
public Set<UUID> getTrustees(){
|
||||
return trustees;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the date when the island was updated (team member connection, etc...)
|
||||
*/
|
||||
public long getUpdatedDate(){
|
||||
return updatedDate;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the world
|
||||
*/
|
||||
public World getWorld() {
|
||||
return world;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the x coordinate of the island center
|
||||
*/
|
||||
public int getX(){
|
||||
return center.getBlockX();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the y coordinate of the island center
|
||||
*/
|
||||
public int getY(){
|
||||
return center.getBlockY();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the z coordinate of the island center
|
||||
*/
|
||||
public int getZ(){
|
||||
return center.getBlockZ();
|
||||
}
|
||||
|
||||
public boolean inIslandSpace(int x, int z) {
|
||||
if (x >= center.getBlockX() - Settings.islandDistance / 2 && x < center.getBlockX() + Settings.islandDistance / 2 && z >= center.getBlockZ() - Settings.islandDistance / 2
|
||||
&& z < center.getBlockZ() + Settings.islandDistance / 2) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -666,10 +545,182 @@ public class Island {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the protectionRange
|
||||
* @return true if the island is locked, otherwise false
|
||||
*/
|
||||
public int getProtectionRange() {
|
||||
return protectionRange;
|
||||
public boolean getLocked(){
|
||||
return locked;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return true if the island is protected from the Purge, otherwise false
|
||||
*/
|
||||
public boolean getPurgeProtected(){
|
||||
return purgeProtected;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return true if the island is the spawn otherwise false
|
||||
*/
|
||||
public boolean getSpawn(){
|
||||
return spawn;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a location is within this island's protected area
|
||||
*
|
||||
* @param target
|
||||
* @return true if it is, false if not
|
||||
*/
|
||||
public boolean onIsland(Location target) {
|
||||
if (center.getWorld() != null) {
|
||||
if (target.getBlockX() >= minProtectedX && target.getBlockX() < (minProtectedX + protectionRange)
|
||||
&& target.getBlockZ() >= minProtectedZ && target.getBlockZ() < (minProtectedZ + protectionRange)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes target from the banned list. May be cancelled by unban event.
|
||||
* @param targetUUID
|
||||
* @return true if successful, otherwise false.
|
||||
*/
|
||||
public boolean removeFromBanList(UUID targetUUID) {
|
||||
// TODO fire unban event
|
||||
banned.remove(targetUUID);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param banned the banned to set
|
||||
*/
|
||||
public void setBanned(Set<UUID> banned) {
|
||||
this.banned = banned;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param center the center to set
|
||||
*/
|
||||
public void setCenter(Location center) {
|
||||
this.center = center;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param coops - the coops to set
|
||||
*/
|
||||
public void setCoops(Set<UUID> coops){
|
||||
this.coops = coops;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param createdDate - the createdDate to sets
|
||||
*/
|
||||
public void setCreatedDate(long createdDate){
|
||||
this.createdDate = createdDate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the Island Guard flag status
|
||||
* @param flag
|
||||
* @param value
|
||||
*/
|
||||
public void setFlag(SettingsFlag flag, boolean value){
|
||||
flags.put(flag, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param flags the flags to set
|
||||
*/
|
||||
public void setFlags(HashMap<SettingsFlag, Boolean> flags) {
|
||||
this.flags = flags;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets the flags to their default as set in config.yml for this island
|
||||
*/
|
||||
public void setFlagsDefaults(){
|
||||
/*for(SettingsFlag flag : SettingsFlag.values()){
|
||||
this.flags.put(flag, Settings.defaultIslandSettings.get(flag));
|
||||
}*/ //TODO default flags
|
||||
}
|
||||
|
||||
/**
|
||||
* Locks/Unlocks the island. May be cancelled by
|
||||
* {@link IslandLockEvent} or {@link IslandUnlockEvent}.
|
||||
* @param locked - the lock state to set
|
||||
*/
|
||||
public void setLocked(boolean locked){
|
||||
if(locked){
|
||||
// Lock the island
|
||||
IslandLockEvent event = new IslandLockEvent(this);
|
||||
Bukkit.getServer().getPluginManager().callEvent(event);
|
||||
|
||||
if(!event.isCancelled()){
|
||||
this.locked = locked;
|
||||
}
|
||||
} else {
|
||||
// Unlock the island
|
||||
IslandUnlockEvent event = new IslandUnlockEvent(this);
|
||||
Bukkit.getServer().getPluginManager().callEvent(event);
|
||||
|
||||
if(!event.isCancelled()){
|
||||
this.locked = locked;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param members - the members to set
|
||||
*/
|
||||
public void setMembers(Set<UUID> members){
|
||||
this.members = members;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param minProtectedX the minProtectedX to set
|
||||
*/
|
||||
public void setMinProtectedX(int minProtectedX) {
|
||||
this.minProtectedX = minProtectedX;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param minProtectedZ the minProtectedZ to set
|
||||
*/
|
||||
public void setMinProtectedZ(int minProtectedZ) {
|
||||
this.minProtectedZ = minProtectedZ;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param minX the minX to set
|
||||
*/
|
||||
public void setMinX(int minX) {
|
||||
this.minX = minX;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param minZ the minZ to set
|
||||
*/
|
||||
public void setMinZ(int minZ) {
|
||||
this.minZ = minZ;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param name - the display name to set
|
||||
* Set to null to remove the display name
|
||||
*/
|
||||
public void setName(String name){
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the owner of the island. If the owner was previous banned, they are unbanned
|
||||
* @param owner - the owner/team leader to set
|
||||
*/
|
||||
public void setOwner(UUID owner){
|
||||
this.owner = owner;
|
||||
this.banned.remove(owner);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -680,9 +731,74 @@ public class Island {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the banned
|
||||
* @param purgeProtected - if the island is protected from the Purge
|
||||
*/
|
||||
public Set<UUID> getBanned() {
|
||||
return banned;
|
||||
public void setPurgeProtected(boolean purgeProtected){
|
||||
this.purgeProtected = purgeProtected;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param range - the range to set
|
||||
*/
|
||||
public void setRange(int range){
|
||||
this.range = range;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param isSpawn - if the island is the spawn
|
||||
*/
|
||||
public void setSpawn(boolean isSpawn){
|
||||
this.spawn = isSpawn;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets the flags to their default as set in config.yml for the spawn
|
||||
*/
|
||||
public void setSpawnFlagsDefaults(){
|
||||
/*for(SettingsFlag flag : SettingsFlag.values()){
|
||||
this.flags.put(flag, Settings.defaultSpawnSettings.get(flag));
|
||||
}*/ //TODO default flags
|
||||
}
|
||||
|
||||
/**
|
||||
* @param trustees - the trustees to set
|
||||
*/
|
||||
public void setTrustees(Set<UUID> trustees){
|
||||
this.trustees = trustees;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param updatedDate - the updatedDate to sets
|
||||
*/
|
||||
public void setUpdatedDate(long updatedDate){
|
||||
this.updatedDate = updatedDate;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param world the world to set
|
||||
*/
|
||||
public void setWorld(World world) {
|
||||
this.world = world;
|
||||
}
|
||||
|
||||
/**
|
||||
* Toggles the Island Guard flag status if it is in the list
|
||||
* @param flag
|
||||
*/
|
||||
public void toggleFlag(SettingsFlag flag){
|
||||
if(flags.containsKey(flag)) {
|
||||
flags.put(flag, (flags.get(flag)) ? false : true);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUniqueId() {
|
||||
return uniqueId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setUniqueId(String uniqueId) {
|
||||
this.uniqueId = uniqueId;
|
||||
|
||||
}
|
||||
}
|
@ -16,7 +16,7 @@ import us.tastybento.bskyblock.config.Settings;
|
||||
*
|
||||
* @author tastybento
|
||||
*/
|
||||
public class Players {
|
||||
public class Players extends DataObject {
|
||||
private HashMap<Integer, Location> homeLocations;
|
||||
private UUID uuid;
|
||||
private String playerName;
|
||||
@ -248,4 +248,16 @@ public class Players {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUniqueId() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setUniqueId(String uniqueId) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,67 +1,16 @@
|
||||
package us.tastybento.bskyblock.database.sqlite;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import us.tastybento.bskyblock.BSkyBlock;
|
||||
import us.tastybento.bskyblock.database.AbstractDatabaseHandler;
|
||||
import us.tastybento.bskyblock.database.BSBDatabase;
|
||||
import us.tastybento.bskyblock.database.objects.Island;
|
||||
import us.tastybento.bskyblock.database.objects.Players;
|
||||
|
||||
public class SQLiteDatabase extends BSBDatabase{
|
||||
|
||||
@Override
|
||||
public Players loadPlayerData(UUID uuid) {
|
||||
// TODO Auto-generated method stub
|
||||
public AbstractDatabaseHandler<?> getHandler(BSkyBlock plugin, Class<?> type) {
|
||||
// return new SQLLiteDatabaseHandler<Island>(plugin, Island.class, new FlatFileDatabaseConnecter(plugin, null));
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void savePlayerData(Players player) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Island loadIslandData(String location) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveIslandData(Island island) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public HashMap<UUID, List<String>> loadOfflineHistoryMessages() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveOfflineHistoryMessages(HashMap<UUID, List<String>> messages) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPlayerKnown(UUID uniqueID) {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID getUUID(String name, boolean adminCheck) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void savePlayerName(String name, UUID uuid) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user