From a4dbb5c58270959884c17d720185da06464fa256 Mon Sep 17 00:00:00 2001 From: peterbell10 Date: Wed, 2 May 2018 08:50:36 +0100 Subject: Prefer static_cast to reinterpret_cast (#4223) * Change reinterpret_cast -> static_cast wherever possible * Remove more unnecessary `const_cast`s. reinterpret_casts should be avoided for the same reason as c-style casts - they don't do any type-checking. reinterpret_cast was mainly being used for down-casting in inheritance hierarchies but static_cast works just as well while also making sure that there is actually an inheritance relationship there. --- src/Protocol/Protocol_1_8.cpp | 72 +++++++++++++++++++++---------------------- 1 file changed, 36 insertions(+), 36 deletions(-) (limited to 'src/Protocol/Protocol_1_8.cpp') diff --git a/src/Protocol/Protocol_1_8.cpp b/src/Protocol/Protocol_1_8.cpp index 41e168c95..58f067278 100644 --- a/src/Protocol/Protocol_1_8.cpp +++ b/src/Protocol/Protocol_1_8.cpp @@ -2842,7 +2842,7 @@ void cProtocol_1_8_0::SendData(const char * a_Data, size_t a_Size) while (a_Size > 0) { size_t NumBytes = (a_Size > sizeof(Encrypted)) ? sizeof(Encrypted) : a_Size; - m_Encryptor.ProcessData(Encrypted, reinterpret_cast(const_cast(a_Data)), NumBytes); + m_Encryptor.ProcessData(Encrypted, reinterpret_cast(a_Data), NumBytes); m_Client->SendData(reinterpret_cast(Encrypted), NumBytes); a_Size -= NumBytes; a_Data += NumBytes; @@ -3161,7 +3161,7 @@ void cProtocol_1_8_0::WriteBlockEntity(cPacketizer & a_Pkt, const cBlockEntity & { case E_BLOCK_BEACON: { - auto & BeaconEntity = reinterpret_cast(a_BlockEntity); + auto & BeaconEntity = static_cast(a_BlockEntity); Writer.AddInt("x", BeaconEntity.GetPosX()); Writer.AddInt("y", BeaconEntity.GetPosY()); Writer.AddInt("z", BeaconEntity.GetPosZ()); @@ -3174,7 +3174,7 @@ void cProtocol_1_8_0::WriteBlockEntity(cPacketizer & a_Pkt, const cBlockEntity & case E_BLOCK_COMMAND_BLOCK: { - auto & CommandBlockEntity = reinterpret_cast(a_BlockEntity); + auto & CommandBlockEntity = static_cast(a_BlockEntity); Writer.AddByte("TrackOutput", 1); // Neither I nor the MC wiki has any idea about this Writer.AddInt("SuccessCount", CommandBlockEntity.GetResult()); Writer.AddInt("x", CommandBlockEntity.GetPosX()); @@ -3195,7 +3195,7 @@ void cProtocol_1_8_0::WriteBlockEntity(cPacketizer & a_Pkt, const cBlockEntity & case E_BLOCK_HEAD: { - auto & MobHeadEntity = reinterpret_cast(a_BlockEntity); + auto & MobHeadEntity = static_cast(a_BlockEntity); Writer.AddInt("x", MobHeadEntity.GetPosX()); Writer.AddInt("y", MobHeadEntity.GetPosY()); Writer.AddInt("z", MobHeadEntity.GetPosZ()); @@ -3221,7 +3221,7 @@ void cProtocol_1_8_0::WriteBlockEntity(cPacketizer & a_Pkt, const cBlockEntity & case E_BLOCK_FLOWER_POT: { - auto & FlowerPotEntity = reinterpret_cast(a_BlockEntity); + auto & FlowerPotEntity = static_cast(a_BlockEntity); Writer.AddInt("x", FlowerPotEntity.GetPosX()); Writer.AddInt("y", FlowerPotEntity.GetPosY()); Writer.AddInt("z", FlowerPotEntity.GetPosZ()); @@ -3233,7 +3233,7 @@ void cProtocol_1_8_0::WriteBlockEntity(cPacketizer & a_Pkt, const cBlockEntity & case E_BLOCK_MOB_SPAWNER: { - auto & MobSpawnerEntity = reinterpret_cast(a_BlockEntity); + auto & MobSpawnerEntity = static_cast(a_BlockEntity); Writer.AddInt("x", MobSpawnerEntity.GetPosX()); Writer.AddInt("y", MobSpawnerEntity.GetPosY()); Writer.AddInt("z", MobSpawnerEntity.GetPosZ()); @@ -3288,7 +3288,7 @@ void cProtocol_1_8_0::WriteEntityMetadata(cPacketizer & a_Pkt, const cEntity & a { case cEntity::etPlayer: { - auto & Player = reinterpret_cast(a_Entity); + auto & Player = static_cast(a_Entity); // Player health (not handled since players aren't monsters) a_Pkt.WriteBEUInt8(0x66); @@ -3303,7 +3303,7 @@ void cProtocol_1_8_0::WriteEntityMetadata(cPacketizer & a_Pkt, const cEntity & a case cEntity::etPickup: { a_Pkt.WriteBEUInt8((5 << 5) | 10); // Slot(5) + index 10 - WriteItem(a_Pkt, reinterpret_cast(a_Entity).GetItem()); + WriteItem(a_Pkt, static_cast(a_Entity).GetItem()); break; } case cEntity::etMinecart: @@ -3317,7 +3317,7 @@ void cProtocol_1_8_0::WriteEntityMetadata(cPacketizer & a_Pkt, const cEntity & a Health: 3 | 3 - (3 - 3) = 3 Health: 1 | 3 - (1 - 3) = 5 */ - auto & Minecart = reinterpret_cast(a_Entity); + auto & Minecart = static_cast(a_Entity); a_Pkt.WriteBEInt32(static_cast((((a_Entity.GetMaxHealth() / 2) - (a_Entity.GetHealth() - (a_Entity.GetMaxHealth() / 2))) * Minecart.LastDamage()) * 4)); a_Pkt.WriteBEUInt8(0x52); a_Pkt.WriteBEInt32(1); // Shaking direction, doesn't seem to affect anything @@ -3326,7 +3326,7 @@ void cProtocol_1_8_0::WriteEntityMetadata(cPacketizer & a_Pkt, const cEntity & a if (Minecart.GetPayload() == cMinecart::mpNone) { - auto & RideableMinecart = reinterpret_cast(Minecart); + auto & RideableMinecart = static_cast(Minecart); const cItem & MinecartContent = RideableMinecart.GetContent(); if (!MinecartContent.IsEmpty()) { @@ -3343,26 +3343,26 @@ void cProtocol_1_8_0::WriteEntityMetadata(cPacketizer & a_Pkt, const cEntity & a else if (Minecart.GetPayload() == cMinecart::mpFurnace) { a_Pkt.WriteBEUInt8(0x10); - a_Pkt.WriteBEUInt8(reinterpret_cast(Minecart).IsFueled() ? 1 : 0); + a_Pkt.WriteBEUInt8(static_cast(Minecart).IsFueled() ? 1 : 0); } break; } // case etMinecart case cEntity::etProjectile: { - auto & Projectile = reinterpret_cast(a_Entity); + auto & Projectile = static_cast(a_Entity); switch (Projectile.GetProjectileKind()) { case cProjectileEntity::pkArrow: { a_Pkt.WriteBEUInt8(0x10); - a_Pkt.WriteBEUInt8(reinterpret_cast(Projectile).IsCritical() ? 1 : 0); + a_Pkt.WriteBEUInt8(static_cast(Projectile).IsCritical() ? 1 : 0); break; } case cProjectileEntity::pkFirework: { a_Pkt.WriteBEUInt8(0xa8); - WriteItem(a_Pkt, reinterpret_cast(Projectile).GetItem()); + WriteItem(a_Pkt, static_cast(Projectile).GetItem()); break; } default: @@ -3375,13 +3375,13 @@ void cProtocol_1_8_0::WriteEntityMetadata(cPacketizer & a_Pkt, const cEntity & a case cEntity::etMonster: { - WriteMobMetadata(a_Pkt, reinterpret_cast(a_Entity)); + WriteMobMetadata(a_Pkt, static_cast(a_Entity)); break; } case cEntity::etItemFrame: { - auto & Frame = reinterpret_cast(a_Entity); + auto & Frame = static_cast(a_Entity); a_Pkt.WriteBEUInt8(0xa8); WriteItem(a_Pkt, Frame.GetItem()); a_Pkt.WriteBEUInt8(0x09); @@ -3419,7 +3419,7 @@ void cProtocol_1_8_0::WriteMobMetadata(cPacketizer & a_Pkt, const cMonster & a_M { case mtBat: { - auto & Bat = reinterpret_cast(a_Mob); + auto & Bat = static_cast(a_Mob); a_Pkt.WriteBEUInt8(0x10); a_Pkt.WriteBEUInt8(Bat.IsHanging() ? 1 : 0); break; @@ -3427,7 +3427,7 @@ void cProtocol_1_8_0::WriteMobMetadata(cPacketizer & a_Pkt, const cMonster & a_M case mtChicken: { - auto & Chicken = reinterpret_cast(a_Mob); + auto & Chicken = static_cast(a_Mob); a_Pkt.WriteBEUInt8(0x0c); a_Pkt.WriteBEInt8(Chicken.IsBaby() ? -1 : (Chicken.IsInLoveCooldown() ? 1 : 0)); break; @@ -3435,7 +3435,7 @@ void cProtocol_1_8_0::WriteMobMetadata(cPacketizer & a_Pkt, const cMonster & a_M case mtCow: { - auto & Cow = reinterpret_cast(a_Mob); + auto & Cow = static_cast(a_Mob); a_Pkt.WriteBEUInt8(0x0c); a_Pkt.WriteBEInt8(Cow.IsBaby() ? -1 : (Cow.IsInLoveCooldown() ? 1 : 0)); break; @@ -3443,7 +3443,7 @@ void cProtocol_1_8_0::WriteMobMetadata(cPacketizer & a_Pkt, const cMonster & a_M case mtCreeper: { - auto & Creeper = reinterpret_cast(a_Mob); + auto & Creeper = static_cast(a_Mob); a_Pkt.WriteBEUInt8(0x10); a_Pkt.WriteBEUInt8(Creeper.IsBlowing() ? 1 : 255); a_Pkt.WriteBEUInt8(0x11); @@ -3453,7 +3453,7 @@ void cProtocol_1_8_0::WriteMobMetadata(cPacketizer & a_Pkt, const cMonster & a_M case mtEnderman: { - auto & Enderman = reinterpret_cast(a_Mob); + auto & Enderman = static_cast(a_Mob); a_Pkt.WriteBEUInt8(0x30); a_Pkt.WriteBEInt16(static_cast(Enderman.GetCarriedBlock())); a_Pkt.WriteBEUInt8(0x11); @@ -3465,7 +3465,7 @@ void cProtocol_1_8_0::WriteMobMetadata(cPacketizer & a_Pkt, const cMonster & a_M case mtGhast: { - auto & Ghast = reinterpret_cast(a_Mob); + auto & Ghast = static_cast(a_Mob); a_Pkt.WriteBEUInt8(0x10); a_Pkt.WriteBEUInt8(Ghast.IsCharging()); break; @@ -3473,7 +3473,7 @@ void cProtocol_1_8_0::WriteMobMetadata(cPacketizer & a_Pkt, const cMonster & a_M case mtHorse: { - auto & Horse = reinterpret_cast(a_Mob); + auto & Horse = static_cast(a_Mob); int Flags = 0; if (Horse.IsTame()) { @@ -3517,7 +3517,7 @@ void cProtocol_1_8_0::WriteMobMetadata(cPacketizer & a_Pkt, const cMonster & a_M case mtMagmaCube: { - auto & MagmaCube = reinterpret_cast(a_Mob); + auto & MagmaCube = static_cast(a_Mob); a_Pkt.WriteBEUInt8(0x10); a_Pkt.WriteBEUInt8(static_cast(MagmaCube.GetSize())); break; @@ -3525,7 +3525,7 @@ void cProtocol_1_8_0::WriteMobMetadata(cPacketizer & a_Pkt, const cMonster & a_M case mtOcelot: { - auto & Ocelot = reinterpret_cast(a_Mob); + auto & Ocelot = static_cast(a_Mob); a_Pkt.WriteBEUInt8(0x0c); a_Pkt.WriteBEInt8(Ocelot.IsBaby() ? -1 : (Ocelot.IsInLoveCooldown() ? 1 : 0)); break; @@ -3533,7 +3533,7 @@ void cProtocol_1_8_0::WriteMobMetadata(cPacketizer & a_Pkt, const cMonster & a_M case mtPig: { - auto & Pig = reinterpret_cast(a_Mob); + auto & Pig = static_cast(a_Mob); a_Pkt.WriteBEUInt8(0x0c); a_Pkt.WriteBEInt8(Pig.IsBaby() ? -1 : (Pig.IsInLoveCooldown() ? 1 : 0)); a_Pkt.WriteBEUInt8(0x10); @@ -3543,7 +3543,7 @@ void cProtocol_1_8_0::WriteMobMetadata(cPacketizer & a_Pkt, const cMonster & a_M case mtSheep: { - auto & Sheep = reinterpret_cast(a_Mob); + auto & Sheep = static_cast(a_Mob); a_Pkt.WriteBEUInt8(0x0c); a_Pkt.WriteBEInt8(Sheep.IsBaby() ? -1 : (Sheep.IsInLoveCooldown() ? 1 : 0)); @@ -3560,7 +3560,7 @@ void cProtocol_1_8_0::WriteMobMetadata(cPacketizer & a_Pkt, const cMonster & a_M case mtRabbit: { - auto & Rabbit = reinterpret_cast(a_Mob); + auto & Rabbit = static_cast(a_Mob); a_Pkt.WriteBEUInt8(0x12); a_Pkt.WriteBEUInt8(static_cast(Rabbit.GetRabbitType())); a_Pkt.WriteBEUInt8(0x0c); @@ -3570,7 +3570,7 @@ void cProtocol_1_8_0::WriteMobMetadata(cPacketizer & a_Pkt, const cMonster & a_M case mtSkeleton: { - auto & Skeleton = reinterpret_cast(a_Mob); + auto & Skeleton = static_cast(a_Mob); a_Pkt.WriteBEUInt8(0x0d); a_Pkt.WriteBEUInt8(Skeleton.IsWither() ? 1 : 0); break; @@ -3578,7 +3578,7 @@ void cProtocol_1_8_0::WriteMobMetadata(cPacketizer & a_Pkt, const cMonster & a_M case mtSlime: { - auto & Slime = reinterpret_cast(a_Mob); + auto & Slime = static_cast(a_Mob); a_Pkt.WriteBEUInt8(0x10); a_Pkt.WriteBEUInt8(static_cast(Slime.GetSize())); break; @@ -3586,7 +3586,7 @@ void cProtocol_1_8_0::WriteMobMetadata(cPacketizer & a_Pkt, const cMonster & a_M case mtVillager: { - auto & Villager = reinterpret_cast(a_Mob); + auto & Villager = static_cast(a_Mob); a_Pkt.WriteBEUInt8(0x50); a_Pkt.WriteBEInt32(Villager.GetVilType()); a_Pkt.WriteBEUInt8(0x0c); @@ -3596,7 +3596,7 @@ void cProtocol_1_8_0::WriteMobMetadata(cPacketizer & a_Pkt, const cMonster & a_M case mtWitch: { - auto & Witch = reinterpret_cast(a_Mob); + auto & Witch = static_cast(a_Mob); a_Pkt.WriteBEUInt8(0x15); a_Pkt.WriteBEUInt8(Witch.IsAngry() ? 1 : 0); break; @@ -3604,7 +3604,7 @@ void cProtocol_1_8_0::WriteMobMetadata(cPacketizer & a_Pkt, const cMonster & a_M case mtWither: { - auto & Wither = reinterpret_cast(a_Mob); + auto & Wither = static_cast(a_Mob); a_Pkt.WriteBEUInt8(0x54); // Int at index 20 a_Pkt.WriteBEInt32(static_cast(Wither.GetWitherInvulnerableTicks())); a_Pkt.WriteBEUInt8(0x66); // Float at index 6 @@ -3614,7 +3614,7 @@ void cProtocol_1_8_0::WriteMobMetadata(cPacketizer & a_Pkt, const cMonster & a_M case mtWolf: { - auto & Wolf = reinterpret_cast(a_Mob); + auto & Wolf = static_cast(a_Mob); Byte WolfStatus = 0; if (Wolf.IsSitting()) { @@ -3644,7 +3644,7 @@ void cProtocol_1_8_0::WriteMobMetadata(cPacketizer & a_Pkt, const cMonster & a_M case mtZombie: { - auto & Zombie = reinterpret_cast(a_Mob); + auto & Zombie = static_cast(a_Mob); a_Pkt.WriteBEUInt8(0x0c); a_Pkt.WriteBEInt8(Zombie.IsBaby() ? 1 : -1); a_Pkt.WriteBEUInt8(0x0d); @@ -3656,7 +3656,7 @@ void cProtocol_1_8_0::WriteMobMetadata(cPacketizer & a_Pkt, const cMonster & a_M case mtZombiePigman: { - auto & ZombiePigman = reinterpret_cast(a_Mob); + auto & ZombiePigman = static_cast(a_Mob); a_Pkt.WriteBEUInt8(0x0c); a_Pkt.WriteBEInt8(ZombiePigman.IsBaby() ? 1 : -1); break; -- cgit v1.2.3