Add singleton to TinyProtocol

This commit is contained in:
2026-05-16 11:37:39 +02:00
parent 5a57b5f799
commit 604657a084
@@ -21,6 +21,7 @@ package com.comphenix.tinyprotocol;
import com.google.common.collect.MapMaker; import com.google.common.collect.MapMaker;
import de.steamwar.Reflection; import de.steamwar.Reflection;
import de.steamwar.core.Core;
import io.netty.channel.*; import io.netty.channel.*;
import net.minecraft.network.Connection; import net.minecraft.network.Connection;
import net.minecraft.network.protocol.login.ServerboundHelloPacket; import net.minecraft.network.protocol.login.ServerboundHelloPacket;
@@ -38,7 +39,6 @@ import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
import java.util.*; import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level; import java.util.logging.Level;
/** /**
@@ -48,9 +48,7 @@ import java.util.logging.Level;
* *
* @author Kristian * @author Kristian
*/ */
public abstract class TinyProtocol { public class TinyProtocol {
private static final AtomicInteger ID = new AtomicInteger(0);
// Speedup channel lookup // Speedup channel lookup
private Map<String, Channel> channelLookup = new MapMaker().weakValues().makeMap(); private Map<String, Channel> channelLookup = new MapMaker().weakValues().makeMap();
private Listener listener; private Listener listener;
@@ -68,11 +66,13 @@ public abstract class TinyProtocol {
private ChannelInitializer<Channel> endInitProtocol; private ChannelInitializer<Channel> endInitProtocol;
// Current handler name // Current handler name
private String handlerName; private static final String HANDLER_NAME = "tiny-steamwar";
protected volatile boolean closed; protected volatile boolean closed;
protected Plugin plugin; protected Plugin plugin;
public static final TinyProtocol instance = new TinyProtocol(Core.getInstance());
/** /**
* Construct a new instance of TinyProtocol, and start intercepting packets for all connected clients and future clients. * Construct a new instance of TinyProtocol, and start intercepting packets for all connected clients and future clients.
* <p> * <p>
@@ -80,12 +80,9 @@ public abstract class TinyProtocol {
* *
* @param plugin - the plugin. * @param plugin - the plugin.
*/ */
public TinyProtocol(final Plugin plugin) { private TinyProtocol(final Plugin plugin) {
this.plugin = plugin; this.plugin = plugin;
// Compute handler name
this.handlerName = getHandlerName();
// Prepare existing players // Prepare existing players
registerBukkitEvents(); registerBukkitEvents();
@@ -314,17 +311,6 @@ public abstract class TinyProtocol {
channel.pipeline().context("encoder").fireChannelRead(packet); channel.pipeline().context("encoder").fireChannelRead(packet);
} }
/**
* Retrieve the name of the channel injector, default implementation is "tiny-" + plugin name + "-" + a unique ID.
* <p>
* Note that this method will only be invoked once. It is no longer necessary to override this to support multiple instances.
*
* @return A unique channel handler name.
*/
protected String getHandlerName() {
return "tiny-" + plugin.getName() + "-" + ID.incrementAndGet();
}
/** /**
* Add a custom channel handler to the given player's channel pipeline, allowing us to intercept sent and received packets. * Add a custom channel handler to the given player's channel pipeline, allowing us to intercept sent and received packets.
* <p> * <p>
@@ -354,19 +340,19 @@ public abstract class TinyProtocol {
*/ */
private PacketInterceptor injectChannelInternal(Channel channel) { private PacketInterceptor injectChannelInternal(Channel channel) {
try { try {
PacketInterceptor interceptor = (PacketInterceptor) channel.pipeline().get(handlerName); PacketInterceptor interceptor = (PacketInterceptor) channel.pipeline().get(HANDLER_NAME);
// Inject our packet interceptor // Inject our packet interceptor
if (interceptor == null) { if (interceptor == null) {
interceptor = new PacketInterceptor(); interceptor = new PacketInterceptor();
channel.pipeline().addBefore("packet_handler", handlerName, interceptor); channel.pipeline().addBefore("packet_handler", HANDLER_NAME, interceptor);
uninjectedChannels.remove(channel); uninjectedChannels.remove(channel);
} }
return interceptor; return interceptor;
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
// Try again // Try again
return (PacketInterceptor) channel.pipeline().get(handlerName); return (PacketInterceptor) channel.pipeline().get(HANDLER_NAME);
} }
} }
@@ -415,7 +401,7 @@ public abstract class TinyProtocol {
@Override @Override
public void run() { public void run() {
channel.pipeline().remove(handlerName); channel.pipeline().remove(HANDLER_NAME);
} }
}); });
@@ -438,7 +424,7 @@ public abstract class TinyProtocol {
* @return TRUE if it is, FALSE otherwise. * @return TRUE if it is, FALSE otherwise.
*/ */
public boolean hasInjected(Channel channel) { public boolean hasInjected(Channel channel) {
return channel.pipeline().get(handlerName) != null; return channel.pipeline().get(HANDLER_NAME) != null;
} }
/** /**