Skip to content

Commit

Permalink
fix: correct the implementation of Actor::hasCategory
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Jan 24, 2025
1 parent 8f5f6c4 commit ad7772a
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 22 deletions.
15 changes: 8 additions & 7 deletions src/bedrock/world/actor/actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,15 @@ void Actor::setStatusFlag(ActorFlags flag, bool value)

bool Actor::isType(ActorType type) const
{
auto component = getPersistentComponent<ActorTypeComponent>();
return component->type == type;
return type == getEntityTypeId();
}

bool Actor::hasType(ActorType type) const
bool Actor::hasType(ActorType types) const
{
auto component = getPersistentComponent<ActorTypeComponent>();
return !!(component->type & type);
if (static_cast<std::underlying_type_t<ActorType>>(types)) {
return types == getEntityTypeId();
}
return (types & getEntityTypeId()) == types;
}

ActorType Actor::getEntityTypeId() const
Expand Down Expand Up @@ -183,9 +184,9 @@ bool Actor::isRiding() const
return getVehicle() != nullptr;
}

bool Actor::hasCategory(ActorCategory category) const
bool Actor::hasCategory(ActorCategory categories) const
{
return !!(categories_ & category);
return (categories & categories_) == categories;
}

Actor *Actor::tryGetFromEntity(EntityContext const &entity, bool include_removed)
Expand Down
4 changes: 2 additions & 2 deletions src/bedrock/world/actor/actor.h
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ class Actor {
[[nodiscard]] bool getStatusFlag(ActorFlags) const;
void setStatusFlag(ActorFlags, bool);
[[nodiscard]] bool isType(ActorType type) const;
[[nodiscard]] bool hasType(ActorType type) const;
[[nodiscard]] bool hasType(ActorType types) const;
[[nodiscard]] ActorType getEntityTypeId() const;
[[nodiscard]] const ActorDefinitionIdentifier &getActorIdentifier() const;
[[nodiscard]] bool isSneaking() const;
Expand All @@ -275,7 +275,7 @@ class Actor {
[[nodiscard]] ActorUniqueID getOrCreateUniqueID() const;
[[nodiscard]] Actor *getVehicle() const;
[[nodiscard]] bool isRiding() const;
[[nodiscard]] bool hasCategory(ActorCategory) const;
[[nodiscard]] bool hasCategory(ActorCategory categories) const;
[[nodiscard]] bool isJumping() const;
[[nodiscard]] std::vector<std::string> getTags() const;
bool addTag(const std::string &tag);
Expand Down
20 changes: 10 additions & 10 deletions src/bedrock/world/actor/actor_category.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,7 @@

#pragma once

#include <entt/entt.hpp>

/**
* From entityCategoryFromString
*/
enum class ActorCategory {
enum class ActorCategory : std::uint32_t {
None = 0,
Player = 1 << 0,
Mob = 1 << 1,
Expand All @@ -38,15 +33,20 @@ enum class ActorCategory {
Zombie = 1 << 15,
Minecart = 1 << 16,
Boat = 1 << 17,

NonTargetable = 1 << 18,
Predictable = 1 << 19,
BoatRideable = Boat | Ridable,
MinecartRidable = Minecart | Ridable,
HumanoidMonster = Humanoid | Monster,
WaterAnimal = Water | Animal,
TamableAnimal = Tamable | Animal,
UndeadMob = Undead | Monster, // wtf mojang
ZombieMonster = Zombie | Monster
ZombieMonster = Zombie | Monster,
EvocationIllagerMonster = Villager | HumanoidMonster,
};

template <>
struct entt::enum_as_bitmask<ActorCategory> : std::true_type {};
constexpr ActorCategory operator&(const ActorCategory lhs, const ActorCategory rhs)
{
return static_cast<ActorCategory>(static_cast<std::underlying_type_t<ActorCategory>>(lhs) &
static_cast<std::underlying_type_t<ActorCategory>>(rhs));
}
7 changes: 6 additions & 1 deletion src/bedrock/world/actor/actor_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,5 +167,10 @@ enum class ActorType : int {
Bogged = 144 | SkeletonMonster,
OminousItemSpawner = 145,
Creaking = 146 | Monster,
_entt_enum_as_bitmask
};

constexpr ActorType operator&(const ActorType lhs, const ActorType rhs)
{
return static_cast<ActorType>(static_cast<std::underlying_type_t<ActorType>>(lhs) &
static_cast<std::underlying_type_t<ActorType>>(rhs));
}
2 changes: 1 addition & 1 deletion src/bedrock/world/actor/mob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Mob *Mob::tryGetFromEntity(EntityContext &entity, bool include_removed)
if (!actor) {
return nullptr;
}
if (!actor->isType(ActorType::Mob) || !actor->hasCategory(ActorCategory::Mob)) {
if (!actor->isType(ActorType::Mob) && !actor->hasCategory(ActorCategory::Mob)) {
return nullptr;
}
return static_cast<Mob *>(actor);
Expand Down
2 changes: 1 addition & 1 deletion src/endstone/core/actor/actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ endstone::core::EndstoneActor &Actor::getEndstoneActor0() const
auto *player = static_cast<Player *>(self);
component.actor = endstone::core::EndstonePlayer::create(server, *player);
}
else if (self->hasType(ActorType::Mob) || self->hasCategory(ActorCategory::Mob)) {
else if (self->isType(ActorType::Mob) || self->hasCategory(ActorCategory::Mob)) {
auto *mob = static_cast<Mob *>(self);
component.actor = endstone::core::EndstoneMob::create(server, *mob);
}
Expand Down

0 comments on commit ad7772a

Please sign in to comment.