From 65724ccf1231306bca748c5f7891ad76a51e08a1 Mon Sep 17 00:00:00 2001 From: Starchier Date: Fri, 23 Jul 2021 23:58:19 +0800 Subject: [PATCH] Add debugger --- .../inventorykeeper/InventoryKeeper.java | 4 +++- .../inventorykeeper/events/DeathHandler.java | 16 ++++++++++++++++ .../inventorykeeper/events/RespawnHandler.java | 18 ++++++------------ .../inventorykeeper/util/DataManager.java | 1 - .../inventorykeeper/util/Debugger.java | 15 +++++++++++++++ .../inventorykeeper/util/PluginHandler.java | 2 +- 6 files changed, 41 insertions(+), 15 deletions(-) create mode 100644 src/main/java/me/starchier/inventorykeeper/util/Debugger.java diff --git a/src/main/java/me/starchier/inventorykeeper/InventoryKeeper.java b/src/main/java/me/starchier/inventorykeeper/InventoryKeeper.java index 5fc2d7d..b8f4480 100644 --- a/src/main/java/me/starchier/inventorykeeper/InventoryKeeper.java +++ b/src/main/java/me/starchier/inventorykeeper/InventoryKeeper.java @@ -9,6 +9,7 @@ import me.starchier.inventorykeeper.hooks.PlaceholderAPIHook; import me.starchier.inventorykeeper.i18n.MessagesUtil; import me.starchier.inventorykeeper.util.DataManager; +import me.starchier.inventorykeeper.util.Debugger; import me.starchier.inventorykeeper.util.ItemHandler; import me.starchier.inventorykeeper.util.PluginHandler; import org.bukkit.Bukkit; @@ -49,6 +50,7 @@ public void onEnable() { ph.initConfigCache(); ItemHandler ih = new ItemHandler(this, ph); ph.loadItems(ih); + Debugger.enabledDebug = ph.getBooleanConfigValue("debug", true); /* TODO: Need to be re-code. if(!ih.isItem()) { @@ -69,7 +71,7 @@ public void onEnable() { CommandExec commandExec = new CommandExec(ph, this); Bukkit.getPluginManager().registerEvents(new DeathHandler(this, dataManager, commandExec, ph), this); Bukkit.getPluginManager().registerEvents(new EntityDamageListener(), this); - Bukkit.getPluginManager().registerEvents(new RespawnHandler(this, dataManager, commandExec, ph), this); + Bukkit.getPluginManager().registerEvents(new RespawnHandler(commandExec), this); Bukkit.getPluginManager().registerEvents(new PlayerDataInit(dataManager), this); //Bukkit.getPluginManager().registerEvents(new InventoryClickHandler(this),this); Bukkit.getPluginManager().registerEvents(new BlockPlaceListener(ih, ph), this); diff --git a/src/main/java/me/starchier/inventorykeeper/events/DeathHandler.java b/src/main/java/me/starchier/inventorykeeper/events/DeathHandler.java index d7c27ce..67195b7 100644 --- a/src/main/java/me/starchier/inventorykeeper/events/DeathHandler.java +++ b/src/main/java/me/starchier/inventorykeeper/events/DeathHandler.java @@ -6,12 +6,14 @@ import me.starchier.inventorykeeper.items.ItemBase; import me.starchier.inventorykeeper.storage.PlayerStorage; import me.starchier.inventorykeeper.util.DataManager; +import me.starchier.inventorykeeper.util.Debugger; import me.starchier.inventorykeeper.util.ExpHandler; import me.starchier.inventorykeeper.util.PluginHandler; import org.bukkit.ChatColor; import org.bukkit.GameRule; import org.bukkit.enchantments.Enchantment; import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.entity.PlayerDeathEvent; import org.bukkit.inventory.ItemStack; @@ -158,6 +160,8 @@ public void onPlayerDeath(PlayerDeathEvent evt) { } } + Debugger.logDebugMessage(evt.getEntity().getName() + " died, consumeType: " + consumeType); + if (consumeType == CONSUME_NONE) { PlayerStorage.removeKiller(evt.getEntity()); PlayerStorage.clearPlayer(evt.getEntity()); @@ -197,6 +201,7 @@ public void onPlayerDeath(PlayerDeathEvent evt) { PlayerStorage.clearPlayer(evt.getEntity()); if (isConsumedFinally) { PlayerStorage.setConsumed(evt.getEntity(), consumeItemNames[consumeType]); + Debugger.logDebugMessage(evt.getEntity().getName() + " died, consumed item: " + consumeItemNames[consumeType]); commandExec.doKeepModInventory(evt.getEntity()); commandExec.runCommands(evt.getEntity(), true, consumeItemNames[consumeType] + ".run-commands-on-death", false); commandExec.runRandomCommands(evt.getEntity(), true, consumeItemNames[consumeType] + ".run-random-commands-on-death", false); @@ -215,6 +220,7 @@ public void onPlayerDeath(PlayerDeathEvent evt) { targetItem.setAmount(amount); } evt.getEntity().getInventory().setItem(physicalSlot, targetItem); + Debugger.logDebugMessage(evt.getEntity().getName() + " died, target slot:" + physicalSlot); } else if (consumeType == CONSUME_VIRTUAL) { dataManager.setConsumed(evt.getEntity(), consumeItemNames[consumeType]); } @@ -267,6 +273,16 @@ public void onPlayerDeath(PlayerDeathEvent evt) { int lost = expHandler.loseExp(evt, consumeItemNames[consumeType]); evt.getEntity().sendMessage(String.format(pluginHandler.getMessage("lost-exp"), lost, (evt.getEntity().getLevel() - lost))); } + Debugger.logDebugMessage(evt.getEntity().getName() + " death status:"); + Debugger.logDebugMessage("keep level: " + evt.getKeepLevel()); + Debugger.logDebugMessage("keep inventory: " + evt.getKeepInventory()); } } + + @EventHandler(priority = EventPriority.MONITOR) + public void playerDeathDebugger(PlayerDeathEvent evt) { + Debugger.logDebugMessage(evt.getEntity().getName() + " : final death status:"); + Debugger.logDebugMessage("keep level: " + evt.getKeepLevel()); + Debugger.logDebugMessage("keep inventory: " + evt.getKeepInventory()); + } } diff --git a/src/main/java/me/starchier/inventorykeeper/events/RespawnHandler.java b/src/main/java/me/starchier/inventorykeeper/events/RespawnHandler.java index ea9fe5e..14c50cc 100644 --- a/src/main/java/me/starchier/inventorykeeper/events/RespawnHandler.java +++ b/src/main/java/me/starchier/inventorykeeper/events/RespawnHandler.java @@ -1,27 +1,17 @@ package me.starchier.inventorykeeper.events; -import me.starchier.inventorykeeper.InventoryKeeper; import me.starchier.inventorykeeper.command.CommandExec; import me.starchier.inventorykeeper.storage.PlayerStorage; -import me.starchier.inventorykeeper.util.DataManager; -import me.starchier.inventorykeeper.util.PluginHandler; +import me.starchier.inventorykeeper.util.Debugger; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerRespawnEvent; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.ItemMeta; public class RespawnHandler implements Listener { - private final InventoryKeeper plugin; - private final DataManager dataManager; private final CommandExec commandExec; - private final PluginHandler pluginHandler; - public RespawnHandler(InventoryKeeper plugin, DataManager dataManager, CommandExec commandExec, PluginHandler pluginHandler) { - this.plugin = plugin; - this.dataManager = dataManager; + public RespawnHandler(CommandExec commandExec) { this.commandExec = commandExec; - this.pluginHandler = pluginHandler; } @EventHandler @@ -30,16 +20,20 @@ public void onPlayerRespawn(PlayerRespawnEvent evt) { try { consumedItem = PlayerStorage.getConsumed(evt.getPlayer()); } catch (NullPointerException e) { + Debugger.logDebugMessage(evt.getPlayer().getName() + " Respawn: no death cause"); return; } if (consumedItem == null) { commandExec.runCommands(evt.getPlayer(), false, "settings.run-commands-on-respawn-if-drops", true); commandExec.runRandomCommands(evt.getPlayer(), false, "settings.run-random-commands-on-respawn-if-drops", true); + Debugger.logDebugMessage(evt.getPlayer().getName() + " Respawn: drop inventory"); PlayerStorage.resetConsumed(evt.getPlayer()); return; } commandExec.doRestoreModInventory(evt.getPlayer()); commandExec.runCommands(evt.getPlayer(), false, consumedItem + ".run-commands-on-respawn", false); commandExec.runRandomCommands(evt.getPlayer(), false, consumedItem + ".run-random-commands-on-respawn", false); + Debugger.logDebugMessage(evt.getPlayer().getName() + " Respawn: consumed " + consumedItem); + PlayerStorage.resetConsumed(evt.getPlayer()); } } diff --git a/src/main/java/me/starchier/inventorykeeper/util/DataManager.java b/src/main/java/me/starchier/inventorykeeper/util/DataManager.java index f61b392..9a4308e 100644 --- a/src/main/java/me/starchier/inventorykeeper/util/DataManager.java +++ b/src/main/java/me/starchier/inventorykeeper/util/DataManager.java @@ -1,6 +1,5 @@ package me.starchier.inventorykeeper.util; -import me.starchier.inventorykeeper.InventoryKeeper; import me.starchier.inventorykeeper.items.ItemBase; import org.bukkit.Bukkit; import org.bukkit.configuration.file.FileConfiguration; diff --git a/src/main/java/me/starchier/inventorykeeper/util/Debugger.java b/src/main/java/me/starchier/inventorykeeper/util/Debugger.java new file mode 100644 index 0000000..51b5990 --- /dev/null +++ b/src/main/java/me/starchier/inventorykeeper/util/Debugger.java @@ -0,0 +1,15 @@ +package me.starchier.inventorykeeper.util; + +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; + +public class Debugger { + public static boolean enabledDebug = false; + + public static void logDebugMessage(String message) { + if (!enabledDebug) { + return; + } + Bukkit.getLogger().info(ChatColor.AQUA + " [DEBUG] " + message); + } +} diff --git a/src/main/java/me/starchier/inventorykeeper/util/PluginHandler.java b/src/main/java/me/starchier/inventorykeeper/util/PluginHandler.java index 3be13f6..68efc17 100644 --- a/src/main/java/me/starchier/inventorykeeper/util/PluginHandler.java +++ b/src/main/java/me/starchier/inventorykeeper/util/PluginHandler.java @@ -13,7 +13,7 @@ import java.util.regex.Pattern; public class PluginHandler { - InventoryKeeper plugin; + private final InventoryKeeper plugin; public static final String SERVER_VERSION = getVersion(); public static final int FIXED_SERVER_VERSION = Integer.parseInt(getVersion().replace(".", "").replace("_", "").replace("v", "").replace("R", "")); public static final boolean IS_LEGACY = isLegacy();