Fix cooldown sync

This commit is contained in:
GeorgH93 2021-03-24 17:32:16 +01:00
parent 35ff602839
commit 2ea150075d
No known key found for this signature in database
GPG Key ID: D1630D37F9E4B3C8
7 changed files with 21 additions and 15 deletions

View File

@ -7,7 +7,6 @@ Language:
# Options: # Options:
# Overwrite (deletes all changes from the file and extracts a new language file) # Overwrite (deletes all changes from the file and extracts a new language file)
# Upgrade (extracts a new language file and copy's all settings from the old language file) # Upgrade (extracts a new language file and copy's all settings from the old language file)
# Update (adds the default (english) text values for all missing values, just some basic formatting)
UpdateMode: Upgrade UpdateMode: Upgrade
# Title to be shown for the opened inventory for everyone except the owner of the backpack. Can contain {OwnerName} (which will be replaced with the players name). # Title to be shown for the opened inventory for everyone except the owner of the backpack. Can contain {OwnerName} (which will be replaced with the players name).

View File

@ -124,10 +124,10 @@ public void loadPlayer(final @NotNull MinepacksPlayerData player)
bp = new Backpack(player, backpackFile.getItemStacks()[0]); bp = new Backpack(player, backpackFile.getItemStacks()[0]);
} }
if(bp == null) bp = new Backpack(player); if(bp == null) bp = new Backpack(player);
player.setLoaded(file); player.setLoaded(file, 0);
player.setBackpack(bp); player.setBackpack(bp);
} }
@Override @Override
public void loadBackpack(@NotNull MinepacksPlayerData player) {} // Already loaded with the player public void loadBackpack(final @NotNull MinepacksPlayerData player) {} // Already loaded with the player
} }

View File

@ -46,7 +46,7 @@ public abstract class SQL extends DatabaseBackend
protected String fieldCdPlayer = "id", fieldCdTime = "time"; // Table Fields protected String fieldCdPlayer = "id", fieldCdTime = "time"; // Table Fields
@Language("SQL") protected String queryUpdatePlayerAdd, queryInsertBp, queryUpdateBp, queryGetPlayer, queryGetBP, queryDeleteOldBackpacks, queryGetUnsetOrInvalidUUIDs, queryFixUUIDs; // DB Querys @Language("SQL") protected String queryUpdatePlayerAdd, queryInsertBp, queryUpdateBp, queryGetPlayer, queryGetBP, queryDeleteOldBackpacks, queryGetUnsetOrInvalidUUIDs, queryFixUUIDs; // DB Querys
@Language("SQL") protected String queryDeleteOldCooldowns, querySyncCooldown, queryGetCooldown; // DB Querys @Language("SQL") protected String queryDeleteOldCooldowns, querySyncCooldown; // DB Querys
protected boolean syncCooldown; protected boolean syncCooldown;
public SQL(@NotNull Minepacks plugin, @NotNull ConnectionProvider connectionProvider) public SQL(@NotNull Minepacks plugin, @NotNull ConnectionProvider connectionProvider)
@ -185,11 +185,12 @@ public Connection getConnection() throws SQLException
protected final void buildQueries() protected final void buildQueries()
{ {
// Build the SQL queries with placeholders for the table and field names // Build the SQL queries with placeholders for the table and field names
queryGetPlayer = "SELECT * FROM {TablePlayers} WHERE {FieldUUID}=?;"; queryGetPlayer = "SELECT * FROM {TablePlayers}" +
(syncCooldown ? " LEFT JOIN {TableCooldowns} ON {TablePlayers}.{FieldPlayerID} = {TableCooldowns}.{FieldCDPlayer}" : "") +
" WHERE {FieldUUID}=?;";
queryUpdatePlayerAdd = "INSERT INTO {TablePlayers} ({FieldName},{FieldUUID}) VALUES (?,?) ON DUPLICATE KEY UPDATE {FieldName}=?;"; queryUpdatePlayerAdd = "INSERT INTO {TablePlayers} ({FieldName},{FieldUUID}) VALUES (?,?) ON DUPLICATE KEY UPDATE {FieldName}=?;";
queryGetBP = "SELECT * FROM {TableBackpacks} WHERE {FieldBPOwner}=?;"; queryGetBP = "SELECT * FROM {TableBackpacks} WHERE {FieldBPOwner}=?;";
queryGetCooldown = "SELECT * FROM {TableCooldowns} WHERE {FieldCDPlayer} IN (SELECT {FieldPlayerID} FROM {TablePlayers} WHERE {FieldUUID}=?);"; querySyncCooldown = "INSERT INTO {TableCooldowns} ({FieldCDPlayer},{FieldCDTime}) VALUE (?,?) ON DUPLICATE KEY UPDATE {FieldCDTime}=?;";
querySyncCooldown = "INSERT INTO {TableCooldowns} ({FieldCDPlayer},{FieldCDTime}) SELECT {FieldPlayerID},? FROM {TablePlayers} WHERE {FieldUUID}=? ON DUPLICATE KEY UPDATE {FieldCDTime}=?;";
queryDeleteOldCooldowns = "DELETE FROM {TableCooldowns} WHERE {FieldCDTime}<?;"; queryDeleteOldCooldowns = "DELETE FROM {TableCooldowns} WHERE {FieldCDTime}<?;";
queryInsertBp = "REPLACE INTO {TableBackpacks} ({FieldBPOwner},{FieldBPITS},{FieldBPVersion}) VALUES (?,?,?);"; queryInsertBp = "REPLACE INTO {TableBackpacks} ({FieldBPOwner},{FieldBPITS},{FieldBPVersion}) VALUES (?,?,?);";
queryUpdateBp = "UPDATE {TableBackpacks} SET {FieldBPITS}=?,{FieldBPVersion}=?,{FieldBPLastUpdate}={NOW} WHERE {FieldBPOwner}=?;"; queryUpdateBp = "UPDATE {TableBackpacks} SET {FieldBPITS}=?,{FieldBPVersion}=?,{FieldBPLastUpdate}={NOW} WHERE {FieldBPOwner}=?;";
@ -221,7 +222,6 @@ protected void setTableAndFieldNames()
queryDeleteOldBackpacks = replacePlaceholders(queryDeleteOldBackpacks.replaceAll("\\{VarMaxAge}", maxAge + "")); queryDeleteOldBackpacks = replacePlaceholders(queryDeleteOldBackpacks.replaceAll("\\{VarMaxAge}", maxAge + ""));
queryGetUnsetOrInvalidUUIDs = replacePlaceholders(queryGetUnsetOrInvalidUUIDs); queryGetUnsetOrInvalidUUIDs = replacePlaceholders(queryGetUnsetOrInvalidUUIDs);
querySyncCooldown = replacePlaceholders(querySyncCooldown); querySyncCooldown = replacePlaceholders(querySyncCooldown);
queryGetCooldown = replacePlaceholders(queryGetCooldown);
queryDeleteOldCooldowns = replacePlaceholders(queryDeleteOldCooldowns); queryDeleteOldCooldowns = replacePlaceholders(queryDeleteOldCooldowns);
} }
@ -277,7 +277,10 @@ public void loadPlayer(final @NotNull MinepacksPlayerData player)
if(rs.next()) if(rs.next())
{ {
final int id = rs.getInt(fieldPlayerID); final int id = rs.getInt(fieldPlayerID);
plugin.getServer().getScheduler().runTask(plugin, () -> player.setLoaded(id)); long cooldown = 0;
if(syncCooldown) cooldown = rs.getTimestamp(fieldCdPlayer).getTime();
final long cd = cooldown;
plugin.getServer().getScheduler().runTask(plugin, () -> player.setLoaded(id, cd));
return; return;
} }
} }
@ -349,6 +352,6 @@ public void saveBackpack(final @NotNull Backpack backpack)
public void saveCooldown(final @NotNull MinepacksPlayerData player) public void saveCooldown(final @NotNull MinepacksPlayerData player)
{ {
final Timestamp ts = new Timestamp(player.getCooldown()); final Timestamp ts = new Timestamp(player.getCooldown());
runStatementAsync(querySyncCooldown, ts, formatUUID(player.getUUID()), ts); runStatementAsync(querySyncCooldown, player.getDatabaseKey(), ts, ts);
} }
} }

View File

@ -57,7 +57,8 @@ protected void loadSettings()
fieldBpIts = "itemstacks"; fieldBpIts = "itemstacks";
// Set fixed settings // Set fixed settings
useUUIDSeparators = false; useUUIDSeparators = false;
syncCooldown = false;
tableCooldowns = "minepacks_cooldown";
} }
@Override @Override
@ -94,6 +95,8 @@ protected void checkDB()
stmt.execute("CREATE TABLE IF NOT EXISTS `backpack_players` (`player_id` INT UNSIGNED PRIMARY KEY AUTOINCREMENT, `name` CHAR(16) NOT NULL , `uuid` CHAR(32) UNIQUE);"); stmt.execute("CREATE TABLE IF NOT EXISTS `backpack_players` (`player_id` INT UNSIGNED PRIMARY KEY AUTOINCREMENT, `name` CHAR(16) NOT NULL , `uuid` CHAR(32) UNIQUE);");
stmt.execute("CREATE TABLE IF NOT EXISTS `backpacks` (`owner` INT UNSIGNED PRIMARY KEY, `itemstacks` BLOB, `version` INT DEFAULT 0, `lastupdate` DATE DEFAULT CURRENT_DATE);"); stmt.execute("CREATE TABLE IF NOT EXISTS `backpacks` (`owner` INT UNSIGNED PRIMARY KEY, `itemstacks` BLOB, `version` INT DEFAULT 0, `lastupdate` DATE DEFAULT CURRENT_DATE);");
stmt.execute("CREATE TABLE IF NOT EXISTS `minepacks_cooldown` (`id` INT UNSIGNED PRIMARY KEY, `time` UNSIGNED BIG INT NOT NULL, " +
"CONSTRAINT fk_minepacks_cooldown_minepacks_players FOREIGN KEY (id) REFERENCES backpack_players (player_id) ON DELETE CASCADE ON UPDATE CASCADE);");
DBTools.runStatement(connection, "INSERT OR REPLACE INTO `minepacks_metadata` (`key`, `value`) VALUES ('db_version',?);", plugin.getDescription().getVersion()); DBTools.runStatement(connection, "INSERT OR REPLACE INTO `minepacks_metadata` (`key`, `value`) VALUES ('db_version',?);", plugin.getDescription().getVersion());
} }

View File

@ -107,7 +107,7 @@ public void writeBackup(@Nullable String userName, @NotNull String userIdentifie
} }
} }
public List<String> getBackups() public @NotNull List<String> getBackups()
{ {
File[] files = backupFolder.listFiles((dir, name) -> name.endsWith(Files.EXT)); File[] files = backupFolder.listFiles((dir, name) -> name.endsWith(Files.EXT));
if(files != null) if(files != null)
@ -120,6 +120,6 @@ public List<String> getBackups()
} }
return backups; return backups;
} }
return new ArrayList<>(); return new ArrayList<>(0);
} }
} }

View File

@ -138,7 +138,7 @@ public void backup(@NotNull Backpack backpack)
return backupHandler.loadBackup(backupName); return backupHandler.loadBackup(backupName);
} }
public List<String> getBackups() public @NotNull List<String> getBackups()
{ {
return backupHandler.getBackups(); return backupHandler.getBackups();
} }

View File

@ -79,9 +79,10 @@ public void setBackpack(final @NotNull Backpack backpack)
backpackLoadedQueue.forEach(backpackCallback -> backpackCallback.onResult(backpack)); backpackLoadedQueue.forEach(backpackCallback -> backpackCallback.onResult(backpack));
} }
public void setLoaded(final @NotNull Object databaseKey) public void setLoaded(final @NotNull Object databaseKey, final long cooldown)
{ {
this.databaseKey = databaseKey; this.databaseKey = databaseKey;
this.cooldown = cooldown;
playerLoadedQueue.forEach(loadedCallback -> loadedCallback.onResult(this)); playerLoadedQueue.forEach(loadedCallback -> loadedCallback.onResult(this));
} }