Implement more rigid logging

This commit is contained in:
Gabriel Tofvesson 2021-04-24 17:43:15 +02:00
parent 23ee65ed40
commit ee9f962fac
5 changed files with 33 additions and 43 deletions

View File

@ -3,7 +3,7 @@ package dev.w1zzrd.invtweaks;
import dev.w1zzrd.invtweaks.command.SortCommandExecutor; import dev.w1zzrd.invtweaks.command.SortCommandExecutor;
import dev.w1zzrd.invtweaks.listener.SortListener; import dev.w1zzrd.invtweaks.listener.SortListener;
import dev.w1zzrd.invtweaks.listener.StackReplaceListener; import dev.w1zzrd.invtweaks.listener.StackReplaceListener;
import dev.w1zzrd.logging.LoggerFactory; import org.bukkit.Bukkit;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
@ -11,16 +11,19 @@ import org.bukkit.plugin.java.JavaPlugin;
import java.util.Objects; import java.util.Objects;
import java.util.logging.Logger; 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 @Override
public void onEnable() { public void onEnable() {
logger.info("Inventory Tweaks enabled"); logger.info(LOG_PLUGIN_NAME + " Plugin enabled");
Objects.requireNonNull(getCommand("sort")).setExecutor(new SortCommandExecutor());
final PluginManager pluginManager = getServer().getPluginManager(); final PluginManager pluginManager = getServer().getPluginManager();
@ -30,7 +33,7 @@ public class InvTweaksPlugin extends JavaPlugin {
@Override @Override
public void onDisable() { public void onDisable() {
logger.info("Inventory Tweaks disabled"); logger.info(LOG_PLUGIN_NAME + " Plugin disabled");
HandlerList.unregisterAll(this); HandlerList.unregisterAll(this);
} }

View File

@ -1,6 +1,6 @@
package dev.w1zzrd.invtweaks.command; package dev.w1zzrd.invtweaks.command;
import dev.w1zzrd.logging.LoggerFactory; import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.block.*; import org.bukkit.block.*;
import org.bukkit.command.Command; import org.bukkit.command.Command;
@ -17,23 +17,27 @@ import java.util.HashMap;
import java.util.Optional; import java.util.Optional;
import java.util.logging.Logger; import java.util.logging.Logger;
import static dev.w1zzrd.invtweaks.InvTweaksPlugin.LOG_PLUGIN_NAME;
import static org.bukkit.Material.*; import static org.bukkit.Material.*;
public class SortCommandExecutor implements CommandExecutor { public class SortCommandExecutor implements CommandExecutor {
private static final Logger logger = LoggerFactory.getLogger(SortCommandExecutor.class); private static final Logger logger = Bukkit.getLogger();
@Override @Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { 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, // 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 // there is an implicit dependency on that the command is called by (at the very least) an entity
if (!(sender instanceof Player)) { if (!(sender instanceof Player)) {
logger.info(LOG_PLUGIN_NAME + " Sort command triggered by non-player");
sender.sendMessage("Command must be run by a player"); sender.sendMessage("Command must be run by a player");
return false; return false;
} }
final Player player = (Player) sender; 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) // The block the player is currently looking at (if applicable)
final Block targetBlock = player.getTargetBlockExact(6); final Block targetBlock = player.getTargetBlockExact(6);
if (targetBlock == null) if (targetBlock == null)
@ -57,6 +61,7 @@ public class SortCommandExecutor implements CommandExecutor {
* @param shulkerBox Shulker Box to sort * @param shulkerBox Shulker Box to sort
*/ */
private static void sortShulkerBox(final ShulkerBox shulkerBox) { private static void sortShulkerBox(final ShulkerBox shulkerBox) {
logger.fine(LOG_PLUGIN_NAME + " Sorting shulker box");
sortInventory(shulkerBox.getInventory()); sortInventory(shulkerBox.getInventory());
} }
@ -66,6 +71,7 @@ public class SortCommandExecutor implements CommandExecutor {
* @param chest Chest (or stand-in for double-chest) to sort * @param chest Chest (or stand-in for double-chest) to sort
*/ */
private static void sortChest(final Chest chest) { private static void sortChest(final Chest chest) {
logger.fine(LOG_PLUGIN_NAME + " Sorting chest");
final InventoryHolder chestInventoryHolder = chest.getBlockInventory().getHolder(); final InventoryHolder chestInventoryHolder = chest.getBlockInventory().getHolder();
assert chestInventoryHolder != null; assert chestInventoryHolder != null;
@ -114,6 +120,7 @@ public class SortCommandExecutor implements CommandExecutor {
* @param player Player whose inventory is to be sorted * @param player Player whose inventory is to be sorted
*/ */
private static void sortPlayer(final Player player) { private static void sortPlayer(final Player player) {
logger.fine(LOG_PLUGIN_NAME + " Sorting player");
final ItemStack[] stacks = player.getInventory().getContents(); final ItemStack[] stacks = player.getInventory().getContents();
final ItemStack[] sortable = Arrays.copyOfRange(stacks, 9, 36); final ItemStack[] sortable = Arrays.copyOfRange(stacks, 9, 36);
@ -204,7 +211,7 @@ public class SortCommandExecutor implements CommandExecutor {
count.put(key, amount - newAmount); count.put(key, amount - newAmount);
} }
} else { } else {
logger.warning("Found untracked ItemStack while merging stacks"); logger.warning(LOG_PLUGIN_NAME + " Found untracked ItemStack while merging stacks");
} }
} }
} }

View File

@ -1,7 +1,6 @@
package dev.w1zzrd.invtweaks.listener; package dev.w1zzrd.invtweaks.listener;
import dev.w1zzrd.logging.LoggerFactory; import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Chest; import org.bukkit.block.Chest;
import org.bukkit.block.ShulkerBox; import org.bukkit.block.ShulkerBox;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
@ -9,11 +8,14 @@ import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.PlayerInventory; import org.bukkit.inventory.PlayerInventory;
import java.util.Objects;
import java.util.logging.Logger; import java.util.logging.Logger;
import static dev.w1zzrd.invtweaks.InvTweaksPlugin.LOG_PLUGIN_NAME;
public class SortListener implements Listener { public class SortListener implements Listener {
private static final Logger logger = LoggerFactory.getLogger(SortListener.class); private static final Logger logger = Bukkit.getLogger();
@EventHandler @EventHandler
public void onPlayerInteractEvent(final PlayerInteractEvent event) { 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.getClickedBlock().getState() instanceof Chest || event.getClickedBlock().getState() instanceof ShulkerBox) &&
event.getPlayer().isSneaking() && event.getPlayer().isSneaking() &&
playerInventory.getItemInMainHand().getType().name().endsWith("_SWORD") playerInventory.getItemInMainHand().getType().name().endsWith("_SWORD")
) ) {
logger.fine(LOG_PLUGIN_NAME + " Triggering /sort for player " + event.getPlayer().getName());
event.getPlayer().performCommand("sort"); event.getPlayer().performCommand("sort");
}
} }
} }

View File

@ -1,6 +1,6 @@
package dev.w1zzrd.invtweaks.listener; package dev.w1zzrd.invtweaks.listener;
import dev.w1zzrd.logging.LoggerFactory; import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
@ -10,12 +10,14 @@ import org.bukkit.inventory.PlayerInventory;
import java.util.logging.Logger; import java.util.logging.Logger;
import static dev.w1zzrd.invtweaks.InvTweaksPlugin.LOG_PLUGIN_NAME;
public class StackReplaceListener implements Listener { public class StackReplaceListener implements Listener {
private static final int MAX_MAIN_INV = 35; private static final int MAX_MAIN_INV = 35;
private final Logger logger = LoggerFactory.getLogger(StackReplaceListener.class); private final Logger logger = Bukkit.getLogger();
@EventHandler @EventHandler
public void onBlockPlacedEvent(final BlockPlaceEvent event) { 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(i, new ItemStack(Material.AIR, 0));
inv.setItem(event.getHand(), checkStack); 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; break;
} }

View File

@ -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;
}
}