Use cache for placeholder

This commit is contained in:
NotAlexNoyle committed 2026-04-04 10:38:27 -04:00
1 parent de9e2dd526
commit b8200a27f0
2 files changed
+19 -40

No files matched your search

@@ -2,46 +2,24 @@
// Author: NotAlexNoyle.
package plugin;
import java.util.UUID;
import org.bukkit.Bukkit;
import net.trueog.diamondbankog.DiamondBankException;
import net.trueog.diamondbankog.PlayerBalanceChangedEvent;
import net.trueog.diamondbankog.PlayerBalanceChangedListener;
// Fetches the balance for the MiniPlaceholder upon balance changes.
// Updates the balance cache for the MiniPlaceholder upon balance changes.
public class BalanceChangeListener implements PlayerBalanceChangedListener {
// Hook into DiamondBank-OG's balance change event.
@Override
public void onUpdate(PlayerBalanceChangedEvent event) {
// Get the UUID of the player whose balance changed.
final UUID playerUUID = event.getUuid();
// The event already carries the up-to-date shard totals, so compute the
// total directly instead of calling getTotalShards() — that would re-acquire
// the transaction lock and risk deadlocking against the main thread.
final long totalShards = event.getPlayerShards().getBank() + event.getPlayerShards().getInventory()
+ event.getPlayerShards().getEnderChest();
// Fetch diamond shard balance asynchronously.
Bukkit.getScheduler().runTaskAsynchronously(DiamondBankMiniPlaceholdersOG.getPlugin(), () -> {
try {
// Ask DiamondBank-OG for the player's total shard count.
final long totalShards = DiamondBankMiniPlaceholdersOG.diamondBankAPI().getTotalShards(playerUUID);
// Cache the new balance for reference by the MiniPlaceholder.
DiamondBankMiniPlaceholdersOG.cacheShardTotal(playerUUID, totalShards);
}
// If the DiamondBank-OG economy is disabled, do this...
catch (DiamondBankException.EconomyDisabledException error) {
// Commit sudoku, inform console of the DiamondBank-OG economy being disabled.
DiamondBankMiniPlaceholdersOG.disableSelf("The DiamondBank-OG economy is disabled — disabling "
+ DiamondBankMiniPlaceholdersOG.getPlugin().getName() + "!");
}
});
// Cache the new balance for reference by the MiniPlaceholder.
DiamondBankMiniPlaceholdersOG.cacheShardTotal(event.getUuid(), totalShards);
}
@@ -12,7 +12,6 @@ import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
import net.trueog.diamondbankog.DiamondBankException;
import net.trueog.diamondbankog.DiamondBankException.EconomyDisabledException;
import net.trueog.diamondbankog.api.DiamondBankAPIJava;
import net.trueog.utilitiesog.UtilitiesOG;
@@ -63,21 +62,23 @@ public class DiamondBankMiniPlaceholdersOG extends JavaPlugin {
// Pre-warm balance cache for already-online players, such as in plugin reloads.
Bukkit.getOnlinePlayers().forEach(p -> refreshShards(p.getUniqueId()));
// Fancy Async MiniPlaceholder declaration that only runs once per player per
// balance update.
// Cache-backed MiniPlaceholder — reads the pre-warmed SHARD_CACHE instead of
// making a blocking API call, so the main thread is never stalled by the
// DiamondBank transaction lock or a database round-trip.
UtilitiesOG.registerAudiencePlaceholder("diamondbankog_balance", player -> {
try {
final Long cachedShards = SHARD_CACHE.get(player.getUniqueId());
if (cachedShards != null) {
return diamondBankAPI.shardsToDiamonds(diamondBankAPI.getTotalShards(player.getUniqueId()));
} catch (EconomyDisabledException economyDisabledException) {
economyDisabledException.printStackTrace();
return "&cERROR. RETRY.";
return diamondBankAPI.shardsToDiamonds(cachedShards);
}
// Cache miss (player just joined, cache not yet warm) — trigger an async
// refresh and show nothing until the cache is populated.
refreshShards(player.getUniqueId());
return "";
});
}