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 commit is contained in:
MathiewMay 2026-03-09 21:45:08 -04:00
parent 9c254bbf4d
commit dac0e883b6
4 changed files with 94 additions and 162 deletions

View file

@ -71,32 +71,32 @@ MoveEntityPacket::PosRot::PosRot()
MoveEntityPacket::PosRot::PosRot(int id, char xa, char ya, char za, char yRot, char xRot) : MoveEntityPacket( id ) MoveEntityPacket::PosRot::PosRot(int id, char xa, char ya, char za, char yRot, char xRot) : MoveEntityPacket( id )
{ {
this->xa = xa; this->xa = (int)(signed char)xa;
this->ya = ya; this->ya = (int)(signed char)ya;
this->za = za; this->za = (int)(signed char)za;
this->yRot = yRot; this->yRot = (int)(signed char)yRot;
this->xRot = xRot; this->xRot = (int)(signed char)xRot;
hasRot = true; hasRot = true;
} }
void MoveEntityPacket::PosRot::read(DataInputStream *dis) //throws IOException void MoveEntityPacket::PosRot::read(DataInputStream *dis) //throws IOException
{ {
MoveEntityPacket::read(dis); MoveEntityPacket::read(dis);
xa = (int)dis->readByte(); xa = (int)(signed char)dis->readByte();
ya = (int)dis->readByte(); ya = (int)(signed char)dis->readByte();
za = (int)dis->readByte(); za = (int)(signed char)dis->readByte();
yRot = (int)dis->readByte(); yRot = (int)(signed char)dis->readByte();
xRot = (int)dis->readByte(); xRot = (int)(signed char)dis->readByte();
} }
void MoveEntityPacket::PosRot::write(DataOutputStream *dos) //throws IOException void MoveEntityPacket::PosRot::write(DataOutputStream *dos) //throws IOException
{ {
MoveEntityPacket::write(dos); MoveEntityPacket::write(dos);
dos->writeByte((uint8_t)xa); dos->writeByte((uint8_t)(xa & 0xFF));
dos->writeByte((uint8_t)ya); dos->writeByte((uint8_t)(ya & 0xFF));
dos->writeByte((uint8_t)za); dos->writeByte((uint8_t)(za & 0xFF));
dos->writeByte((uint8_t)yRot); dos->writeByte((uint8_t)(yRot & 0xFF));
dos->writeByte((uint8_t)xRot); dos->writeByte((uint8_t)(xRot & 0xFF));
} }
int MoveEntityPacket::PosRot::getEstimatedSize() int MoveEntityPacket::PosRot::getEstimatedSize()
@ -118,17 +118,17 @@ MoveEntityPacket::Pos::Pos(int id, char xa, char ya, char za) : MoveEntityPacket
void MoveEntityPacket::Pos::read(DataInputStream *dis) //throws IOException void MoveEntityPacket::Pos::read(DataInputStream *dis) //throws IOException
{ {
MoveEntityPacket::read(dis); MoveEntityPacket::read(dis);
xa = (int)dis->readByte(); xa = (int)(signed char)dis->readByte();
ya = (int)dis->readByte(); ya = (int)(signed char)dis->readByte();
za = (int)dis->readByte(); za = (int)(signed char)dis->readByte();
} }
void MoveEntityPacket::Pos::write(DataOutputStream *dos) //throws IOException void MoveEntityPacket::Pos::write(DataOutputStream *dos) //throws IOException
{ {
MoveEntityPacket::write(dos); MoveEntityPacket::write(dos);
dos->writeByte((uint8_t)xa); dos->writeByte((uint8_t)(xa & 0xFF));
dos->writeByte((uint8_t)ya); dos->writeByte((uint8_t)(ya & 0xFF));
dos->writeByte((uint8_t)za); dos->writeByte((uint8_t)(za & 0xFF));
} }
int MoveEntityPacket::Pos::getEstimatedSize() int MoveEntityPacket::Pos::getEstimatedSize()
@ -151,15 +151,15 @@ MoveEntityPacket::Rot::Rot(int id, char yRot, char xRot) : MoveEntityPacket(id)
void MoveEntityPacket::Rot::read(DataInputStream *dis) //throws IOException void MoveEntityPacket::Rot::read(DataInputStream *dis) //throws IOException
{ {
MoveEntityPacket::read(dis); MoveEntityPacket::read(dis);
yRot = (int)dis->readByte(); yRot = (int)(signed char)dis->readByte();
xRot = (int)dis->readByte(); xRot = (int)(signed char)dis->readByte();
} }
void MoveEntityPacket::Rot::write(DataOutputStream *dos) //throws IOException void MoveEntityPacket::Rot::write(DataOutputStream *dos) //throws IOException
{ {
MoveEntityPacket::write(dos); MoveEntityPacket::write(dos);
dos->writeByte((uint8_t)yRot); dos->writeByte((uint8_t)(yRot & 0xFF));
dos->writeByte((uint8_t)xRot); dos->writeByte((uint8_t)(xRot & 0xFF));
} }
int MoveEntityPacket::Rot::getEstimatedSize() int MoveEntityPacket::Rot::getEstimatedSize()

View file

@ -19,20 +19,9 @@ MoveEntityPacketSmall::MoveEntityPacketSmall()
MoveEntityPacketSmall::MoveEntityPacketSmall(int id) 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; this->id = id;
hasRot = false; 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
@ -42,7 +31,7 @@ void MoveEntityPacketSmall::read(DataInputStream *dis) //throws IOException
void MoveEntityPacketSmall::write(DataOutputStream *dos) //throws IOException void MoveEntityPacketSmall::write(DataOutputStream *dos) //throws IOException
{ {
if( (id < 0 ) || (id >= 2048 ) ) if(id < 0 || id > 32767 )
{ {
// We shouln't be tracking an entity that doesn't have a short type of id // We shouln't be tracking an entity that doesn't have a short type of id
__debugbreak(); __debugbreak();
@ -88,31 +77,27 @@ MoveEntityPacketSmall::PosRot::PosRot(int id, char xa, char ya, char za, char yR
void MoveEntityPacketSmall::PosRot::read(DataInputStream *dis) //throws IOException void MoveEntityPacketSmall::PosRot::read(DataInputStream *dis) //throws IOException
{ {
int idAndRot = dis->readShort(); MoveEntityPacketSmall::read(dis);
this->id = idAndRot & 0x07ff; xa = (signed char)dis->readByte();
this->yRot = idAndRot >> 11; ya = (signed char)dis->readByte();
int xAndYAndZ = (int)dis->readShort(); za = (signed char)dis->readByte();
this->xa = xAndYAndZ >> 11; yRot = (signed char)dis->readByte();
this->ya = (xAndYAndZ << 21 ) >> 26; xRot = (signed char)dis->readByte();
this->za = (xAndYAndZ << 27 ) >> 27;
} }
void MoveEntityPacketSmall::PosRot::write(DataOutputStream *dos) //throws IOException void MoveEntityPacketSmall::PosRot::write(DataOutputStream *dos) //throws IOException
{ {
if( (id < 0 ) || (id >= 2048 ) ) MoveEntityPacketSmall::write(dos);
{ dos->writeByte((uint8_t)(xa & 0xFF));
// We shouln't be tracking an entity that doesn't have a short type of id dos->writeByte((uint8_t)(ya & 0xFF));
__debugbreak(); dos->writeByte((uint8_t)(za & 0xFF));
} dos->writeByte((uint8_t)(yRot & 0xFF));
short idAndRot = id | yRot << 11; dos->writeByte((uint8_t)(xRot & 0xFF));
dos->writeShort(idAndRot);
short xAndYAndZ = ( xa << 11 ) | ( ( ya & 0x3f ) << 5 ) | ( za & 0x1f );
dos->writeShort(xAndYAndZ);
} }
int MoveEntityPacketSmall::PosRot::getEstimatedSize() int MoveEntityPacketSmall::PosRot::getEstimatedSize()
{ {
return 4; return 2+5;
} }
MoveEntityPacketSmall::Pos::Pos() MoveEntityPacketSmall::Pos::Pos()
@ -128,30 +113,23 @@ MoveEntityPacketSmall::Pos::Pos(int id, char xa, char ya, char za) : MoveEntityP
void MoveEntityPacketSmall::Pos::read(DataInputStream *dis) //throws IOException void MoveEntityPacketSmall::Pos::read(DataInputStream *dis) //throws IOException
{ {
int idAndY = dis->readShort(); MoveEntityPacketSmall::read(dis);
this->id = idAndY & 0x07ff; xa = (signed char)dis->readByte();
this->ya = idAndY >> 11; ya = (signed char)dis->readByte();
int XandZ = (int)((signed char)(dis->readByte())); za = (signed char)dis->readByte();
xa = XandZ >> 4;
za = ( XandZ << 28 ) >> 28;
} }
void MoveEntityPacketSmall::Pos::write(DataOutputStream *dos) //throws IOException void MoveEntityPacketSmall::Pos::write(DataOutputStream *dos) //throws IOException
{ {
if( (id < 0 ) || (id >= 2048 ) ) MoveEntityPacketSmall::write(dos);
{ dos->writeByte((uint8_t)(xa & 0xFF));
// We shouln't be tracking an entity that doesn't have a short type of id dos->writeByte((uint8_t)(ya & 0xFF));
__debugbreak(); dos->writeByte((uint8_t)(za & 0xFF));
}
short idAndY = id | ya << 11;
dos->writeShort(idAndY);
char XandZ = ( xa << 4 ) | ( za & 0x0f );
dos->writeByte((uint8_t)XandZ);
} }
int MoveEntityPacketSmall::Pos::getEstimatedSize() int MoveEntityPacketSmall::Pos::getEstimatedSize()
{ {
return 3; return 2+3;
} }
MoveEntityPacketSmall::Rot::Rot() MoveEntityPacketSmall::Rot::Rot()
@ -169,23 +147,17 @@ MoveEntityPacketSmall::Rot::Rot(int id, char yRot, char xRot) : MoveEntityPacket
void MoveEntityPacketSmall::Rot::read(DataInputStream *dis) //throws IOException void MoveEntityPacketSmall::Rot::read(DataInputStream *dis) //throws IOException
{ {
int idAndRot = (int)dis->readShort(); MoveEntityPacketSmall::read(dis);
this->id = idAndRot & 0x07ff; yRot = (signed char)dis->readByte();
this->yRot = idAndRot >> 11;
} }
void MoveEntityPacketSmall::Rot::write(DataOutputStream *dos) //throws IOException void MoveEntityPacketSmall::Rot::write(DataOutputStream *dos) //throws IOException
{ {
if( (id < 0 ) || (id >= 2048 ) ) MoveEntityPacketSmall::write(dos);
{ dos->writeByte((uint8_t)(yRot & 0xFF));
// 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() int MoveEntityPacketSmall::Rot::getEstimatedSize()
{ {
return 2; return 2+1;
} }

View file

@ -20,16 +20,8 @@ void SetEntityMotionPacket::_init(int id, double xd, double yd, double zd)
xa = (int) (xd * 8000.0); xa = (int) (xd * 8000.0);
ya = (int) (yd * 8000.0); ya = (int) (yd * 8000.0);
za = (int) (zd * 8000.0); za = (int) (zd * 8000.0);
// 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 ) ) && useBytes = false;
( xa < (128 * 16 ) ) && ( ya < (128 * 16 ) ) && ( za < (128 * 16 ) ) )
{
useBytes = true;
}
else
{
useBytes = false;
}
} }
SetEntityMotionPacket::SetEntityMotionPacket() SetEntityMotionPacket::SetEntityMotionPacket()
@ -49,48 +41,22 @@ SetEntityMotionPacket::SetEntityMotionPacket(int id, double xd, double yd, doubl
void SetEntityMotionPacket::read(DataInputStream *dis) //throws IOException void SetEntityMotionPacket::read(DataInputStream *dis) //throws IOException
{ {
short idAndFlag = dis->readShort(); id = dis->readShort();
id = idAndFlag & 0x07ff;
if( idAndFlag & 0x0800 ) xa = dis->readShort();
{ ya = dis->readShort();
xa = (int)dis->readByte(); za = dis->readShort();
ya = (int)dis->readByte();
za = (int)dis->readByte(); useBytes = false;
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 void SetEntityMotionPacket::write(DataOutputStream *dos) //throws IOException
{ {
if( useBytes ) dos->writeShort(id);
{
// Masking the id to 11 bits before writing to account for large entitty ids. dos->writeShort(xa);
dos->writeShort((id & 0x07FF) | 0x800); dos->writeShort(ya);
dos->writeByte(xa/16); dos->writeShort(za);
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) void SetEntityMotionPacket::handle(PacketListener *listener)
@ -100,7 +66,7 @@ void SetEntityMotionPacket::handle(PacketListener *listener)
int SetEntityMotionPacket::getEstimatedSize() int SetEntityMotionPacket::getEstimatedSize()
{ {
return useBytes ? 5 : 8; return 8;
} }
bool SetEntityMotionPacket::canBeInvalidated() bool SetEntityMotionPacket::canBeInvalidated()

View file

@ -55,16 +55,10 @@ void TeleportEntityPacket::read(DataInputStream *dis) //throws IOException
void TeleportEntityPacket::write(DataOutputStream *dos) //throws IOException void TeleportEntityPacket::write(DataOutputStream *dos) //throws IOException
{ {
dos->writeShort(id); dos->writeShort((short)id);
#ifdef _LARGE_WORLDS
dos->writeInt(x); dos->writeInt(x);
dos->writeInt(y); dos->writeInt(y);
dos->writeInt(z); dos->writeInt(z);
#else
dos->writeShort(x);
dos->writeShort(y);
dos->writeShort(z);
#endif
dos->write(yRot); dos->write(yRot);
dos->write(xRot); dos->write(xRot);
} }