9
0
mirror of https://github.com/LeavesMC/Leaves.git synced 2026-01-01 05:16:29 +00:00

Improve protocol core behavior

This commit is contained in:
Lumine1909
2025-10-08 15:48:39 -07:00
parent 207626c779
commit 0749458e61
3 changed files with 17 additions and 11 deletions

View File

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

View File

@@ -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<ByteBuf, RegistryFriendlyByteBuf> bufDecorator = buf -> buf instanceof RegistryFriendlyByteBuf registry ? registry : new RegistryFriendlyByteBuf(buf, MinecraftServer.getServer().registryAccess());
private static final Cache<ServerCommonPacketListenerImpl, IdentifierSelector> SELECTOR_CACHE = CacheBuilder.newBuilder().expireAfterAccess(30, TimeUnit.SECONDS).build();
private static final Function<ByteBuf, RegistryFriendlyByteBuf> 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) {

View File

@@ -15,7 +15,6 @@ public abstract class AbstractInvokerHolder<T> {
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<T> {
this.handler = handler;
this.returnType = returnType;
this.parameterTypes = parameterTypes;
this.isStatic = Modifier.isStatic(invoker.getModifiers());
validateMethodSignature();
}
@@ -61,11 +59,7 @@ public abstract class AbstractInvokerHolder<T> {
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) {