From 0749458e61d4e037fefc7ac8a1111fbbc5aee1ec Mon Sep 17 00:00:00 2001 From: Lumine1909 <133463833+Lumine1909@users.noreply.github.com> Date: Wed, 8 Oct 2025 15:48:39 -0700 Subject: [PATCH] Improve protocol core behavior --- .../protocol/core/LeavesProtocolManager.java | 2 +- .../leaves/protocol/core/ProtocolUtils.java | 18 +++++++++++++++--- .../core/invoker/AbstractInvokerHolder.java | 8 +------- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/protocol/core/LeavesProtocolManager.java b/leaves-server/src/main/java/org/leavesmc/leaves/protocol/core/LeavesProtocolManager.java index 88b6ead4..87eb1cf2 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/protocol/core/LeavesProtocolManager.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/protocol/core/LeavesProtocolManager.java @@ -208,7 +208,7 @@ public class LeavesProtocolManager { try { return codec.decode(ProtocolUtils.decorate(buf)); } catch (Exception e) { - LOGGER.severe("Failed to decode " + location, e); + LOGGER.severe("Failed to decode payload " + location, e); throw e; } } diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/protocol/core/ProtocolUtils.java b/leaves-server/src/main/java/org/leavesmc/leaves/protocol/core/ProtocolUtils.java index ec6771d7..93b61305 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/protocol/core/ProtocolUtils.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/protocol/core/ProtocolUtils.java @@ -1,5 +1,7 @@ package org.leavesmc.leaves.protocol.core; +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBufUtil; import io.netty.buffer.Unpooled; @@ -16,12 +18,14 @@ import net.minecraft.server.network.ServerGamePacketListenerImpl; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.concurrent.TimeUnit; import java.util.function.Consumer; import java.util.function.Function; public class ProtocolUtils { - private static final Function bufDecorator = buf -> buf instanceof RegistryFriendlyByteBuf registry ? registry : new RegistryFriendlyByteBuf(buf, MinecraftServer.getServer().registryAccess()); + private static final Cache SELECTOR_CACHE = CacheBuilder.newBuilder().expireAfterAccess(30, TimeUnit.SECONDS).build(); + private static final Function BUF_DECORATOR = buf -> buf instanceof RegistryFriendlyByteBuf registry ? registry : new RegistryFriendlyByteBuf(buf, MinecraftServer.getServer().registryAccess()); private static final byte[] EMPTY = new byte[0]; public static String buildProtocolVersion(String protocol) { @@ -57,12 +61,20 @@ public class ProtocolUtils { } public static RegistryFriendlyByteBuf decorate(ByteBuf buf) { - return bufDecorator.apply(buf); + return BUF_DECORATOR.apply(buf); } public static IdentifierSelector createSelector(ServerCommonPacketListenerImpl common) { + IdentifierSelector selector = SELECTOR_CACHE.getIfPresent(common); + if (selector != null) { + return selector; + } ServerPlayer player = common instanceof ServerGamePacketListenerImpl game ? game.getPlayer() : null; - return new IdentifierSelector(new Context(common.profile, common.connection), player); + selector = new IdentifierSelector(new Context(common.profile, common.connection), player); + if (player != null) { + SELECTOR_CACHE.put(common, selector); + } + return selector; } public static ByteBuf wrapNullable(byte @Nullable [] data) { diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/protocol/core/invoker/AbstractInvokerHolder.java b/leaves-server/src/main/java/org/leavesmc/leaves/protocol/core/invoker/AbstractInvokerHolder.java index 906b7264..824ad76c 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/protocol/core/invoker/AbstractInvokerHolder.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/protocol/core/invoker/AbstractInvokerHolder.java @@ -15,7 +15,6 @@ public abstract class AbstractInvokerHolder { protected final T handler; protected final Class returnType; protected final Class[] parameterTypes; - protected final boolean isStatic; protected AbstractInvokerHolder(LeavesProtocol owner, Method invoker, T handler, @Nullable Class returnType, @NotNull Class... parameterTypes) { this.owner = owner; @@ -23,7 +22,6 @@ public abstract class AbstractInvokerHolder { this.handler = handler; this.returnType = returnType; this.parameterTypes = parameterTypes; - this.isStatic = Modifier.isStatic(invoker.getModifiers()); validateMethodSignature(); } @@ -61,11 +59,7 @@ public abstract class AbstractInvokerHolder { return null; } try { - if (isStatic) { - return invoker.invoke(null, args); - } else { - return invoker.invoke(owner, args); - } + return invoker.invoke(owner, args); } catch (InvocationTargetException e) { throw new RuntimeException(e.getCause()); } catch (Exception e) {