Performance improvements

Update for MC 1.9
This commit is contained in:
GeorgH93 2016-03-01 00:39:28 +01:00
parent 9be8d64e6f
commit d7aeac4feb
7 changed files with 303 additions and 383 deletions

16
pom.xml
View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>at.pcgamingfreaks</groupId>
<artifactId>MinePacks</artifactId>
<version>1.14-SNAPSHOT</version>
<version>1.14</version>
<scm>
<connection>scm:git:git@github.com:GeorgH93/Bukkit_Minepacks.git</connection>
@ -45,7 +45,7 @@
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
<version>LATEST</version>
<version>1.9-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
@ -53,6 +53,16 @@
<artifactId>Bukkit_Bungee_PluginLib</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.6.4</version>
</dependency>
</dependencies>
<properties>
@ -97,6 +107,8 @@
<artifactSet>
<includes>
<include>at.pcgamingfreaks:Bukkit_Bungee_PluginLib</include>
<include>com.zaxxer:HikariCP</include>
<include>org.slf4j:*</include>
</includes>
</artifactSet>
</configuration>

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2014-2015 GeorgH93
* Copyright (C) 2014-2016 GeorgH93
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -17,45 +17,48 @@
package at.pcgamingfreaks.MinePacks.Database;
import java.util.HashMap;
import at.pcgamingfreaks.MinePacks.Backpack;
import at.pcgamingfreaks.MinePacks.MinePacks;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import at.pcgamingfreaks.MinePacks.Backpack;
import at.pcgamingfreaks.MinePacks.MinePacks;
import java.util.HashMap;
public class Database
{
protected MinePacks plugin;
protected boolean useUUIDs, useUUIDSeparators;
protected long maxAge;
private HashMap<OfflinePlayer, Backpack> backpacks = new HashMap<>();
protected InventorySerializer itsSerializer = new InventorySerializer();
public Database(MinePacks mp)
{
plugin = mp;
useUUIDSeparators = plugin.config.getUseUUIDSeparators();
useUUIDs = plugin.config.getUseUUIDs();
maxAge = plugin.config.getAutoCleanupMaxInactiveDays();
maxAge = plugin.config.getAutoCleanupMaxInactiveDays();
}
public static Database getDatabase(MinePacks Plugin)
{
switch(Plugin.config.getDatabaseType().toLowerCase())
{
case "mysql": return new MySQL(Plugin);
case "mysql":
return new MySQL(Plugin);
case "flat":
case "file":
case "files": return new Files(Plugin);
case "files":
return new Files(Plugin);
case "sqlite":
default: return new SQLite(Plugin);
default:
return new SQLite(Plugin);
}
}
protected String getPlayerNameOrUUID(OfflinePlayer player)
{
if(useUUIDs)
@ -79,11 +82,7 @@ protected String getPlayerFormattedUUID(OfflinePlayer player)
public Backpack getBackpack(OfflinePlayer player)
{
if(player == null)
{
return null;
}
return backpacks.get(player);
return (player == null) ? null : backpacks.get(player);
}
@SuppressWarnings("unused")
@ -138,7 +137,7 @@ public void onFail()
callback.onResult(lbp);
}
}
public void unloadBackpack(Backpack backpack)
{
backpacks.remove(backpack.getOwner());
@ -164,7 +163,7 @@ public void onFail()
});
}
}
// DB Functions
public void close() { }
@ -173,11 +172,11 @@ public void updatePlayerAndLoadBackpack(Player player)
updatePlayer(player);
asyncLoadBackpack(player);
}
public void updatePlayer(Player player) {}
public void saveBackpack(Backpack backpack) {}
protected Backpack loadBackpack(OfflinePlayer player) { return null; }
protected void loadBackpack(final OfflinePlayer player, final Callback<Backpack> callback)

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2014-2015 GeorgH93
* Copyright (C) 2014-2016 GeorgH93
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -27,10 +27,11 @@ public Language(JavaPlugin plugin)
{
super(plugin, LANG_VERSION);
}
protected void doUpdate(int version)
@Override
protected void doUpdate()
{
switch(version)
switch(getVersion())
{
case 1: lang.set("Language.Ingame.Cooldown", "Please wait till you reopen your backpack.");
case 2: lang.set("Language.Ingame.InvalidBackpack", lang.getString("Language.Ingame.IvalidBackpack", "Invalid backpack."));

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2014-2015 GeorgH93
* Copyright (C) 2014-2016 GeorgH93
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -17,29 +17,32 @@
package at.pcgamingfreaks.MinePacks.Database;
import at.pcgamingfreaks.MinePacks.MinePacks;
import com.zaxxer.hikari.HikariConfig;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import at.pcgamingfreaks.MinePacks.Backpack;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import at.pcgamingfreaks.MinePacks.MinePacks;
import org.bukkit.inventory.ItemStack;
public class MySQL extends SQL
{
public MySQL(MinePacks mp)
{
super(mp); // Load Settings
}
// Fire DB request every 10 minutes to keep database connection alive
plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable(){ @Override public void run() { try { getConnection().createStatement().execute("SELECT 1"); }
catch(Exception ignored) {} }}, 600*20, 600*20);
@Override
protected HikariConfig getPoolConfig()
{
HikariConfig poolConfig = new HikariConfig();
poolConfig.setJdbcUrl("jdbc:mysql://" + plugin.config.getMySQLHost() + "/" + plugin.config.getMySQLDatabase() + "?allowMultiQueries=true&autoReconnect=true");
poolConfig.setUsername(plugin.config.getMySQLUser());
poolConfig.setPassword(plugin.config.getMySQLPassword());
poolConfig.setMinimumIdle(1);
poolConfig.setMaximumPoolSize(8);
return poolConfig;
}
@Override
@ -49,42 +52,11 @@ protected void updateQuerysForDialect()
Query_UpdateBP = Query_UpdateBP.replaceAll("\\{NOW\\}", "NOW()");
}
@Override
protected Connection getConnection()
{
try
{
if(conn == null || conn.isClosed())
{
conn = DriverManager.getConnection("jdbc:mysql://" + plugin.config.getMySQLHost() + "/" + plugin.config.getMySQLDatabase() + "?autoReconnect=true&timeBetweenEvictionRunsMillis=300000&testWhileIdle=true", plugin.config.getMySQLUser(), plugin.config.getMySQLPassword());
}
}
catch (SQLException e)
{
e.printStackTrace();
}
return conn;
}
protected Connection getNewConnection()
{
try
{
return DriverManager.getConnection("jdbc:mysql://" + plugin.config.getMySQLHost() + "/" + plugin.config.getMySQLDatabase() + "?autoReconnect=true&timeBetweenEvictionRunsMillis=300000&testWhileIdle=true", plugin.config.getMySQLUser(), plugin.config.getMySQLPassword());
}
catch (SQLException e)
{
e.printStackTrace();
}
return null;
}
@Override
protected void checkDB()
{
try
try(Connection connection = getConnection(); Statement stmt = connection.createStatement())
{
Statement stmt = getConnection().createStatement();
ResultSet res;
if(useUUIDs)
{
@ -137,172 +109,10 @@ protected void checkDB()
}
res.close();
}
stmt.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
// Plugin Functions
@Override
public void updatePlayer(final Player player)
{
if(!UpdatePlayer)
{
return;
}
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable()
{
@Override
public void run()
{
try
{
Connection con = getNewConnection();
PreparedStatement ps = con.prepareStatement(Query_UpdatePlayerAdd);
ps.setString(1, player.getName());
if(useUUIDs)
{
String uuid = getPlayerFormattedUUID(player);
ps.setString(2, uuid);
ps.setString(3, player.getName());
}
ps.execute();
ps.close();
con.close();
}
catch(SQLException e)
{
plugin.log.info("Failed to add/update user: " + player.getName());
e.printStackTrace();
}
}
});
}
@Override
protected void loadBackpack(final OfflinePlayer player, final Callback<Backpack> callback)
{
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {
@Override
public void run()
{
try
{
Connection conn = getNewConnection();
PreparedStatement ps = getConnection().prepareStatement(Query_GetBP);
ps.setString(1, getPlayerNameOrUUID(player));
ResultSet rs = ps.executeQuery();
final int bpID, version;
final byte[] data;
if(rs.next())
{
bpID = rs.getInt(1);
version = rs.getInt(3);
data = rs.getBytes(2);
}
else
{
bpID = -1;
version = 0;
data = null;
}
plugin.getServer().getScheduler().runTask(plugin, new Runnable()
{
@Override
public void run()
{
ItemStack[] its = (data != null) ? itsSerializer.deserialize(data, version) : null;
if(its != null)
{
callback.onResult(new Backpack(player, its, bpID));
}
else
{
callback.onFail();
}
}
});
rs.close();
ps.close();
conn.close();
}
catch(Exception e)
{
e.printStackTrace();
callback.onFail();
}
}
});
}
@Override
public void saveBackpack(final Backpack backpack)
{
final byte[] data = itsSerializer.serialize(backpack.getInventory());
final int id = backpack.getOwnerID();
final String nameOrUUID = getPlayerNameOrUUID(backpack.getOwner()), name = backpack.getOwner().getName();
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {
@Override
public void run()
{
try
{
Connection conn = getNewConnection();
PreparedStatement ps; // Statement Variable
// Building the mysql statement
if(id <= 0)
{
final int newID;
ps = conn.prepareStatement(Query_GetPlayerID);
ps.setString(1, nameOrUUID);
ResultSet rs = ps.executeQuery();
if(rs.next())
{
newID = rs.getInt(1);
plugin.getServer().getScheduler().runTask(plugin, new Runnable() {
@Override
public void run()
{
backpack.setOwnerID(newID);
}
});
}
else
{
newID = -1;
}
rs.close();
ps.close();
if(newID <= 0)
{
plugin.log.warning("Failed saving backpack for: " + name + " (Unable to get players ID from database)");
conn.close();
return;
}
ps = conn.prepareStatement(Query_InsertBP);
ps.setInt(1, newID);
ps.setBytes(2, data);
ps.setInt(3, itsSerializer.getUsedSerializer());
}
else
{
ps = conn.prepareStatement(Query_UpdateBP);
ps.setBytes(1, data);
ps.setInt(2, itsSerializer.getUsedSerializer());
ps.setInt(3, id);
}
ps.execute();
ps.close();
conn.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
});
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2014-2015 GeorgH93
* Copyright (C) 2014-2016 GeorgH93
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -17,31 +17,40 @@
package at.pcgamingfreaks.MinePacks.Database;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import at.pcgamingfreaks.MinePacks.Backpack;
import at.pcgamingfreaks.MinePacks.MinePacks;
import at.pcgamingfreaks.UUIDConverter;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import at.pcgamingfreaks.MinePacks.Backpack;
import at.pcgamingfreaks.MinePacks.MinePacks;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public abstract class SQL extends Database
{
protected Connection conn = null;
private HikariDataSource dataSource;
protected String Table_Players, Table_Backpacks; // Table Names
protected String Field_Name, Field_PlayerID, Field_UUID, Field_BPOwner, Field_BPITS, Field_BPVersion, Field_BPLastUpdate; // Table Fields
protected String Query_UpdatePlayerAdd, Query_GetPlayerID, Query_InsertBP, Query_UpdateBP, Query_GetBP, Query_DeleteOldBackpacks, Query_GetUnsetOrInvalidUUIDs, Query_FixUUIDs; // DB Querys
protected boolean UpdatePlayer;
public SQL(MinePacks mp)
{
super(mp);
dataSource = new HikariDataSource(getPoolConfig());
loadSettings();
buildQuerys();
checkDB();
@ -57,35 +66,33 @@ public SQL(MinePacks mp)
{
getConnection().createStatement().execute(Query_DeleteOldBackpacks);
}
catch (SQLException e)
catch(SQLException e)
{
e.printStackTrace();
}
}
}
protected abstract HikariConfig getPoolConfig();
protected void loadSettings()
{
// Load table and field names
Table_Players = plugin.config.getUserTable();
Table_Backpacks = plugin.config.getBackpackTable();
Field_PlayerID = plugin.config.getDBFields("User.Player_ID");
Field_Name = plugin.config.getDBFields("User.Name");
Field_UUID = plugin.config.getDBFields("User.UUID");
Field_BPOwner = plugin.config.getDBFields("Backpack.Owner_ID");
Field_BPITS = plugin.config.getDBFields("Backpack.ItemStacks");
Field_BPVersion = plugin.config.getDBFields("Backpack.Version");
Field_BPLastUpdate = plugin.config.getDBFields("Backpack.LastUpdate");
UpdatePlayer = plugin.config.getUpdatePlayer();
Table_Players = plugin.config.getUserTable();
Table_Backpacks = plugin.config.getBackpackTable();
Field_PlayerID = plugin.config.getDBFields("User.Player_ID");
Field_Name = plugin.config.getDBFields("User.Name");
Field_UUID = plugin.config.getDBFields("User.UUID");
Field_BPOwner = plugin.config.getDBFields("Backpack.Owner_ID");
Field_BPITS = plugin.config.getDBFields("Backpack.ItemStacks");
Field_BPVersion = plugin.config.getDBFields("Backpack.Version");
Field_BPLastUpdate = plugin.config.getDBFields("Backpack.LastUpdate");
UpdatePlayer = plugin.config.getUpdatePlayer();
}
public void close()
{
try
{
conn.close();
}
catch(Exception ignored) { }
dataSource.close();
}
protected void checkUUIDs()
@ -94,7 +101,13 @@ class UpdateData // Helper class for fixing UUIDs
{
int id;
String name, uuid;
public UpdateData(String name, String uuid, int id) { this.id = id; this.name = name; this.uuid = uuid; }
public UpdateData(String name, String uuid, int id)
{
this.id = id;
this.name = name;
this.uuid = uuid;
}
}
try
{
@ -112,7 +125,7 @@ class UpdateData // Helper class for fixing UUIDs
res.close();
if(converter.size() > 0)
{
for (UpdateData data : converter)
for(UpdateData data : converter)
{
if(data.uuid == null)
{
@ -128,16 +141,19 @@ class UpdateData // Helper class for fixing UUIDs
}
ps.close();
}
catch (SQLException e)
catch(SQLException e)
{
e.printStackTrace();
}
}
protected abstract Connection getConnection();
protected Connection getConnection() throws SQLException
{
return dataSource.getConnection();
}
protected abstract void checkDB();
protected final void buildQuerys()
{
// Build the SQL querys with placeholders for the table and field names
@ -177,9 +193,9 @@ protected final void buildQuerys()
// Replace the table and filed names with the names from the config
Query_UpdatePlayerAdd = Query_UpdatePlayerAdd.replaceAll("\\{TablePlayers\\}", Table_Players).replaceAll("\\{FieldName\\}", Field_Name).replaceAll("\\{FieldUUID\\}", Field_UUID).replaceAll("\\{FieldPlayerID\\}", Field_PlayerID);
Query_GetPlayerID = Query_GetPlayerID.replaceAll("\\{TablePlayers\\}", Table_Players).replaceAll("\\{FieldName\\}", Field_Name).replaceAll("\\{FieldUUID\\}", Field_UUID).replaceAll("\\{FieldPlayerID\\}", Field_PlayerID);
Query_GetBP = Query_GetBP.replaceAll("\\{FieldBPOwner\\}", Field_BPOwner).replaceAll("\\{FieldBPITS\\}",Field_BPITS).replaceAll("\\{FieldBPVersion\\}", Field_BPVersion).replaceAll("\\{TableBackpacks\\}", Table_Backpacks).replaceAll("\\{TablePlayers\\}", Table_Players).replaceAll("\\{FieldPlayerID\\}", Field_PlayerID).replaceAll("\\{FieldName\\}", Field_Name).replaceAll("\\{FieldUUID\\}", Field_UUID);
Query_InsertBP = Query_InsertBP.replaceAll("\\{TableBackpacks\\}", Table_Backpacks).replaceAll("\\{FieldBPOwner\\}", Field_BPOwner).replaceAll("\\{FieldBPITS\\}",Field_BPITS).replaceAll("\\{FieldBPVersion\\}", Field_BPVersion).replaceAll("\\{FieldBPLastUpdate\\}", Field_BPLastUpdate);
Query_UpdateBP = Query_UpdateBP.replaceAll("\\{TableBackpacks\\}", Table_Backpacks).replaceAll("\\{FieldBPOwner\\}", Field_BPOwner).replaceAll("\\{FieldBPITS\\}",Field_BPITS).replaceAll("\\{FieldBPVersion\\}", Field_BPVersion).replaceAll("\\{FieldBPLastUpdate\\}", Field_BPLastUpdate);
Query_GetBP = Query_GetBP.replaceAll("\\{FieldBPOwner\\}", Field_BPOwner).replaceAll("\\{FieldBPITS\\}", Field_BPITS).replaceAll("\\{FieldBPVersion\\}", Field_BPVersion).replaceAll("\\{TableBackpacks\\}", Table_Backpacks).replaceAll("\\{TablePlayers\\}", Table_Players).replaceAll("\\{FieldPlayerID\\}", Field_PlayerID).replaceAll("\\{FieldName\\}", Field_Name).replaceAll("\\{FieldUUID\\}", Field_UUID);
Query_InsertBP = Query_InsertBP.replaceAll("\\{TableBackpacks\\}", Table_Backpacks).replaceAll("\\{FieldBPOwner\\}", Field_BPOwner).replaceAll("\\{FieldBPITS\\}", Field_BPITS).replaceAll("\\{FieldBPVersion\\}", Field_BPVersion).replaceAll("\\{FieldBPLastUpdate\\}", Field_BPLastUpdate);
Query_UpdateBP = Query_UpdateBP.replaceAll("\\{TableBackpacks\\}", Table_Backpacks).replaceAll("\\{FieldBPOwner\\}", Field_BPOwner).replaceAll("\\{FieldBPITS\\}", Field_BPITS).replaceAll("\\{FieldBPVersion\\}", Field_BPVersion).replaceAll("\\{FieldBPLastUpdate\\}", Field_BPLastUpdate);
Query_DeleteOldBackpacks = Query_DeleteOldBackpacks.replaceAll("\\{TableBackpacks\\}", Table_Backpacks).replaceAll("\\{FieldBPLastUpdate\\}", Field_BPLastUpdate).replaceAll("\\{VarMaxAge\\}", maxAge + "");
Query_GetUnsetOrInvalidUUIDs = Query_GetUnsetOrInvalidUUIDs.replaceAll("\\{TablePlayers\\}", Table_Players).replaceAll("\\{FieldName\\}", Field_Name).replaceAll("\\{FieldUUID\\}", Field_UUID).replaceAll("\\{FieldPlayerID\\}", Field_PlayerID);
Query_FixUUIDs = Query_FixUUIDs.replaceAll("\\{TablePlayers\\}", Table_Players).replaceAll("\\{FieldUUID\\}", Field_UUID).replaceAll("\\{FieldPlayerID\\}", Field_PlayerID);
@ -187,105 +203,185 @@ protected final void buildQuerys()
protected abstract void updateQuerysForDialect();
private void runStatementAsync(final String query, final Object... args)
{
Bukkit.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable()
{
@Override
public void run()
{
runStatement(query, args);
}
});
}
private void runStatement(final String query, final Object... args)
{
try(Connection connection = getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(query))
{
for(int i = 0; args != null && i < args.length; i++)
{
preparedStatement.setObject(i + 1, args[i]);
}
preparedStatement.execute();
}
catch(SQLException e)
{
System.out.print("Query: " + query);
e.printStackTrace();
}
}
// Plugin Functions
public void updatePlayer(final Player player)
{
try
if(useUUIDs)
{
PreparedStatement ps = getConnection().prepareStatement(Query_UpdatePlayerAdd);
ps.setString(1, player.getName());
if(useUUIDs)
{
String uuid = getPlayerFormattedUUID(player);
ps.setString(2, uuid);
ps.setString(3, player.getName());
}
ps.execute();
ps.close();
runStatementAsync(Query_UpdatePlayerAdd, player.getName(), getPlayerFormattedUUID(player), player.getName());
}
else
{
runStatementAsync(Query_UpdatePlayerAdd, player.getName());
}
catch (SQLException e)
{
plugin.log.info("Failed to add/update user: " + player.getName());
e.printStackTrace();
}
}
public int getPlayerID(OfflinePlayer player)
public void saveBackpack(final Backpack backpack)
{
int id = -1;
try
{
PreparedStatement ps = getConnection().prepareStatement(Query_GetPlayerID);
ps.setString(1, getPlayerNameOrUUID(player));
ResultSet rs = ps.executeQuery();
if(rs.next())
{
id = rs.getInt(1);
}
rs.close();
ps.close();
}
catch(Exception e)
{
e.printStackTrace();
}
return id;
}
final byte[] data = itsSerializer.serialize(backpack.getInventory());
final int id = backpack.getOwnerID(), usedSerializer = itsSerializer.getUsedSerializer();
final String nameOrUUID = getPlayerNameOrUUID(backpack.getOwner()), name = backpack.getOwner().getName();
public void saveBackpack(Backpack backpack)
{
try
Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable()
{
PreparedStatement ps; // Statement Variable
// Building the mysql statement
if(backpack.getOwnerID() <= 0)
@Override
public void run()
{
backpack.setOwnerID(getPlayerID(backpack.getOwner()));
if(backpack.getOwnerID() <= 0)
try(Connection connection = getConnection())
{
plugin.log.warning("Failed saving backpack for: " + backpack.getOwner().getName() + " (Unable to get players ID from database)");
return;
if(id <= 0)
{
final int newID;
try(PreparedStatement ps = connection.prepareStatement(Query_GetPlayerID))
{
ps.setString(1, nameOrUUID);
try(ResultSet rs = ps.executeQuery())
{
if(rs.next())
{
newID = rs.getInt(1);
plugin.getServer().getScheduler().runTask(plugin, new Runnable()
{
@Override
public void run()
{
backpack.setOwnerID(newID);
}
});
}
else
{
newID = -1;
}
}
}
if(newID <= 0)
{
plugin.log.warning("Failed saving backpack for: " + name + " (Unable to get players ID from database)");
return;
}
try(PreparedStatement ps = connection.prepareStatement(Query_InsertBP))
{
ps.setInt(1, newID);
ps.setBytes(2, data);
ps.setInt(3, usedSerializer);
ps.execute();
}
}
else
{
try(PreparedStatement ps = connection.prepareStatement(Query_UpdateBP))
{
ps.setBytes(1, data);
ps.setInt(2, usedSerializer);
ps.setInt(3, id);
ps.execute();
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
ps = getConnection().prepareStatement(Query_InsertBP);
ps.setInt(1, backpack.getOwnerID());
ps.setBytes(2, itsSerializer.serialize(backpack.getInventory()));
ps.setInt(3, itsSerializer.getUsedSerializer());
ps.execute();
ps.close();
return;
}
else
{
ps = getConnection().prepareStatement(Query_UpdateBP);
ps.setBytes(1, itsSerializer.serialize(backpack.getInventory()));
ps.setInt(2, itsSerializer.getUsedSerializer());
ps.setInt(3, backpack.getOwnerID());
}
ps.execute();
ps.close();
}
catch(Exception e)
{
e.printStackTrace();
}
});
}
public Backpack loadBackpack(OfflinePlayer player)
@Override
protected void loadBackpack(final OfflinePlayer player, final Callback<Backpack> callback)
{
try
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable()
{
PreparedStatement ps = getConnection().prepareStatement(Query_GetBP);
ps.setString(1, getPlayerNameOrUUID(player));
ResultSet rs = ps.executeQuery();
if(!rs.next())
@Override
public void run()
{
return null;
try(Connection conn = getConnection(); PreparedStatement ps = conn.prepareStatement(Query_GetBP))
{
ps.setString(1, getPlayerNameOrUUID(player));
final int bpID, version;
final byte[] data;
try(ResultSet rs = ps.executeQuery())
{
if(rs.next())
{
bpID = rs.getInt(1);
version = rs.getInt(3);
data = rs.getBytes(2);
}
else
{
bpID = -1;
version = 0;
data = null;
}
}
plugin.getServer().getScheduler().runTask(plugin, new Runnable()
{
@Override
public void run()
{
ItemStack[] its = (data != null) ? itsSerializer.deserialize(data, version) : null;
if(its != null)
{
callback.onResult(new Backpack(player, its, bpID));
}
else
{
callback.onFail();
}
}
});
}
catch(Exception e)
{
e.printStackTrace();
callback.onFail();
}
}
});
}
public Backpack loadBackpack(OfflinePlayer player) // The sync function shouldn't be called at all
{
try(Connection connection = getConnection(); PreparedStatement ps = connection.prepareStatement(Query_GetBP))
{
ps.setString(1, getPlayerNameOrUUID(player));
try(ResultSet rs = ps.executeQuery())
{
if(rs.next())
{
return new Backpack(player, itsSerializer.deserialize(rs.getBytes(2), rs.getInt(3)), rs.getInt(1));
}
}
int bpID = rs.getInt(1);
ItemStack[] its = itsSerializer.deserialize(rs.getBytes(2), rs.getInt(3));
rs.close();
ps.close();
return new Backpack(player, its, bpID);
}
catch(Exception e)
{

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2014-2015 GeorgH93
* Copyright (C) 2014-2016 GeorgH93
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -17,15 +17,16 @@
package at.pcgamingfreaks.MinePacks.Database;
import at.pcgamingfreaks.MinePacks.MinePacks;
import com.zaxxer.hikari.HikariConfig;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import at.pcgamingfreaks.MinePacks.MinePacks;
public class SQLite extends SQL
{
public SQLite(MinePacks mp)
@ -51,6 +52,24 @@ protected void loadSettings()
UpdatePlayer = true;
}
@Override
protected HikariConfig getPoolConfig()
{
try
{
Class.forName("org.sqlite.JDBC");
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
return null;
}
HikariConfig poolConfig = new HikariConfig();
poolConfig.setJdbcUrl("jdbc:sqlite:" + plugin.getDataFolder().getAbsolutePath() + File.separator + "backpack.db");
poolConfig.setConnectionTestQuery("SELECT 1;");
return poolConfig;
}
@Override
protected void updateQuerysForDialect()
{
@ -63,45 +82,30 @@ protected void updateQuerysForDialect()
Query_UpdatePlayerAdd = Query_UpdatePlayerAdd.replaceAll("INSERT IGNORE INTO", "INSERT OR IGNORE INTO");
}
@Override
protected Connection getConnection()
{
try
{
if(conn == null || conn.isClosed())
{
Class.forName("org.sqlite.JDBC"); // Throws an exception if the SQLite driver is not found.
conn = DriverManager.getConnection("jdbc:sqlite:" + plugin.getDataFolder().getAbsolutePath() + File.separator + "backpack.db");
}
}
catch (ClassNotFoundException | SQLException e)
{
e.printStackTrace();
}
return conn;
}
@Override
protected void checkDB()
{
try
try(Connection connection = getConnection(); Statement stmt = connection.createStatement())
{
Statement stmt = getConnection().createStatement();
stmt.execute("CREATE TABLE IF NOT EXISTS `backpack_players` (`player_id` INTEGER PRIMARY KEY AUTOINCREMENT, `name` CHAR(16) NOT NULL UNIQUE" + ((useUUIDs) ? ", `uuid` CHAR(32) UNIQUE" : "") +");");
stmt.execute("CREATE TABLE IF NOT EXISTS `backpack_players` (`player_id` INTEGER PRIMARY KEY AUTOINCREMENT, `name` CHAR(16) NOT NULL UNIQUE" + ((useUUIDs) ? ", `uuid` CHAR(32) UNIQUE" : "") + ");");
if(useUUIDs)
{
try
{
stmt.execute("ALTER TABLE `backpack_players` ADD COLUMN `uuid` CHAR(32);");
}
catch(SQLException ignored) {}
catch(SQLException ignored)
{
}
}
stmt.execute("CREATE TABLE IF NOT EXISTS `backpacks` (`owner` INT UNSIGNED PRIMARY KEY, `itemstacks` BLOB, `version` INT DEFAULT 0);");
try
{
stmt.execute("ALTER TABLE `backpacks` ADD COLUMN `version` INT DEFAULT 0;");
}
catch(SQLException ignored) {}
catch(SQLException ignored)
{
}
if(maxAge > 0)
{
try
@ -110,11 +114,12 @@ protected void checkDB()
rs.next();
stmt.execute("ALTER TABLE `backpacks` ADD COLUMN `lastupdate` DATE DEFAULT '" + rs.getString(1) + "';");
}
catch(SQLException ignored) {}
catch(SQLException ignored)
{
}
}
stmt.close();
}
catch (SQLException e)
catch(SQLException e)
{
e.printStackTrace();
}

View File

@ -101,10 +101,7 @@ public void onResult(Backpack backpack)
}
@Override
public void onFail()
{
}
public void onFail() {}
});
}