#include "EntityInteraction.h" #include #include #include #include #include "botcraft/Game/Entities/EntityManager.hpp" #include "botcraft/Game/Entities/entities/decoration/ItemFrameEntity.hpp" #include "botcraft/Game/Entities/entities/decoration/painting/PaintingEntity.hpp" #include "botcraft/Game/Enums.hpp" #include "protocolCraft/Packets/Game/Clientbound/ClientboundAddEntityPacket.hpp" #include "protocolCraft/Packets/Game/Clientbound/ClientboundSetEntityDataPacket.hpp" #include "protocolCraft/Packets/Game/Clientbound/ClientboundEntityPositionSyncPacket.hpp" #include "protocolCraft/Packets/Game/Clientbound/ClientboundTeleportEntityPacket.hpp" static_assert(PROTOCOL_VERSION == 774, "Spawn/metadata replay fixtures describe Minecraft 1.21.11"); namespace { using Botcraft::EntityType; using namespace ProtocolCraft; constexpr int EntityId = 715; const UUID EntityUuid{0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80, 0x90, 0xa0, 0xb0, 0xc0, 0xd0, 0xe0, 0xf0, 0x01}; // Direction 3D data order: Down, Up, North, South, West, East. const std::array Normals{{{0, -1, 0}, {0, 1, 0}, {0, 0, -1}, {0, 0, 1}, {-1, 0, 0}, {1, 0, 0}}}; Bilicraft::ActionCommand SupportFor(int direction) { Bilicraft::ActionCommand command{}; command.kind = Bilicraft::ActionKind::RightClick; command.entity = true; command.x = -17 - Normals[direction].x; command.y = 64 - Normals[direction].y; command.z = -31 - Normals[direction].z; return command; } // Exercise the public packet dispatch path, including VarInt decoding, rather // than bypassing EntityManager with direct frame setters or an exposed Handle. void Spawn(Botcraft::EntityManager &manager, EntityType type, int data) { ClientboundAddEntityPacket sent, received; sent.SetEntityId(EntityId); sent.SetUuid(EntityUuid); sent.SetType(static_cast(type)); sent.SetX(-17.0); sent.SetY(64.0); sent.SetZ(-31.0); LpVec3 movement; movement.SetX(0.0); movement.SetY(0.0); movement.SetZ(0.0); sent.SetMovement(movement); // Deliberately unrelated angles: facing must come from Data. sent.SetXRot(32); sent.SetYRot(64); sent.SetYHeadRot(128); sent.SetData(data); WriteContainer wire; sent.Write(wire); auto iter = wire.cbegin(); std::size_t remaining = wire.size(); ASSERT_EQ(ReadData(iter, remaining), sent.GetId()); received.Read(iter, remaining); ASSERT_EQ(remaining, 0U); received.Dispatch(&manager); } void MetadataDirection(Botcraft::EntityManager &manager, int direction) { // Official 1.21.11 HangingEntity DATA_DIRECTION: index 8, DIRECTION=12. // This decodes to Botcraft::Direction in std::any, NOT an int serializer. // Use the real manager metadata path so later authoritative updates must // override the initial spawn value, including the invalid-data sentinel. WriteContainer packed; WriteData(8, packed); WriteData(12, packed); WriteData(direction, packed); WriteData(0xff, packed); ClientboundSetEntityDataPacket sent, received; sent.SetEntityId(EntityId); sent.SetPackedItems(packed); WriteContainer wire; sent.Write(wire); auto iter = wire.cbegin(); std::size_t remaining = wire.size(); ASSERT_EQ(ReadData(iter, remaining), sent.GetId()); received.Read(iter, remaining); ASSERT_EQ(remaining, 0U); received.Dispatch(&manager); } void ExpectSpawnFields(const Botcraft::Entity &entity, EntityType type) { EXPECT_EQ(entity.GetType(), type); EXPECT_EQ(entity.GetEntityID(), EntityId); EXPECT_EQ(entity.GetUUID(), EntityUuid); // The packet contains the attachment cell, not a rendered frame center. EXPECT_DOUBLE_EQ(entity.GetX(), -17.0); EXPECT_DOUBLE_EQ(entity.GetY(), 64.0); EXPECT_DOUBLE_EQ(entity.GetZ(), -31.0); EXPECT_FLOAT_EQ(entity.GetYaw(), 90.0f); EXPECT_FLOAT_EQ(entity.GetPitch(), 45.0f); EXPECT_DOUBLE_EQ(entity.GetSpeed().x, 0.0); EXPECT_DOUBLE_EQ(entity.GetSpeed().y, 0.0); EXPECT_DOUBLE_EQ(entity.GetSpeed().z, 0.0); } class ItemFrameSpawn : public testing::TestWithParam { protected: Botcraft::EntityManager manager{nullptr}; // Offline packet replay; no connection. std::shared_ptr Frame() const { return std::dynamic_pointer_cast(manager.GetEntity(EntityId)); } }; TEST_P(ItemFrameSpawn, AllSixDirectionsAreAvailableImmediatelyWithoutMetadata) { for (int direction = 0; direction < 6; ++direction) { SCOPED_TRACE(direction); Spawn(manager, GetParam(), direction); const auto frame = Frame(); ASSERT_NE(frame, nullptr); EXPECT_EQ(frame->GetDataDirection(), direction); ExpectSpawnFields(*frame, GetParam()); EXPECT_EQ(Bilicraft::FindItemFrame(manager, SupportFor(direction)), frame); // Before the fix, non-South frames were selected on this WRONG block. for (int other = 0; other < 6; ++other) { if (other != direction) EXPECT_EQ(Bilicraft::FindItemFrame(manager, SupportFor(other)), nullptr); } } } template void PositionRoundTrip(Packet &sent, Botcraft::EntityManager &manager) { WriteContainer bytes; sent.Write(bytes); auto iter = bytes.cbegin(); std::size_t remaining = bytes.size(); ASSERT_EQ(ReadData(iter, remaining), sent.GetId()); Packet received; received.Read(iter, remaining); ASSERT_EQ(remaining, 0U); received.Dispatch(&manager); } TEST_P(ItemFrameSpawn, AbsoluteCenterUpdatesPreserveTargetAndTeleportChangesSupport) { for (int direction = 0; direction < 6; ++direction) { SCOPED_TRACE(direction); Spawn(manager, GetParam(), direction); const auto frame = Frame(); auto command = SupportFor(direction); const auto center = Bilicraft::ItemFrameCenter(*frame); PositionMoveRotation values; values.SetPosition({center.x, center.y, center.z}); values.SetDeltaMovement({0.0, 0.0, 0.0}); values.SetYRot(0.0f); values.SetXRot(0.0f); ClientboundEntityPositionSyncPacket sync; sync.SetEntityId(EntityId); sync.SetValues(values); sync.SetOnGround(false); PositionRoundTrip(sync, manager); EXPECT_EQ(Bilicraft::FindItemFrame(manager, command), frame); EXPECT_NO_THROW({ const auto actual = Bilicraft::ItemFrameCenter(*frame); EXPECT_DOUBLE_EQ(actual.x, center.x); EXPECT_DOUBLE_EQ(actual.y, center.y); EXPECT_DOUBLE_EQ(actual.z, center.z); }); values.SetPosition({center.x + 16.0, center.y, center.z}); ClientboundTeleportEntityPacket teleport; teleport.SetEntityId(EntityId); teleport.SetChange(values); teleport.SetRelatives(0); teleport.SetOnGround(false); PositionRoundTrip(teleport, manager); EXPECT_EQ(Bilicraft::FindItemFrame(manager, command), nullptr); command.x += 16; EXPECT_EQ(Bilicraft::FindItemFrame(manager, command), frame); } } TEST_P(ItemFrameSpawn, UnrelatedMetadataDoesNotResetSpawnDirection) { Spawn(manager, GetParam(), 2); // North, not the constructor's South default. const auto frame = Frame(); ASSERT_NE(frame, nullptr); ClientboundSetEntityDataPacket packet; packet.SetEntityId(EntityId); // ItemFrame item rotation: metadata index 10, serializer INT=1, rotation 3. packet.SetPackedItems({10, 1, 3, 0xff}); packet.Dispatch(&manager); EXPECT_EQ(frame->GetDataDirection(), 2); EXPECT_EQ(frame->GetDataRotation(), 3); EXPECT_EQ(Bilicraft::FindItemFrame(manager, SupportFor(2)), frame); } TEST_P(ItemFrameSpawn, LaterDirectionMetadataOverridesSpawnWithoutChangingIdentityOrPosition) { Spawn(manager, GetParam(), 2); const auto frame = Frame(); ASSERT_NE(frame, nullptr); for (int direction = 0; direction < 6; ++direction) { SCOPED_TRACE(direction); MetadataDirection(manager, direction); EXPECT_EQ(Frame(), frame); EXPECT_EQ(frame->GetDataDirection(), direction); ExpectSpawnFields(*frame, GetParam()); EXPECT_EQ(Bilicraft::FindItemFrame(manager, SupportFor(direction)), frame); } } TEST_P(ItemFrameSpawn, InvalidSpawnDataNeverAliasesAValidFaceAndMetadataCanRecover) { for (int data : {-1, 6, 255, std::numeric_limits::min(), std::numeric_limits::max()}) { SCOPED_TRACE(data); Spawn(manager, GetParam(), data); const auto frame = Frame(); ASSERT_NE(frame, nullptr); EXPECT_EQ(frame->GetDataDirection(), -1); ExpectSpawnFields(*frame, GetParam()); for (int direction = 0; direction < 6; ++direction) EXPECT_EQ(Bilicraft::FindItemFrame(manager, SupportFor(direction)), nullptr); EXPECT_THROW(Bilicraft::ItemFrameCenter(*frame), std::runtime_error); MetadataDirection(manager, 4); EXPECT_EQ(frame->GetDataDirection(), 4); EXPECT_EQ(Bilicraft::FindItemFrame(manager, SupportFor(4)), frame); } } INSTANTIATE_TEST_SUITE_P(OrdinaryAndGlow, ItemFrameSpawn, testing::Values(EntityType::ItemFrame, EntityType::GlowItemFrame)); TEST(ItemFrameSpawnOtherEntities, DoesNotInterpretOtherEntitiesDataAsFrameDirection) { Botcraft::EntityManager manager{nullptr}; for (const auto type : {EntityType::Painting, EntityType::ItemEntity}) { for (int data : {0, 5, 6, -1}) { SCOPED_TRACE(static_cast(type)); SCOPED_TRACE(data); Spawn(manager, type, data); const auto entity = manager.GetEntity(EntityId); ASSERT_NE(entity, nullptr); ExpectSpawnFields(*entity, type); EXPECT_EQ(std::dynamic_pointer_cast(entity), nullptr); if (type == EntityType::Painting) { const auto painting = std::dynamic_pointer_cast(entity); ASSERT_NE(painting, nullptr); EXPECT_EQ(painting->GetDataDirection(), static_cast(Botcraft::Direction::South)); } } } } } // namespace