Skip to content

Commit

Permalink
feat: add Actor::getType function to retrieve actor types (e.g., mi…
Browse files Browse the repository at this point in the history
…necraft:pig)
  • Loading branch information
wu-vincent committed Dec 23, 2024
1 parent 1519507 commit 3b4ce8b
Show file tree
Hide file tree
Showing 17 changed files with 163 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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
};
1 change: 0 additions & 1 deletion include/bedrock/forward.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class ActorChunkTransferEntry;
class ActorDamageCause;
class ActorDefinitionDescriptor;
class ActorDefinitionGroup;
class ActorDefinitionIdentifier;
class ActorEvent;
class ActorFactory;
class ActorInteraction;
Expand Down
3 changes: 3 additions & 0 deletions include/bedrock/world/actor/actor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
Expand Down
38 changes: 38 additions & 0 deletions include/bedrock/world/actor/actor_definition_identifier.h
Original file line number Diff line number Diff line change
@@ -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 <string>

#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
};
1 change: 1 addition & 0 deletions include/bedrock/world/item/item.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 9 additions & 0 deletions include/endstone/actor/actor.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ class Actor : public CommandSender {
return const_cast<Actor *>(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
*
Expand Down
1 change: 1 addition & 0 deletions include/endstone/detail/actor/actor.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class EndstoneActor : public Actor, public std::enable_shared_from_this<Endstone
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<float> getVelocity() const override;
Expand Down
1 change: 1 addition & 0 deletions include/endstone/detail/actor/mob.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<float> getVelocity() const override;
Expand Down
1 change: 1 addition & 0 deletions include/endstone/detail/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<float> getVelocity() const override;
Expand Down
5 changes: 5 additions & 0 deletions python/src/endstone/_internal/endstone_python.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions src/endstone_core/actor/actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions src/endstone_core/actor/mob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 5 additions & 0 deletions src/endstone_core/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion src/endstone_python/actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ namespace endstone::detail {

void init_actor(py::module_ &m, py::class_<Actor, CommandSender> &actor, py::class_<Mob, Actor> &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,
Expand Down
1 change: 1 addition & 0 deletions src/endstone_runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions src/endstone_runtime/bedrock/world/actor/actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -95,6 +96,20 @@ bool Actor::hasType(ActorType type) const
return !!(component->type & type);
}

ActorType Actor::getEntityTypeId() const
{
return getPersistentComponent<ActorTypeComponent>()->type;
}

const ActorDefinitionIdentifier &Actor::getActorIdentifier() const
{
static ActorDefinitionIdentifier empty;
if (auto *component = tryGetComponent<ActorDefinitionIdentifierComponent>(); component) {
return component->identifier;
}
return empty;
}

bool Actor::isPlayer() const
{
return hasComponent<PlayerComponent>();
Expand Down
Original file line number Diff line number Diff line change
@@ -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";
}

0 comments on commit 3b4ce8b

Please sign in to comment.