Skip to content

Commit

Permalink
Update Servux Protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
s-yh-china committed Jul 22, 2024
1 parent d2a4562 commit bc4cd4f
Show file tree
Hide file tree
Showing 2 changed files with 429 additions and 8 deletions.
61 changes: 56 additions & 5 deletions patches/server/0007-Leaves-Protocol-Core.patch
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,13 @@ index 0000000000000000000000000000000000000000..986d2a6641ff8017dddf3e5f2655adfc
+}
diff --git a/src/main/java/org/leavesmc/leaves/protocol/core/LeavesProtocolManager.java b/src/main/java/org/leavesmc/leaves/protocol/core/LeavesProtocolManager.java
new file mode 100644
index 0000000000000000000000000000000000000000..4fd8e06c8d2d3016ccc0baf1c43d77d87ea1dca5
index 0000000000000000000000000000000000000000..e5eb67c0bbdf4953ed0ccc3281f06eda26a7956e
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/protocol/core/LeavesProtocolManager.java
@@ -0,0 +1,386 @@
@@ -0,0 +1,435 @@
+package org.leavesmc.leaves.protocol.core;
+
+import com.google.common.collect.ImmutableSet;
+import net.minecraft.network.FriendlyByteBuf;
+import net.minecraft.network.chat.Component;
+import net.minecraft.resources.ResourceLocation;
Expand All @@ -211,6 +212,7 @@ index 0000000000000000000000000000000000000000..4fd8e06c8d2d3016ccc0baf1c43d77d8
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Map;
Expand All @@ -226,6 +228,7 @@ index 0000000000000000000000000000000000000000..4fd8e06c8d2d3016ccc0baf1c43d77d8
+
+ private static final Map<LeavesProtocol, Map<ProtocolHandler.PayloadReceiver, Executable>> KNOWN_TYPES = new HashMap<>();
+ private static final Map<LeavesProtocol, Map<ProtocolHandler.PayloadReceiver, Method>> KNOW_RECEIVERS = new HashMap<>();
+ private static Set<ResourceLocation> ALL_KNOWN_ID = new HashSet<>();
+
+ private static final List<Method> TICKERS = new ArrayList<>();
+ private static final List<Method> PLAYER_JOIN = new ArrayList<>();
Expand Down Expand Up @@ -340,6 +343,20 @@ index 0000000000000000000000000000000000000000..4fd8e06c8d2d3016ccc0baf1c43d77d8
+ KNOWN_TYPES.put(protocol, map);
+ }
+ }
+
+ for (LeavesProtocol protocol : KNOWN_TYPES.keySet()) {
+ Map<ProtocolHandler.PayloadReceiver, Executable> map = KNOWN_TYPES.get(protocol);
+ for (ProtocolHandler.PayloadReceiver receiver : map.keySet()) {
+ if (receiver.sendFabricRegister() && !receiver.ignoreId()) {
+ for (String payloadId : receiver.payloadId()) {
+ for (String namespace : protocol.namespace()) {
+ ALL_KNOWN_ID.add(new ResourceLocation(namespace, payloadId));
+ }
+ }
+ }
+ }
+ }
+ ALL_KNOWN_ID = ImmutableSet.copyOf(ALL_KNOWN_ID);
+ }
+
+ public static LeavesCustomPayload<?> decode(ResourceLocation id, FriendlyByteBuf buf) {
Expand Down Expand Up @@ -416,6 +433,8 @@ index 0000000000000000000000000000000000000000..4fd8e06c8d2d3016ccc0baf1c43d77d8
+ LOGGER.warning("Failed to handle player join, " + exception.getCause() + ": " + exception.getMessage());
+ }
+ }
+
+ ProtocolUtils.sendPayloadPacket(player, new FabricRegisterPayload(ALL_KNOWN_ID));
+ }
+
+ public static void handlePlayerLeave(ServerPlayer player) {
Expand Down Expand Up @@ -452,7 +471,7 @@ index 0000000000000000000000000000000000000000..4fd8e06c8d2d3016ccc0baf1c43d77d8
+ Map<ProtocolHandler.MinecraftRegister, Method> map = MINECRAFT_REGISTER.get(protocol);
+ for (ProtocolHandler.MinecraftRegister register : map.keySet()) {
+ if (register.ignoreId() || register.channelId().equals(channel[1]) ||
+ ArrayUtils.contains(register.channelIds(), channel[1])) {
+ ArrayUtils.contains(register.channelIds(), channel[1])) {
+ try {
+ map.get(register).invoke(null, player);
+ } catch (InvocationTargetException | IllegalAccessException exception) {
Expand Down Expand Up @@ -568,13 +587,43 @@ index 0000000000000000000000000000000000000000..4fd8e06c8d2d3016ccc0baf1c43d77d8
+ buf.writeBytes(data);
+ }
+ }
+
+ public record FabricRegisterPayload(Set<ResourceLocation> channels) implements LeavesCustomPayload<FabricRegisterPayload> {
+
+ public static final ResourceLocation CHANNEL = ResourceLocation.withDefaultNamespace("register");
+
+ @New
+ public FabricRegisterPayload(ResourceLocation location, FriendlyByteBuf buf) {
+ this(buf.readCollection(HashSet::new, FriendlyByteBuf::readResourceLocation));
+ }
+
+ @Override
+ public void write(FriendlyByteBuf buf) {
+ boolean first = true;
+
+ ResourceLocation channel;
+ for (Iterator<ResourceLocation> var3 = this.channels.iterator(); var3.hasNext(); buf.writeBytes(channel.toString().getBytes(StandardCharsets.US_ASCII))) {
+ channel = var3.next();
+ if (first) {
+ first = false;
+ } else {
+ buf.writeByte(0);
+ }
+ }
+ }
+
+ @Override
+ public ResourceLocation id() {
+ return CHANNEL;
+ }
+ }
+}
diff --git a/src/main/java/org/leavesmc/leaves/protocol/core/ProtocolHandler.java b/src/main/java/org/leavesmc/leaves/protocol/core/ProtocolHandler.java
new file mode 100644
index 0000000000000000000000000000000000000000..f941f095184cf4b7af284d1357ea1a85815e8a66
index 0000000000000000000000000000000000000000..9d71f8e6af24301bedf60f5c87e0bb3c1697d5e3
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/protocol/core/ProtocolHandler.java
@@ -0,0 +1,61 @@
@@ -0,0 +1,63 @@
+package org.leavesmc.leaves.protocol.core;
+
+import java.lang.annotation.ElementType;
Expand All @@ -599,6 +648,8 @@ index 0000000000000000000000000000000000000000..f941f095184cf4b7af284d1357ea1a85
+ String[] payloadId() default "";
+
+ boolean ignoreId() default false;
+
+ boolean sendFabricRegister() default true;
+ }
+
+ @Target(ElementType.METHOD)
Expand Down
Loading

0 comments on commit bc4cd4f

Please sign in to comment.