Skip to content

Commit

Permalink
Update Bytebuf API
Browse files Browse the repository at this point in the history
  • Loading branch information
Lumine1909 committed Nov 28, 2024
1 parent ae6d5d6 commit b5fa4d7
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 27 deletions.
19 changes: 15 additions & 4 deletions patches/api/0010-Bytebuf-API.patch
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,18 @@ index 95f0b3186e313c7fbd5c8531d52b82a69e525f94..ec2a5f77c135706a544a870808241ea1
Spigot spigot();
diff --git a/src/main/java/org/leavesmc/leaves/bytebuf/Bytebuf.java b/src/main/java/org/leavesmc/leaves/bytebuf/Bytebuf.java
new file mode 100644
index 0000000000000000000000000000000000000000..657eee1d4a18f765480135ef50f5ef65fdc3ed28
index 0000000000000000000000000000000000000000..7038b2a5090154fe8d75ba9c9413952d834bb609
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/bytebuf/Bytebuf.java
@@ -0,0 +1,102 @@
@@ -0,0 +1,107 @@
+package org.leavesmc.leaves.bytebuf;
+
+import com.google.gson.JsonElement;
+import org.bukkit.Bukkit;
+import org.bukkit.inventory.ItemStack;
+
+import java.util.UUID;
+import java.util.List;
+
+public interface Bytebuf {
+
Expand Down Expand Up @@ -155,6 +156,10 @@ index 0000000000000000000000000000000000000000..657eee1d4a18f765480135ef50f5ef65
+
+ ItemStack readItemStack();
+
+ Bytebuf writeItemStackList(List<ItemStack> itemStacks);
+
+ List<ItemStack> readItemStackList();
+
+ Bytebuf copy();
+
+ boolean release();
Expand Down Expand Up @@ -210,10 +215,10 @@ index 0000000000000000000000000000000000000000..e246c0a8725fa3b2be065433652a19a5
+}
diff --git a/src/main/java/org/leavesmc/leaves/bytebuf/packet/PacketType.java b/src/main/java/org/leavesmc/leaves/bytebuf/packet/PacketType.java
new file mode 100644
index 0000000000000000000000000000000000000000..4f8ea5c78df6429358dbbc75042521b657d50d3c
index 0000000000000000000000000000000000000000..ceebd752f4c3e50943b22902e01eaf9648941785
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/bytebuf/packet/PacketType.java
@@ -0,0 +1,185 @@
@@ -0,0 +1,191 @@
+package org.leavesmc.leaves.bytebuf.packet;
+
+public enum PacketType {
Expand Down Expand Up @@ -382,6 +387,12 @@ index 0000000000000000000000000000000000000000..4f8ea5c78df6429358dbbc75042521b6
+ ClientboundTickingStep,
+ ClientboundCustomPayload,
+ ClientboundCustomDetails,
+ ClientboundRecipeBookAdd,
+ ServerboundClientTickEnd,
+ ClientboundSetHeldSlot,
+ ServerboundSelectBundleItem,
+ ClientboundSetPlayerInventory,
+ ClientboundSetCursorItem,
+ ClientboundDisconnect,
+ ClientboundKeepAlive,
+ ClientboundPing,
Expand Down
73 changes: 50 additions & 23 deletions patches/server/0115-Bytebuf-API.patch
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ index 0000000000000000000000000000000000000000..443f7f6e0b8d40eaafb8009b3b7e405c
+}
diff --git a/src/main/java/org/leavesmc/leaves/bytebuf/WrappedBytebuf.java b/src/main/java/org/leavesmc/leaves/bytebuf/WrappedBytebuf.java
new file mode 100644
index 0000000000000000000000000000000000000000..707d784cf17926e869481c39b0da908eab501708
index 0000000000000000000000000000000000000000..9601eb5aa8cbcf1836cba7853af9ece2d93ec9c3
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/bytebuf/WrappedBytebuf.java
@@ -0,0 +1,260 @@
@@ -0,0 +1,277 @@
+package org.leavesmc.leaves.bytebuf;
+
+import com.google.gson.JsonElement;
Expand All @@ -143,6 +143,7 @@ index 0000000000000000000000000000000000000000..707d784cf17926e869481c39b0da908e
+import org.bukkit.inventory.ItemStack;
+import org.bukkit.craftbukkit.inventory.CraftItemStack;
+
+import java.util.List;
+import java.util.UUID;
+
+public class WrappedBytebuf implements Bytebuf {
Expand All @@ -166,7 +167,10 @@ index 0000000000000000000000000000000000000000..707d784cf17926e869481c39b0da908e
+
+ @Override
+ public byte[] toArray() {
+ return buf.array();
+ int length = buf.readableBytes();
+ byte[] data = new byte[length];
+ buf.getBytes(buf.readerIndex(), data);
+ return data;
+ }
+
+ @Override
Expand Down Expand Up @@ -381,6 +385,19 @@ index 0000000000000000000000000000000000000000..707d784cf17926e869481c39b0da908e
+ }
+
+ @Override
+ public Bytebuf writeItemStackList(List<ItemStack> itemStacks) {
+ List<net.minecraft.world.item.ItemStack> nmsItemList = itemStacks.stream().map(CraftItemStack::unwrap).toList();
+ net.minecraft.world.item.ItemStack.OPTIONAL_LIST_STREAM_CODEC.encode(this.registryBuf, nmsItemList);
+ return this;
+ }
+
+ @Override
+ public List<ItemStack> readItemStackList() {
+ List<net.minecraft.world.item.ItemStack> nmsItemList = net.minecraft.world.item.ItemStack.OPTIONAL_LIST_STREAM_CODEC.decode(this.registryBuf);
+ return nmsItemList.stream().map(net.minecraft.world.item.ItemStack::asBukkitMirror).toList();
+ }
+
+ @Override
+ public Bytebuf copy() {
+ return new WrappedBytebuf(this.buf.copy());
+ }
Expand All @@ -392,10 +409,10 @@ index 0000000000000000000000000000000000000000..707d784cf17926e869481c39b0da908e
+}
diff --git a/src/main/java/org/leavesmc/leaves/bytebuf/internal/InternalBytebufHandler.java b/src/main/java/org/leavesmc/leaves/bytebuf/internal/InternalBytebufHandler.java
new file mode 100644
index 0000000000000000000000000000000000000000..fb7625ce28d2d8c79aad680ff34d704ca8f9b426
index 0000000000000000000000000000000000000000..dccb9b0bdd1fea20f1b073ccaf8e911140b5e333
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/bytebuf/internal/InternalBytebufHandler.java
@@ -0,0 +1,225 @@
@@ -0,0 +1,235 @@
+package org.leavesmc.leaves.bytebuf.internal;
+
+import com.google.common.cache.Cache;
Expand Down Expand Up @@ -452,13 +469,10 @@ index 0000000000000000000000000000000000000000..fb7625ce28d2d8c79aad680ff34d704c
+ }
+
+ if (msg instanceof net.minecraft.network.protocol.Packet<?> nmsPacket) {
+ PacketType type = toEnumType(nmsPacket.type());
+ if (type != null) {
+ try {
+ msg = callPacketInEvent(player, createBytebufPacket(type, nmsPacket));
+ } catch (Exception e) {
+ MinecraftServer.LOGGER.error("Error on PacketInEvent.", e);
+ }
+ try {
+ msg = callPacketInEvent(player, nmsPacket);
+ } catch (Exception e) {
+ MinecraftServer.LOGGER.error("Error on PacketInEvent.", e);
+ }
+ }
+
Expand All @@ -475,13 +489,10 @@ index 0000000000000000000000000000000000000000..fb7625ce28d2d8c79aad680ff34d704c
+ }
+
+ if (msg instanceof net.minecraft.network.protocol.Packet<?> nmsPacket) {
+ PacketType type = toEnumType(nmsPacket.type());
+ if (type != null) {
+ try {
+ msg = callPacketOutEvent(player, createBytebufPacket(type, nmsPacket));
+ } catch (Exception e) {
+ MinecraftServer.LOGGER.error("Error on PacketInEvent.", e);
+ }
+ try {
+ msg = callPacketOutEvent(player, nmsPacket);
+ } catch (Exception e) {
+ MinecraftServer.LOGGER.error("Error on PacketInEvent.", e);
+ }
+ }
+
Expand Down Expand Up @@ -533,7 +544,7 @@ index 0000000000000000000000000000000000000000..fb7625ce28d2d8c79aad680ff34d704c
+ }
+
+ public void injectPlayer(ServerPlayer player) {
+ if (LeavesConfig.leavesPacketEvent) {
+ if (LeavesConfig.mics.leavesPacketEvent) {
+ player.connection.connection.channel.pipeline().addBefore("packet_handler", PacketHandler.handlerName, new PacketHandler(player.getBukkitEntity()));
+ }
+ }
Expand All @@ -542,7 +553,15 @@ index 0000000000000000000000000000000000000000..fb7625ce28d2d8c79aad680ff34d704c
+ return manager;
+ }
+
+ public net.minecraft.network.protocol.Packet<?> callPacketInEvent(Player player, Packet packet) {
+ public net.minecraft.network.protocol.Packet<?> callPacketInEvent(Player player, net.minecraft.network.protocol.Packet<?> nmsPacket) {
+ if (listenerMap.isEmpty()) {
+ return nmsPacket;
+ }
+ PacketType type = toEnumType(nmsPacket.type());
+ if (type == null) {
+ return nmsPacket;
+ }
+ Packet packet = createBytebufPacket(type, nmsPacket);
+ for (PacketListener listener : listenerMap.keySet()) {
+ if (listenerMap.get(listener).isEnabled()) {
+ packet = listener.onPacketIn(player, packet);
Expand All @@ -554,7 +573,15 @@ index 0000000000000000000000000000000000000000..fb7625ce28d2d8c79aad680ff34d704c
+ return createNMSPacket(packet);
+ }
+
+ public net.minecraft.network.protocol.Packet<?> callPacketOutEvent(Player player, Packet packet) {
+ public net.minecraft.network.protocol.Packet<?> callPacketOutEvent(Player player, net.minecraft.network.protocol.Packet<?> nmsPacket) {
+ if (listenerMap.isEmpty()) {
+ return nmsPacket;
+ }
+ PacketType type = toEnumType(nmsPacket.type());
+ if (type == null) {
+ return nmsPacket;
+ }
+ Packet packet = createBytebufPacket(type, nmsPacket);
+ for (PacketListener listener : listenerMap.keySet()) {
+ if (listenerMap.get(listener).isEnabled()) {
+ packet = listener.onPacketOut(player, packet);
Expand Down Expand Up @@ -612,7 +639,7 @@ index 0000000000000000000000000000000000000000..fb7625ce28d2d8c79aad680ff34d704c
+ }
+
+ public Packet createBytebufPacket(PacketType type, net.minecraft.network.protocol.Packet<?> nmsPacket) {
+ RegistryFriendlyByteBuf buf = new RegistryFriendlyByteBuf(Unpooled.buffer(8192), MinecraftServer.getServer().registryAccess());
+ RegistryFriendlyByteBuf buf = new RegistryFriendlyByteBuf(Unpooled.buffer(), MinecraftServer.getServer().registryAccess());
+ StreamCodec<FriendlyByteBuf, net.minecraft.network.protocol.Packet<?>> codec = type2CodecMap.get(type);
+ if (codec == null) {
+ throw new UnsupportedOperationException("This feature is not completely finished yet, packet type " + type + " is not supported temporary.");
Expand Down

0 comments on commit b5fa4d7

Please sign in to comment.