diff --git a/src/main/kotlin/net/trueog/diamondbankog/CachingBalanceManager.kt b/src/main/kotlin/net/trueog/diamondbankog/CachingBalanceManager.kt index 246107c..145a10c 100644 --- a/src/main/kotlin/net/trueog/diamondbankog/CachingBalanceManager.kt +++ b/src/main/kotlin/net/trueog/diamondbankog/CachingBalanceManager.kt @@ -79,10 +79,16 @@ internal class CachingBalanceManager : BalanceManager { override suspend fun getShardTypeShards(uuid: UUID, type: ShardType): Result { if ((beingModified[uuid to type]?.get() ?: 0) > 0) { + if (DiamondBankOG.debug) { + DiamondBankOG.plugin.logger.info("Cache miss (being modified) for $uuid!") + } return postgreSQL.getShardTypeShards(uuid, type) } val cacheBalance = cache.getBalance(uuid, type) if (cacheBalance == -1L) { + if (DiamondBankOG.debug) { + DiamondBankOG.plugin.logger.info("Cache miss for $uuid!") + } increment(uuid, type) val dbBalance = postgreSQL.getShardTypeShards(uuid, type).getOrElse { @@ -91,6 +97,8 @@ internal class CachingBalanceManager : BalanceManager { cache.setBalance(uuid, dbBalance, type) decrement(uuid, type) return Result.success(dbBalance) + } else if (DiamondBankOG.debug) { + DiamondBankOG.plugin.logger.info("Cache hit for $uuid!") } return Result.success(cacheBalance) } @@ -98,10 +106,16 @@ internal class CachingBalanceManager : BalanceManager { override suspend fun getTotalShards(uuid: UUID): Result { val anyBeingModified = beingModified.any { it.key.first == uuid && it.value.get() > 0 } if (anyBeingModified) { + if (DiamondBankOG.debug) { + DiamondBankOG.plugin.logger.info("Cache miss (being modified) for $uuid!") + } return postgreSQL.getTotalShards(uuid) } val cacheBalance = cache.getBalance(uuid, ShardType.TOTAL) if (cacheBalance == -1L) { + if (DiamondBankOG.debug) { + DiamondBankOG.plugin.logger.info("Cache miss for $uuid!") + } increment(uuid, ShardType.TOTAL) val dbBalance = postgreSQL.getTotalShards(uuid).getOrElse { @@ -110,6 +124,8 @@ internal class CachingBalanceManager : BalanceManager { cache.setBalance(uuid, dbBalance, ShardType.TOTAL) decrement(uuid, ShardType.TOTAL) return Result.success(dbBalance) + } else if (DiamondBankOG.debug) { + DiamondBankOG.plugin.logger.info("Cache hit for $uuid!") } return Result.success(cacheBalance) } @@ -117,12 +133,18 @@ internal class CachingBalanceManager : BalanceManager { override suspend fun getAllShards(uuid: UUID): Result { val anyBeingModified = beingModified.any { it.key.first == uuid && it.value.get() > 0 } if (anyBeingModified) { + if (DiamondBankOG.debug) { + DiamondBankOG.plugin.logger.info("Cache miss (being modified) for $uuid!") + } return postgreSQL.getAllShards(uuid) } val cacheBankBalance = cache.getBalance(uuid, ShardType.BANK) val cacheInventoryBalance = cache.getBalance(uuid, ShardType.INVENTORY) val cacheEnderChestBalance = cache.getBalance(uuid, ShardType.ENDER_CHEST) if (cacheBankBalance == -1L || cacheInventoryBalance == -1L || cacheEnderChestBalance == -1L) { + if (DiamondBankOG.debug) { + DiamondBankOG.plugin.logger.info("Cache miss for $uuid!") + } increment(uuid, ShardType.TOTAL) val dbPlayerShards = postgreSQL.getAllShards(uuid).getOrElse { @@ -133,6 +155,8 @@ internal class CachingBalanceManager : BalanceManager { cache.setBalance(uuid, dbPlayerShards.enderChest, ShardType.ENDER_CHEST) decrement(uuid, ShardType.TOTAL) return Result.success(dbPlayerShards) + } else if (DiamondBankOG.debug) { + DiamondBankOG.plugin.logger.info("Cache hit for $uuid!") } return Result.success(PlayerShards(cacheBankBalance, cacheInventoryBalance, cacheEnderChestBalance)) } diff --git a/src/main/kotlin/net/trueog/diamondbankog/DiamondBankOG.kt b/src/main/kotlin/net/trueog/diamondbankog/DiamondBankOG.kt index b166e9d..6e97483 100644 --- a/src/main/kotlin/net/trueog/diamondbankog/DiamondBankOG.kt +++ b/src/main/kotlin/net/trueog/diamondbankog/DiamondBankOG.kt @@ -25,6 +25,8 @@ internal open class DiamondBankOG : JavaPlugin() { lateinit var redis: Redis lateinit var luckPerms: LuckPerms + var debug: Boolean = false + fun isRedisInitialized() = ::redis.isInitialized fun isBalanceManagerInitialized() = ::balanceManager.isInitialized @@ -102,6 +104,8 @@ internal open class DiamondBankOG : JavaPlugin() { this.getCommand("enableeconomy")?.setExecutor(EnableEconomy()) this.getCommand("disableeconomy")?.setExecutor(DisableEconomy()) + this.getCommand("dbogtoggledebug")?.setExecutor(ToggleDebug()) + Shard.createCraftingRecipes() val diamondBankAPIJava = DiamondBankAPIJava() diff --git a/src/main/kotlin/net/trueog/diamondbankog/commands/ToggleDebug.kt b/src/main/kotlin/net/trueog/diamondbankog/commands/ToggleDebug.kt new file mode 100644 index 0000000..15a2f80 --- /dev/null +++ b/src/main/kotlin/net/trueog/diamondbankog/commands/ToggleDebug.kt @@ -0,0 +1,27 @@ +package net.trueog.diamondbankog.commands + +import net.trueog.diamondbankog.DiamondBankOG +import net.trueog.diamondbankog.DiamondBankOG.Companion.config +import net.trueog.diamondbankog.DiamondBankOG.Companion.mm +import org.bukkit.command.Command +import org.bukkit.command.CommandExecutor +import org.bukkit.command.CommandSender + +class ToggleDebug : CommandExecutor { + override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array?): Boolean { + if (!sender.hasPermission("diamondbank-og.debug")) { + sender.sendMessage( + mm.deserialize("${config.prefix}: You do not have permission to use this command.") + ) + return true + } + + DiamondBankOG.debug = !DiamondBankOG.debug + sender.sendMessage( + mm.deserialize( + "${config.prefix}: Debug mode is now turned ${if (DiamondBankOG.debug) "on" else "off"}." + ) + ) + return true + } +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 4b7618a..a86e91c 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -51,9 +51,9 @@ commands: autodeposit: description: Toggles auto-deposit, automatically deposits picked up Diamond currency items, mutually exclusive with auto-compress usage: /autodeposit - dbogtests: - description: Run the DiamondBank-OG tests, only available in dev builds - usage: /dbogtests + dbogtoggledebug: + description: Toggles debug mode + usage: /dbtoggledebug # So we can overwrite Essentials' commands softdepend: [ Essentials-OG ]