From 702839176ee8e7837d7f4b65d0e6837637530de0 Mon Sep 17 00:00:00 2001 From: MathiewMay Date: Wed, 11 Mar 2026 08:29:24 -0400 Subject: [PATCH 1/7] Revert " applied changes requested by tropicaaal" This reverts commit 41d8202c478bba47103d050758faf7ae7cd8dca7. # Conflicts: # Minecraft.World/Network/Packets/MoveEntityPacket.cpp --- Minecraft.Client/Rendering/GameRenderer.cpp | 2 +- .../Network/Packets/MoveEntityPacket.cpp | 36 +++++++-------- .../Network/Packets/MoveEntityPacket.h | 8 ++-- .../Network/Packets/MoveEntityPacketSmall.cpp | 45 +++++++++---------- .../Network/Packets/MoveEntityPacketSmall.h | 12 ++--- .../Network/Packets/SetEntityMotionPacket.cpp | 6 +-- .../Network/Packets/SetEntityMotionPacket.h | 2 +- .../Network/Packets/TeleportEntityPacket.cpp | 10 ++--- .../Network/Packets/TeleportEntityPacket.h | 8 ++-- 9 files changed, 64 insertions(+), 65 deletions(-) diff --git a/Minecraft.Client/Rendering/GameRenderer.cpp b/Minecraft.Client/Rendering/GameRenderer.cpp index 6aae7c93e..6bda6429e 100644 --- a/Minecraft.Client/Rendering/GameRenderer.cpp +++ b/Minecraft.Client/Rendering/GameRenderer.cpp @@ -1407,7 +1407,7 @@ void GameRenderer::renderLevel(float a, __int64 until) #endif PIXEndNamedEvent(); PIXBeginNamedEvent(0,"Particle render"); - turnOnLightLayer(a); // 4J - brought forward from 1.8.2 + //turnOnLightLayer(a); // 4J - brought forward from 1.8.2 particleEngine->renderLit(cameraEntity, a); Lighting::turnOff(); setupFog(0, a); diff --git a/Minecraft.World/Network/Packets/MoveEntityPacket.cpp b/Minecraft.World/Network/Packets/MoveEntityPacket.cpp index 241f5f021..b0f6298fc 100644 --- a/Minecraft.World/Network/Packets/MoveEntityPacket.cpp +++ b/Minecraft.World/Network/Packets/MoveEntityPacket.cpp @@ -69,24 +69,24 @@ MoveEntityPacket::PosRot::PosRot() hasRot = true; } -MoveEntityPacket::PosRot::PosRot(int id, int8_t xa, int8_t ya, int8_t za, int8_t yRot, int8_t xRot) : MoveEntityPacket( id ) +MoveEntityPacket::PosRot::PosRot(int id, char xa, char ya, char za, char yRot, char xRot) : MoveEntityPacket( id ) { - this->xa = xa; - this->ya = ya; - this->za = za; - this->yRot = yRot; - this->xRot = xRot; + this->xa = (int)(signed char)xa; + this->ya = (int)(signed char)ya; + this->za = (int)(signed char)za; + this->yRot = (int)(signed char)yRot; + this->xRot = (int)(signed char)xRot; hasRot = true; } void MoveEntityPacket::PosRot::read(DataInputStream *dis) //throws IOException { MoveEntityPacket::read(dis); - xa = (int8_t)dis->readByte(); - ya = (int8_t)dis->readByte(); - za = (int8_t)dis->readByte(); - yRot = (int8_t)dis->readByte(); - xRot = (int8_t)dis->readByte(); + xa = (int)(signed char)dis->readByte(); + ya = (int)(signed char)dis->readByte(); + za = (int)(signed char)dis->readByte(); + yRot = (int)(signed char)dis->readByte(); + xRot = (int)(signed char)dis->readByte(); } void MoveEntityPacket::PosRot::write(DataOutputStream *dos) //throws IOException @@ -108,7 +108,7 @@ MoveEntityPacket::Pos::Pos() { } -MoveEntityPacket::Pos::Pos(int id, int8_t xa, int8_t ya, int8_t za) : MoveEntityPacket(id) +MoveEntityPacket::Pos::Pos(int id, char xa, char ya, char za) : MoveEntityPacket(id) { this->xa = xa; this->ya = ya; @@ -118,9 +118,9 @@ MoveEntityPacket::Pos::Pos(int id, int8_t xa, int8_t ya, int8_t za) : MoveEntity void MoveEntityPacket::Pos::read(DataInputStream *dis) //throws IOException { MoveEntityPacket::read(dis); - xa = (int8_t)dis->readByte(); - ya = (int8_t)dis->readByte(); - za = (int8_t)dis->readByte(); + xa = (int)(signed char)dis->readByte(); + ya = (int)(signed char)dis->readByte(); + za = (int)(signed char)dis->readByte(); } void MoveEntityPacket::Pos::write(DataOutputStream *dos) //throws IOException @@ -141,7 +141,7 @@ MoveEntityPacket::Rot::Rot() hasRot = true; } -MoveEntityPacket::Rot::Rot(int id, int8_t yRot, int8_t xRot) : MoveEntityPacket(id) +MoveEntityPacket::Rot::Rot(int id, char yRot, char xRot) : MoveEntityPacket(id) { this->yRot = yRot; this->xRot = xRot; @@ -151,8 +151,8 @@ MoveEntityPacket::Rot::Rot(int id, int8_t yRot, int8_t xRot) : MoveEntityPacket( void MoveEntityPacket::Rot::read(DataInputStream *dis) //throws IOException { MoveEntityPacket::read(dis); - yRot = (int8_t)dis->readByte(); - xRot = (int8_t)dis->readByte(); + yRot = (int)(signed char)dis->readByte(); + xRot = (int)(signed char)dis->readByte(); } void MoveEntityPacket::Rot::write(DataOutputStream *dos) //throws IOException diff --git a/Minecraft.World/Network/Packets/MoveEntityPacket.h b/Minecraft.World/Network/Packets/MoveEntityPacket.h index 8d2ac049c..62560809f 100644 --- a/Minecraft.World/Network/Packets/MoveEntityPacket.h +++ b/Minecraft.World/Network/Packets/MoveEntityPacket.h @@ -13,7 +13,7 @@ public: class Rot; int id; - int8_t xa, ya, za, yRot, xRot; + char xa, ya, za, yRot, xRot; bool hasRot; MoveEntityPacket(); @@ -35,7 +35,7 @@ class MoveEntityPacket::PosRot : public MoveEntityPacket { public: PosRot(); - PosRot(int id, int8_t xa, int8_t ya, int8_t za, int8_t yRot, int8_t xRot); + PosRot(int id, char xa, char ya, char za, char yRot, char xRot); virtual void read(DataInputStream *dis); virtual void write(DataOutputStream *dos); @@ -50,7 +50,7 @@ class MoveEntityPacket::Pos : public MoveEntityPacket { public: Pos(); - Pos(int id, int8_t xa, int8_t ya, int8_t za); + Pos(int id, char xa, char ya, char za); virtual void read(DataInputStream *dis); virtual void write(DataOutputStream *dos); @@ -65,7 +65,7 @@ class MoveEntityPacket::Rot : public MoveEntityPacket { public: Rot(); - Rot(int id, int8_t yRot, int8_t xRot); + Rot(int id, char yRot, char xRot); virtual void read(DataInputStream *dis); virtual void write(DataOutputStream *dos); diff --git a/Minecraft.World/Network/Packets/MoveEntityPacketSmall.cpp b/Minecraft.World/Network/Packets/MoveEntityPacketSmall.cpp index daf3eb8a9..1c698f720 100644 --- a/Minecraft.World/Network/Packets/MoveEntityPacketSmall.cpp +++ b/Minecraft.World/Network/Packets/MoveEntityPacketSmall.cpp @@ -1,6 +1,5 @@ #include "../../Platform/stdafx.h" #include -#include #include "../../IO/Streams/InputOutputStream.h" #include "PacketListener.h" #include "MoveEntityPacketSmall.h" @@ -32,7 +31,7 @@ void MoveEntityPacketSmall::read(DataInputStream *dis) //throws IOException void MoveEntityPacketSmall::write(DataOutputStream *dos) //throws IOException { - if(id < 0 || id > std::numeric_limits::max() ) + if(id < 0 || id > 32767 ) { // We shouln't be tracking an entity that doesn't have a short type of id __debugbreak(); @@ -66,7 +65,7 @@ MoveEntityPacketSmall::PosRot::PosRot() hasRot = true; } -MoveEntityPacketSmall::PosRot::PosRot(int id, int8_t xa, int8_t ya, int8_t za, int8_t yRot, int8_t xRot) : MoveEntityPacketSmall( id ) +MoveEntityPacketSmall::PosRot::PosRot(int id, char xa, char ya, char za, char yRot, char xRot) : MoveEntityPacketSmall( id ) { this->xa = xa; this->ya = ya; @@ -78,22 +77,22 @@ MoveEntityPacketSmall::PosRot::PosRot(int id, int8_t xa, int8_t ya, int8_t za, i void MoveEntityPacketSmall::PosRot::read(DataInputStream *dis) //throws IOException { - MoveEntityPacketSmall::read(dis); - xa = (int8_t)dis->readByte(); - ya = (int8_t)dis->readByte(); - za = (int8_t)dis->readByte(); - yRot = (int8_t)dis->readByte(); - xRot = (int8_t)dis->readByte(); + MoveEntityPacketSmall::read(dis); + xa = (signed char)dis->readByte(); + ya = (signed char)dis->readByte(); + za = (signed char)dis->readByte(); + yRot = (signed char)dis->readByte(); + xRot = (signed char)dis->readByte(); } void MoveEntityPacketSmall::PosRot::write(DataOutputStream *dos) //throws IOException { - MoveEntityPacketSmall::write(dos); - dos->writeByte((uint8_t)(xa & 0xFF)); - dos->writeByte((uint8_t)(ya & 0xFF)); - dos->writeByte((uint8_t)(za & 0xFF)); - dos->writeByte((uint8_t)(yRot & 0xFF)); - dos->writeByte((uint8_t)(xRot & 0xFF)); + MoveEntityPacketSmall::write(dos); + dos->writeByte((uint8_t)(xa & 0xFF)); + dos->writeByte((uint8_t)(ya & 0xFF)); + dos->writeByte((uint8_t)(za & 0xFF)); + dos->writeByte((uint8_t)(yRot & 0xFF)); + dos->writeByte((uint8_t)(xRot & 0xFF)); } int MoveEntityPacketSmall::PosRot::getEstimatedSize() @@ -105,7 +104,7 @@ MoveEntityPacketSmall::Pos::Pos() { } -MoveEntityPacketSmall::Pos::Pos(int id, int8_t xa, int8_t ya, int8_t za) : MoveEntityPacketSmall(id) +MoveEntityPacketSmall::Pos::Pos(int id, char xa, char ya, char za) : MoveEntityPacketSmall(id) { this->xa = xa; this->ya = ya; @@ -114,10 +113,10 @@ MoveEntityPacketSmall::Pos::Pos(int id, int8_t xa, int8_t ya, int8_t za) : MoveE void MoveEntityPacketSmall::Pos::read(DataInputStream *dis) //throws IOException { - MoveEntityPacketSmall::read(dis); - xa = (int8_t)dis->readByte(); - ya = (int8_t)dis->readByte(); - za = (int8_t)dis->readByte(); + MoveEntityPacketSmall::read(dis); + xa = (signed char)dis->readByte(); + ya = (signed char)dis->readByte(); + za = (signed char)dis->readByte(); } void MoveEntityPacketSmall::Pos::write(DataOutputStream *dos) //throws IOException @@ -138,7 +137,7 @@ MoveEntityPacketSmall::Rot::Rot() hasRot = true; } -MoveEntityPacketSmall::Rot::Rot(int id, int8_t yRot, int8_t xRot) : MoveEntityPacketSmall(id) +MoveEntityPacketSmall::Rot::Rot(int id, char yRot, char xRot) : MoveEntityPacketSmall(id) { this->yRot = yRot; @@ -148,8 +147,8 @@ MoveEntityPacketSmall::Rot::Rot(int id, int8_t yRot, int8_t xRot) : MoveEntityPa void MoveEntityPacketSmall::Rot::read(DataInputStream *dis) //throws IOException { - MoveEntityPacketSmall::read(dis); - yRot = (int8_t)dis->readByte(); + MoveEntityPacketSmall::read(dis); + yRot = (signed char)dis->readByte(); } void MoveEntityPacketSmall::Rot::write(DataOutputStream *dos) //throws IOException diff --git a/Minecraft.World/Network/Packets/MoveEntityPacketSmall.h b/Minecraft.World/Network/Packets/MoveEntityPacketSmall.h index a5cb220ac..fffdc7e56 100644 --- a/Minecraft.World/Network/Packets/MoveEntityPacketSmall.h +++ b/Minecraft.World/Network/Packets/MoveEntityPacketSmall.h @@ -13,8 +13,8 @@ public: class Rot; int id; - int8_t xa, ya, za, yRot, xRot; - bool hasRot; + char xa, ya, za, yRot, xRot; + bool hasRot; MoveEntityPacketSmall(); MoveEntityPacketSmall(int id); @@ -34,8 +34,8 @@ public: class MoveEntityPacketSmall::PosRot : public MoveEntityPacketSmall { public: - PosRot(); - PosRot(int id, int8_t xa, int8_t ya, int8_t za, int8_t yRot, int8_t xRot); + PosRot(); + PosRot(int id, char xa, char ya, char za, char yRot, char xRot); virtual void read(DataInputStream *dis); virtual void write(DataOutputStream *dos); @@ -50,7 +50,7 @@ class MoveEntityPacketSmall::Pos : public MoveEntityPacketSmall { public: Pos(); - Pos(int id, int8_t xa, int8_t ya, int8_t za); + Pos(int id, char xa, char ya, char za); virtual void read(DataInputStream *dis); virtual void write(DataOutputStream *dos); @@ -66,7 +66,7 @@ class MoveEntityPacketSmall::Rot : public MoveEntityPacketSmall { public: Rot(); - Rot(int id, int8_t yRot, int8_t xRot); + Rot(int id, char yRot, char xRot); virtual void read(DataInputStream *dis); virtual void write(DataOutputStream *dos); diff --git a/Minecraft.World/Network/Packets/SetEntityMotionPacket.cpp b/Minecraft.World/Network/Packets/SetEntityMotionPacket.cpp index 72ee76ee7..70ea64f92 100644 --- a/Minecraft.World/Network/Packets/SetEntityMotionPacket.cpp +++ b/Minecraft.World/Network/Packets/SetEntityMotionPacket.cpp @@ -17,9 +17,9 @@ void SetEntityMotionPacket::_init(int id, double xd, double yd, double zd) if (xd > m) xd = m; if (yd > m) yd = m; if (zd > m) zd = m; - xa = (int16_t) (xd * 8000.0); - ya = (int16_t) (yd * 8000.0); - za = (int16_t) (zd * 8000.0); + xa = (int) (xd * 8000.0); + ya = (int) (yd * 8000.0); + za = (int) (zd * 8000.0); useBytes = false; } diff --git a/Minecraft.World/Network/Packets/SetEntityMotionPacket.h b/Minecraft.World/Network/Packets/SetEntityMotionPacket.h index 5c3702577..537c5a4fd 100644 --- a/Minecraft.World/Network/Packets/SetEntityMotionPacket.h +++ b/Minecraft.World/Network/Packets/SetEntityMotionPacket.h @@ -7,7 +7,7 @@ class SetEntityMotionPacket : public Packet, public std::enable_shared_from_this { public: int id; - int16_t xa, ya, za; + int xa, ya, za; bool useBytes; // 4J added private: diff --git a/Minecraft.World/Network/Packets/TeleportEntityPacket.cpp b/Minecraft.World/Network/Packets/TeleportEntityPacket.cpp index 9dc2742ed..3a0c9927f 100644 --- a/Minecraft.World/Network/Packets/TeleportEntityPacket.cpp +++ b/Minecraft.World/Network/Packets/TeleportEntityPacket.cpp @@ -17,7 +17,7 @@ TeleportEntityPacket::TeleportEntityPacket() xRot = 0; } -TeleportEntityPacket::TeleportEntityPacket(std::shared_ptr e) +TeleportEntityPacket::TeleportEntityPacket(std::shared_ptr e) { id = e->entityId; x = Mth::floor(e->x * 32); @@ -27,7 +27,7 @@ TeleportEntityPacket::TeleportEntityPacket(std::shared_ptr e) xRot = (uint8_t) (e->xRot * 256 / 360); } -TeleportEntityPacket::TeleportEntityPacket(int id, int32_t x, int32_t y, int32_t z, uint8_t yRot, uint8_t xRot) +TeleportEntityPacket::TeleportEntityPacket(int id, int x, int y, int z, uint8_t yRot, uint8_t xRot) { this->id = id; this->x = x; @@ -53,7 +53,7 @@ void TeleportEntityPacket::read(DataInputStream *dis) //throws IOException xRot = (uint8_t) dis->read(); } -void TeleportEntityPacket::write(DataOutputStream *dos) //throws IOException +void TeleportEntityPacket::write(DataOutputStream *dos) //throws IOException { dos->writeShort((short)id); dos->writeInt(x); @@ -63,12 +63,12 @@ void TeleportEntityPacket::write(DataOutputStream *dos) //throws IOException dos->write(xRot); } -void TeleportEntityPacket::handle(PacketListener *listener) +void TeleportEntityPacket::handle(PacketListener *listener) { listener->handleTeleportEntity(shared_from_this()); } -int TeleportEntityPacket::getEstimatedSize() +int TeleportEntityPacket::getEstimatedSize() { return 2 + 2 + 2 + 2 + 1 + 1; } diff --git a/Minecraft.World/Network/Packets/TeleportEntityPacket.h b/Minecraft.World/Network/Packets/TeleportEntityPacket.h index 5ee91255d..0b85a3b99 100644 --- a/Minecraft.World/Network/Packets/TeleportEntityPacket.h +++ b/Minecraft.World/Network/Packets/TeleportEntityPacket.h @@ -7,19 +7,19 @@ class TeleportEntityPacket : public Packet, public std::enable_shared_from_this< { public: int id; - int32_t x, y, z; - uint8_t yRot, xRot; + int x, y, z; + uint8_t yRot, xRot; TeleportEntityPacket(); TeleportEntityPacket(std::shared_ptr e); - TeleportEntityPacket(int id, int32_t x, int32_t y, int32_t z, uint8_t yRot, uint8_t xRot); + TeleportEntityPacket(int id, int x, int y, int z, uint8_t yRot, uint8_t xRot); virtual void read(DataInputStream *dis); virtual void write(DataOutputStream *dos); virtual void handle(PacketListener *listener); virtual int getEstimatedSize(); virtual bool canBeInvalidated(); - virtual bool isInvalidatedBy(std::shared_ptr packet); + virtual bool isInvalidatedBy(std::shared_ptr packet); public: static std::shared_ptr create() { return std::shared_ptr(new TeleportEntityPacket()); } From 047615dbbe40e0d82e202f8a9483471aa54c62cd Mon Sep 17 00:00:00 2001 From: MathiewMay Date: Wed, 11 Mar 2026 08:34:20 -0400 Subject: [PATCH 2/7] Fix entity ID TLS key recreating on every call of getSmallid() for linux, this caused a ID pool corruption. --- Minecraft.World/Entities/Entity.cpp | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/Minecraft.World/Entities/Entity.cpp b/Minecraft.World/Entities/Entity.cpp index 9c88a00e2..0a62c9b3e 100644 --- a/Minecraft.World/Entities/Entity.cpp +++ b/Minecraft.World/Entities/Entity.cpp @@ -24,7 +24,11 @@ int Entity::entityCounter = 2048; // 4J - changed initialiser to 2048, as we are using range 0 - 2047 as special unique smaller ids for things that need network tracked +#ifdef _WIN32 DWORD Entity::tlsIdx = TlsAlloc(); +#else +pthread_key_t Entity::tlsIdx = 0; +#endif // 4J - added getSmallId & freeSmallId methods unsigned int Entity::entityIdUsedFlags[2048/32] = {0}; @@ -47,7 +51,6 @@ int Entity::getSmallId() #ifdef _WIN32 if( ((size_t)TlsGetValue(tlsIdx) != 0 ) ) #else - pthread_key_create(&tlsIdx, nullptr); if ( ((size_t)pthread_getspecific(tlsIdx) != 0) ) #endif { @@ -151,7 +154,16 @@ void Entity::freeSmallId(int index) void Entity::useSmallIds() { - pthread_setspecific(tlsIdx,(LPVOID)1); +#ifdef _WIN32 + TlsSetValue(tlsIdx, (LPVOID)1); +#else + static bool keyCreated = false; + if (!keyCreated) { + pthread_key_create(&tlsIdx, nullptr); + keyCreated = true; + } + pthread_setspecific(tlsIdx,(void*)1); +#endif } // Things also added here to be able to manage the concept of a number of extra "wandering" entities - normally path finding entities aren't allowed to From 77deb67b4a80dc938fd1cd93501c159ef0ac4c66 Mon Sep 17 00:00:00 2001 From: MathiewMay Date: Wed, 11 Mar 2026 08:39:22 -0400 Subject: [PATCH 3/7] Revert "Rewrote the entity movement packets to use full 16-bit signed short increasing the entity ID limit from 2048 (artifical lce limit) to 32768 ids (0 to 32767)" This reverts commit dac0e883b659f2e6cfce651a8fa3b1d8dc2274fe. --- .../Network/Packets/MoveEntityPacket.cpp | 72 ++++++------ .../Network/Packets/MoveEntityPacketSmall.cpp | 106 +++++++++++------- .../Network/Packets/SetEntityMotionPacket.cpp | 70 +++++++++--- .../Network/Packets/TeleportEntityPacket.cpp | 8 +- 4 files changed, 162 insertions(+), 94 deletions(-) diff --git a/Minecraft.World/Network/Packets/MoveEntityPacket.cpp b/Minecraft.World/Network/Packets/MoveEntityPacket.cpp index b0f6298fc..3fde6d6ab 100644 --- a/Minecraft.World/Network/Packets/MoveEntityPacket.cpp +++ b/Minecraft.World/Network/Packets/MoveEntityPacket.cpp @@ -4,7 +4,7 @@ #include "PacketListener.h" #include "MoveEntityPacket.h" -MoveEntityPacket::MoveEntityPacket() +MoveEntityPacket::MoveEntityPacket() { hasRot = false; @@ -28,7 +28,7 @@ MoveEntityPacket::MoveEntityPacket(int id) xRot = 0; } -void MoveEntityPacket::read(DataInputStream *dis) //throws IOException +void MoveEntityPacket::read(DataInputStream *dis) //throws IOException { id = dis->readShort(); } @@ -48,7 +48,7 @@ void MoveEntityPacket::handle(PacketListener *listener) listener->handleMoveEntity(shared_from_this()); } -int MoveEntityPacket::getEstimatedSize() +int MoveEntityPacket::getEstimatedSize() { return 2; } @@ -71,40 +71,40 @@ MoveEntityPacket::PosRot::PosRot() MoveEntityPacket::PosRot::PosRot(int id, char xa, char ya, char za, char yRot, char xRot) : MoveEntityPacket( id ) { - this->xa = (int)(signed char)xa; - this->ya = (int)(signed char)ya; - this->za = (int)(signed char)za; - this->yRot = (int)(signed char)yRot; - this->xRot = (int)(signed char)xRot; + this->xa = xa; + this->ya = ya; + this->za = za; + this->yRot = yRot; + this->xRot = xRot; hasRot = true; } -void MoveEntityPacket::PosRot::read(DataInputStream *dis) //throws IOException +void MoveEntityPacket::PosRot::read(DataInputStream *dis) //throws IOException { MoveEntityPacket::read(dis); - xa = (int)(signed char)dis->readByte(); - ya = (int)(signed char)dis->readByte(); - za = (int)(signed char)dis->readByte(); - yRot = (int)(signed char)dis->readByte(); - xRot = (int)(signed char)dis->readByte(); + xa = (int)dis->readByte(); + ya = (int)dis->readByte(); + za = (int)dis->readByte(); + yRot = (int)dis->readByte(); + xRot = (int)dis->readByte(); } -void MoveEntityPacket::PosRot::write(DataOutputStream *dos) //throws IOException +void MoveEntityPacket::PosRot::write(DataOutputStream *dos) //throws IOException { MoveEntityPacket::write(dos); - dos->writeByte((uint8_t)(xa & 0xFF)); - dos->writeByte((uint8_t)(ya & 0xFF)); - dos->writeByte((uint8_t)(za & 0xFF)); - dos->writeByte((uint8_t)(yRot & 0xFF)); - dos->writeByte((uint8_t)(xRot & 0xFF)); + dos->writeByte((uint8_t)xa); + dos->writeByte((uint8_t)ya); + dos->writeByte((uint8_t)za); + dos->writeByte((uint8_t)yRot); + dos->writeByte((uint8_t)xRot); } -int MoveEntityPacket::PosRot::getEstimatedSize() +int MoveEntityPacket::PosRot::getEstimatedSize() { return 2+5; } -MoveEntityPacket::Pos::Pos() +MoveEntityPacket::Pos::Pos() { } @@ -115,20 +115,20 @@ MoveEntityPacket::Pos::Pos(int id, char xa, char ya, char za) : MoveEntityPacket this->za = za; } -void MoveEntityPacket::Pos::read(DataInputStream *dis) //throws IOException +void MoveEntityPacket::Pos::read(DataInputStream *dis) //throws IOException { MoveEntityPacket::read(dis); - xa = (int)(signed char)dis->readByte(); - ya = (int)(signed char)dis->readByte(); - za = (int)(signed char)dis->readByte(); + xa = (int)dis->readByte(); + ya = (int)dis->readByte(); + za = (int)dis->readByte(); } void MoveEntityPacket::Pos::write(DataOutputStream *dos) //throws IOException { MoveEntityPacket::write(dos); - dos->writeByte((uint8_t)(xa & 0xFF)); - dos->writeByte((uint8_t)(ya & 0xFF)); - dos->writeByte((uint8_t)(za & 0xFF)); + dos->writeByte((uint8_t)xa); + dos->writeByte((uint8_t)ya); + dos->writeByte((uint8_t)za); } int MoveEntityPacket::Pos::getEstimatedSize() @@ -136,7 +136,7 @@ int MoveEntityPacket::Pos::getEstimatedSize() return 2+3; } -MoveEntityPacket::Rot::Rot() +MoveEntityPacket::Rot::Rot() { hasRot = true; } @@ -148,18 +148,18 @@ MoveEntityPacket::Rot::Rot(int id, char yRot, char xRot) : MoveEntityPacket(id) hasRot = true; } -void MoveEntityPacket::Rot::read(DataInputStream *dis) //throws IOException +void MoveEntityPacket::Rot::read(DataInputStream *dis) //throws IOException { MoveEntityPacket::read(dis); - yRot = (int)(signed char)dis->readByte(); - xRot = (int)(signed char)dis->readByte(); + yRot = (int)dis->readByte(); + xRot = (int)dis->readByte(); } -void MoveEntityPacket::Rot::write(DataOutputStream *dos) //throws IOException +void MoveEntityPacket::Rot::write(DataOutputStream *dos) //throws IOException { MoveEntityPacket::write(dos); - dos->writeByte((uint8_t)(yRot & 0xFF)); - dos->writeByte((uint8_t)(xRot & 0xFF)); + dos->writeByte((uint8_t)yRot); + dos->writeByte((uint8_t)xRot); } int MoveEntityPacket::Rot::getEstimatedSize() diff --git a/Minecraft.World/Network/Packets/MoveEntityPacketSmall.cpp b/Minecraft.World/Network/Packets/MoveEntityPacketSmall.cpp index 1c698f720..23cf4af51 100644 --- a/Minecraft.World/Network/Packets/MoveEntityPacketSmall.cpp +++ b/Minecraft.World/Network/Packets/MoveEntityPacketSmall.cpp @@ -19,19 +19,30 @@ MoveEntityPacketSmall::MoveEntityPacketSmall() MoveEntityPacketSmall::MoveEntityPacketSmall(int id) { + if( (id < 0 ) || (id >= 2048 ) ) + { + // We shouln't be tracking an entity that doesn't have a short type of id + __debugbreak(); + } + this->id = id; hasRot = false; - xa = ya = za = yRot = xRot = 0; + + xa = 0; + ya = 0; + za = 0; + yRot = 0; + xRot = 0; } -void MoveEntityPacketSmall::read(DataInputStream *dis) //throws IOException +void MoveEntityPacketSmall::read(DataInputStream *dis) //throws IOException { id = dis->readShort(); } void MoveEntityPacketSmall::write(DataOutputStream *dos) //throws IOException { - if(id < 0 || id > 32767 ) + if( (id < 0 ) || (id >= 2048 ) ) { // We shouln't be tracking an entity that doesn't have a short type of id __debugbreak(); @@ -44,7 +55,7 @@ void MoveEntityPacketSmall::handle(PacketListener *listener) listener->handleMoveEntitySmall(shared_from_this()); } -int MoveEntityPacketSmall::getEstimatedSize() +int MoveEntityPacketSmall::getEstimatedSize() { return 2; } @@ -75,32 +86,36 @@ MoveEntityPacketSmall::PosRot::PosRot(int id, char xa, char ya, char za, char yR hasRot = true; } -void MoveEntityPacketSmall::PosRot::read(DataInputStream *dis) //throws IOException +void MoveEntityPacketSmall::PosRot::read(DataInputStream *dis) //throws IOException { - MoveEntityPacketSmall::read(dis); - xa = (signed char)dis->readByte(); - ya = (signed char)dis->readByte(); - za = (signed char)dis->readByte(); - yRot = (signed char)dis->readByte(); - xRot = (signed char)dis->readByte(); + 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; } -void MoveEntityPacketSmall::PosRot::write(DataOutputStream *dos) //throws IOException +void MoveEntityPacketSmall::PosRot::write(DataOutputStream *dos) //throws IOException { - MoveEntityPacketSmall::write(dos); - dos->writeByte((uint8_t)(xa & 0xFF)); - dos->writeByte((uint8_t)(ya & 0xFF)); - dos->writeByte((uint8_t)(za & 0xFF)); - dos->writeByte((uint8_t)(yRot & 0xFF)); - dos->writeByte((uint8_t)(xRot & 0xFF)); + if( (id < 0 ) || (id >= 2048 ) ) + { + // We shouln't be tracking an entity that doesn't have a short type of id + __debugbreak(); + } + short idAndRot = id | yRot << 11; + dos->writeShort(idAndRot); + short xAndYAndZ = ( xa << 11 ) | ( ( ya & 0x3f ) << 5 ) | ( za & 0x1f ); + dos->writeShort(xAndYAndZ); } -int MoveEntityPacketSmall::PosRot::getEstimatedSize() +int MoveEntityPacketSmall::PosRot::getEstimatedSize() { - return 2+5; + return 4; } -MoveEntityPacketSmall::Pos::Pos() +MoveEntityPacketSmall::Pos::Pos() { } @@ -111,28 +126,35 @@ MoveEntityPacketSmall::Pos::Pos(int id, char xa, char ya, char za) : MoveEntityP this->za = za; } -void MoveEntityPacketSmall::Pos::read(DataInputStream *dis) //throws IOException +void MoveEntityPacketSmall::Pos::read(DataInputStream *dis) //throws IOException { - MoveEntityPacketSmall::read(dis); - xa = (signed char)dis->readByte(); - ya = (signed char)dis->readByte(); - za = (signed char)dis->readByte(); + int idAndY = dis->readShort(); + this->id = idAndY & 0x07ff; + this->ya = idAndY >> 11; + int XandZ = (int)((signed char)(dis->readByte())); + xa = XandZ >> 4; + za = ( XandZ << 28 ) >> 28; } void MoveEntityPacketSmall::Pos::write(DataOutputStream *dos) //throws IOException { - MoveEntityPacketSmall::write(dos); - dos->writeByte((uint8_t)(xa & 0xFF)); - dos->writeByte((uint8_t)(ya & 0xFF)); - dos->writeByte((uint8_t)(za & 0xFF)); + if( (id < 0 ) || (id >= 2048 ) ) + { + // We shouln't be tracking an entity that doesn't have a short type of id + __debugbreak(); + } + short idAndY = id | ya << 11; + dos->writeShort(idAndY); + char XandZ = ( xa << 4 ) | ( za & 0x0f ); + dos->writeByte((uint8_t)XandZ); } int MoveEntityPacketSmall::Pos::getEstimatedSize() { - return 2+3; + return 3; } -MoveEntityPacketSmall::Rot::Rot() +MoveEntityPacketSmall::Rot::Rot() { hasRot = true; } @@ -145,19 +167,25 @@ MoveEntityPacketSmall::Rot::Rot(int id, char yRot, char xRot) : MoveEntityPacket hasRot = true; } -void MoveEntityPacketSmall::Rot::read(DataInputStream *dis) //throws IOException +void MoveEntityPacketSmall::Rot::read(DataInputStream *dis) //throws IOException { - MoveEntityPacketSmall::read(dis); - yRot = (signed char)dis->readByte(); + int idAndRot = (int)dis->readShort(); + this->id = idAndRot & 0x07ff; + this->yRot = idAndRot >> 11; } -void MoveEntityPacketSmall::Rot::write(DataOutputStream *dos) //throws IOException +void MoveEntityPacketSmall::Rot::write(DataOutputStream *dos) //throws IOException { - MoveEntityPacketSmall::write(dos); - dos->writeByte((uint8_t)(yRot & 0xFF)); + if( (id < 0 ) || (id >= 2048 ) ) + { + // We shouln't be tracking an entity that doesn't have a short type of id + __debugbreak(); + } + short idAndRot = id | yRot << 11; + dos->writeShort(idAndRot); } int MoveEntityPacketSmall::Rot::getEstimatedSize() { - return 2+1; + return 2; } diff --git a/Minecraft.World/Network/Packets/SetEntityMotionPacket.cpp b/Minecraft.World/Network/Packets/SetEntityMotionPacket.cpp index 70ea64f92..cb6d2a42c 100644 --- a/Minecraft.World/Network/Packets/SetEntityMotionPacket.cpp +++ b/Minecraft.World/Network/Packets/SetEntityMotionPacket.cpp @@ -20,11 +20,19 @@ void SetEntityMotionPacket::_init(int id, double xd, double yd, double zd) xa = (int) (xd * 8000.0); ya = (int) (yd * 8000.0); za = (int) (zd * 8000.0); - - useBytes = false; + // 4J - if we could transmit this as bytes (in 1/16 accuracy) then flag to do so + if( ( xa >= (-128 * 16 ) ) && ( ya >= (-128 * 16 ) ) && ( za >= (-128 * 16 ) ) && + ( xa < (128 * 16 ) ) && ( ya < (128 * 16 ) ) && ( za < (128 * 16 ) ) ) + { + useBytes = true; + } + else + { + useBytes = false; + } } -SetEntityMotionPacket::SetEntityMotionPacket() +SetEntityMotionPacket::SetEntityMotionPacket() { _init(0, 0.0f, 0.0f, 0.0f); } @@ -36,27 +44,53 @@ SetEntityMotionPacket::SetEntityMotionPacket(std::shared_ptr e) SetEntityMotionPacket::SetEntityMotionPacket(int id, double xd, double yd, double zd) { - _init(id, xd, yd, zd); + _init(id, xd, yd, zd); } -void SetEntityMotionPacket::read(DataInputStream *dis) //throws IOException +void SetEntityMotionPacket::read(DataInputStream *dis) //throws IOException { - id = dis->readShort(); - - xa = dis->readShort(); - ya = dis->readShort(); - za = dis->readShort(); - - useBytes = false; + short idAndFlag = dis->readShort(); + id = idAndFlag & 0x07ff; + if( idAndFlag & 0x0800 ) + { + xa = (int)dis->readByte(); + ya = (int)dis->readByte(); + za = (int)dis->readByte(); + xa = ( xa << 24 ) >> 24; + ya = ( ya << 24 ) >> 24; + za = ( za << 24 ) >> 24; + 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->writeShort(id); - - dos->writeShort(xa); - dos->writeShort(ya); - dos->writeShort(za); + if( useBytes ) + { + // Masking the id to 11 bits before writing to account for large entitty ids. + dos->writeShort((id & 0x07FF) | 0x800); + dos->writeByte(xa/16); + dos->writeByte(ya/16); + dos->writeByte(za/16); + } + else + { + // same thing as line 80 here + dos->writeShort((id & 0x07FF)); + dos->writeShort(xa); + dos->writeShort(ya); + dos->writeShort(za); + } } void SetEntityMotionPacket::handle(PacketListener *listener) @@ -66,7 +100,7 @@ void SetEntityMotionPacket::handle(PacketListener *listener) int SetEntityMotionPacket::getEstimatedSize() { - return 8; + return useBytes ? 5 : 8; } bool SetEntityMotionPacket::canBeInvalidated() diff --git a/Minecraft.World/Network/Packets/TeleportEntityPacket.cpp b/Minecraft.World/Network/Packets/TeleportEntityPacket.cpp index 3a0c9927f..fcd88af71 100644 --- a/Minecraft.World/Network/Packets/TeleportEntityPacket.cpp +++ b/Minecraft.World/Network/Packets/TeleportEntityPacket.cpp @@ -55,10 +55,16 @@ void TeleportEntityPacket::read(DataInputStream *dis) //throws IOException void TeleportEntityPacket::write(DataOutputStream *dos) //throws IOException { - dos->writeShort((short)id); + dos->writeShort(id); +#ifdef _LARGE_WORLDS dos->writeInt(x); dos->writeInt(y); dos->writeInt(z); +#else + dos->writeShort(x); + dos->writeShort(y); + dos->writeShort(z); +#endif dos->write(yRot); dos->write(xRot); } From e36e58609d8fb163c158b651380e20fea4712fa8 Mon Sep 17 00:00:00 2001 From: MathiewMay Date: Wed, 11 Mar 2026 08:42:19 -0400 Subject: [PATCH 4/7] Reverted the SetEntityMotionPacket.cpp changes --- Minecraft.World/Network/Packets/SetEntityMotionPacket.cpp | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/Minecraft.World/Network/Packets/SetEntityMotionPacket.cpp b/Minecraft.World/Network/Packets/SetEntityMotionPacket.cpp index cb6d2a42c..bff87c5bd 100644 --- a/Minecraft.World/Network/Packets/SetEntityMotionPacket.cpp +++ b/Minecraft.World/Network/Packets/SetEntityMotionPacket.cpp @@ -73,20 +73,18 @@ void SetEntityMotionPacket::read(DataInputStream *dis) //throws IOException } } -void SetEntityMotionPacket::write(DataOutputStream *dos) //throws IOException +void SetEntityMotionPacket::write(DataOutputStream *dos) //throws IOException { if( useBytes ) { - // Masking the id to 11 bits before writing to account for large entitty ids. - dos->writeShort((id & 0x07FF) | 0x800); + dos->writeShort(id | 0x800); dos->writeByte(xa/16); dos->writeByte(ya/16); dos->writeByte(za/16); } else { - // same thing as line 80 here - dos->writeShort((id & 0x07FF)); + dos->writeShort(id); dos->writeShort(xa); dos->writeShort(ya); dos->writeShort(za); From 026cb24d6667a6a39e2fbc7a4c74207a38cf0742 Mon Sep 17 00:00:00 2001 From: MathiewMay Date: Wed, 11 Mar 2026 13:29:14 -0400 Subject: [PATCH 5/7] Revert "Fix entity ID TLS key recreating on every call of getSmallid() for linux, this caused a ID pool corruption." This reverts commit 047615dbbe40e0d82e202f8a9483471aa54c62cd. --- Minecraft.World/Entities/Entity.cpp | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/Minecraft.World/Entities/Entity.cpp b/Minecraft.World/Entities/Entity.cpp index 0a62c9b3e..9c88a00e2 100644 --- a/Minecraft.World/Entities/Entity.cpp +++ b/Minecraft.World/Entities/Entity.cpp @@ -24,11 +24,7 @@ int Entity::entityCounter = 2048; // 4J - changed initialiser to 2048, as we are using range 0 - 2047 as special unique smaller ids for things that need network tracked -#ifdef _WIN32 DWORD Entity::tlsIdx = TlsAlloc(); -#else -pthread_key_t Entity::tlsIdx = 0; -#endif // 4J - added getSmallId & freeSmallId methods unsigned int Entity::entityIdUsedFlags[2048/32] = {0}; @@ -51,6 +47,7 @@ int Entity::getSmallId() #ifdef _WIN32 if( ((size_t)TlsGetValue(tlsIdx) != 0 ) ) #else + pthread_key_create(&tlsIdx, nullptr); if ( ((size_t)pthread_getspecific(tlsIdx) != 0) ) #endif { @@ -154,16 +151,7 @@ void Entity::freeSmallId(int index) void Entity::useSmallIds() { -#ifdef _WIN32 - TlsSetValue(tlsIdx, (LPVOID)1); -#else - static bool keyCreated = false; - if (!keyCreated) { - pthread_key_create(&tlsIdx, nullptr); - keyCreated = true; - } - pthread_setspecific(tlsIdx,(void*)1); -#endif + pthread_setspecific(tlsIdx,(LPVOID)1); } // Things also added here to be able to manage the concept of a number of extra "wandering" entities - normally path finding entities aren't allowed to From 96e3832b22d8f9bb1f112b08004f0667264d1b2b Mon Sep 17 00:00:00 2001 From: MathiewMay Date: Wed, 11 Mar 2026 13:36:49 -0400 Subject: [PATCH 6/7] Reverted the SetEntityMotionPacket.cpp changes --- Minecraft.World/Entities/Entity.cpp | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/Minecraft.World/Entities/Entity.cpp b/Minecraft.World/Entities/Entity.cpp index 9c88a00e2..30a39142a 100644 --- a/Minecraft.World/Entities/Entity.cpp +++ b/Minecraft.World/Entities/Entity.cpp @@ -44,12 +44,9 @@ int Entity::getSmallId() // for final notification to the client that the entities are removed. We can't go re-using these small Ids yet, as otherwise we will // potentially end up telling the client that the entity has been removed After we have already re-used its Id and created a new entity. // This ends up with newly created client-side entities being removed by accident, causing invisible mobs. -#ifdef _WIN32 - if( ((size_t)TlsGetValue(tlsIdx) != 0 ) ) -#else - pthread_key_create(&tlsIdx, nullptr); + + //4jcraft - i have no idea what 4j was cooking here ngl if ( ((size_t)pthread_getspecific(tlsIdx) != 0) ) -#endif { MinecraftServer *server = MinecraftServer::getInstance(); if( server ) From 5fee035383077b37966cce4e1d6fbbb638149eaa Mon Sep 17 00:00:00 2001 From: Mathiew May <79875811+MathiewMay@users.noreply.github.com> Date: Wed, 11 Mar 2026 15:08:12 -0400 Subject: [PATCH 7/7] Removed a unreleated comment. --- Minecraft.Client/Rendering/GameRenderer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Minecraft.Client/Rendering/GameRenderer.cpp b/Minecraft.Client/Rendering/GameRenderer.cpp index 6bda6429e..6aae7c93e 100644 --- a/Minecraft.Client/Rendering/GameRenderer.cpp +++ b/Minecraft.Client/Rendering/GameRenderer.cpp @@ -1407,7 +1407,7 @@ void GameRenderer::renderLevel(float a, __int64 until) #endif PIXEndNamedEvent(); PIXBeginNamedEvent(0,"Particle render"); - //turnOnLightLayer(a); // 4J - brought forward from 1.8.2 + turnOnLightLayer(a); // 4J - brought forward from 1.8.2 particleEngine->renderLit(cameraEntity, a); Lighting::turnOff(); setupFog(0, a);