Skip to content

Commit

Permalink
Fix and rewrite bytebuf api (#218)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: violetc <[email protected]>
  • Loading branch information
Lumine1909 and s-yh-china authored May 25, 2024
1 parent 6b62f76 commit 46ac3c6
Show file tree
Hide file tree
Showing 5 changed files with 989 additions and 553 deletions.
396 changes: 396 additions & 0 deletions patches/api/0010-Bytebuf-API.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,396 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Lumine1909 <[email protected]>
Date: Wed, 22 May 2024 10:12:04 +0800
Subject: [PATCH] Bytebuf API


diff --git a/src/main/java/org/bukkit/Bukkit.java b/src/main/java/org/bukkit/Bukkit.java
index 3c2b252243c1fa0d3adcc9c860c24af73165de33..1caffc8771680505be1c51a2f5050528435cdc33 100644
--- a/src/main/java/org/bukkit/Bukkit.java
+++ b/src/main/java/org/bukkit/Bukkit.java
@@ -2919,6 +2919,12 @@ public final class Bukkit {
}
// Leaves end - Photographer API

+ // Leaves start - Bytebuf API
+ public static org.leavesmc.leaves.bytebuf.BytebufManager getBytebufManager() {
+ return server.getBytebufManager();
+ }
+ // Leaves end - Bytebuf API
+
@NotNull
public static Server.Spigot spigot() {
return server.spigot();
diff --git a/src/main/java/org/bukkit/Server.java b/src/main/java/org/bukkit/Server.java
index 4a03044938630e4a0e701bc481f15da5a3323115..7a006d2582cd91b83ab05aeb6d6a6f96b77d2dbc 100644
--- a/src/main/java/org/bukkit/Server.java
+++ b/src/main/java/org/bukkit/Server.java
@@ -2564,4 +2564,8 @@ public interface Server extends PluginMessageRecipient, net.kyori.adventure.audi
// Leaves start - Photographer API
@NotNull PhotographerManager getPhotographerManager();
// Leaves end - Photographer API
+
+ // Leaves start - Bytebuf API
+ org.leavesmc.leaves.bytebuf.BytebufManager getBytebufManager();
+ // Leaves end - Bytebuf API
}
diff --git a/src/main/java/org/bukkit/entity/Player.java b/src/main/java/org/bukkit/entity/Player.java
index 6c327a07bf8a6aa11a2d7dad12b2830acc539484..e3e4bed888133422bd879479793a01eadbdef85e 100644
--- a/src/main/java/org/bukkit/entity/Player.java
+++ b/src/main/java/org/bukkit/entity/Player.java
@@ -3802,6 +3802,12 @@ public interface Player extends HumanEntity, Conversable, OfflinePlayer, PluginM
boolean isChunkSent(long chunkKey);
// Paper end

+ // Leaves start - Bytebuf API
+ void sendPacket(org.leavesmc.leaves.bytebuf.packet.Packet packet);
+
+ void sendPacket(org.leavesmc.leaves.bytebuf.Bytebuf buf, org.leavesmc.leaves.bytebuf.packet.PacketType type);
+ // Leaves end - Bytebuf API
+
@NotNull
@Override
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
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/bytebuf/Bytebuf.java
@@ -0,0 +1,102 @@
+package org.leavesmc.leaves.bytebuf;
+
+import com.google.gson.JsonElement;
+import org.bukkit.Bukkit;
+import org.bukkit.inventory.ItemStack;
+
+import java.util.UUID;
+
+public interface Bytebuf {
+
+ static Bytebuf buf(int size) {
+ return Bukkit.getBytebufManager().newBytebuf(size);
+ }
+
+ static Bytebuf buf() {
+ return buf(128);
+ }
+
+ static Bytebuf of(byte[] bytes) {
+ return Bukkit.getBytebufManager().toBytebuf(bytes);
+ }
+
+ byte[] toArray();
+
+ Bytebuf skipBytes(int i);
+
+ int readerIndex();
+
+ Bytebuf readerIndex(int i);
+
+ int writerIndex();
+
+ Bytebuf writerIndex(int i);
+
+ Bytebuf resetReaderIndex();
+
+ Bytebuf resetWriterIndex();
+
+ Bytebuf writeByte(int i);
+
+ byte readByte();
+
+ Bytebuf writeBoolean(boolean b);
+
+ boolean readBoolean();
+
+ Bytebuf writeFloat(float f);
+
+ float readFloat();
+
+ Bytebuf writeDouble(double d);
+
+ double readDouble();
+
+ Bytebuf writeShort(int i);
+
+ short readShort();
+
+ Bytebuf writeInt(int i);
+
+ int readInt();
+
+ Bytebuf writeLong(long i);
+
+ long readLong();
+
+ Bytebuf writeVarInt(int i);
+
+ int readVarInt();
+
+ Bytebuf writeVarLong(long i);
+
+ long readVarLong();
+
+ Bytebuf writeUUID(UUID uuid);
+
+ UUID readUUID();
+
+ Bytebuf writeEnum(Enum<?> instance);
+
+ <T extends Enum<T>> T readEnum(Class<T> enumClass);
+
+ Bytebuf writeUTFString(String utf);
+
+ String readUTFString();
+
+ Bytebuf writeComponentPlain(String str);
+
+ String readComponentPlain();
+
+ Bytebuf writeComponentJson(JsonElement json);
+
+ JsonElement readComponentJson();
+
+ Bytebuf writeItemStack(ItemStack itemStack);
+
+ ItemStack readItemStack();
+
+ Bytebuf copy();
+
+ boolean release();
+}
diff --git a/src/main/java/org/leavesmc/leaves/bytebuf/BytebufManager.java b/src/main/java/org/leavesmc/leaves/bytebuf/BytebufManager.java
new file mode 100644
index 0000000000000000000000000000000000000000..8668e47dee7a23d7843efc4a9752919f7a1dce88
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/bytebuf/BytebufManager.java
@@ -0,0 +1,15 @@
+package org.leavesmc.leaves.bytebuf;
+
+import org.bukkit.plugin.Plugin;
+import org.leavesmc.leaves.bytebuf.packet.PacketListener;
+
+public interface BytebufManager {
+
+ void registerListener(Plugin plugin, PacketListener listener);
+
+ void unregisterListener(Plugin plugin, PacketListener listener);
+
+ Bytebuf newBytebuf(int size);
+
+ Bytebuf toBytebuf(byte[] bytes);
+}
diff --git a/src/main/java/org/leavesmc/leaves/bytebuf/packet/Packet.java b/src/main/java/org/leavesmc/leaves/bytebuf/packet/Packet.java
new file mode 100644
index 0000000000000000000000000000000000000000..8bb00c16fe3e6089397cc0fcaaec4715960dd724
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/bytebuf/packet/Packet.java
@@ -0,0 +1,6 @@
+package org.leavesmc.leaves.bytebuf.packet;
+
+import org.leavesmc.leaves.bytebuf.Bytebuf;
+
+public record Packet(PacketType type, Bytebuf bytebuf) {
+}
diff --git a/src/main/java/org/leavesmc/leaves/bytebuf/packet/PacketListener.java b/src/main/java/org/leavesmc/leaves/bytebuf/packet/PacketListener.java
new file mode 100644
index 0000000000000000000000000000000000000000..e246c0a8725fa3b2be065433652a19a565c7205c
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/bytebuf/packet/PacketListener.java
@@ -0,0 +1,10 @@
+package org.leavesmc.leaves.bytebuf.packet;
+
+import org.bukkit.entity.Player;
+
+public interface PacketListener {
+
+ Packet onPacketIn(Player player, Packet packet);
+
+ Packet onPacketOut(Player player, Packet packet);
+}
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..4fd80d703653b8fb2f2bec329b1c5d04ae419d2d
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/bytebuf/packet/PacketType.java
@@ -0,0 +1,180 @@
+package org.leavesmc.leaves.bytebuf.packet;
+
+public enum PacketType {
+ // ClientboundBundle,
+ // ClientboundBundleDelimiter,
+ ClientboundAddEntity,
+ ClientboundAddExperienceOrb,
+ ClientboundAnimate,
+ ClientboundAwardStats,
+ ClientboundBlockChangedAck,
+ ClientboundBlockDestruction,
+ ClientboundBlockEntityData,
+ ClientboundBlockEvent,
+ ClientboundBlockUpdate,
+ ClientboundBossEvent,
+ ClientboundChangeDifficulty,
+ ClientboundChunkBatchFinished,
+ ClientboundChunkBatchStart,
+ ClientboundChunksBiomes,
+ ClientboundClearTitles,
+ ClientboundCommandSuggestions,
+ ClientboundCommands,
+ ClientboundContainerClose,
+ ClientboundContainerSetContent,
+ ClientboundContainerSetData,
+ ClientboundContainerSetSlot,
+ ClientboundCooldown,
+ ClientboundCustomChatCompletions,
+ ClientboundDamageEvent,
+ ClientboundDebugSample,
+ ClientboundDeleteChat,
+ ClientboundDisguisedChat,
+ ClientboundEntityEvent,
+ ClientboundExplode,
+ ClientboundForgetLevelChunk,
+ ClientboundGameEvent,
+ ClientboundHorseScreenOpen,
+ ClientboundHurtAnimation,
+ ClientboundInitializeBorder,
+ ClientboundLevelChunkWithLight,
+ ClientboundLevelEvent,
+ ClientboundLevelParticles,
+ ClientboundLightUpdate,
+ ClientboundLogin,
+ ClientboundMapItemData,
+ ClientboundMerchantOffers,
+ ClientboundMoveEntityPos,
+ ClientboundMoveEntityPosRot,
+ ClientboundMoveEntityRot,
+ ClientboundMoveVehicle,
+ ClientboundOpenBook,
+ ClientboundOpenScreen,
+ ClientboundOpenSignEditor,
+ ClientboundPlaceGhostRecipe,
+ ClientboundPlayerAbilities,
+ ClientboundPlayerChat,
+ ClientboundPlayerCombatEnd,
+ ClientboundPlayerCombatEnter,
+ ClientboundPlayerCombatKill,
+ ClientboundPlayerInfoRemove,
+ ClientboundPlayerInfoUpdate,
+ ClientboundPlayerLookAt,
+ ClientboundPlayerPosition,
+ ClientboundRecipe,
+ ClientboundRemoveEntities,
+ ClientboundRemoveMobEffect,
+ ClientboundRespawn,
+ ClientboundRotateHead,
+ ClientboundSectionBlocksUpdate,
+ ClientboundSelectAdvancementsTab,
+ ClientboundServerData,
+ ClientboundSetActionBarText,
+ ClientboundSetBorderCenter,
+ ClientboundSetBorderLerpSize,
+ ClientboundSetBorderSize,
+ ClientboundSetBorderWarningDelay,
+ ClientboundSetBorderWarningDistance,
+ ClientboundSetCamera,
+ ClientboundSetCarriedItem,
+ ClientboundSetChunkCacheCenter,
+ ClientboundSetChunkCacheRadius,
+ ClientboundSetDefaultSpawnPosition,
+ ClientboundSetDisplayObjective,
+ ClientboundSetEntityData,
+ ClientboundSetEntityLink,
+ ClientboundSetEntityMotion,
+ ClientboundSetEquipment,
+ ClientboundSetExperience,
+ ClientboundSetHealth,
+ ClientboundSetObjective,
+ ClientboundSetPassengers,
+ ClientboundSetPlayerTeam,
+ ClientboundSetScore,
+ ClientboundSetSimulationDistance,
+ ClientboundSetSubtitleText,
+ ClientboundSetTime,
+ ClientboundSetTitleText,
+ ClientboundSetTitlesAnimation,
+ ClientboundSoundEntity,
+ ClientboundSound,
+ ClientboundStartConfiguration,
+ ClientboundStopSound,
+ ClientboundSystemChat,
+ ClientboundTabList,
+ ClientboundTagQuery,
+ ClientboundTakeItemEntity,
+ ClientboundTeleportEntity,
+ ClientboundUpdateAdvancements,
+ ClientboundUpdateAttributes,
+ ClientboundUpdateMobEffect,
+ ClientboundUpdateRecipes,
+ ClientboundProjectilePower,
+ ServerboundAcceptTeleportation,
+ ServerboundBlockEntityTagQuery,
+ ServerboundChangeDifficulty,
+ ServerboundChatAck,
+ ServerboundChatCommand,
+ ServerboundChatCommandSigned,
+ ServerboundChat,
+ ServerboundChatSessionUpdate,
+ ServerboundChunkBatchReceived,
+ ServerboundClientCommand,
+ ServerboundCommandSuggestion,
+ ServerboundConfigurationAcknowledged,
+ ServerboundContainerButtonClick,
+ ServerboundContainerClick,
+ ServerboundContainerClose,
+ ServerboundContainerSlotStateChanged,
+ ServerboundDebugSampleSubscription,
+ ServerboundEditBook,
+ ServerboundEntityTagQuery,
+ ServerboundInteract,
+ ServerboundJigsawGenerate,
+ ServerboundLockDifficulty,
+ ServerboundMovePlayerPos,
+ ServerboundMovePlayerPosRot,
+ ServerboundMovePlayerRot,
+ ServerboundMovePlayerStatusOnly,
+ ServerboundMoveVehicle,
+ ServerboundPaddleBoat,
+ ServerboundPickItem,
+ ServerboundPlaceRecipe,
+ ServerboundPlayerAbilities,
+ ServerboundPlayerAction,
+ ServerboundPlayerCommand,
+ ServerboundPlayerInput,
+ ServerboundRecipeBookChangeSettings,
+ ServerboundRecipeBookSeenRecipe,
+ ServerboundRenameItem,
+ ServerboundSeenAdvancements,
+ ServerboundSelectTrade,
+ ServerboundSetBeacon,
+ ServerboundSetCarriedItem,
+ ServerboundSetCommandBlock,
+ ServerboundSetCommandMinecart,
+ ServerboundSetCreativeModeSlot,
+ ServerboundSetJigsawBlock,
+ ServerboundSetStructureBlock,
+ ServerboundSignUpdate,
+ ServerboundSwing,
+ ServerboundTeleportToEntity,
+ ServerboundUseItemOn,
+ ServerboundUseItem,
+ ClientboundResetScore,
+ ClientboundTickingState,
+ ClientboundTickingStep,
+ ClientboundCustomPayload,
+ ClientboundDisconnect,
+ ClientboundKeepAlive,
+ ClientboundResourcePackPop,
+ ClientboundResourcePackPush,
+ ClientboundStoreCookie,
+ ClientboundTransfer,
+ ClientboundUpdateTags,
+ ServerboundClientInformation,
+ ServerboundCustomPayload,
+ ServerboundKeepAlive,
+ ServerboundPong,
+ ServerboundResourcePack
+}
Loading

0 comments on commit 46ac3c6

Please sign in to comment.