From 7df6e6b78bd538c7357bf1f36f68e99af962c3f8 Mon Sep 17 00:00:00 2001 From: GeorgH93 Date: Wed, 18 Oct 2023 17:35:36 +0200 Subject: [PATCH] Fix issues with BungeeCord networks not beeing fully in sync --- .../Database/Backend/DatabaseBackend.java | 19 ++++--- .../MarriageMaster/Database/Backend/SQL.java | 46 ++++++++++------ .../MarriageMaster/Database/BaseDatabase.java | 53 ++++++++++++++----- .../Helper/DbElementStatementRunnable.java | 6 ++- .../Database/MarriageSavedCallback.java | 23 -------- 5 files changed, 84 insertions(+), 63 deletions(-) delete mode 100644 MarriageMaster/src/at/pcgamingfreaks/MarriageMaster/Database/MarriageSavedCallback.java diff --git a/MarriageMaster/src/at/pcgamingfreaks/MarriageMaster/Database/Backend/DatabaseBackend.java b/MarriageMaster/src/at/pcgamingfreaks/MarriageMaster/Database/Backend/DatabaseBackend.java index 6e9b2cef..be4b4683 100644 --- a/MarriageMaster/src/at/pcgamingfreaks/MarriageMaster/Database/Backend/DatabaseBackend.java +++ b/MarriageMaster/src/at/pcgamingfreaks/MarriageMaster/Database/Backend/DatabaseBackend.java @@ -23,10 +23,10 @@ import at.pcgamingfreaks.MarriageMaster.Database.FilesMigrator.MigrationPlayer; import org.jetbrains.annotations.NotNull; - -import lombok.Setter; +import org.jetbrains.annotations.Nullable; import java.util.UUID; +import java.util.function.Consumer; import java.util.logging.Logger; public abstract class DatabaseBackend @@ -35,7 +35,6 @@ public abstract class DatabaseBackend cache; protected final Logger logger; - @Setter protected MarriageSavedCallback marriageSavedCallback = null; protected DatabaseBackend(final @NotNull IPlatformSpecific platform, final @NotNull DatabaseConfiguration dbConfig, final boolean useBungee, final boolean surnameEnabled, final @NotNull Cache cache, final @NotNull Logger logger) @@ -86,19 +85,19 @@ protected void runAsync(final @NotNull Runnable runnable, final long delay) public abstract void load(final @NotNull MARRIAGE_PLAYER player); - public abstract void updateHome(final @NotNull MARRIAGE marriage); + public abstract void updateHome(final @NotNull MARRIAGE marriage, final @Nullable Consumer updateCallback); - public abstract void updatePvPState(final @NotNull MARRIAGE marriage); + public abstract void updatePvPState(final @NotNull MARRIAGE marriage, final @Nullable Consumer updateCallback); - public abstract void updateBackpackShareState(final @NotNull MARRIAGE_PLAYER player); + public abstract void updateBackpackShareState(final @NotNull MARRIAGE_PLAYER player, final @Nullable Consumer updateCallback); - public abstract void updatePriestStatus(final @NotNull MARRIAGE_PLAYER player); + public abstract void updatePriestStatus(final @NotNull MARRIAGE_PLAYER player, final @Nullable Consumer updateCallback); - public abstract void updateSurname(final @NotNull MARRIAGE marriage); + public abstract void updateSurname(final @NotNull MARRIAGE marriage, final @Nullable Consumer updateCallback); - public abstract void updateMarriageColor(final @NotNull MARRIAGE marriage); + public abstract void updateMarriageColor(final @NotNull MARRIAGE marriage, final @Nullable Consumer updateCallback); - public abstract void marry(final @NotNull MARRIAGE marriage); + public abstract void marry(final @NotNull MARRIAGE marriage, final @Nullable Consumer updateCallback); public abstract void divorce(final @NotNull MARRIAGE marriage); diff --git a/MarriageMaster/src/at/pcgamingfreaks/MarriageMaster/Database/Backend/SQL.java b/MarriageMaster/src/at/pcgamingfreaks/MarriageMaster/Database/Backend/SQL.java index 68fbc9d7..5abb3e4a 100644 --- a/MarriageMaster/src/at/pcgamingfreaks/MarriageMaster/Database/Backend/SQL.java +++ b/MarriageMaster/src/at/pcgamingfreaks/MarriageMaster/Database/Backend/SQL.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022 GeorgH93 + * Copyright (C) 2023 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 @@ -39,6 +39,7 @@ import java.sql.*; import java.util.*; +import java.util.function.Consumer; import java.util.logging.Level; import java.util.logging.Logger; @@ -188,6 +189,20 @@ protected void runStatementAsyncIncludeKeyFirst(final @Language("SQL") @NotNull { runAsync(new DbElementStatementWithKeyFirstRunnable(this, databaseElement, query, args), databaseElement); } + + protected void runStatementAsyncIncludeKey(final @Nullable Runnable callback, final @Language("SQL") @NotNull String query, final @NotNull DatabaseElement databaseElement, final @NotNull Object... args) + { + DbElementStatementWithKeyRunnable runnable = new DbElementStatementWithKeyRunnable(this, databaseElement, query, args); + runnable.callback = callback; + runAsync(runnable, databaseElement); + } + + protected void runStatementAsyncIncludeKeyFirst(final @Nullable Runnable callback, final @Language("SQL") @NotNull String query, final @NotNull DatabaseElement databaseElement, final @NotNull Object... args) + { + DbElementStatementWithKeyFirstRunnable runnable = new DbElementStatementWithKeyFirstRunnable(this, databaseElement, query, args); + runnable.callback = callback; + runAsync(runnable, databaseElement); + } //endregion @Override @@ -518,19 +533,19 @@ protected boolean queryPlayer(final MARRIAGE_PLAYER player, final Connection con } @Override - public void updateBackpackShareState(final @NotNull MARRIAGE_PLAYER player) + public void updateBackpackShareState(final @NotNull MARRIAGE_PLAYER player, final @Nullable Consumer updateCallback) { - runStatementAsyncIncludeKey(querySetBackpackShareState, player, player.isSharingBackpack()); + runStatementAsyncIncludeKey((updateCallback != null) ? () -> updateCallback.accept(player) : null, querySetBackpackShareState, player, player.isSharingBackpack()); } @Override - public void updatePriestStatus(final @NotNull MARRIAGE_PLAYER player) + public void updatePriestStatus(final @NotNull MARRIAGE_PLAYER player, final @Nullable Consumer updateCallback) { - runStatementAsyncIncludeKey(player.isPriest() ? querySetPriest : queryRemovePriest, player); + runStatementAsyncIncludeKey((updateCallback != null) ? () -> updateCallback.accept(player) : null, player.isPriest() ? querySetPriest : queryRemovePriest, player); } @Override - public void updateHome(final @NotNull MARRIAGE marriage) + public void updateHome(final @NotNull MARRIAGE marriage, final @Nullable Consumer updateCallback) { Home home = marriage.getHome(); if(home == null) @@ -541,7 +556,8 @@ public void updateHome(final @NotNull MARRIAGE marriage) { if(useBungee) { - runStatementAsyncIncludeKeyFirst(queryUpdateHome, marriage, home.getX(), home.getY(), home.getZ(), home.getYaw(), home.getPitch(), home.getWorldName(), home.getHomeServer()); + runStatementAsyncIncludeKeyFirst((updateCallback != null) ? () -> updateCallback.accept(marriage) : null, + queryUpdateHome, marriage, home.getX(), home.getY(), home.getZ(), home.getYaw(), home.getPitch(), home.getWorldName(), home.getHomeServer()); } else { @@ -551,15 +567,15 @@ public void updateHome(final @NotNull MARRIAGE marriage) } @Override - public void updatePvPState(final @NotNull MARRIAGE marriage) + public void updatePvPState(final @NotNull MARRIAGE marriage, final @Nullable Consumer updateCallback) { - runStatementAsyncIncludeKey(queryPvPState, marriage, marriage.isPVPEnabled()); + runStatementAsyncIncludeKey((updateCallback != null) ? () -> updateCallback.accept(marriage) : null, queryPvPState, marriage, marriage.isPVPEnabled()); } @Override - public void updateMarriageColor(final @NotNull MARRIAGE marriage) + public void updateMarriageColor(final @NotNull MARRIAGE marriage, final @Nullable Consumer updateCallback) { - runStatementAsyncIncludeKey(queryUpdateMarriageColor, marriage, marriage.getColor().name()); + runStatementAsyncIncludeKey((updateCallback != null) ? () -> updateCallback.accept(marriage) : null, queryUpdateMarriageColor, marriage, marriage.getColor().name()); } @Override @@ -569,7 +585,7 @@ public void divorce(final @NotNull MARRIAGE marriage) } @Override - public void marry(final @NotNull MARRIAGE marriage) + public void marry(final @NotNull MARRIAGE marriage, final @Nullable Consumer updateCallback) { //TODO test if the player id is available runAsync(() -> { @@ -585,7 +601,7 @@ public void marry(final @NotNull MARRIAGE marriage) { marriage.setDatabaseKey(rs.getInt(1)); cache.addDbKey(marriage); - if(marriageSavedCallback != null) marriageSavedCallback.run(marriage); + if(updateCallback != null) updateCallback.accept(marriage); } } } @@ -597,9 +613,9 @@ public void marry(final @NotNull MARRIAGE marriage) } @Override - public void updateSurname(final @NotNull MARRIAGE marriage) + public void updateSurname(final @NotNull MARRIAGE marriage, final @Nullable Consumer updateCallback) { - runStatementAsyncIncludeKey(querySetSurname, marriage, marriage.getSurname()); + runStatementAsyncIncludeKey((updateCallback != null) ? () -> updateCallback.accept(marriage) : null, querySetSurname, marriage, marriage.getSurname()); } @Override diff --git a/MarriageMaster/src/at/pcgamingfreaks/MarriageMaster/Database/BaseDatabase.java b/MarriageMaster/src/at/pcgamingfreaks/MarriageMaster/Database/BaseDatabase.java index fd33fa55..16df467f 100644 --- a/MarriageMaster/src/at/pcgamingfreaks/MarriageMaster/Database/BaseDatabase.java +++ b/MarriageMaster/src/at/pcgamingfreaks/MarriageMaster/Database/BaseDatabase.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021 GeorgH93 + * Copyright (C) 2023 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 @@ -20,6 +20,7 @@ import at.pcgamingfreaks.ConsoleColor; import at.pcgamingfreaks.Database.ConnectionProvider.ConnectionProvider; import at.pcgamingfreaks.MarriageMaster.API.Home; +import at.pcgamingfreaks.MarriageMaster.API.Marriage; import at.pcgamingfreaks.MarriageMaster.API.MarriageMasterPlugin; import at.pcgamingfreaks.MarriageMaster.Database.Backend.DatabaseBackend; import at.pcgamingfreaks.MarriageMaster.Database.Backend.MySQL; @@ -36,6 +37,7 @@ import java.io.File; import java.util.Collection; import java.util.UUID; +import java.util.function.Consumer; import java.util.logging.Level; import java.util.logging.Logger; @@ -58,6 +60,16 @@ public abstract class BaseDatabase callbackMarry = null; + private Consumer callbackUpdateHome = null; + private Consumer callbackUpdatePvP = null; + private Consumer callbackUpdateColor = null; + private Consumer callbackUpdateSurname = null; + private Consumer callbackUpdateBackpack = null; + private Consumer callbackUpdatePriest = null; + //endregion + protected BaseDatabase(final @NotNull MARRIAGE_MASTER plugin, final @NotNull Logger logger, final @NotNull IPlatformSpecific platform, final @NotNull DatabaseConfiguration dbConfig, final @NotNull String pluginName, final @NotNull File dataFolder, final boolean bungee, final boolean bungeeSupportRequired) { @@ -142,7 +154,26 @@ protected void close() void setCommunicatorBase(PluginChannelCommunicatorBase communicatorBase) { this.communicatorBase = communicatorBase; - backend.setMarriageSavedCallback(communicatorBase::marry); + if (communicatorBase != null) + { + callbackMarry = communicatorBase::marry; + callbackUpdateHome = communicatorBase::updateHome; + callbackUpdatePvP = communicatorBase::updatePvP; + callbackUpdateColor = communicatorBase::updateMarriageColor; + callbackUpdateSurname = communicatorBase::updateSurname; + callbackUpdateBackpack = communicatorBase::updateBackpackShareState; + callbackUpdatePriest = communicatorBase::updatePriestStatus; + } + else + { + callbackMarry = null; + callbackUpdateHome = null; + callbackUpdatePvP = null; + callbackUpdateColor = null; + callbackUpdateSurname = null; + callbackUpdateBackpack = null; + callbackUpdatePriest = null; + } } public Cache getCache() @@ -212,43 +243,39 @@ public void loadHome(final MARRIAGE_DATA marriage) public void updateHome(final MARRIAGE_DATA marriage) { - backend.updateHome(marriage); - if(bungee && communicatorBase != null) communicatorBase.updateHome(marriage); + backend.updateHome(marriage, callbackUpdateHome); } public void updatePvPState(final MARRIAGE_DATA marriage) { - backend.updatePvPState(marriage); - if(bungee && communicatorBase != null) communicatorBase.updatePvP(marriage); + backend.updatePvPState(marriage, callbackUpdatePvP); } public void updateMarriageColor(final MARRIAGE_DATA marriage) { - backend.updateMarriageColor(marriage); - if(bungee && communicatorBase != null) communicatorBase.updateMarriageColor(marriage); + backend.updateMarriageColor(marriage, callbackUpdateColor); } public void updateBackpackShareState(final MARRIAGE_PLAYER_DATA player) { - backend.updateBackpackShareState(player); - if(bungee && communicatorBase != null) communicatorBase.updateBackpackShareState(player); + backend.updateBackpackShareState(player, callbackUpdateBackpack); } public void updatePriestStatus(final MARRIAGE_PLAYER_DATA player) { - backend.updatePriestStatus(player); + backend.updatePriestStatus(player, callbackUpdatePriest); if(bungee && communicatorBase != null) communicatorBase.updatePriestStatus(player); } protected void updateSurname(final MARRIAGE_DATA marriage) { - backend.updateSurname(marriage); + backend.updateSurname(marriage, callbackUpdateSurname); if(bungee && communicatorBase != null) communicatorBase.updateSurname(marriage); } protected void marry(final MARRIAGE_DATA marriage) { - backend.marry(marriage); + backend.marry(marriage, callbackMarry); } protected void divorce(final MARRIAGE_DATA marriage) diff --git a/MarriageMaster/src/at/pcgamingfreaks/MarriageMaster/Database/Helper/DbElementStatementRunnable.java b/MarriageMaster/src/at/pcgamingfreaks/MarriageMaster/Database/Helper/DbElementStatementRunnable.java index bd8445cd..79189b87 100644 --- a/MarriageMaster/src/at/pcgamingfreaks/MarriageMaster/Database/Helper/DbElementStatementRunnable.java +++ b/MarriageMaster/src/at/pcgamingfreaks/MarriageMaster/Database/Helper/DbElementStatementRunnable.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022 GeorgH93 + * Copyright (C) 2023 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 @@ -38,6 +38,7 @@ public class DbElementStatementRunnable implements Runnable protected final SQLBasedDatabase database; protected final @Language("SQL") String query; protected final Object[] args; + public @Nullable Runnable callback = null; public DbElementStatementRunnable(@NotNull SQLBasedDatabase database, @NotNull DatabaseElement databaseElement, @NonNls @Language("SQL") String query, @Nullable Object... args) { @@ -48,12 +49,13 @@ public DbElementStatementRunnable(@NotNull SQLBasedDatabase database, @NotNull D } @Override - public void run() + public final void run() { if(!(databaseElement.getDatabaseKey() instanceof Integer)) return; try(Connection connection = database.getConnection()) { runStatement(connection); + if (callback != null) callback.run(); } catch(SQLException e) { diff --git a/MarriageMaster/src/at/pcgamingfreaks/MarriageMaster/Database/MarriageSavedCallback.java b/MarriageMaster/src/at/pcgamingfreaks/MarriageMaster/Database/MarriageSavedCallback.java deleted file mode 100644 index ea1e0a3e..00000000 --- a/MarriageMaster/src/at/pcgamingfreaks/MarriageMaster/Database/MarriageSavedCallback.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright (C) 2021 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 - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package at.pcgamingfreaks.MarriageMaster.Database; - -public interface MarriageSavedCallback -{ - void run(MarriageDataBase marriage); -} \ No newline at end of file