Update Protocols from Leaves

This commit is contained in:
Helvetica Volubi
2025-12-28 00:29:37 +08:00
parent 300e891906
commit 0783c97224
10 changed files with 47 additions and 39 deletions
@@ -4,5 +4,5 @@ import com.mojang.logging.LogUtils;
import org.slf4j.Logger;
public class LophineLogger {
public static final Logger LOGGER = LogUtils.getClassLogger(); // only provided for some Logger can not be initialized
public static final Logger LOGGER = LogUtils.getLogger(); // only provided for some Logger can not be initialized
}
@@ -36,12 +36,12 @@ public class XaeroMapProtocol implements LeavesProtocol {
@Contract("_ -> new")
public static Identifier idMini(String path) {
return Identifier.tryBuild(PROTOCOL_ID_MINI, path);
return Identifier.fromNamespaceAndPath(PROTOCOL_ID_MINI, path);
}
@Contract("_ -> new")
public static Identifier idWorld(String path) {
return Identifier.tryBuild(PROTOCOL_ID_WORLD, path);
return Identifier.fromNamespaceAndPath(PROTOCOL_ID_WORLD, path);
}
public static void onSendWorldInfo(@NotNull ServerPlayer player) {
@@ -213,7 +213,12 @@ public class LeavesProtocolManager {
if (codec == null) {
return null;
}
return codec.decode(ProtocolUtils.decorate(buf));
try {
return codec.decode(ProtocolUtils.decorate(buf));
} catch (Exception e) {
LOGGER.error("Failed to decode payload {}", location, e);
throw e;
}
}
public static void encode(FriendlyByteBuf buf, LeavesCustomPayload payload) {
@@ -222,8 +227,13 @@ public class LeavesProtocolManager {
if (location == null || codec == null) {
throw new IllegalArgumentException("Payload " + payload.getClass() + " is not configured correctly " + location + " " + codec);
}
buf.writeIdentifier(location);
codec.encode(ProtocolUtils.decorate(buf), payload);
try {
buf.writeIdentifier(location);
codec.encode(ProtocolUtils.decorate(buf), payload);
} catch (Exception e) {
LOGGER.error("Failed to encode payload {}", location, e);
throw e;
}
}
public static void handlePayload(IdentifierSelector selector, LeavesCustomPayload payload) {
@@ -18,6 +18,8 @@
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;
@@ -34,12 +36,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) {
@@ -75,12 +79,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) {
@@ -23,7 +23,6 @@ import org.leavesmc.leaves.protocol.core.LeavesProtocol;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
public abstract class AbstractInvokerHolder<T> {
@@ -32,7 +31,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;
@@ -40,7 +38,6 @@ public abstract class AbstractInvokerHolder<T> {
this.handler = handler;
this.returnType = returnType;
this.parameterTypes = parameterTypes;
this.isStatic = Modifier.isStatic(invoker.getModifiers());
validateMethodSignature();
}
@@ -78,11 +75,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) {
@@ -17,7 +17,7 @@
package org.leavesmc.leaves.protocol.rei;
import com.mojang.logging.LogUtils;
import fun.bm.lophine.LophineLogger;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
@@ -26,7 +26,6 @@ import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.protocol.common.custom.DiscardedPayload;
import net.minecraft.resources.Identifier;
import net.minecraft.server.level.ServerPlayer;
import org.slf4j.Logger;
import java.nio.charset.StandardCharsets;
import java.util.*;
@@ -34,8 +33,6 @@ import java.util.function.BiConsumer;
public class PacketTransformer {
private static final Logger LOGGER = LogUtils.getLogger();
private static final byte START = 0x0;
private static final byte PART = 0x1;
private static final byte END = 0x2;
@@ -58,17 +55,17 @@ public class PacketTransformer {
int partsNum = buf.readInt();
data = new PartData(id, partsNum);
if (cache.put(key, data) != null) {
LOGGER.warn("Received invalid START packet for SplitPacketTransformer with packet id " + id);
LophineLogger.LOGGER.warn("Received invalid START packet for SplitPacketTransformer with packet id {}", id);
}
buf.retain();
data.parts.add(buf);
}
case PART -> {
if ((data = cache.get(key)) == null) {
LOGGER.warn("Received invalid PART packet for SplitPacketTransformer with packet id " + id);
LophineLogger.LOGGER.warn("Received invalid PART packet for SplitPacketTransformer with packet id {}", id);
buf.release();
} else if (!data.id.equals(id)) {
LOGGER.warn("Received invalid PART packet for SplitPacketTransformer with packet id " + id + ", id in cache is {}" + data.id);
LophineLogger.LOGGER.warn("Received invalid PART packet for SplitPacketTransformer with packet id {}, id in cache is {}", id, data.id);
buf.release();
for (RegistryFriendlyByteBuf part : data.parts) {
if (part != buf) {
@@ -83,10 +80,10 @@ public class PacketTransformer {
}
case END -> {
if ((data = cache.get(key)) == null) {
LOGGER.warn("Received invalid END packet for SplitPacketTransformer with packet id {}" + id);
LophineLogger.LOGGER.warn("Received invalid END packet for SplitPacketTransformer with packet id {}", id);
buf.release();
} else if (!data.id.equals(id)) {
LOGGER.warn("Received invalid END packet for SplitPacketTransformer with packet id " + id + ", id in cache is {}" + data.id);
LophineLogger.LOGGER.warn("Received invalid END packet for SplitPacketTransformer with packet id {}, id in cache is {}", id, data.id);
buf.release();
for (RegistryFriendlyByteBuf part : data.parts) {
if (part != buf) {
@@ -102,7 +99,7 @@ public class PacketTransformer {
return;
}
if (data.parts.size() != data.partsNum) {
LOGGER.warn("Received invalid END packet for SplitPacketTransformer with packet id " + id + " with size " + data.parts + ", parts expected is {}" + data.partsNum);
LophineLogger.LOGGER.warn("Received invalid END packet for SplitPacketTransformer with packet id {} with size {}, parts expected is {}", id, data.parts, data.partsNum);
for (RegistryFriendlyByteBuf part : data.parts) {
if (part != buf) {
part.release();
@@ -338,17 +338,17 @@ public class REIServerProtocol implements LeavesProtocol {
player.sendSystemMessage(Component.translatable(e.getMessage()).withStyle(ChatFormatting.RED));
} catch (Exception e) {
player.sendSystemMessage(Component.translatable("error.rei.internal.error", e.getMessage()).withStyle(ChatFormatting.RED));
LOGGER.warn("Failed to move items for player " + player.getScoreboardName(), e);
LOGGER.error("Failed to move items for player {}", player.getScoreboardName(), e);
}
});
} catch (IllegalStateException e) {
player.sendSystemMessage(Component.translatable(e.getMessage()).withStyle(ChatFormatting.RED));
} catch (Exception e) {
player.sendSystemMessage(Component.translatable("error.rei.internal.error", e.getMessage()).withStyle(ChatFormatting.RED));
LOGGER.warn("Failed to move items for player " + player.getScoreboardName(), e);
LOGGER.error("Failed to move items for player {}", player.getScoreboardName(), e);
}
} catch (Exception e) {
LOGGER.warn("Failed to move items for player " + player.getScoreboardName(), e);
LOGGER.error("Failed to move items for player {}", player.getScoreboardName(), e);
}
};
inboundTransform(player, MOVE_ITEMS_NEW_PACKET, buf, consumer);
@@ -17,7 +17,7 @@
package org.leavesmc.leaves.protocol.rei.payload;
import com.mojang.logging.LogUtils;
import fun.bm.lophine.LophineLogger;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
@@ -28,7 +28,6 @@ import net.minecraft.util.ByIdMap;
import org.jetbrains.annotations.NotNull;
import org.leavesmc.leaves.protocol.core.LeavesCustomPayload;
import org.leavesmc.leaves.protocol.rei.display.Display;
import org.slf4j.Logger;
import java.util.ArrayList;
import java.util.Collection;
@@ -43,8 +42,6 @@ public record DisplaySyncPayload(
long version
) implements LeavesCustomPayload {
private static final Logger LOGGER = LogUtils.getLogger();
public static final StreamCodec<? super RegistryFriendlyByteBuf, DisplaySyncPayload> STREAM_CODEC = StreamCodec.composite(
SyncType.STREAM_CODEC,
DisplaySyncPayload::syncType,
@@ -57,7 +54,7 @@ public record DisplaySyncPayload(
} catch (Exception e) {
tmpBuf.release();
buf.writeBoolean(false);
LOGGER.warn("Failed to encode display: " + display, e);
LophineLogger.LOGGER.warn("Failed to encode display: {}", display, e);
return;
}
buf.writeBoolean(true);
@@ -53,7 +53,7 @@ public class ServuxHudDataProtocol implements LeavesProtocol {
public static final int PROTOCOL_VERSION = 2;
private static final List<ServerPlayer> players = Collections.synchronizedList(new ArrayList<>());
private static final List<ServerPlayer> players = new ArrayList<>();
private static final int updateInterval = 80;
private static final Map<ServerPlayer, List<DataLogger.Type>> loggerPlayers = new ConcurrentHashMap<>();
@@ -117,7 +117,7 @@ public class ServuxHudDataProtocol implements LeavesProtocol {
sendPacket(player, new HudDataPayload(HudDataPayloadType.PACKET_S2C_METADATA, metadata));
}
public static void refreshSpawnMetadata(ServerPlayer player) {
public static void refreshSpawnMetadata(ServerPlayer player) { // TODO: 1.21.9 removed spawn chunk, should we keep this?
CompoundTag metadata = new CompoundTag();
metadata.putString("id", HudDataPayload.CHANNEL.toString());
metadata.putString("servux", ServuxProtocol.SERVUX_STRING);
@@ -180,7 +180,7 @@ public class ServuxHudDataProtocol implements LeavesProtocol {
private static void putWorldData(@NotNull CompoundTag metadata) {
ServerLevel level = MinecraftServer.getServer().overworld();
BlockPos spawnPos = level.serverLevelData.getRespawnData().pos();
BlockPos spawnPos = level.levelData.getRespawnData().pos();
metadata.putInt("spawnPosX", spawnPos.getX());
metadata.putInt("spawnPosY", spawnPos.getY());
metadata.putInt("spawnPosZ", spawnPos.getZ());
@@ -118,7 +118,6 @@ public class ServuxLitematicsProtocol implements LeavesProtocol {
case PACKET_C2S_ENTITY_REQUEST -> onEntityRequest(player, payload.getEntityId());
case PACKET_C2S_BULK_ENTITY_NBT_REQUEST ->
onBulkEntityRequest(player, payload.getChunkPos(), payload.getCompound());