mirror of
https://github.com/exituser/Brewery.git
synced 2026-09-17 04:19:01 +00:00
SQL Saving Fixes
This commit is contained in:
@@ -126,6 +126,10 @@ public class BPlayer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void sqlRemoved(UUID uuid) {
|
||||||
|
players.remove(uuid.toString());
|
||||||
|
}
|
||||||
|
|
||||||
public static int numDrunkPlayers() {
|
public static int numDrunkPlayers() {
|
||||||
return players.size();
|
return players.size();
|
||||||
}
|
}
|
||||||
@@ -192,7 +196,7 @@ public class BPlayer {
|
|||||||
if (bPlayer.drunkeness > 100) {
|
if (bPlayer.drunkeness > 100) {
|
||||||
bPlayer.drinkCap(player);
|
bPlayer.drinkCap(player);
|
||||||
}
|
}
|
||||||
bPlayer.syncToSQL();
|
bPlayer.syncToSQL(false);
|
||||||
//player.sendMessage("Betrunkenheit: §8[§7⭑⭑⭑⭒§0⭑§8] §8[§6|||||||||||||||||§0|||||||||§8]");
|
//player.sendMessage("Betrunkenheit: §8[§7⭑⭑⭑⭒§0⭑§8] §8[§6|||||||||||||||||§0|||||||||§8]");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -201,7 +205,7 @@ public class BPlayer {
|
|||||||
public void drinkCap(Player player) {
|
public void drinkCap(Player player) {
|
||||||
quality = getQuality() * 100;
|
quality = getQuality() * 100;
|
||||||
drunkeness = 100;
|
drunkeness = 100;
|
||||||
syncToSQL();
|
syncToSQL(false);
|
||||||
if (BConfig.overdrinkKick && !player.hasPermission("brewery.bypass.overdrink")) {
|
if (BConfig.overdrinkKick && !player.hasPermission("brewery.bypass.overdrink")) {
|
||||||
P.p.getServer().getScheduler().scheduleSyncDelayedTask(P.p, () -> passOut(player), 1);
|
P.p.getServer().getScheduler().scheduleSyncDelayedTask(P.p, () -> passOut(player), 1);
|
||||||
} else {
|
} else {
|
||||||
@@ -244,11 +248,11 @@ public class BPlayer {
|
|||||||
}
|
}
|
||||||
quality = getQuality();
|
quality = getQuality();
|
||||||
if (drunkeness <= -offlineDrunk) {
|
if (drunkeness <= -offlineDrunk) {
|
||||||
syncToSQL();
|
syncToSQL(true);
|
||||||
return drunkeness <= -BConfig.hangoverTime;
|
return drunkeness <= -BConfig.hangoverTime;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
syncToSQL();
|
syncToSQL(offlineDrunk > 0);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -304,7 +308,7 @@ public class BPlayer {
|
|||||||
public void passOut(Player player) {
|
public void passOut(Player player) {
|
||||||
player.kickPlayer(P.p.languageReader.get("Player_DrunkPassOut"));
|
player.kickPlayer(P.p.languageReader.get("Player_DrunkPassOut"));
|
||||||
offlineDrunk = drunkeness;
|
offlineDrunk = drunkeness;
|
||||||
syncToSQL();
|
syncToSQL(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -356,9 +360,13 @@ public class BPlayer {
|
|||||||
goHome(player);
|
goHome(player);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
hangoverEffects(player);
|
if (offlineDrunk > 20) {
|
||||||
// wird der spieler noch gebraucht?
|
hangoverEffects(player);
|
||||||
remove(player);
|
}
|
||||||
|
if (drunkeness <= 0) {
|
||||||
|
// wird der spieler noch gebraucht?
|
||||||
|
remove(player);
|
||||||
|
}
|
||||||
|
|
||||||
} else if (offlineDrunk - drunkeness >= 30) {
|
} else if (offlineDrunk - drunkeness >= 30) {
|
||||||
Location randomLoc = Wakeup.getRandom(player.getLocation());
|
Location randomLoc = Wakeup.getRandom(player.getLocation());
|
||||||
@@ -368,16 +376,15 @@ public class BPlayer {
|
|||||||
P.p.msg(player, P.p.languageReader.get("Player_Wake"));
|
P.p.msg(player, P.p.languageReader.get("Player_Wake"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
offlineDrunk = 0;
|
||||||
|
syncToSQL(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
offlineDrunk = 0;
|
offlineDrunk = 0;
|
||||||
syncToSQL();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void disconnecting() {
|
public void disconnecting() {
|
||||||
offlineDrunk = drunkeness;
|
offlineDrunk = drunkeness;
|
||||||
syncToSQL();
|
syncToSQL(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void goHome(final Player player) {
|
public void goHome(final Player player) {
|
||||||
@@ -692,9 +699,9 @@ public class BPlayer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Sync Drunkeness Data to SQL if enabled
|
// Sync Drunkeness Data to SQL if enabled
|
||||||
public void syncToSQL() {
|
public void syncToSQL(boolean playerOffline) {
|
||||||
if (BConfig.sqlDrunkSync && BConfig.sqlSync != null) {
|
if (BConfig.sqlDrunkSync && BConfig.sqlSync != null) {
|
||||||
BConfig.sqlSync.updatePlayer(UUID.fromString(uuid), this);
|
BConfig.sqlSync.updatePlayer(UUID.fromString(uuid), this, playerOffline);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -734,7 +741,7 @@ public class BPlayer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.drunkeness = drunkeness;
|
this.drunkeness = drunkeness;
|
||||||
syncToSQL();
|
syncToSQL(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getQuality() {
|
public int getQuality() {
|
||||||
@@ -748,6 +755,10 @@ public class BPlayer {
|
|||||||
return Math.round((float) quality / (float) drunkeness);
|
return Math.round((float) quality / (float) drunkeness);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getQualityData() {
|
||||||
|
return quality;
|
||||||
|
}
|
||||||
|
|
||||||
// opposite of quality
|
// opposite of quality
|
||||||
public int getHangoverQuality() {
|
public int getHangoverQuality() {
|
||||||
if (drunkeness < 0) {
|
if (drunkeness < 0) {
|
||||||
|
|||||||
@@ -19,20 +19,19 @@ public class SQLSync {
|
|||||||
private String user, password;
|
private String user, password;
|
||||||
|
|
||||||
|
|
||||||
public void updatePlayer(UUID uuid, BPlayer bPlayer) {
|
public void updatePlayer(UUID uuid, BPlayer bPlayer, boolean offlineDrain) {
|
||||||
removePlayer(uuid);
|
|
||||||
SQLData_BP bP = new SQLData_BP();
|
SQLData_BP bP = new SQLData_BP();
|
||||||
bP.uuid = uuid;
|
bP.uuid = uuid;
|
||||||
bP.drunkeness = bPlayer.getDrunkeness();
|
bP.drunkeness = bPlayer.getDrunkeness();
|
||||||
bP.offlineDrunk = bPlayer.getOfflineDrunkeness();
|
bP.offlineDrunk = bPlayer.getOfflineDrunkeness();
|
||||||
bP.quality = bPlayer.getQuality();
|
bP.quality = bPlayer.getQualityData();
|
||||||
bP.data = null;
|
bP.data = null;
|
||||||
|
bP.offlineDrain = offlineDrain;
|
||||||
|
|
||||||
addSaveData(bP);
|
addSaveData(bP);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateData(String name, String data) {
|
public void updateData(String name, String data) {
|
||||||
removeData(name);
|
|
||||||
SQLData_BD bD = new SQLData_BD();
|
SQLData_BD bD = new SQLData_BD();
|
||||||
bD.name = name;
|
bD.name = name;
|
||||||
bD.data = data;
|
bD.data = data;
|
||||||
@@ -78,16 +77,20 @@ public class SQLSync {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Statement statement = connection.createStatement();
|
Statement statement = connection.createStatement();
|
||||||
if (statement.execute("SELECT * FROM BreweryZ_BPlayers WHERE uuid = " + uuid.toString() + ";")) {
|
if (statement.execute("SELECT * FROM Brewery_Z_BPlayers WHERE uuid = '" + uuid.toString() + "';")) {
|
||||||
final ResultSet result = statement.getResultSet();
|
final ResultSet result = statement.getResultSet();
|
||||||
P.p.getServer().getScheduler().runTask(P.p, () -> {
|
if (result.next()) {
|
||||||
try {
|
P.p.getServer().getScheduler().runTask(P.p, () -> {
|
||||||
new BPlayer(uuid.toString(), result.getInt("quality"), result.getInt("drunkeness"), result.getInt("offlineDrunk"));
|
try {
|
||||||
} catch (SQLException e) {
|
new BPlayer(uuid.toString(), result.getInt("quality"), result.getInt("drunkeness"), result.getInt("offlineDrunk"));
|
||||||
e.printStackTrace();
|
} catch (SQLException e) {
|
||||||
}
|
e.printStackTrace();
|
||||||
});
|
}
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
P.p.getServer().getScheduler().runTask(P.p, () -> BPlayer.sqlRemoved(uuid));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
@@ -115,7 +118,7 @@ public class SQLSync {
|
|||||||
connection = DriverManager.getConnection(str, user, password);
|
connection = DriverManager.getConnection(str, user, password);
|
||||||
|
|
||||||
Statement statement = connection.createStatement();
|
Statement statement = connection.createStatement();
|
||||||
statement.execute("CREATE TABLE IF NOT EXISTS BreweryZ_BPlayers (" +
|
statement.execute("CREATE TABLE IF NOT EXISTS Brewery_Z_BPlayers (" +
|
||||||
"uuid CHAR(36) NOT NULL, " +
|
"uuid CHAR(36) NOT NULL, " +
|
||||||
"quality INT, " +
|
"quality INT, " +
|
||||||
"drunkeness INT, " +
|
"drunkeness INT, " +
|
||||||
@@ -123,8 +126,8 @@ public class SQLSync {
|
|||||||
"data VARCHAR(127), " +
|
"data VARCHAR(127), " +
|
||||||
"PRIMARY KEY (uuid));");
|
"PRIMARY KEY (uuid));");
|
||||||
statement = connection.createStatement();
|
statement = connection.createStatement();
|
||||||
statement.execute("CREATE TABLE IF NOT EXISTS BreweryZ_BData (" +
|
statement.execute("CREATE TABLE IF NOT EXISTS Brewery_Z_BData (" +
|
||||||
"id SMALLINT AUTOINCREMENT, " +
|
"id SMALLINT AUTO_INCREMENT, " +
|
||||||
"name VARCHAR(127) NOT NULL UNIQUE, " +
|
"name VARCHAR(127) NOT NULL UNIQUE, " +
|
||||||
"data TEXT, " +
|
"data TEXT, " +
|
||||||
"PRIMARY KEY (id));");
|
"PRIMARY KEY (id));");
|
||||||
@@ -169,6 +172,7 @@ public class SQLSync {
|
|||||||
public int drunkeness;
|
public int drunkeness;
|
||||||
public int offlineDrunk;
|
public int offlineDrunk;
|
||||||
public String data;
|
public String data;
|
||||||
|
public boolean offlineDrain;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class SQLData_BD {
|
private static class SQLData_BD {
|
||||||
@@ -202,7 +206,27 @@ public class SQLSync {
|
|||||||
|
|
||||||
if (o instanceof SQLData_BP) {
|
if (o instanceof SQLData_BP) {
|
||||||
SQLData_BP d = ((SQLData_BP) o);
|
SQLData_BP d = ((SQLData_BP) o);
|
||||||
PreparedStatement ps = connection.prepareStatement("INSERT INTO BreweryZ_BPlayers (uuid, quality, drunkeness, offlineDrunk, data) VALUES (?, ?, ?, ?, ?);");
|
PreparedStatement ps;
|
||||||
|
|
||||||
|
if (d.offlineDrain) {
|
||||||
|
// We need to check if the player data is changed by some other server
|
||||||
|
// and if so, we ignore him from now on
|
||||||
|
ps = connection.prepareStatement("SELECT offlineDrunk FROM Brewery_Z_BPlayers WHERE uuid = ?;");
|
||||||
|
ps.setString(1, d.uuid.toString());
|
||||||
|
|
||||||
|
ResultSet resultSet = ps.executeQuery();
|
||||||
|
if (resultSet.next()) {
|
||||||
|
int storedOfflineDrunk = resultSet.getInt("offlineDrunk");
|
||||||
|
if (storedOfflineDrunk != d.offlineDrunk) {
|
||||||
|
// The player is not offlineDrunk anymore,
|
||||||
|
// Someone else is changing the mysql data
|
||||||
|
P.p.getServer().getScheduler().runTask(P.p, () -> BPlayer.sqlRemoved(d.uuid));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ps = connection.prepareStatement("REPLACE INTO Brewery_Z_BPlayers (uuid, quality, drunkeness, offlineDrunk, data) VALUES (?, ?, ?, ?, ?);");
|
||||||
ps.setString(1, d.uuid.toString());
|
ps.setString(1, d.uuid.toString());
|
||||||
ps.setInt(2, d.quality);
|
ps.setInt(2, d.quality);
|
||||||
ps.setInt(3, d.drunkeness);
|
ps.setInt(3, d.drunkeness);
|
||||||
@@ -212,20 +236,20 @@ public class SQLSync {
|
|||||||
ps.executeUpdate();
|
ps.executeUpdate();
|
||||||
} else if (o instanceof SQLData_BD) {
|
} else if (o instanceof SQLData_BD) {
|
||||||
SQLData_BD d = ((SQLData_BD) o);
|
SQLData_BD d = ((SQLData_BD) o);
|
||||||
PreparedStatement ps = connection.prepareStatement("INSERT INTO BreweryZ_BData (name, data) VALUES (?, ?);");
|
PreparedStatement ps = connection.prepareStatement("REPLACE INTO Brewery_Z_BData (name, data) VALUES (?, ?);");
|
||||||
ps.setString(1, d.name);
|
ps.setString(1, d.name);
|
||||||
ps.setString(2, d.data);
|
ps.setString(2, d.data);
|
||||||
|
|
||||||
ps.executeUpdate();
|
ps.executeUpdate();
|
||||||
} else if (o instanceof SQLRemove_BP) {
|
} else if (o instanceof SQLRemove_BP) {
|
||||||
SQLRemove_BP r = ((SQLRemove_BP) o);
|
SQLRemove_BP r = ((SQLRemove_BP) o);
|
||||||
PreparedStatement ps = connection.prepareStatement("DELETE FROM BreweryZ_BPlayers WHERE uuid = ?;");
|
PreparedStatement ps = connection.prepareStatement("DELETE FROM Brewery_Z_BPlayers WHERE uuid = ?;");
|
||||||
ps.setString(1, r.uuid.toString());
|
ps.setString(1, r.uuid.toString());
|
||||||
|
|
||||||
ps.executeUpdate();
|
ps.executeUpdate();
|
||||||
} else if (o instanceof SQLRemove_BD) {
|
} else if (o instanceof SQLRemove_BD) {
|
||||||
SQLRemove_BD r = ((SQLRemove_BD) o);
|
SQLRemove_BD r = ((SQLRemove_BD) o);
|
||||||
PreparedStatement ps = connection.prepareStatement("DELETE FROM BreweryZ_BData WHERE name = ?;");
|
PreparedStatement ps = connection.prepareStatement("DELETE FROM Brewery_Z_BData WHERE name = ?;");
|
||||||
ps.setString(1, r.name);
|
ps.setString(1, r.name);
|
||||||
|
|
||||||
ps.executeUpdate();
|
ps.executeUpdate();
|
||||||
|
|||||||
Reference in New Issue
Block a user