Gave NPCLib its own logger.
This commit is contained in:
parent
9ded01ce7f
commit
74399cd502
|
@ -12,6 +12,19 @@
|
||||||
|
|
||||||
<artifactId>npclib-commons</artifactId>
|
<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>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.spigotmc</groupId>
|
<groupId>org.spigotmc</groupId>
|
||||||
|
@ -19,13 +32,12 @@
|
||||||
<version>1.7.10-R0.1-SNAPSHOT</version>
|
<version>1.7.10-R0.1-SNAPSHOT</version>
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- Netty (implemented in version above 1.7.10) -->
|
<!-- Netty (implemented in versions above 1.7.10) -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.netty</groupId>
|
<groupId>io.netty</groupId>
|
||||||
<artifactId>netty-all</artifactId>
|
<artifactId>netty-all</artifactId>
|
||||||
<version>4.1.33.Final</version>
|
<version>4.1.33.Final</version>
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -2,6 +2,7 @@ package com.comphenix.tinyprotocol;
|
||||||
|
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
import com.google.common.collect.MapMaker;
|
import com.google.common.collect.MapMaker;
|
||||||
|
import net.jitse.npclib.logging.NPCLibLogger;
|
||||||
import net.minecraft.util.com.mojang.authlib.GameProfile;
|
import net.minecraft.util.com.mojang.authlib.GameProfile;
|
||||||
import net.minecraft.util.io.netty.channel.*;
|
import net.minecraft.util.io.netty.channel.*;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
|
@ -18,11 +19,13 @@ import org.bukkit.scheduler.BukkitRunnable;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Minimized version of TinyProtocol by Kristian suited for NPCLib.
|
* Minimized version of TinyProtocol by Kristian suited for NPCLib.
|
||||||
*/
|
*/
|
||||||
public abstract class LegacyTinyProtocol {
|
public abstract class LegacyTinyProtocol {
|
||||||
|
|
||||||
private static final AtomicInteger ID = new AtomicInteger(0);
|
private static final AtomicInteger ID = new AtomicInteger(0);
|
||||||
|
|
||||||
// Used in order to lookup a channel
|
// 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 Map<Channel, Integer> protocolLookup = new MapMaker().weakKeys().makeMap();
|
||||||
private Listener listener;
|
private Listener listener;
|
||||||
|
|
||||||
|
private Logger logger;
|
||||||
|
|
||||||
// Channels that have already been removed
|
// Channels that have already been removed
|
||||||
private Set<Channel> uninjectedChannels = Collections.newSetFromMap(new MapMaker().weakKeys().makeMap());
|
private Set<Channel> uninjectedChannels = Collections.newSetFromMap(new MapMaker().weakKeys().makeMap());
|
||||||
|
|
||||||
|
@ -71,6 +76,7 @@ public abstract class LegacyTinyProtocol {
|
||||||
|
|
||||||
protected LegacyTinyProtocol(final Plugin plugin) {
|
protected LegacyTinyProtocol(final Plugin plugin) {
|
||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
|
this.logger = new NPCLibLogger(plugin);
|
||||||
|
|
||||||
// Compute handler name
|
// Compute handler name
|
||||||
this.handlerName = "tiny-" + plugin.getName() + "-" + ID.incrementAndGet();
|
this.handlerName = "tiny-" + plugin.getName() + "-" + ID.incrementAndGet();
|
||||||
|
@ -79,19 +85,19 @@ public abstract class LegacyTinyProtocol {
|
||||||
registerBukkitEvents();
|
registerBukkitEvents();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
plugin.getLogger().info("[NPCLib] Attempting to inject into netty.");
|
logger.info("Attempting to inject into netty");
|
||||||
registerChannelHandler();
|
registerChannelHandler();
|
||||||
registerPlayers(plugin);
|
registerPlayers(plugin);
|
||||||
} catch (IllegalArgumentException ex) {
|
} catch (IllegalArgumentException exceptionx) {
|
||||||
// Damn you, late bind
|
// Damn you, late bind
|
||||||
plugin.getLogger().log(Level.WARNING, "[NPCLib] Attempting to delay injection.");
|
logger.log(Level.WARNING, "Attempting to delay injection");
|
||||||
|
|
||||||
new BukkitRunnable() {
|
new BukkitRunnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
registerChannelHandler();
|
registerChannelHandler();
|
||||||
registerPlayers(plugin);
|
registerPlayers(plugin);
|
||||||
plugin.getLogger().info("[NPCLib] Injection complete.");
|
logger.info("Injection complete");
|
||||||
}
|
}
|
||||||
}.runTask(plugin);
|
}.runTask(plugin);
|
||||||
}
|
}
|
||||||
|
@ -112,8 +118,8 @@ public abstract class LegacyTinyProtocol {
|
||||||
channel.eventLoop().submit(() -> injectChannelInternal(channel));
|
channel.eventLoop().submit(() -> injectChannelInternal(channel));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception exception) {
|
||||||
plugin.getLogger().log(Level.SEVERE, "[NPCLib] Cannot inject incomming channel " + channel, e);
|
logger.log(Level.SEVERE, "Cannot inject incomming channel " + channel, exception);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -198,7 +204,7 @@ public abstract class LegacyTinyProtocol {
|
||||||
serverChannels.add(serverChannel);
|
serverChannels.add(serverChannel);
|
||||||
|
|
||||||
serverChannel.pipeline().addFirst(serverChannelHandler);
|
serverChannel.pipeline().addFirst(serverChannelHandler);
|
||||||
plugin.getLogger().info("[NPCLib] Server channel handler injected (" + serverChannel + ")");
|
logger.info("Server channel handler injected (" + serverChannel + ")");
|
||||||
looking = false;
|
looking = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -215,7 +221,7 @@ public abstract class LegacyTinyProtocol {
|
||||||
serverChannel.eventLoop().execute(() -> {
|
serverChannel.eventLoop().execute(() -> {
|
||||||
try {
|
try {
|
||||||
pipeline.remove(serverChannelHandler);
|
pipeline.remove(serverChannelHandler);
|
||||||
} catch (NoSuchElementException e) {
|
} catch (NoSuchElementException exception) {
|
||||||
// That's fine
|
// That's fine
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -248,7 +254,7 @@ public abstract class LegacyTinyProtocol {
|
||||||
}
|
}
|
||||||
|
|
||||||
return interceptor;
|
return interceptor;
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException exception) {
|
||||||
// Try again
|
// Try again
|
||||||
return (PacketInterceptor) channel.pipeline().get(handlerName);
|
return (PacketInterceptor) channel.pipeline().get(handlerName);
|
||||||
}
|
}
|
||||||
|
@ -313,8 +319,8 @@ public abstract class LegacyTinyProtocol {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
msg = onPacketInAsync(player, msg);
|
msg = onPacketInAsync(player, msg);
|
||||||
} catch (Exception e) {
|
} catch (Exception exception) {
|
||||||
plugin.getLogger().log(Level.SEVERE, "[NPCLib] Error in onPacketInAsync().", e);
|
logger.log(Level.SEVERE, "Error in onPacketInAsync()", exception);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (msg != null) {
|
if (msg != null) {
|
||||||
|
|
|
@ -5,6 +5,7 @@ import com.comphenix.tinyprotocol.Reflection.MethodInvoker;
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
import com.google.common.collect.MapMaker;
|
import com.google.common.collect.MapMaker;
|
||||||
import io.netty.channel.*;
|
import io.netty.channel.*;
|
||||||
|
import net.jitse.npclib.logging.NPCLibLogger;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
|
@ -19,11 +20,13 @@ import org.bukkit.scheduler.BukkitRunnable;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Minimized version of TinyProtocol by Kristian suited for NPCLib.
|
* Minimized version of TinyProtocol by Kristian suited for NPCLib.
|
||||||
*/
|
*/
|
||||||
public abstract class TinyProtocol {
|
public abstract class TinyProtocol {
|
||||||
|
|
||||||
private static final AtomicInteger ID = new AtomicInteger(0);
|
private static final AtomicInteger ID = new AtomicInteger(0);
|
||||||
|
|
||||||
// Used in order to lookup a channel
|
// 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 Map<String, Channel> channelLookup = new MapMaker().weakValues().makeMap();
|
||||||
private Listener listener;
|
private Listener listener;
|
||||||
|
|
||||||
|
private Logger logger;
|
||||||
|
|
||||||
// Channels that have already been removed
|
// Channels that have already been removed
|
||||||
private Set<Channel> uninjectedChannels = Collections.newSetFromMap(new MapMaker().weakKeys().makeMap());
|
private Set<Channel> uninjectedChannels = Collections.newSetFromMap(new MapMaker().weakKeys().makeMap());
|
||||||
|
|
||||||
|
@ -68,6 +73,7 @@ public abstract class TinyProtocol {
|
||||||
|
|
||||||
protected TinyProtocol(final Plugin plugin) {
|
protected TinyProtocol(final Plugin plugin) {
|
||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
|
this.logger = new NPCLibLogger(plugin);
|
||||||
|
|
||||||
// Compute handler name
|
// Compute handler name
|
||||||
this.handlerName = "tiny-" + plugin.getName() + "-" + ID.incrementAndGet();
|
this.handlerName = "tiny-" + plugin.getName() + "-" + ID.incrementAndGet();
|
||||||
|
@ -76,19 +82,19 @@ public abstract class TinyProtocol {
|
||||||
registerBukkitEvents();
|
registerBukkitEvents();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
plugin.getLogger().info("[NPCLib] Attempting to inject into netty.");
|
logger.info("Attempting to inject into netty");
|
||||||
registerChannelHandler();
|
registerChannelHandler();
|
||||||
registerPlayers(plugin);
|
registerPlayers(plugin);
|
||||||
} catch (IllegalArgumentException ex) {
|
} catch (IllegalArgumentException exceptionx) {
|
||||||
// Damn you, late bind
|
// Damn you, late bind
|
||||||
plugin.getLogger().log(Level.WARNING, "[NPCLib] Attempting to delay injection.");
|
logger.log(Level.WARNING, "Attempting to delay injection");
|
||||||
|
|
||||||
new BukkitRunnable() {
|
new BukkitRunnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
registerChannelHandler();
|
registerChannelHandler();
|
||||||
registerPlayers(plugin);
|
registerPlayers(plugin);
|
||||||
plugin.getLogger().info("[NPCLib] Injection complete.");
|
logger.info("Injection complete");
|
||||||
}
|
}
|
||||||
}.runTask(plugin);
|
}.runTask(plugin);
|
||||||
}
|
}
|
||||||
|
@ -109,8 +115,8 @@ public abstract class TinyProtocol {
|
||||||
channel.eventLoop().submit(() -> injectChannelInternal(channel));
|
channel.eventLoop().submit(() -> injectChannelInternal(channel));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception exception) {
|
||||||
plugin.getLogger().log(Level.SEVERE, "[NPCLib] Cannot inject incomming channel " + channel, e);
|
logger.log(Level.SEVERE, "Cannot inject incomming channel " + channel, exception);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -211,7 +217,7 @@ public abstract class TinyProtocol {
|
||||||
serverChannel.eventLoop().execute(() -> {
|
serverChannel.eventLoop().execute(() -> {
|
||||||
try {
|
try {
|
||||||
pipeline.remove(serverChannelHandler);
|
pipeline.remove(serverChannelHandler);
|
||||||
} catch (NoSuchElementException e) {
|
} catch (NoSuchElementException exception) {
|
||||||
// That's fine
|
// That's fine
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -244,7 +250,7 @@ public abstract class TinyProtocol {
|
||||||
}
|
}
|
||||||
|
|
||||||
return interceptor;
|
return interceptor;
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException exception) {
|
||||||
// Try again
|
// Try again
|
||||||
return (PacketInterceptor) channel.pipeline().get(handlerName);
|
return (PacketInterceptor) channel.pipeline().get(handlerName);
|
||||||
}
|
}
|
||||||
|
@ -298,8 +304,8 @@ public abstract class TinyProtocol {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
msg = onPacketInAsync(player, msg);
|
msg = onPacketInAsync(player, msg);
|
||||||
} catch (Exception e) {
|
} catch (Exception exception) {
|
||||||
plugin.getLogger().log(Level.SEVERE, "[NPCLib] Error in onPacketInAsync().", e);
|
logger.log(Level.SEVERE, "Error in onPacketInAsync()", exception);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (msg != null) {
|
if (msg != null) {
|
||||||
|
|
|
@ -8,15 +8,16 @@ import net.jitse.npclib.api.NPC;
|
||||||
import net.jitse.npclib.listeners.ChunkListener;
|
import net.jitse.npclib.listeners.ChunkListener;
|
||||||
import net.jitse.npclib.listeners.PacketListener;
|
import net.jitse.npclib.listeners.PacketListener;
|
||||||
import net.jitse.npclib.listeners.PlayerListener;
|
import net.jitse.npclib.listeners.PlayerListener;
|
||||||
|
import net.jitse.npclib.logging.NPCLibLogger;
|
||||||
import net.jitse.npclib.skin.Skin;
|
import net.jitse.npclib.skin.Skin;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.ChatColor;
|
|
||||||
import org.bukkit.Server;
|
import org.bukkit.Server;
|
||||||
import org.bukkit.plugin.PluginManager;
|
import org.bukkit.plugin.PluginManager;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Jitse Boonstra
|
* @author Jitse Boonstra
|
||||||
|
@ -27,16 +28,15 @@ public class NPCLib {
|
||||||
private final JavaPlugin plugin;
|
private final JavaPlugin plugin;
|
||||||
private final Class<?> npcClass;
|
private final Class<?> npcClass;
|
||||||
|
|
||||||
public NPCLib(JavaPlugin plugin) {
|
private Logger logger;
|
||||||
this(plugin, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
public NPCLib(JavaPlugin plugin, boolean message) {
|
public NPCLib(JavaPlugin plugin) {
|
||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
this.server = plugin.getServer();
|
this.server = plugin.getServer();
|
||||||
|
this.logger = new NPCLibLogger(plugin);
|
||||||
|
|
||||||
// TODO: Change this to a more dynamic variable (maven file filtering?).
|
// TODO: Change this variable to a dynamic variable (maven file filtering?).
|
||||||
plugin.getLogger().info("[NPCLib] Initiating NPCLib v1.4.");
|
// logger.info("Initiating NPCLib v1.4");
|
||||||
|
|
||||||
String versionName = server.getClass().getPackage().getName().split("\\.")[3];
|
String versionName = server.getClass().getPackage().getName().split("\\.")[3];
|
||||||
|
|
||||||
|
@ -51,14 +51,12 @@ public class NPCLib {
|
||||||
this.npcClass = npcClass;
|
this.npcClass = npcClass;
|
||||||
|
|
||||||
if (npcClass == null) {
|
if (npcClass == null) {
|
||||||
plugin.getLogger().log(Level.SEVERE, "NPCLib failed to initiate. Your server's version ("
|
logger.log(Level.SEVERE, "Failed to initiate. Your server's version ("
|
||||||
+ versionName + ") is not supported.");
|
+ versionName + ") is not supported");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (message) {
|
logger.info("Enabled for MC " + versionName);
|
||||||
plugin.getLogger().info("[NPCLib] Enabled for version " + versionName + ".");
|
|
||||||
}
|
|
||||||
|
|
||||||
registerInternal();
|
registerInternal();
|
||||||
}
|
}
|
||||||
|
@ -85,8 +83,7 @@ public class NPCLib {
|
||||||
try {
|
try {
|
||||||
return (NPC) npcClass.getConstructors()[0].newInstance(plugin, skin, autoHideDistance, lines);
|
return (NPC) npcClass.getConstructors()[0].newInstance(plugin, skin, autoHideDistance, lines);
|
||||||
} catch (Exception exception) {
|
} catch (Exception exception) {
|
||||||
server.getConsoleSender().sendMessage(ChatColor.RED + "NPCLib failed to create NPC. Please report this stacktrace:");
|
logger.log(Level.SEVERE, "Failed to create NPC. Please report the following stacktrace", exception);
|
||||||
exception.printStackTrace();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
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>
|
<finalName>npclib-plugin-v${project.parent.version}</finalName>
|
||||||
<resources>
|
<resources>
|
||||||
<resource>
|
<resource>
|
||||||
<directory>src/main/resources</directory>
|
<targetPath>.</targetPath>
|
||||||
|
<directory>${basedir}/src/main/resources</directory>
|
||||||
<filtering>true</filtering>
|
<filtering>true</filtering>
|
||||||
|
<includes>
|
||||||
|
<include>plugin.yml</include>
|
||||||
|
</includes>
|
||||||
</resource>
|
</resource>
|
||||||
</resources>
|
</resources>
|
||||||
</build>
|
</build>
|
||||||
|
|
|
@ -13,11 +13,11 @@ public class NPCLibPlugin extends JavaPlugin {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
getLogger().info("NPC library loaded.");
|
getLogger().info("NPCLib classes loaded");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDisable() {
|
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}
|
version: ${project.parent.version}
|
||||||
author: JitseB
|
author: JitseB
|
||||||
main: net.jitse.npclib.plugin.NPCLibPlugin
|
main: net.jitse.npclib.plugin.NPCLibPlugin
|
||||||
|
|
Loading…
Reference in New Issue