Skip to content

Commit

Permalink
Jade protocol v7
Browse files Browse the repository at this point in the history
  • Loading branch information
Lumine1909 committed Nov 27, 2024
1 parent 09ee53c commit 49bf2ca
Showing 1 changed file with 81 additions and 25 deletions.
106 changes: 81 additions & 25 deletions patches/server/0033-Jade-Protocol.patch
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,15 @@ index 30d0133a42ce990352f5c492fcf9beb105364848..1ab2eab686b3a89d406f127a6036c0e2
protected CompositeLootItemCondition(List<LootItemCondition> terms, Predicate<LootContext> 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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<UUID> enabledPlayers = new ArrayList<>();
+
+ public static PriorityStore<ResourceLocation, IJadeProvider> priorities;
+ private static List<Block> shearableBlocks = null;
+
+ public static final String PROTOCOL_ID = "jade";
+ public static final String PROTOCOL_VERSION = "7";
+
+ public static final HierarchyLookup<IServerDataProvider<EntityAccessor>> entityDataProviders = new HierarchyLookup<>(Entity.class);
+ public static final PairHierarchyLookup<IServerDataProvider<BlockAccessor>> blockDataProviders = new PairHierarchyLookup<>(new HierarchyLookup<>(Block.class), new HierarchyLookup<>(BlockEntity.class));
Expand Down Expand Up @@ -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;
+ }
+
Expand Down Expand Up @@ -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;
+ }
+
Expand Down Expand Up @@ -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<UUID> 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
Expand Down Expand Up @@ -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<ClientHandshakePayload> {
+
+ private static final StreamCodec<RegistryFriendlyByteBuf, ClientHandshakePayload> 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
Expand Down Expand Up @@ -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;
+
Expand All @@ -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<ResourceLocation, Object> serverConfig,
+ List<Block> shearableBlocks,
+ List<ResourceLocation> blockProviderIds,
+ List<ResourceLocation> entityProviderIds) implements LeavesCustomPayload<ServerPingPayload> {
+ List<ResourceLocation> entityProviderIds) implements LeavesCustomPayload<ServerHandshakePayload> {
+
+ private static final ResourceLocation PACKET_SERVER_HANDSHAKE = JadeProtocol.id("server_ping_v1");
+ private static final StreamCodec<RegistryFriendlyByteBuf, ServerPingPayload> CODEC = StreamCodec.composite(
+ private static final ResourceLocation PACKET_SERVER_HANDSHAKE = JadeProtocol.id("server_handshake");
+ private static final StreamCodec<RegistryFriendlyByteBuf, ServerHandshakePayload> 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.<ByteBuf, ResourceLocation>list().apply(ResourceLocation.STREAM_CODEC),
+ ServerPingPayload::blockProviderIds,
+ ServerHandshakePayload::blockProviderIds,
+ ByteBufCodecs.<ByteBuf, ResourceLocation>list().apply(ResourceLocation.STREAM_CODEC),
+ ServerPingPayload::entityProviderIds,
+ ServerPingPayload::new);
+ ServerHandshakePayload::entityProviderIds,
+ ServerHandshakePayload::new);
+
+ @Override
+ public void write(FriendlyByteBuf buf) {
Expand Down

0 comments on commit 49bf2ca

Please sign in to comment.