|
|
|
@ -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) { |
|
|
|
|