From 9f37450178e327823c2de9d51bca0bec8a7b8540 Mon Sep 17 00:00:00 2001 From: DrPerkyLegit <116128211+DrPerkyLegit@users.noreply.github.com> Date: Fri, 22 May 2026 11:38:53 -0400 Subject: [PATCH] chore: switch all entity ids to dedicated ints (#86) * switch all entity ids to dedicated ints * Increment NETWORK_PROTOCOL_VERSION to 79 --- Minecraft.World/AddEntityPacket.cpp | 6 +-- Minecraft.World/AddMobPacket.cpp | 6 +-- Minecraft.World/MoveEntityPacket.cpp | 12 +++--- Minecraft.World/MoveEntityPacketSmall.cpp | 51 +++++++++++------------ Minecraft.World/RespawnPacket.cpp | 6 +-- Minecraft.World/SetEntityMotionPacket.cpp | 15 ++++--- Minecraft.World/SharedConstants.h | 4 +- Minecraft.World/TeleportEntityPacket.cpp | 6 +-- 8 files changed, 51 insertions(+), 55 deletions(-) diff --git a/Minecraft.World/AddEntityPacket.cpp b/Minecraft.World/AddEntityPacket.cpp index f70082b8..99c5d0ab 100644 --- a/Minecraft.World/AddEntityPacket.cpp +++ b/Minecraft.World/AddEntityPacket.cpp @@ -54,7 +54,7 @@ AddEntityPacket::AddEntityPacket(shared_ptr e, int type, int data, int y void AddEntityPacket::read(DataInputStream *dis) // throws IOException TODO 4J JEV add throws statement { - id = dis->readShort(); + id = dis->readInt(); type = dis->readByte(); #ifdef _LARGE_WORLDS x = dis->readInt(); @@ -78,7 +78,7 @@ void AddEntityPacket::read(DataInputStream *dis) // throws IOException TODO 4J void AddEntityPacket::write(DataOutputStream *dos) // throws IOException TODO 4J JEV add throws statement { - dos->writeShort(id); + dos->writeInt(id); dos->writeByte(type); #ifdef _LARGE_WORLDS dos->writeInt(x); @@ -107,5 +107,5 @@ void AddEntityPacket::handle(PacketListener *listener) int AddEntityPacket::getEstimatedSize() { - return 11 + data > -1 ? 6 : 0; + return (11 + data > -1 ? 6 : 0) + 2; } diff --git a/Minecraft.World/AddMobPacket.cpp b/Minecraft.World/AddMobPacket.cpp index d5ccfe08..8aca1055 100644 --- a/Minecraft.World/AddMobPacket.cpp +++ b/Minecraft.World/AddMobPacket.cpp @@ -66,7 +66,7 @@ AddMobPacket::AddMobPacket(shared_ptr mob, int yRotp, int xRotp, i void AddMobPacket::read(DataInputStream *dis) //throws IOException { - id = dis->readShort(); + id = dis->readInt(); type = dis->readByte() & 0xff; #ifdef _LARGE_WORLDS x = dis->readInt(); @@ -90,7 +90,7 @@ void AddMobPacket::read(DataInputStream *dis) //throws IOException void AddMobPacket::write(DataOutputStream *dos) //throws IOException { - dos->writeShort(id); + dos->writeInt(id); dos->writeByte(type & 0xff); #ifdef _LARGE_WORLDS dos->writeInt(x); @@ -127,7 +127,7 @@ int AddMobPacket::getEstimatedSize() // 4J Stu - This is an incoming value which we aren't currently analysing //size += unpack->get } - return size; + return size + 2; } vector > *AddMobPacket::getUnpackedData() diff --git a/Minecraft.World/MoveEntityPacket.cpp b/Minecraft.World/MoveEntityPacket.cpp index a2b79db3..f071a49f 100644 --- a/Minecraft.World/MoveEntityPacket.cpp +++ b/Minecraft.World/MoveEntityPacket.cpp @@ -30,7 +30,7 @@ MoveEntityPacket::MoveEntityPacket(int id) void MoveEntityPacket::read(DataInputStream *dis) //throws IOException { - id = dis->readShort(); + id = dis->readInt(); } void MoveEntityPacket::write(DataOutputStream *dos) //throws IOException @@ -40,7 +40,7 @@ void MoveEntityPacket::write(DataOutputStream *dos) //throws IOException // We shouln't be tracking an entity that doesn't have a short type of id DEBUG_BREAK(); } - dos->writeShort(static_cast(id)); + dos->writeInt(static_cast(id)); } void MoveEntityPacket::handle(PacketListener *listener) @@ -50,7 +50,7 @@ void MoveEntityPacket::handle(PacketListener *listener) int MoveEntityPacket::getEstimatedSize() { - return 2; + return 4; } bool MoveEntityPacket::canBeInvalidated() @@ -101,7 +101,7 @@ void MoveEntityPacket::PosRot::write(DataOutputStream *dos) //throws IOException int MoveEntityPacket::PosRot::getEstimatedSize() { - return 2+5; + return 4+5; } MoveEntityPacket::Pos::Pos() @@ -133,7 +133,7 @@ void MoveEntityPacket::Pos::write(DataOutputStream *dos) //throws IOException int MoveEntityPacket::Pos::getEstimatedSize() { - return 2+3; + return 4+3; } MoveEntityPacket::Rot::Rot() @@ -164,5 +164,5 @@ void MoveEntityPacket::Rot::write(DataOutputStream *dos) //throws IOException int MoveEntityPacket::Rot::getEstimatedSize() { - return 2+2; + return 4+2; } diff --git a/Minecraft.World/MoveEntityPacketSmall.cpp b/Minecraft.World/MoveEntityPacketSmall.cpp index 3e1b922c..2f6d3bc2 100644 --- a/Minecraft.World/MoveEntityPacketSmall.cpp +++ b/Minecraft.World/MoveEntityPacketSmall.cpp @@ -37,7 +37,7 @@ MoveEntityPacketSmall::MoveEntityPacketSmall(int id) void MoveEntityPacketSmall::read(DataInputStream *dis) //throws IOException { - id = dis->readShort(); + id = dis->readInt(); } void MoveEntityPacketSmall::write(DataOutputStream *dos) //throws IOException @@ -47,7 +47,7 @@ void MoveEntityPacketSmall::write(DataOutputStream *dos) //throws IOException // We shouln't be tracking an entity that doesn't have a short type of id DEBUG_BREAK(); } - dos->writeShort(static_cast(id)); + dos->writeInt(id); } void MoveEntityPacketSmall::handle(PacketListener *listener) @@ -57,7 +57,7 @@ void MoveEntityPacketSmall::handle(PacketListener *listener) int MoveEntityPacketSmall::getEstimatedSize() { - return 2; + return 4; } bool MoveEntityPacketSmall::canBeInvalidated() @@ -88,13 +88,12 @@ MoveEntityPacketSmall::PosRot::PosRot(int id, char xa, char ya, char za, char yR void MoveEntityPacketSmall::PosRot::read(DataInputStream *dis) //throws IOException { - int idAndRot = dis->readShort(); - this->id = idAndRot & 0x07ff; - this->yRot = idAndRot >> 11; - int xAndYAndZ = (int)dis->readShort(); - this->xa = xAndYAndZ >> 11; - this->ya = (xAndYAndZ << 21 ) >> 26; - this->za = (xAndYAndZ << 27 ) >> 27; + this->id = dis->readInt(); + this->yRot = dis->readChar(); + int XandYandZ = (int)dis->readShort(); + this->xa = XandYandZ >> 11; + this->ya = (XandYandZ << 21 ) >> 26; + this->za = (XandYandZ << 27 ) >> 27; } void MoveEntityPacketSmall::PosRot::write(DataOutputStream *dos) //throws IOException @@ -104,15 +103,15 @@ void MoveEntityPacketSmall::PosRot::write(DataOutputStream *dos) //throws IOExce // We shouln't be tracking an entity that doesn't have a short type of id DEBUG_BREAK(); } - short idAndRot = id | yRot << 11; - dos->writeShort(idAndRot); - short xAndYAndZ = ( xa << 11 ) | ( ( ya & 0x3f ) << 5 ) | ( za & 0x1f ); - dos->writeShort(xAndYAndZ); + dos->writeInt(id); + dos->writeChar(yRot); + short XandYandZ = ( xa << 11 ) | ( ( ya & 0x3f ) << 5 ) | ( za & 0x1f ); + dos->writeShort(XandYandZ); } int MoveEntityPacketSmall::PosRot::getEstimatedSize() { - return 4; + return 7; } MoveEntityPacketSmall::Pos::Pos() @@ -128,9 +127,8 @@ MoveEntityPacketSmall::Pos::Pos(int id, char xa, char ya, char za) : MoveEntityP void MoveEntityPacketSmall::Pos::read(DataInputStream *dis) //throws IOException { - int idAndY = dis->readShort(); - this->id = idAndY & 0x07ff; - this->ya = idAndY >> 11; + this->id = dis->readInt(); + this->ya = dis->readChar(); int XandZ = (int)static_cast(dis->readByte()); xa = XandZ >> 4; za = ( XandZ << 28 ) >> 28; @@ -143,15 +141,15 @@ void MoveEntityPacketSmall::Pos::write(DataOutputStream *dos) //throws IOExcepti // We shouln't be tracking an entity that doesn't have a short type of id DEBUG_BREAK(); } - short idAndY = id | ya << 11; - dos->writeShort(idAndY); + dos->writeInt(id); + dos->writeChar(ya); char XandZ = ( xa << 4 ) | ( za & 0x0f ); dos->writeByte(XandZ); } int MoveEntityPacketSmall::Pos::getEstimatedSize() { - return 3; + return 7; } MoveEntityPacketSmall::Rot::Rot() @@ -169,9 +167,8 @@ MoveEntityPacketSmall::Rot::Rot(int id, char yRot, char xRot) : MoveEntityPacket void MoveEntityPacketSmall::Rot::read(DataInputStream *dis) //throws IOException { - int idAndRot = (int)dis->readShort(); - this->id = idAndRot & 0x07ff; - this->yRot = idAndRot >> 11; + this->id = dis->readInt(); + this->yRot = dis->readChar(); } void MoveEntityPacketSmall::Rot::write(DataOutputStream *dos) //throws IOException @@ -181,11 +178,11 @@ void MoveEntityPacketSmall::Rot::write(DataOutputStream *dos) //throws IOExcepti // We shouln't be tracking an entity that doesn't have a short type of id DEBUG_BREAK(); } - short idAndRot = id | yRot << 11; - dos->writeShort(idAndRot); + dos->writeInt(id); + dos->writeChar(yRot); } int MoveEntityPacketSmall::Rot::getEstimatedSize() { - return 2; + return 5; } diff --git a/Minecraft.World/RespawnPacket.cpp b/Minecraft.World/RespawnPacket.cpp index c4a19c8c..aee7d729 100644 --- a/Minecraft.World/RespawnPacket.cpp +++ b/Minecraft.World/RespawnPacket.cpp @@ -58,7 +58,7 @@ void RespawnPacket::read(DataInputStream *dis) //throws IOException mapSeed = dis->readLong(); difficulty = dis->readByte(); m_newSeaLevel = dis->readBoolean(); - m_newEntityId = dis->readShort(); + m_newEntityId = dis->readInt(); #ifdef _LARGE_WORLDS m_xzSize = dis->readShort(); m_hellScale = dis->read(); @@ -83,7 +83,7 @@ void RespawnPacket::write(DataOutputStream *dos) //throws IOException dos->writeLong(mapSeed); dos->writeByte(difficulty); dos->writeBoolean(m_newSeaLevel); - dos->writeShort(m_newEntityId); + dos->writeInt(m_newEntityId); #ifdef _LARGE_WORLDS dos->writeShort(m_xzSize); dos->write(m_hellScale); @@ -97,5 +97,5 @@ int RespawnPacket::getEstimatedSize() { length = static_cast(m_pLevelType->getGeneratorName().length()); } - return 13+length; + return 13+length+2; } diff --git a/Minecraft.World/SetEntityMotionPacket.cpp b/Minecraft.World/SetEntityMotionPacket.cpp index 20d841f1..8488eee7 100644 --- a/Minecraft.World/SetEntityMotionPacket.cpp +++ b/Minecraft.World/SetEntityMotionPacket.cpp @@ -49,9 +49,9 @@ SetEntityMotionPacket::SetEntityMotionPacket(int id, double xd, double yd, doubl void SetEntityMotionPacket::read(DataInputStream *dis) //throws IOException { - short idAndFlag = dis->readShort(); - id = idAndFlag & 0x07ff; - if( idAndFlag & 0x0800 ) + useBytes = dis->readBoolean(); + id = dis->readInt(); + if(useBytes) { xa = static_cast(dis->readByte()); ya = static_cast(dis->readByte()); @@ -62,29 +62,28 @@ void SetEntityMotionPacket::read(DataInputStream *dis) //throws IOException xa *= 16; ya *= 16; za *= 16; - useBytes = true; } else { xa = dis->readShort(); ya = dis->readShort(); za = dis->readShort(); - useBytes = false; } } void SetEntityMotionPacket::write(DataOutputStream *dos) //throws IOException { + dos->writeBoolean(useBytes); if( useBytes ) { - dos->writeShort(id | 0x800); + dos->writeInt(id); dos->writeByte(xa/16); dos->writeByte(ya/16); dos->writeByte(za/16); } else { - dos->writeShort(id); + dos->writeInt(id); dos->writeShort(xa); dos->writeShort(ya); dos->writeShort(za); @@ -98,7 +97,7 @@ void SetEntityMotionPacket::handle(PacketListener *listener) int SetEntityMotionPacket::getEstimatedSize() { - return useBytes ? 5 : 8; + return useBytes ? 8 : 11; } bool SetEntityMotionPacket::canBeInvalidated() diff --git a/Minecraft.World/SharedConstants.h b/Minecraft.World/SharedConstants.h index c4db88ae..875fdba1 100644 --- a/Minecraft.World/SharedConstants.h +++ b/Minecraft.World/SharedConstants.h @@ -7,7 +7,7 @@ class SharedConstants public: static void staticCtor(); static const wstring VERSION_STRING; - static const int NETWORK_PROTOCOL_VERSION = 78; + static const int NETWORK_PROTOCOL_VERSION = 79; static const bool INGAME_DEBUG_OUTPUT = false; // NOT texture resolution. How many sub-blocks each block face is made up of. @@ -31,4 +31,4 @@ class SharedConstants static const int TICKS_PER_SECOND = 20; static const int FULLBRIGHT_LIGHTVALUE = 15 << 20 | 15 << 4; -}; \ No newline at end of file +}; diff --git a/Minecraft.World/TeleportEntityPacket.cpp b/Minecraft.World/TeleportEntityPacket.cpp index ccac5e51..7d7a492f 100644 --- a/Minecraft.World/TeleportEntityPacket.cpp +++ b/Minecraft.World/TeleportEntityPacket.cpp @@ -39,7 +39,7 @@ TeleportEntityPacket::TeleportEntityPacket(int id, int x, int y, int z, byte yRo void TeleportEntityPacket::read(DataInputStream *dis) //throws IOException { - id = dis->readShort(); + id = dis->readInt(); #ifdef _LARGE_WORLDS x = dis->readInt(); y = dis->readInt(); @@ -55,7 +55,7 @@ void TeleportEntityPacket::read(DataInputStream *dis) //throws IOException void TeleportEntityPacket::write(DataOutputStream *dos) //throws IOException { - dos->writeShort(id); + dos->writeInt(id); #ifdef _LARGE_WORLDS dos->writeInt(x); dos->writeInt(y); @@ -76,7 +76,7 @@ void TeleportEntityPacket::handle(PacketListener *listener) int TeleportEntityPacket::getEstimatedSize() { - return 2 + 2 + 2 + 2 + 1 + 1; + return 4 + 2 + 2 + 2 + 1 + 1; } bool TeleportEntityPacket::canBeInvalidated()