Gave NPCLib its own logger.

master
Jitse Boonstra 4 years ago
parent 9ded01ce7f
commit 74399cd502
  1. 16
      commons/pom.xml
  2. 28
      commons/src/main/java/com/comphenix/tinyprotocol/LegacyTinyProtocol.java
  3. 26
      commons/src/main/java/com/comphenix/tinyprotocol/TinyProtocol.java
  4. 25
      commons/src/main/java/net/jitse/npclib/NPCLib.java
  5. 26
      commons/src/main/java/net/jitse/npclib/logging/NPCLibLogger.java
  6. 6
      plugin/pom.xml
  7. 4
      plugin/src/main/java/net/jitse/npclib/plugin/NPCLibPlugin.java
  8. 2
      plugin/src/main/resources/plugin.yml

@ -12,6 +12,19 @@
<artifactId>npclib-commons</artifactId>
<!--<build>-->
<!--<resources>-->
<!--<resource>-->
<!--<targetPath>.</targetPath>-->
<!--<directory>${basedir}/src/main/java/net/jitse/npclib</directory>-->
<!--<filtering>true</filtering>-->
<!--<includes>-->
<!--<include>NPCLib.java</include>-->
<!--</includes>-->
<!--</resource>-->
<!--</resources>-->
<!--</build>-->
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
@ -19,13 +32,12 @@
<version>1.7.10-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<!-- Netty (implemented in version above 1.7.10) -->
<!-- Netty (implemented in versions above 1.7.10) -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.33.Final</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

@ -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<Channel, Integer> protocolLookup = new MapMaker().weakKeys().makeMap();
private Listener listener;
private Logger logger;
// Channels that have already been removed
private Set<Channel> 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) {

@ -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<String, Channel> channelLookup = new MapMaker().weakValues().makeMap();
private Listener listener;
private Logger logger;
// Channels that have already been removed
private Set<Channel> 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) {

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

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

@ -17,8 +17,12 @@
<finalName>npclib-plugin-v${project.parent.version}</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<targetPath>.</targetPath>
<directory>${basedir}/src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>plugin.yml</include>
</includes>
</resource>
</resources>
</build>

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

@ -1,4 +1,4 @@
name: NPCLib
name: NPCLib-Plugin
version: ${project.parent.version}
author: JitseB
main: net.jitse.npclib.plugin.NPCLibPlugin

Loading…
Cancel
Save