Skip to content

Commit

Permalink
Fix issues with BungeeCord networks not beeing fully in sync
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgH93 committed Oct 18, 2023
1 parent 097fbcd commit 7df6e6b
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<MARRIAGE_PLAYER extends MarriagePlayerDataBase, MARRIAGE extends MarriageDataBase, HOME extends Home>
Expand All @@ -35,7 +35,6 @@ public abstract class DatabaseBackend<MARRIAGE_PLAYER extends MarriagePlayerData
protected final boolean useBungee, useUUIDSeparators, useOnlineUUIDs, surnameEnabled;
protected final Cache<MARRIAGE_PLAYER, MARRIAGE> cache;
protected final Logger logger;
@Setter protected MarriageSavedCallback marriageSavedCallback = null;

protected DatabaseBackend(final @NotNull IPlatformSpecific<MARRIAGE_PLAYER, MARRIAGE, HOME> platform, final @NotNull DatabaseConfiguration dbConfig, final boolean useBungee, final boolean surnameEnabled,
final @NotNull Cache<MARRIAGE_PLAYER, MARRIAGE> cache, final @NotNull Logger logger)
Expand Down Expand Up @@ -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<MarriageDataBase> updateCallback);

public abstract void updatePvPState(final @NotNull MARRIAGE marriage);
public abstract void updatePvPState(final @NotNull MARRIAGE marriage, final @Nullable Consumer<MarriageDataBase> updateCallback);

public abstract void updateBackpackShareState(final @NotNull MARRIAGE_PLAYER player);
public abstract void updateBackpackShareState(final @NotNull MARRIAGE_PLAYER player, final @Nullable Consumer<MarriagePlayerDataBase> updateCallback);

public abstract void updatePriestStatus(final @NotNull MARRIAGE_PLAYER player);
public abstract void updatePriestStatus(final @NotNull MARRIAGE_PLAYER player, final @Nullable Consumer<MarriagePlayerDataBase> updateCallback);

public abstract void updateSurname(final @NotNull MARRIAGE marriage);
public abstract void updateSurname(final @NotNull MARRIAGE marriage, final @Nullable Consumer<MarriageDataBase> updateCallback);

public abstract void updateMarriageColor(final @NotNull MARRIAGE marriage);
public abstract void updateMarriageColor(final @NotNull MARRIAGE marriage, final @Nullable Consumer<MarriageDataBase> updateCallback);

public abstract void marry(final @NotNull MARRIAGE marriage);
public abstract void marry(final @NotNull MARRIAGE marriage, final @Nullable Consumer<MarriageDataBase> updateCallback);

public abstract void divorce(final @NotNull MARRIAGE marriage);

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<MarriagePlayerDataBase> 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<MarriagePlayerDataBase> 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<MarriageDataBase> updateCallback)
{
Home home = marriage.getHome();
if(home == null)
Expand All @@ -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
{
Expand All @@ -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<MarriageDataBase> 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<MarriageDataBase> updateCallback)
{
runStatementAsyncIncludeKey(queryUpdateMarriageColor, marriage, marriage.getColor().name());
runStatementAsyncIncludeKey((updateCallback != null) ? () -> updateCallback.accept(marriage) : null, queryUpdateMarriageColor, marriage, marriage.getColor().name());
}

@Override
Expand All @@ -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<MarriageDataBase> updateCallback)
{
//TODO test if the player id is available
runAsync(() -> {
Expand All @@ -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);
}
}
}
Expand All @@ -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<MarriageDataBase> updateCallback)
{
runStatementAsyncIncludeKey(querySetSurname, marriage, marriage.getSurname());
runStatementAsyncIncludeKey((updateCallback != null) ? () -> updateCallback.accept(marriage) : null, querySetSurname, marriage, marriage.getSurname());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
Expand All @@ -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;

Expand All @@ -58,6 +60,16 @@ public abstract class BaseDatabase<MARRIAGE_MASTER extends MarriageMasterPlugin,
protected final Runnable loadRunnable;
private PluginChannelCommunicatorBase communicatorBase = null;

//region callbacks
private Consumer<MarriageDataBase> callbackMarry = null;
private Consumer<MarriageDataBase> callbackUpdateHome = null;
private Consumer<MarriageDataBase> callbackUpdatePvP = null;
private Consumer<MarriageDataBase> callbackUpdateColor = null;
private Consumer<MarriageDataBase> callbackUpdateSurname = null;
private Consumer<MarriagePlayerDataBase> callbackUpdateBackpack = null;
private Consumer<MarriagePlayerDataBase> callbackUpdatePriest = null;
//endregion

protected BaseDatabase(final @NotNull MARRIAGE_MASTER plugin, final @NotNull Logger logger, final @NotNull IPlatformSpecific<MARRIAGE_PLAYER_DATA, MARRIAGE_DATA, HOME> platform,
final @NotNull DatabaseConfiguration dbConfig, final @NotNull String pluginName, final @NotNull File dataFolder, final boolean bungee, final boolean bungeeSupportRequired)
{
Expand Down Expand Up @@ -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<MARRIAGE_PLAYER_DATA, MARRIAGE_DATA> getCache()
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand Down

This file was deleted.

0 comments on commit 7df6e6b

Please sign in to comment.