From 49bf2ca1ddcbe35a25c29b9fb4d66653dbc85e94 Mon Sep 17 00:00:00 2001 From: Lumine1909 <133463833+Lumine1909@users.noreply.github.com> Date: Wed, 27 Nov 2024 15:39:27 -0500 Subject: [PATCH] Jade protocol v7 --- patches/server/0033-Jade-Protocol.patch | 106 ++++++++++++++++++------ 1 file changed, 81 insertions(+), 25 deletions(-) diff --git a/patches/server/0033-Jade-Protocol.patch b/patches/server/0033-Jade-Protocol.patch index 236c015..2920284 100644 --- a/patches/server/0033-Jade-Protocol.patch +++ b/patches/server/0033-Jade-Protocol.patch @@ -124,13 +124,15 @@ index 30d0133a42ce990352f5c492fcf9beb105364848..1ab2eab686b3a89d406f127a6036c0e2 protected CompositeLootItemCondition(List terms, Predicate predicate) { diff --git a/src/main/java/org/leavesmc/leaves/protocol/jade/JadeProtocol.java b/src/main/java/org/leavesmc/leaves/protocol/jade/JadeProtocol.java new file mode 100644 -index 0000000000000000000000000000000000000000..c72d6eeb61cda12e49939deebb9791d5c7f36f0a +index 0000000000000000000000000000000000000000..334dd36a9b98a4e74c99e09886ca8109256d311d --- /dev/null +++ b/src/main/java/org/leavesmc/leaves/protocol/jade/JadeProtocol.java -@@ -0,0 +1,312 @@ +@@ -0,0 +1,333 @@ +package org.leavesmc.leaves.protocol.jade; + +import com.google.common.base.Suppliers; ++import net.kyori.adventure.text.Component; ++import net.kyori.adventure.text.format.NamedTextColor; +import net.minecraft.core.BlockPos; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.core.registries.Registries; @@ -181,10 +183,11 @@ index 0000000000000000000000000000000000000000..c72d6eeb61cda12e49939deebb9791d5 +import org.leavesmc.leaves.protocol.jade.accessor.BlockAccessorImpl; +import org.leavesmc.leaves.protocol.jade.accessor.EntityAccessor; +import org.leavesmc.leaves.protocol.jade.accessor.EntityAccessorImpl; ++import org.leavesmc.leaves.protocol.jade.payload.ClientHandshakePayload; +import org.leavesmc.leaves.protocol.jade.payload.ReceiveDataPayload; +import org.leavesmc.leaves.protocol.jade.payload.RequestBlockPayload; +import org.leavesmc.leaves.protocol.jade.payload.RequestEntityPayload; -+import org.leavesmc.leaves.protocol.jade.payload.ServerPingPayload; ++import org.leavesmc.leaves.protocol.jade.payload.ServerHandshakePayload; +import org.leavesmc.leaves.protocol.jade.provider.IJadeProvider; +import org.leavesmc.leaves.protocol.jade.provider.IServerDataProvider; +import org.leavesmc.leaves.protocol.jade.provider.IServerExtensionProvider; @@ -214,16 +217,22 @@ index 0000000000000000000000000000000000000000..c72d6eeb61cda12e49939deebb9791d5 +import org.leavesmc.leaves.protocol.jade.util.PriorityStore; +import org.leavesmc.leaves.protocol.jade.util.WrappedHierarchyLookup; + ++import java.util.ArrayList; +import java.util.Collections; ++import java.util.Iterator; +import java.util.List; ++import java.util.UUID; + +@LeavesProtocol(namespace = "jade") +public class JadeProtocol { + ++ private static List enabledPlayers = new ArrayList<>(); ++ + public static PriorityStore priorities; + private static List shearableBlocks = null; + + public static final String PROTOCOL_ID = "jade"; ++ public static final String PROTOCOL_VERSION = "7"; + + public static final HierarchyLookup> entityDataProviders = new HierarchyLookup<>(Entity.class); + public static final PairHierarchyLookup> blockDataProviders = new PairHierarchyLookup<>(new HierarchyLookup<>(Block.class), new HierarchyLookup<>(BlockEntity.class)); @@ -299,18 +308,24 @@ index 0000000000000000000000000000000000000000..c72d6eeb61cda12e49939deebb9791d5 + } + } + -+ @ProtocolHandler.PlayerJoin -+ public static void onPlayerJoin(ServerPlayer player) { -+ if (!LeavesConfig.jadeProtocol) { ++ @ProtocolHandler.PlayerLeave ++ public static void playerLeave(ServerPlayer player) { ++ enabledPlayers.remove(player.getUUID()); ++ } ++ ++ @ProtocolHandler.PayloadReceiver(payload = ClientHandshakePayload.class, payloadId = "client_handshake") ++ public static void clientHandshake(ServerPlayer player, ClientHandshakePayload payload) { ++ if (!payload.protocolVersion().equals(PROTOCOL_VERSION)) { ++ player.getBukkitEntity().sendMessage(Component.text("You are using a different version of Jade than the server. Please update your Jade or report it to the server operator").color(NamedTextColor.RED)); + return; + } -+ ++ enabledPlayers.add(player.getUUID()); + sendPingPacket(player); + } + + @ProtocolHandler.PayloadReceiver(payload = RequestEntityPayload.class, payloadId = "request_entity") + public static void requestEntityData(ServerPlayer player, RequestEntityPayload payload) { -+ if (!LeavesConfig.jadeProtocol) { ++ if (!LeavesConfig.protocol.jadeProtocol) { + return; + } + @@ -362,7 +377,7 @@ index 0000000000000000000000000000000000000000..c72d6eeb61cda12e49939deebb9791d5 + + @ProtocolHandler.PayloadReceiver(payload = RequestBlockPayload.class, payloadId = "request_block") + public static void requestBlockData(ServerPlayer player, RequestBlockPayload payload) { -+ if (!LeavesConfig.jadeProtocol) { ++ if (!LeavesConfig.protocol.jadeProtocol) { + return; + } + @@ -425,19 +440,25 @@ index 0000000000000000000000000000000000000000..c72d6eeb61cda12e49939deebb9791d5 + + @ProtocolHandler.ReloadServer + public static void onServerReload() { -+ if (LeavesConfig.jadeProtocol) { ++ if (LeavesConfig.protocol.jadeProtocol) { + enableAllPlayer(); + } + } + + public static void enableAllPlayer() { -+ for (ServerPlayer player : MinecraftServer.getServer().getPlayerList().players) { -+ sendPingPacket(player); ++ for (Iterator itr = enabledPlayers.iterator(); itr.hasNext(); ) { ++ UUID uuid = itr.next(); ++ ServerPlayer player = MinecraftServer.getServer().getPlayerList().getPlayer(uuid); ++ if (player == null) { ++ itr.remove(); ++ } else { ++ sendPingPacket(player); ++ } + } + } + + public static void sendPingPacket(ServerPlayer player) { -+ ProtocolUtils.sendPayloadPacket(player, new ServerPingPayload(Collections.emptyMap(), shearableBlocks, blockDataProviders.mappedIds(), entityDataProviders.mappedIds())); ++ ProtocolUtils.sendPayloadPacket(player, new ServerHandshakePayload(Collections.emptyMap(), shearableBlocks, blockDataProviders.mappedIds(), entityDataProviders.mappedIds())); + } +} \ No newline at end of file @@ -974,6 +995,41 @@ index 0000000000000000000000000000000000000000..65d16c0024372ede4cec230b7aad54de + } +} \ No newline at end of file +diff --git a/src/main/java/org/leavesmc/leaves/protocol/jade/payload/ClientHandshakePayload.java b/src/main/java/org/leavesmc/leaves/protocol/jade/payload/ClientHandshakePayload.java +new file mode 100644 +index 0000000000000000000000000000000000000000..5e1c17907af0f967ed7109bbbf9999bb7ddfe64b +--- /dev/null ++++ b/src/main/java/org/leavesmc/leaves/protocol/jade/payload/ClientHandshakePayload.java +@@ -0,0 +1,29 @@ ++package org.leavesmc.leaves.protocol.jade.payload; ++ ++import net.minecraft.network.FriendlyByteBuf; ++import net.minecraft.network.RegistryFriendlyByteBuf; ++import net.minecraft.network.codec.ByteBufCodecs; ++import net.minecraft.network.codec.StreamCodec; ++import net.minecraft.resources.ResourceLocation; ++import net.minecraft.server.MinecraftServer; ++import org.leavesmc.leaves.protocol.core.LeavesCustomPayload; ++import org.leavesmc.leaves.protocol.jade.JadeProtocol; ++ ++public record ClientHandshakePayload(String protocolVersion) implements LeavesCustomPayload { ++ ++ private static final StreamCodec CODEC = StreamCodec.composite( ++ ByteBufCodecs.STRING_UTF8, ++ ClientHandshakePayload::protocolVersion, ++ ClientHandshakePayload::new); ++ private static final ResourceLocation PACKET_CLIENT_HANDSHAKE = JadeProtocol.id("client_handshake"); ++ ++ @Override ++ public void write(FriendlyByteBuf buf) { ++ CODEC.encode(new RegistryFriendlyByteBuf(buf, MinecraftServer.getServer().registryAccess()), this); ++ } ++ ++ @Override ++ public ResourceLocation id() { ++ return PACKET_CLIENT_HANDSHAKE; ++ } ++} diff --git a/src/main/java/org/leavesmc/leaves/protocol/jade/payload/ReceiveDataPayload.java b/src/main/java/org/leavesmc/leaves/protocol/jade/payload/ReceiveDataPayload.java new file mode 100644 index 0000000000000000000000000000000000000000..1b474ea8c1075b3dbaa7cd27e5bd95aa904fbe97 @@ -1126,11 +1182,11 @@ index 0000000000000000000000000000000000000000..ef7ee32f0f2fd78b7e7891d622f76cdd + } +} \ No newline at end of file -diff --git a/src/main/java/org/leavesmc/leaves/protocol/jade/payload/ServerPingPayload.java b/src/main/java/org/leavesmc/leaves/protocol/jade/payload/ServerPingPayload.java +diff --git a/src/main/java/org/leavesmc/leaves/protocol/jade/payload/ServerHandshakePayload.java b/src/main/java/org/leavesmc/leaves/protocol/jade/payload/ServerHandshakePayload.java new file mode 100644 -index 0000000000000000000000000000000000000000..f4419962a7fedaea05140bbf6eaa01cc94c05049 +index 0000000000000000000000000000000000000000..444629ffcb3d174078da0fef8ad4e9bbb554e4a8 --- /dev/null -+++ b/src/main/java/org/leavesmc/leaves/protocol/jade/payload/ServerPingPayload.java ++++ b/src/main/java/org/leavesmc/leaves/protocol/jade/payload/ServerHandshakePayload.java @@ -0,0 +1,49 @@ +package org.leavesmc.leaves.protocol.jade.payload; + @@ -1152,23 +1208,23 @@ index 0000000000000000000000000000000000000000..f4419962a7fedaea05140bbf6eaa01cc + +import static org.leavesmc.leaves.protocol.jade.util.JadeCodec.PRIMITIVE_STREAM_CODEC; + -+public record ServerPingPayload( ++public record ServerHandshakePayload( + Map serverConfig, + List shearableBlocks, + List blockProviderIds, -+ List entityProviderIds) implements LeavesCustomPayload { ++ List entityProviderIds) implements LeavesCustomPayload { + -+ private static final ResourceLocation PACKET_SERVER_HANDSHAKE = JadeProtocol.id("server_ping_v1"); -+ private static final StreamCodec CODEC = StreamCodec.composite( ++ private static final ResourceLocation PACKET_SERVER_HANDSHAKE = JadeProtocol.id("server_handshake"); ++ private static final StreamCodec CODEC = StreamCodec.composite( + ByteBufCodecs.map(Maps::newHashMapWithExpectedSize, ResourceLocation.STREAM_CODEC, PRIMITIVE_STREAM_CODEC), -+ ServerPingPayload::serverConfig, ++ ServerHandshakePayload::serverConfig, + ByteBufCodecs.registry(Registries.BLOCK).apply(ByteBufCodecs.list()), -+ ServerPingPayload::shearableBlocks, ++ ServerHandshakePayload::shearableBlocks, + ByteBufCodecs.list().apply(ResourceLocation.STREAM_CODEC), -+ ServerPingPayload::blockProviderIds, ++ ServerHandshakePayload::blockProviderIds, + ByteBufCodecs.list().apply(ResourceLocation.STREAM_CODEC), -+ ServerPingPayload::entityProviderIds, -+ ServerPingPayload::new); ++ ServerHandshakePayload::entityProviderIds, ++ ServerHandshakePayload::new); + + @Override + public void write(FriendlyByteBuf buf) {