From 74399cd502ee2da629c4b2b9af431641ee350391 Mon Sep 17 00:00:00 2001 From: Jitse Boonstra Date: Thu, 21 Feb 2019 10:19:27 +0100 Subject: [PATCH] Gave NPCLib its own logger. --- commons/pom.xml | 16 +++++++++-- .../tinyprotocol/LegacyTinyProtocol.java | 28 +++++++++++-------- .../comphenix/tinyprotocol/TinyProtocol.java | 26 ++++++++++------- .../main/java/net/jitse/npclib/NPCLib.java | 25 ++++++++--------- .../jitse/npclib/logging/NPCLibLogger.java | 26 +++++++++++++++++ plugin/pom.xml | 6 +++- .../net/jitse/npclib/plugin/NPCLibPlugin.java | 4 +-- plugin/src/main/resources/plugin.yml | 2 +- 8 files changed, 92 insertions(+), 41 deletions(-) create mode 100644 commons/src/main/java/net/jitse/npclib/logging/NPCLibLogger.java diff --git a/commons/pom.xml b/commons/pom.xml index ee0e809..981a1a5 100755 --- a/commons/pom.xml +++ b/commons/pom.xml @@ -12,6 +12,19 @@ npclib-commons + + + + + + + + + + + + + org.spigotmc @@ -19,13 +32,12 @@ 1.7.10-R0.1-SNAPSHOT provided - + io.netty netty-all 4.1.33.Final provided - diff --git a/commons/src/main/java/com/comphenix/tinyprotocol/LegacyTinyProtocol.java b/commons/src/main/java/com/comphenix/tinyprotocol/LegacyTinyProtocol.java index 61e34a2..a5e6372 100755 --- a/commons/src/main/java/com/comphenix/tinyprotocol/LegacyTinyProtocol.java +++ b/commons/src/main/java/com/comphenix/tinyprotocol/LegacyTinyProtocol.java @@ -2,6 +2,7 @@ package com.comphenix.tinyprotocol; import com.google.common.collect.Lists; import com.google.common.collect.MapMaker; +import net.jitse.npclib.logging.NPCLibLogger; import net.minecraft.util.com.mojang.authlib.GameProfile; import net.minecraft.util.io.netty.channel.*; import org.bukkit.Bukkit; @@ -18,11 +19,13 @@ import org.bukkit.scheduler.BukkitRunnable; import java.util.*; import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Level; +import java.util.logging.Logger; /** * Minimized version of TinyProtocol by Kristian suited for NPCLib. */ public abstract class LegacyTinyProtocol { + private static final AtomicInteger ID = new AtomicInteger(0); // Used in order to lookup a channel @@ -51,6 +54,8 @@ public abstract class LegacyTinyProtocol { private Map protocolLookup = new MapMaker().weakKeys().makeMap(); private Listener listener; + private Logger logger; + // Channels that have already been removed private Set uninjectedChannels = Collections.newSetFromMap(new MapMaker().weakKeys().makeMap()); @@ -71,6 +76,7 @@ public abstract class LegacyTinyProtocol { protected LegacyTinyProtocol(final Plugin plugin) { this.plugin = plugin; + this.logger = new NPCLibLogger(plugin); // Compute handler name this.handlerName = "tiny-" + plugin.getName() + "-" + ID.incrementAndGet(); @@ -79,19 +85,19 @@ public abstract class LegacyTinyProtocol { registerBukkitEvents(); try { - plugin.getLogger().info("[NPCLib] Attempting to inject into netty."); + logger.info("Attempting to inject into netty"); registerChannelHandler(); registerPlayers(plugin); - } catch (IllegalArgumentException ex) { + } catch (IllegalArgumentException exceptionx) { // Damn you, late bind - plugin.getLogger().log(Level.WARNING, "[NPCLib] Attempting to delay injection."); + logger.log(Level.WARNING, "Attempting to delay injection"); new BukkitRunnable() { @Override public void run() { registerChannelHandler(); registerPlayers(plugin); - plugin.getLogger().info("[NPCLib] Injection complete."); + logger.info("Injection complete"); } }.runTask(plugin); } @@ -112,8 +118,8 @@ public abstract class LegacyTinyProtocol { channel.eventLoop().submit(() -> injectChannelInternal(channel)); } } - } catch (Exception e) { - plugin.getLogger().log(Level.SEVERE, "[NPCLib] Cannot inject incomming channel " + channel, e); + } catch (Exception exception) { + logger.log(Level.SEVERE, "Cannot inject incomming channel " + channel, exception); } } @@ -198,7 +204,7 @@ public abstract class LegacyTinyProtocol { serverChannels.add(serverChannel); serverChannel.pipeline().addFirst(serverChannelHandler); - plugin.getLogger().info("[NPCLib] Server channel handler injected (" + serverChannel + ")"); + logger.info("Server channel handler injected (" + serverChannel + ")"); looking = false; } } @@ -215,7 +221,7 @@ public abstract class LegacyTinyProtocol { serverChannel.eventLoop().execute(() -> { try { pipeline.remove(serverChannelHandler); - } catch (NoSuchElementException e) { + } catch (NoSuchElementException exception) { // That's fine } }); @@ -248,7 +254,7 @@ public abstract class LegacyTinyProtocol { } return interceptor; - } catch (IllegalArgumentException e) { + } catch (IllegalArgumentException exception) { // Try again return (PacketInterceptor) channel.pipeline().get(handlerName); } @@ -313,8 +319,8 @@ public abstract class LegacyTinyProtocol { try { msg = onPacketInAsync(player, msg); - } catch (Exception e) { - plugin.getLogger().log(Level.SEVERE, "[NPCLib] Error in onPacketInAsync().", e); + } catch (Exception exception) { + logger.log(Level.SEVERE, "Error in onPacketInAsync()", exception); } if (msg != null) { diff --git a/commons/src/main/java/com/comphenix/tinyprotocol/TinyProtocol.java b/commons/src/main/java/com/comphenix/tinyprotocol/TinyProtocol.java index 507ff6a..50bd16a 100755 --- a/commons/src/main/java/com/comphenix/tinyprotocol/TinyProtocol.java +++ b/commons/src/main/java/com/comphenix/tinyprotocol/TinyProtocol.java @@ -5,6 +5,7 @@ import com.comphenix.tinyprotocol.Reflection.MethodInvoker; import com.google.common.collect.Lists; import com.google.common.collect.MapMaker; import io.netty.channel.*; +import net.jitse.npclib.logging.NPCLibLogger; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -19,11 +20,13 @@ import org.bukkit.scheduler.BukkitRunnable; import java.util.*; import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Level; +import java.util.logging.Logger; /** * Minimized version of TinyProtocol by Kristian suited for NPCLib. */ public abstract class TinyProtocol { + private static final AtomicInteger ID = new AtomicInteger(0); // Used in order to lookup a channel @@ -48,6 +51,8 @@ public abstract class TinyProtocol { private Map channelLookup = new MapMaker().weakValues().makeMap(); private Listener listener; + private Logger logger; + // Channels that have already been removed private Set uninjectedChannels = Collections.newSetFromMap(new MapMaker().weakKeys().makeMap()); @@ -68,6 +73,7 @@ public abstract class TinyProtocol { protected TinyProtocol(final Plugin plugin) { this.plugin = plugin; + this.logger = new NPCLibLogger(plugin); // Compute handler name this.handlerName = "tiny-" + plugin.getName() + "-" + ID.incrementAndGet(); @@ -76,19 +82,19 @@ public abstract class TinyProtocol { registerBukkitEvents(); try { - plugin.getLogger().info("[NPCLib] Attempting to inject into netty."); + logger.info("Attempting to inject into netty"); registerChannelHandler(); registerPlayers(plugin); - } catch (IllegalArgumentException ex) { + } catch (IllegalArgumentException exceptionx) { // Damn you, late bind - plugin.getLogger().log(Level.WARNING, "[NPCLib] Attempting to delay injection."); + logger.log(Level.WARNING, "Attempting to delay injection"); new BukkitRunnable() { @Override public void run() { registerChannelHandler(); registerPlayers(plugin); - plugin.getLogger().info("[NPCLib] Injection complete."); + logger.info("Injection complete"); } }.runTask(plugin); } @@ -109,8 +115,8 @@ public abstract class TinyProtocol { channel.eventLoop().submit(() -> injectChannelInternal(channel)); } } - } catch (Exception e) { - plugin.getLogger().log(Level.SEVERE, "[NPCLib] Cannot inject incomming channel " + channel, e); + } catch (Exception exception) { + logger.log(Level.SEVERE, "Cannot inject incomming channel " + channel, exception); } } @@ -211,7 +217,7 @@ public abstract class TinyProtocol { serverChannel.eventLoop().execute(() -> { try { pipeline.remove(serverChannelHandler); - } catch (NoSuchElementException e) { + } catch (NoSuchElementException exception) { // That's fine } }); @@ -244,7 +250,7 @@ public abstract class TinyProtocol { } return interceptor; - } catch (IllegalArgumentException e) { + } catch (IllegalArgumentException exception) { // Try again return (PacketInterceptor) channel.pipeline().get(handlerName); } @@ -298,8 +304,8 @@ public abstract class TinyProtocol { try { msg = onPacketInAsync(player, msg); - } catch (Exception e) { - plugin.getLogger().log(Level.SEVERE, "[NPCLib] Error in onPacketInAsync().", e); + } catch (Exception exception) { + logger.log(Level.SEVERE, "Error in onPacketInAsync()", exception); } if (msg != null) { diff --git a/commons/src/main/java/net/jitse/npclib/NPCLib.java b/commons/src/main/java/net/jitse/npclib/NPCLib.java index 9e361d9..c3e5843 100755 --- a/commons/src/main/java/net/jitse/npclib/NPCLib.java +++ b/commons/src/main/java/net/jitse/npclib/NPCLib.java @@ -8,15 +8,16 @@ import net.jitse.npclib.api.NPC; import net.jitse.npclib.listeners.ChunkListener; import net.jitse.npclib.listeners.PacketListener; import net.jitse.npclib.listeners.PlayerListener; +import net.jitse.npclib.logging.NPCLibLogger; import net.jitse.npclib.skin.Skin; import org.bukkit.Bukkit; -import org.bukkit.ChatColor; import org.bukkit.Server; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; import java.util.List; import java.util.logging.Level; +import java.util.logging.Logger; /** * @author Jitse Boonstra @@ -27,16 +28,15 @@ public class NPCLib { private final JavaPlugin plugin; private final Class npcClass; - public NPCLib(JavaPlugin plugin) { - this(plugin, true); - } + private Logger logger; - public NPCLib(JavaPlugin plugin, boolean message) { + public NPCLib(JavaPlugin plugin) { this.plugin = plugin; this.server = plugin.getServer(); + this.logger = new NPCLibLogger(plugin); - // TODO: Change this to a more dynamic variable (maven file filtering?). - plugin.getLogger().info("[NPCLib] Initiating NPCLib v1.4."); + // TODO: Change this variable to a dynamic variable (maven file filtering?). + // logger.info("Initiating NPCLib v1.4"); String versionName = server.getClass().getPackage().getName().split("\\.")[3]; @@ -51,14 +51,12 @@ public class NPCLib { this.npcClass = npcClass; if (npcClass == null) { - plugin.getLogger().log(Level.SEVERE, "NPCLib failed to initiate. Your server's version (" - + versionName + ") is not supported."); + logger.log(Level.SEVERE, "Failed to initiate. Your server's version (" + + versionName + ") is not supported"); return; } - if (message) { - plugin.getLogger().info("[NPCLib] Enabled for version " + versionName + "."); - } + logger.info("Enabled for MC " + versionName); registerInternal(); } @@ -85,8 +83,7 @@ public class NPCLib { try { return (NPC) npcClass.getConstructors()[0].newInstance(plugin, skin, autoHideDistance, lines); } catch (Exception exception) { - server.getConsoleSender().sendMessage(ChatColor.RED + "NPCLib failed to create NPC. Please report this stacktrace:"); - exception.printStackTrace(); + logger.log(Level.SEVERE, "Failed to create NPC. Please report the following stacktrace", exception); } return null; diff --git a/commons/src/main/java/net/jitse/npclib/logging/NPCLibLogger.java b/commons/src/main/java/net/jitse/npclib/logging/NPCLibLogger.java new file mode 100644 index 0000000..32f1e9d --- /dev/null +++ b/commons/src/main/java/net/jitse/npclib/logging/NPCLibLogger.java @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2018 Jitse Boonstra + */ + +package net.jitse.npclib.logging; + +import org.bukkit.plugin.Plugin; + +import java.util.logging.Level; +import java.util.logging.LogRecord; +import java.util.logging.Logger; + +public class NPCLibLogger extends Logger { + + public NPCLibLogger(Plugin context) { + super(context.getClass().getCanonicalName(), null); + setParent(context.getServer().getLogger()); + setLevel(Level.ALL); + } + + @Override + public void log(LogRecord logRecord) { + logRecord.setMessage("[NPCLib] " + logRecord.getMessage()); + super.log(logRecord); + } +} diff --git a/plugin/pom.xml b/plugin/pom.xml index 8e2e1fc..6dbb31f 100755 --- a/plugin/pom.xml +++ b/plugin/pom.xml @@ -17,8 +17,12 @@ npclib-plugin-v${project.parent.version} - src/main/resources + . + ${basedir}/src/main/resources true + + plugin.yml + diff --git a/plugin/src/main/java/net/jitse/npclib/plugin/NPCLibPlugin.java b/plugin/src/main/java/net/jitse/npclib/plugin/NPCLibPlugin.java index db84b22..a7151b8 100755 --- a/plugin/src/main/java/net/jitse/npclib/plugin/NPCLibPlugin.java +++ b/plugin/src/main/java/net/jitse/npclib/plugin/NPCLibPlugin.java @@ -13,11 +13,11 @@ public class NPCLibPlugin extends JavaPlugin { @Override public void onEnable() { - getLogger().info("NPC library loaded."); + getLogger().info("NPCLib classes loaded"); } @Override public void onDisable() { - getLogger().info("NPC library unloaded."); + getLogger().info("NPCLib classes unloaded"); } } diff --git a/plugin/src/main/resources/plugin.yml b/plugin/src/main/resources/plugin.yml index eda63db..30f6853 100755 --- a/plugin/src/main/resources/plugin.yml +++ b/plugin/src/main/resources/plugin.yml @@ -1,4 +1,4 @@ -name: NPCLib +name: NPCLib-Plugin version: ${project.parent.version} author: JitseB main: net.jitse.npclib.plugin.NPCLibPlugin