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_9.cpp | 78 +++++++++++++++++++++---------------------- 1 file changed, 39 insertions(+), 39 deletions(-) (limited to 'src/Protocol/Protocol_1_9.cpp') diff --git a/src/Protocol/Protocol_1_9.cpp b/src/Protocol/Protocol_1_9.cpp index 3e600cefd..64721ed04 100644 --- a/src/Protocol/Protocol_1_9.cpp +++ b/src/Protocol/Protocol_1_9.cpp @@ -2405,7 +2405,7 @@ void cProtocol_1_9_0::HandlePacketBoatSteer(cByteBuffer & a_ByteBuffer) { if (Vehicle->GetEntityType() == cEntity::etBoat) { - auto * Boat = reinterpret_cast(Vehicle); + auto * Boat = static_cast(Vehicle); Boat->UpdatePaddles(RightPaddle, LeftPaddle); } } @@ -2970,7 +2970,7 @@ void cProtocol_1_9_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; @@ -3527,7 +3527,7 @@ void cProtocol_1_9_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()); @@ -3540,7 +3540,7 @@ void cProtocol_1_9_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()); @@ -3561,7 +3561,7 @@ void cProtocol_1_9_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()); @@ -3587,7 +3587,7 @@ void cProtocol_1_9_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()); @@ -3599,7 +3599,7 @@ void cProtocol_1_9_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()); @@ -3657,7 +3657,7 @@ void cProtocol_1_9_0::WriteEntityMetadata(cPacketizer & a_Pkt, const cEntity & a { case cEntity::etPlayer: { - auto & Player = reinterpret_cast(a_Entity); + auto & Player = static_cast(a_Entity); // TODO Set player custom name to their name. // Then it's possible to move the custom name of mobs to the entities @@ -3683,7 +3683,7 @@ void cProtocol_1_9_0::WriteEntityMetadata(cPacketizer & a_Pkt, const cEntity & a { a_Pkt.WriteBEUInt8(5); // Index 5: Item a_Pkt.WriteBEUInt8(METADATA_TYPE_ITEM); - WriteItem(a_Pkt, reinterpret_cast(a_Entity).GetItem()); + WriteItem(a_Pkt, static_cast(a_Entity).GetItem()); break; } case cEntity::etMinecart: @@ -3692,7 +3692,7 @@ void cProtocol_1_9_0::WriteEntityMetadata(cPacketizer & a_Pkt, const cEntity & a a_Pkt.WriteBEUInt8(METADATA_TYPE_VARINT); // The following expression makes Minecarts shake more with less health or higher damage taken - auto & Minecart = reinterpret_cast(a_Entity); + auto & Minecart = static_cast(a_Entity); auto maxHealth = a_Entity.GetMaxHealth(); auto curHealth = a_Entity.GetHealth(); a_Pkt.WriteVarInt32(static_cast((maxHealth - curHealth) * Minecart.LastDamage() * 4)); @@ -3707,7 +3707,7 @@ void cProtocol_1_9_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()) { @@ -3730,35 +3730,35 @@ void cProtocol_1_9_0::WriteEntityMetadata(cPacketizer & a_Pkt, const cEntity & a { a_Pkt.WriteBEUInt8(11); // Index 11: Is powered a_Pkt.WriteBEUInt8(METADATA_TYPE_BOOL); - a_Pkt.WriteBool(reinterpret_cast(Minecart).IsFueled()); + a_Pkt.WriteBool(static_cast(Minecart).IsFueled()); } 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(5); // Index 5: Is critical a_Pkt.WriteBEUInt8(METADATA_TYPE_BYTE); - a_Pkt.WriteBEInt8(reinterpret_cast(Projectile).IsCritical() ? 1 : 0); + a_Pkt.WriteBEInt8(static_cast(Projectile).IsCritical() ? 1 : 0); break; } case cProjectileEntity::pkFirework: { a_Pkt.WriteBEUInt8(5); // Index 5: Firework item used for this firework a_Pkt.WriteBEUInt8(METADATA_TYPE_ITEM); - WriteItem(a_Pkt, reinterpret_cast(Projectile).GetItem()); + WriteItem(a_Pkt, static_cast(Projectile).GetItem()); break; } case cProjectileEntity::pkSplashPotion: { a_Pkt.WriteBEUInt8(5); // Index 5: Potion item which was thrown a_Pkt.WriteBEUInt8(METADATA_TYPE_ITEM); - WriteItem(a_Pkt, reinterpret_cast(Projectile).GetItem()); + WriteItem(a_Pkt, static_cast(Projectile).GetItem()); } default: { @@ -3770,13 +3770,13 @@ void cProtocol_1_9_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::etBoat: { - auto & Boat = reinterpret_cast(a_Entity); + auto & Boat = static_cast(a_Entity); a_Pkt.WriteBEInt8(5); // Index 6: Time since last hit a_Pkt.WriteBEInt8(METADATA_TYPE_VARINT); @@ -3807,7 +3807,7 @@ void cProtocol_1_9_0::WriteEntityMetadata(cPacketizer & a_Pkt, const cEntity & a case cEntity::etItemFrame: { - auto & Frame = reinterpret_cast(a_Entity); + auto & Frame = static_cast(a_Entity); a_Pkt.WriteBEUInt8(5); // Index 5: Item a_Pkt.WriteBEUInt8(METADATA_TYPE_ITEM); WriteItem(a_Pkt, Frame.GetItem()); @@ -3851,7 +3851,7 @@ void cProtocol_1_9_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(11); // Index 11: Bat flags - currently only hanging a_Pkt.WriteBEUInt8(METADATA_TYPE_BYTE); a_Pkt.WriteBEInt8(Bat.IsHanging() ? 1 : 0); @@ -3860,7 +3860,7 @@ void cProtocol_1_9_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(11); // Index 11: Is baby a_Pkt.WriteBEUInt8(METADATA_TYPE_BOOL); @@ -3870,7 +3870,7 @@ void cProtocol_1_9_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(11); // Index 11: Is baby a_Pkt.WriteBEUInt8(METADATA_TYPE_BOOL); @@ -3880,7 +3880,7 @@ void cProtocol_1_9_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(11); // Index 11: State (idle or "blowing") a_Pkt.WriteBEUInt8(METADATA_TYPE_VARINT); a_Pkt.WriteVarInt32(Creeper.IsBlowing() ? 1 : 0xffffffff); @@ -3897,7 +3897,7 @@ void cProtocol_1_9_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(11); // Index 11: Carried block a_Pkt.WriteBEUInt8(METADATA_TYPE_BLOCKID); UInt32 Carried = 0; @@ -3913,7 +3913,7 @@ void cProtocol_1_9_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(11); // Is attacking a_Pkt.WriteBEUInt8(METADATA_TYPE_BOOL); a_Pkt.WriteBool(Ghast.IsCharging()); @@ -3922,7 +3922,7 @@ void cProtocol_1_9_0::WriteMobMetadata(cPacketizer & a_Pkt, const cMonster & a_M case mtHorse: { - auto & Horse = reinterpret_cast(a_Mob); + auto & Horse = static_cast(a_Mob); Int8 Flags = 0; if (Horse.IsTame()) { @@ -3975,7 +3975,7 @@ void cProtocol_1_9_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(11); // Index 11: Size a_Pkt.WriteBEUInt8(METADATA_TYPE_VARINT); a_Pkt.WriteVarInt32(static_cast(MagmaCube.GetSize())); @@ -3984,7 +3984,7 @@ void cProtocol_1_9_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(11); // Index 11: Is baby a_Pkt.WriteBEUInt8(METADATA_TYPE_BOOL); @@ -3994,7 +3994,7 @@ void cProtocol_1_9_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(11); // Index 11: Is baby a_Pkt.WriteBEUInt8(METADATA_TYPE_BOOL); @@ -4009,7 +4009,7 @@ void cProtocol_1_9_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(11); // Index 11: Is baby a_Pkt.WriteBEUInt8(METADATA_TYPE_BOOL); a_Pkt.WriteBool(Rabbit.IsBaby()); @@ -4022,7 +4022,7 @@ void cProtocol_1_9_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(11); // Index 11: Is baby a_Pkt.WriteBEUInt8(METADATA_TYPE_BOOL); @@ -4042,7 +4042,7 @@ void cProtocol_1_9_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(11); // Index 11: Type a_Pkt.WriteBEUInt8(METADATA_TYPE_VARINT); a_Pkt.WriteVarInt32(Skeleton.IsWither() ? 1 : 0); @@ -4051,7 +4051,7 @@ void cProtocol_1_9_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(11); // Index 11: Size a_Pkt.WriteBEUInt8(METADATA_TYPE_VARINT); a_Pkt.WriteVarInt32(static_cast(Slime.GetSize())); @@ -4060,7 +4060,7 @@ void cProtocol_1_9_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(11); // Index 11: Is baby a_Pkt.WriteBEUInt8(METADATA_TYPE_BOOL); a_Pkt.WriteBool(Villager.IsBaby()); @@ -4073,7 +4073,7 @@ void cProtocol_1_9_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(11); // Index 11: Is angry a_Pkt.WriteBEUInt8(METADATA_TYPE_BOOL); a_Pkt.WriteBool(Witch.IsAngry()); @@ -4082,7 +4082,7 @@ void cProtocol_1_9_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(14); // Index 14: Invulnerable ticks a_Pkt.WriteBEUInt8(METADATA_TYPE_VARINT); a_Pkt.WriteVarInt32(Wither.GetWitherInvulnerableTicks()); @@ -4093,7 +4093,7 @@ void cProtocol_1_9_0::WriteMobMetadata(cPacketizer & a_Pkt, const cMonster & a_M case mtWolf: { - auto & Wolf = reinterpret_cast(a_Mob); + auto & Wolf = static_cast(a_Mob); a_Pkt.WriteBEUInt8(11); // Index 11: Is baby a_Pkt.WriteBEUInt8(METADATA_TYPE_BOOL); a_Pkt.WriteBool(Wolf.IsBaby()); @@ -4131,7 +4131,7 @@ void cProtocol_1_9_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(11); // Index 11: Is baby a_Pkt.WriteBEUInt8(METADATA_TYPE_BOOL); a_Pkt.WriteBool(Zombie.IsBaby()); @@ -4148,7 +4148,7 @@ void cProtocol_1_9_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(11); // Index 11: Is baby a_Pkt.WriteBEUInt8(METADATA_TYPE_BOOL); a_Pkt.WriteBool(ZombiePigman.IsBaby()); -- cgit v1.2.3