From ee9f962facbfde514f7867f81412cd205a0398ac Mon Sep 17 00:00:00 2001 From: Gabriel Tofvesson Date: Sat, 24 Apr 2021 17:43:15 +0200 Subject: [PATCH] Implement more rigid logging --- src/dev/w1zzrd/invtweaks/InvTweaksPlugin.java | 17 +++++++----- .../command/SortCommandExecutor.java | 13 +++++++--- .../invtweaks/listener/SortListener.java | 12 ++++++--- .../listener/StackReplaceListener.java | 8 +++--- src/dev/w1zzrd/logging/LoggerFactory.java | 26 ------------------- 5 files changed, 33 insertions(+), 43 deletions(-) delete mode 100644 src/dev/w1zzrd/logging/LoggerFactory.java diff --git a/src/dev/w1zzrd/invtweaks/InvTweaksPlugin.java b/src/dev/w1zzrd/invtweaks/InvTweaksPlugin.java index 9da275e..8f8d8e1 100644 --- a/src/dev/w1zzrd/invtweaks/InvTweaksPlugin.java +++ b/src/dev/w1zzrd/invtweaks/InvTweaksPlugin.java @@ -3,7 +3,7 @@ package dev.w1zzrd.invtweaks; import dev.w1zzrd.invtweaks.command.SortCommandExecutor; import dev.w1zzrd.invtweaks.listener.SortListener; import dev.w1zzrd.invtweaks.listener.StackReplaceListener; -import dev.w1zzrd.logging.LoggerFactory; +import org.bukkit.Bukkit; import org.bukkit.event.HandlerList; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; @@ -11,16 +11,19 @@ import org.bukkit.plugin.java.JavaPlugin; import java.util.Objects; import java.util.logging.Logger; -public class InvTweaksPlugin extends JavaPlugin { +/** + * Main plugin class + */ +public final class InvTweaksPlugin extends JavaPlugin { - private final Logger logger = LoggerFactory.getLogger(InvTweaksPlugin.class); + public static final String LOG_PLUGIN_NAME = "[InvTweaks]"; + + private final Logger logger = Bukkit.getLogger(); @Override public void onEnable() { - logger.info("Inventory Tweaks enabled"); - - Objects.requireNonNull(getCommand("sort")).setExecutor(new SortCommandExecutor()); + logger.info(LOG_PLUGIN_NAME + " Plugin enabled"); final PluginManager pluginManager = getServer().getPluginManager(); @@ -30,7 +33,7 @@ public class InvTweaksPlugin extends JavaPlugin { @Override public void onDisable() { - logger.info("Inventory Tweaks disabled"); + logger.info(LOG_PLUGIN_NAME + " Plugin disabled"); HandlerList.unregisterAll(this); } diff --git a/src/dev/w1zzrd/invtweaks/command/SortCommandExecutor.java b/src/dev/w1zzrd/invtweaks/command/SortCommandExecutor.java index f49836c..b10a4c8 100644 --- a/src/dev/w1zzrd/invtweaks/command/SortCommandExecutor.java +++ b/src/dev/w1zzrd/invtweaks/command/SortCommandExecutor.java @@ -1,6 +1,6 @@ package dev.w1zzrd.invtweaks.command; -import dev.w1zzrd.logging.LoggerFactory; +import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.block.*; import org.bukkit.command.Command; @@ -17,23 +17,27 @@ import java.util.HashMap; import java.util.Optional; import java.util.logging.Logger; +import static dev.w1zzrd.invtweaks.InvTweaksPlugin.LOG_PLUGIN_NAME; import static org.bukkit.Material.*; public class SortCommandExecutor implements CommandExecutor { - private static final Logger logger = LoggerFactory.getLogger(SortCommandExecutor.class); + private static final Logger logger = Bukkit.getLogger(); @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { // Since we rely on targeting an inventory either by what we're looking at or by whom it was called, // there is an implicit dependency on that the command is called by (at the very least) an entity if (!(sender instanceof Player)) { + logger.info(LOG_PLUGIN_NAME + " Sort command triggered by non-player"); sender.sendMessage("Command must be run by a player"); return false; } final Player player = (Player) sender; + logger.fine(LOG_PLUGIN_NAME + " Sort triggered by player " + player.getName()); + // The block the player is currently looking at (if applicable) final Block targetBlock = player.getTargetBlockExact(6); if (targetBlock == null) @@ -57,6 +61,7 @@ public class SortCommandExecutor implements CommandExecutor { * @param shulkerBox Shulker Box to sort */ private static void sortShulkerBox(final ShulkerBox shulkerBox) { + logger.fine(LOG_PLUGIN_NAME + " Sorting shulker box"); sortInventory(shulkerBox.getInventory()); } @@ -66,6 +71,7 @@ public class SortCommandExecutor implements CommandExecutor { * @param chest Chest (or stand-in for double-chest) to sort */ private static void sortChest(final Chest chest) { + logger.fine(LOG_PLUGIN_NAME + " Sorting chest"); final InventoryHolder chestInventoryHolder = chest.getBlockInventory().getHolder(); assert chestInventoryHolder != null; @@ -114,6 +120,7 @@ public class SortCommandExecutor implements CommandExecutor { * @param player Player whose inventory is to be sorted */ private static void sortPlayer(final Player player) { + logger.fine(LOG_PLUGIN_NAME + " Sorting player"); final ItemStack[] stacks = player.getInventory().getContents(); final ItemStack[] sortable = Arrays.copyOfRange(stacks, 9, 36); @@ -204,7 +211,7 @@ public class SortCommandExecutor implements CommandExecutor { count.put(key, amount - newAmount); } } else { - logger.warning("Found untracked ItemStack while merging stacks"); + logger.warning(LOG_PLUGIN_NAME + " Found untracked ItemStack while merging stacks"); } } } diff --git a/src/dev/w1zzrd/invtweaks/listener/SortListener.java b/src/dev/w1zzrd/invtweaks/listener/SortListener.java index 18c92ff..f666145 100644 --- a/src/dev/w1zzrd/invtweaks/listener/SortListener.java +++ b/src/dev/w1zzrd/invtweaks/listener/SortListener.java @@ -1,7 +1,6 @@ package dev.w1zzrd.invtweaks.listener; -import dev.w1zzrd.logging.LoggerFactory; -import org.bukkit.Material; +import org.bukkit.Bukkit; import org.bukkit.block.Chest; import org.bukkit.block.ShulkerBox; import org.bukkit.event.EventHandler; @@ -9,11 +8,14 @@ import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.inventory.PlayerInventory; +import java.util.Objects; import java.util.logging.Logger; +import static dev.w1zzrd.invtweaks.InvTweaksPlugin.LOG_PLUGIN_NAME; + public class SortListener implements Listener { - private static final Logger logger = LoggerFactory.getLogger(SortListener.class); + private static final Logger logger = Bukkit.getLogger(); @EventHandler public void onPlayerInteractEvent(final PlayerInteractEvent event) { @@ -24,7 +26,9 @@ public class SortListener implements Listener { (event.getClickedBlock().getState() instanceof Chest || event.getClickedBlock().getState() instanceof ShulkerBox) && event.getPlayer().isSneaking() && playerInventory.getItemInMainHand().getType().name().endsWith("_SWORD") - ) + ) { + logger.fine(LOG_PLUGIN_NAME + " Triggering /sort for player " + event.getPlayer().getName()); event.getPlayer().performCommand("sort"); + } } } diff --git a/src/dev/w1zzrd/invtweaks/listener/StackReplaceListener.java b/src/dev/w1zzrd/invtweaks/listener/StackReplaceListener.java index 52c0606..c8b42b6 100644 --- a/src/dev/w1zzrd/invtweaks/listener/StackReplaceListener.java +++ b/src/dev/w1zzrd/invtweaks/listener/StackReplaceListener.java @@ -1,6 +1,6 @@ package dev.w1zzrd.invtweaks.listener; -import dev.w1zzrd.logging.LoggerFactory; +import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; @@ -10,12 +10,14 @@ import org.bukkit.inventory.PlayerInventory; import java.util.logging.Logger; +import static dev.w1zzrd.invtweaks.InvTweaksPlugin.LOG_PLUGIN_NAME; + public class StackReplaceListener implements Listener { private static final int MAX_MAIN_INV = 35; - private final Logger logger = LoggerFactory.getLogger(StackReplaceListener.class); + private final Logger logger = Bukkit.getLogger(); @EventHandler public void onBlockPlacedEvent(final BlockPlaceEvent event) { @@ -29,7 +31,7 @@ public class StackReplaceListener implements Listener { inv.setItem(i, new ItemStack(Material.AIR, 0)); inv.setItem(event.getHand(), checkStack); - logger.info("Moved stack into players empty hand"); + logger.fine(LOG_PLUGIN_NAME + " Moved stack into empty hand for player " + event.getPlayer().getName()); break; } diff --git a/src/dev/w1zzrd/logging/LoggerFactory.java b/src/dev/w1zzrd/logging/LoggerFactory.java deleted file mode 100644 index df24aef..0000000 --- a/src/dev/w1zzrd/logging/LoggerFactory.java +++ /dev/null @@ -1,26 +0,0 @@ -package dev.w1zzrd.logging; - -import java.util.logging.Level; -import java.util.logging.Logger; - -public class LoggerFactory { - public static Logger getLogger(final Class cls) { - return getLogger(cls, Level.ALL); - } - - public static Logger getLogger(final Class cls, final Level level) { - final Logger l = Logger.getLogger(cls.getName()); - l.setLevel(level); - return l; - } - - public static Logger getLogger(final String name) { - return getLogger(name, Level.ALL); - } - - public static Logger getLogger(final String name, final Level level) { - final Logger l = Logger.getLogger(name); - l.setLevel(level); - return l; - } -}