diff --git a/include/bedrock/entity/components/actor_definition_identifier_component.h b/include/bedrock/entity/components/actor_definition_identifier_component.h new file mode 100644 index 000000000..2ab3b72fe --- /dev/null +++ b/include/bedrock/entity/components/actor_definition_identifier_component.h @@ -0,0 +1,21 @@ +// Copyright (c) 2024, The Endstone Project. (https://endstone.dev) All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once + +#include "bedrock/world/actor/actor_definition_identifier.h" + +struct ActorDefinitionIdentifierComponent { + ActorDefinitionIdentifier identifier; // +0 +}; diff --git a/include/bedrock/forward.h b/include/bedrock/forward.h index ea1ee1627..36e74ea94 100644 --- a/include/bedrock/forward.h +++ b/include/bedrock/forward.h @@ -24,7 +24,6 @@ class ActorChunkTransferEntry; class ActorDamageCause; class ActorDefinitionDescriptor; class ActorDefinitionGroup; -class ActorDefinitionIdentifier; class ActorEvent; class ActorFactory; class ActorInteraction; diff --git a/include/bedrock/world/actor/actor.h b/include/bedrock/world/actor/actor.h index d2ab0db86..242452431 100644 --- a/include/bedrock/world/actor/actor.h +++ b/include/bedrock/world/actor/actor.h @@ -41,6 +41,7 @@ #include "bedrock/util/variant_parameter_list.h" #include "bedrock/world/actor/actor_category.h" #include "bedrock/world/actor/actor_damage_source.h" +#include "bedrock/world/actor/actor_definition_identifier.h" #include "bedrock/world/actor/actor_flags.h" #include "bedrock/world/actor/actor_initialization_method.h" #include "bedrock/world/actor/actor_runtime_id.h" @@ -250,6 +251,8 @@ class Actor { void setStatusFlag(ActorFlags, bool); [[nodiscard]] bool isType(ActorType type) const; [[nodiscard]] bool hasType(ActorType type) const; + [[nodiscard]] ActorType getEntityTypeId() const; + [[nodiscard]] const ActorDefinitionIdentifier &getActorIdentifier() const; [[nodiscard]] bool isPlayer() const; [[nodiscard]] bool isRemoved() const; [[nodiscard]] bool isOnGround() const; diff --git a/include/bedrock/world/actor/actor_definition_identifier.h b/include/bedrock/world/actor/actor_definition_identifier.h new file mode 100644 index 000000000..aaf46050e --- /dev/null +++ b/include/bedrock/world/actor/actor_definition_identifier.h @@ -0,0 +1,38 @@ +// Copyright (c) 2024, The Endstone Project. (https://endstone.dev) All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once + +#include + +#include "bedrock/core/string/string_hash.h" + +struct ActorDefinitionIdentifier { +public: + ActorDefinitionIdentifier() = default; + [[nodiscard]] const std::string &getNamespace() const; + [[nodiscard]] const std::string &getIdentifier() const; + [[nodiscard]] const std::string &getInitEvent() const; + [[nodiscard]] const std::string &getCanonicalName() const; + [[nodiscard]] const HashedString &getCanonicalHash() const; + [[nodiscard]] const std::string &getFullName() const; + [[nodiscard]] bool isVanilla() const; + +private: + std::string namespace_; // +0 + std::string identifier_; // +24 + std::string init_event_; // +48 + std::string full_name_; // +72 + HashedString canonical_name_{nullptr}; // +96 +}; diff --git a/include/bedrock/world/item/item.h b/include/bedrock/world/item/item.h index 06feaa653..c028556c5 100644 --- a/include/bedrock/world/item/item.h +++ b/include/bedrock/world/item/item.h @@ -21,6 +21,7 @@ #include "bedrock/gameplayhandlers/coordinator_result.h" #include "bedrock/resources/base_game_version.h" #include "bedrock/shared_ptr.h" +#include "bedrock/world/actor/actor_definition_identifier.h" #include "bedrock/world/actor/actor_location.h" #include "bedrock/world/gamemode/interaction_result.h" #include "bedrock/world/interactions/mining/mine_block_item_effect_type.h" diff --git a/include/endstone/actor/actor.h b/include/endstone/actor/actor.h index f6f1a52e7..d1524b69d 100644 --- a/include/endstone/actor/actor.h +++ b/include/endstone/actor/actor.h @@ -31,6 +31,15 @@ class Actor : public CommandSender { return const_cast(this); } + /** + * @brief Get the type of the actor. + * + * This method returns the type of the actor as a string, for example, minecraft:pig. + * + * @return The type of the actor. + */ + [[nodiscard]] virtual std::string getType() const = 0; + /** * Returns the runtime id for this actor * diff --git a/include/endstone/detail/actor/actor.h b/include/endstone/detail/actor/actor.h index 595d116a7..a8f8281a1 100644 --- a/include/endstone/detail/actor/actor.h +++ b/include/endstone/detail/actor/actor.h @@ -54,6 +54,7 @@ class EndstoneActor : public Actor, public std::enable_shared_from_this getVelocity() const override; diff --git a/include/endstone/detail/actor/mob.h b/include/endstone/detail/actor/mob.h index d5a9d5799..ed2f5f637 100644 --- a/include/endstone/detail/actor/mob.h +++ b/include/endstone/detail/actor/mob.h @@ -45,6 +45,7 @@ class EndstoneMob : public EndstoneActor, public Mob { void setOp(bool value) override; // Actor + [[nodiscard]] std::string getType() const override; [[nodiscard]] std::uint64_t getRuntimeId() const override; [[nodiscard]] Location getLocation() const override; [[nodiscard]] Vector getVelocity() const override; diff --git a/include/endstone/detail/player.h b/include/endstone/detail/player.h index d221999e8..eca95b7e8 100644 --- a/include/endstone/detail/player.h +++ b/include/endstone/detail/player.h @@ -58,6 +58,7 @@ class EndstonePlayer : public EndstoneMob, public Player { void setOp(bool value) override; // Actor + [[nodiscard]] std::string getType() const override; [[nodiscard]] std::uint64_t getRuntimeId() const override; [[nodiscard]] Location getLocation() const override; [[nodiscard]] Vector getVelocity() const override; diff --git a/python/src/endstone/_internal/endstone_python.pyi b/python/src/endstone/_internal/endstone_python.pyi index eb5669a5b..33593f6c7 100644 --- a/python/src/endstone/_internal/endstone_python.pyi +++ b/python/src/endstone/_internal/endstone_python.pyi @@ -207,6 +207,11 @@ class Actor(CommandSender): Returns a list of scoreboard tags for this actor. """ @property + def type(self) -> str: + """ + Gets the type of the actor. + """ + @property def velocity(self) -> Vector: """ Gets this actor's current velocity. diff --git a/src/endstone_core/actor/actor.cpp b/src/endstone_core/actor/actor.cpp index 89e4cd885..f22c312fb 100644 --- a/src/endstone_core/actor/actor.cpp +++ b/src/endstone_core/actor/actor.cpp @@ -102,6 +102,11 @@ void EndstoneActor::setOp(bool value) getPermissibleBase().setOp(value); } +std::string EndstoneActor::getType() const +{ + return actor_.getActorIdentifier().getCanonicalName(); +} + std::uint64_t EndstoneActor::getRuntimeId() const { return actor_.getRuntimeID().raw_id; diff --git a/src/endstone_core/actor/mob.cpp b/src/endstone_core/actor/mob.cpp index ac28d221b..23053e013 100644 --- a/src/endstone_core/actor/mob.cpp +++ b/src/endstone_core/actor/mob.cpp @@ -96,6 +96,11 @@ void EndstoneMob::setOp(bool value) EndstoneActor::setOp(value); } +std::string EndstoneMob::getType() const +{ + return EndstoneActor::getType(); +} + std::uint64_t EndstoneMob::getRuntimeId() const { return EndstoneActor::getRuntimeId(); diff --git a/src/endstone_core/player.cpp b/src/endstone_core/player.cpp index f8efd58d9..794e44813 100644 --- a/src/endstone_core/player.cpp +++ b/src/endstone_core/player.cpp @@ -184,6 +184,11 @@ void EndstonePlayer::setOp(bool value) getHandle().setPermissions(value ? CommandPermissionLevel::Any : CommandPermissionLevel::Admin); } +std::string EndstonePlayer::getType() const +{ + return EndstoneMob::getType(); +} + std::uint64_t EndstonePlayer::getRuntimeId() const { return EndstoneMob::getRuntimeId(); diff --git a/src/endstone_python/actor.cpp b/src/endstone_python/actor.cpp index bb862c4a1..a0c7254d1 100644 --- a/src/endstone_python/actor.cpp +++ b/src/endstone_python/actor.cpp @@ -28,7 +28,8 @@ namespace endstone::detail { void init_actor(py::module_ &m, py::class_ &actor, py::class_ &mob) { - actor.def_property_readonly("runtime_id", &Actor::getRuntimeId, "Returns the runtime id for this actor.") + actor.def_property_readonly("type", &Actor::getType, "Gets the type of the actor.") + .def_property_readonly("runtime_id", &Actor::getRuntimeId, "Returns the runtime id for this actor.") .def_property_readonly("location", &Actor::getLocation, "Gets the actor's current position.") .def_property_readonly("velocity", &Actor::getVelocity, "Gets this actor's current velocity.") .def_property_readonly("is_on_ground", &Actor::isOnGround, diff --git a/src/endstone_runtime/CMakeLists.txt b/src/endstone_runtime/CMakeLists.txt index 61b7a0539..dcc0e48c9 100644 --- a/src/endstone_runtime/CMakeLists.txt +++ b/src/endstone_runtime/CMakeLists.txt @@ -80,6 +80,7 @@ add_library(endstone_runtime SHARED bedrock/world/game_session.cpp bedrock/world/minecraft.cpp bedrock/world/actor/actor.cpp + bedrock/world/actor/actor_definition_identifier.cpp bedrock/world/actor/mob.cpp bedrock/world/actor/synched_actor_data.cpp bedrock/world/actor/player/abilities.cpp diff --git a/src/endstone_runtime/bedrock/world/actor/actor.cpp b/src/endstone_runtime/bedrock/world/actor/actor.cpp index 046da8591..b3d12ed54 100644 --- a/src/endstone_runtime/bedrock/world/actor/actor.cpp +++ b/src/endstone_runtime/bedrock/world/actor/actor.cpp @@ -15,6 +15,7 @@ #include "bedrock/world/actor/actor.h" #include "bedrock/entity/components/actor_data_flag_component.h" +#include "bedrock/entity/components/actor_definition_identifier_component.h" #include "bedrock/entity/components/actor_owner_component.h" #include "bedrock/entity/components/actor_type_flag_component.h" #include "bedrock/entity/components/actor_unique_id_component.h" @@ -95,6 +96,20 @@ bool Actor::hasType(ActorType type) const return !!(component->type & type); } +ActorType Actor::getEntityTypeId() const +{ + return getPersistentComponent()->type; +} + +const ActorDefinitionIdentifier &Actor::getActorIdentifier() const +{ + static ActorDefinitionIdentifier empty; + if (auto *component = tryGetComponent(); component) { + return component->identifier; + } + return empty; +} + bool Actor::isPlayer() const { return hasComponent(); diff --git a/src/endstone_runtime/bedrock/world/actor/actor_definition_identifier.cpp b/src/endstone_runtime/bedrock/world/actor/actor_definition_identifier.cpp new file mode 100644 index 000000000..1aa634a41 --- /dev/null +++ b/src/endstone_runtime/bedrock/world/actor/actor_definition_identifier.cpp @@ -0,0 +1,50 @@ +// Copyright (c) 2024, The Endstone Project. (https://endstone.dev) All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "bedrock/world/actor/actor_definition_identifier.h" + +const std::string &ActorDefinitionIdentifier::getNamespace() const +{ + return namespace_; +} + +const std::string &ActorDefinitionIdentifier::getIdentifier() const +{ + return identifier_; +} + +const std::string &ActorDefinitionIdentifier::getInitEvent() const +{ + return init_event_; +} + +const std::string &ActorDefinitionIdentifier::getCanonicalName() const +{ + return canonical_name_.getString(); +} + +const HashedString &ActorDefinitionIdentifier::getCanonicalHash() const +{ + return canonical_name_; +} + +const std::string &ActorDefinitionIdentifier::getFullName() const +{ + return full_name_; +} + +bool ActorDefinitionIdentifier::isVanilla() const +{ + return namespace_ == "minecraft"; +}