Skip to content

Commit

Permalink
refactor: ActorRemoveEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Jan 24, 2025
1 parent 0d35ccf commit 8f5f6c4
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 23 deletions.
2 changes: 0 additions & 2 deletions src/bedrock/symbol_generator/symbols.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ version = "1.21.50"
[windows]
"?getI18n@@YAAEAVI18n@@XZ" = 14120528
# Actor
"?remove@Actor@@UEAAXXZ" = 37853696
"?teleportTo@Actor@@UEAAXAEBVVec3@@_NHH1@Z" = 37887936
# Bedrock
"?getServerThread@Threading@Bedrock@@YAAEAVAssignedThread@12@XZ" = 62131040
Expand Down Expand Up @@ -101,7 +100,6 @@ version = "1.21.50"
[linux]
"_Z7getI18nv" = 91294352
# Actor
"_ZN5Actor6removeEv" = 121537872
"_ZN5Actor10teleportToERK4Vec3biib" = 121564592
# Bedrock
"_ZN7Bedrock9Threading15getServerThreadEv" = 164283584
Expand Down
38 changes: 29 additions & 9 deletions src/bedrock/world/actor/actor.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class Actor {
virtual ~Actor() = 0;
virtual void resetUserPos(bool) = 0;
virtual ActorType getOwnerEntityType() = 0;
ENDSTONE_HOOK virtual void remove();
virtual void remove() = 0;
[[nodiscard]] virtual Vec3 getFiringPos() const = 0;
[[nodiscard]] virtual float getInterpolatedBodyRot(float) const = 0;
[[nodiscard]] virtual float getInterpolatedHeadRot(float) const = 0;
Expand Down Expand Up @@ -247,6 +247,7 @@ class Actor {
virtual void _playStepSound(BlockPos const &, Block const &) = 0;

public:
Actor(ILevel &, EntityContext &);
[[nodiscard]] bool getStatusFlag(ActorFlags) const;
void setStatusFlag(ActorFlags, bool);
[[nodiscard]] bool isType(ActorType type) const;
Expand All @@ -260,7 +261,8 @@ class Actor {
[[nodiscard]] bool isInWater() const;
[[nodiscard]] bool isInLava() const;
[[nodiscard]] Dimension &getDimension() const;
[[nodiscard]] Level &getLevel() const;
[[nodiscard]] Level &getLevel();
[[nodiscard]] const Level &getLevel() const;
[[nodiscard]] Vec3 const &getPosition() const; // NOTE: this returns the eye position instead of feet position
[[nodiscard]] Vec3 const &getPosPrev() const;
void applyImpulse(Vec3 const &impulse);
Expand Down Expand Up @@ -308,7 +310,7 @@ class Actor {
std::string alias;
std::optional<glm::mat4x4> previous_render_transform;
int last_hurt_by_player_time;
std::map<HashedString, std::vector<std::vector<glm::mat4x4>>> previous_bone_matrices;
std::map<HashedString, std::vector<std::vector<glm::highp_fmat4x4>>> previous_bone_matrices;
SynchedActorDataEntityWrapper entity_data;
std::unique_ptr<SpatialActorNetworkData> network_data;
Vec3 sent_delta;
Expand Down Expand Up @@ -342,13 +344,31 @@ class Actor {
ActorUniqueID legacy_unique_id;

private:
WeakRef<Dimension> dimension_; // +456
Level *level_; // +472
HashedString actor_renderer_id_; //
ActorCategory categories_; //
BuiltInActorComponents built_in_components_; //
// ...
WeakRef<Dimension> dimension_;
ILevel *level_;
HashedString actor_renderer_id_;
ActorCategory categories_;
BuiltInActorComponents built_in_components_;

protected:
HashedString actor_renderer_id_that_animation_component_was_initialized_with_;
bool changed_;
bool removed_;
bool moved_to_limbo_;
bool moved_to_unloaded_chunk_;
bool blocks_building_;
std::shared_ptr<AnimationComponent> animation_component_;
std::shared_ptr<AnimationComponent> ui_animation_component_;
ActorUniqueID target_id_;
ActorUniqueID in_love_partner_;
std::unique_ptr<CompoundTag> persisting_trade_offers_;
int persisting_trade_riches_;
bool persisting_trade_;
bool effects_dirty_;
bool loot_dropped_;
bool loaded_from_nbt_this_frame_;

private:
endstone::core::EndstoneActor &getEndstoneActor0() const;

public:
Expand Down
2 changes: 1 addition & 1 deletion src/bedrock/world/level/level_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ class ILevel : public Bedrock::EnableNonOwnerReferences {
virtual void runCommand(HashedString const &, CommandOrigin &, CommandOriginSystem, CurrentCmdVersion) = 0;
virtual void runCommand(Command &, CommandOrigin &, CommandOriginSystem) = 0;
[[nodiscard]] virtual PlayerCapabilities::ISharedController const &getCapabilities() const = 0;
virtual LevelTagRegistry &getTagRegistry() = 0;
[[nodiscard]] virtual LevelTagRegistry &getTagRegistry() const = 0;
[[nodiscard]] virtual PlayerMovementSettings const &getPlayerMovementSettings() const = 0;
virtual void setPlayerMovementSettings(PlayerMovementSettings const &) = 0;
[[nodiscard]] virtual void *getPlayerMovementSettingsManager() = 0;
Expand Down
15 changes: 14 additions & 1 deletion src/endstone/core/event/handlers/actor_gameplay_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

#include "endstone/core/event/handlers/actor_gameplay_handler.h"

#include <endstone/event/actor/actor_remove_event.h>

#include "bedrock/world/actor/actor.h"
#include "endstone/core/server.h"
#include "endstone/event/actor/actor_death_event.h"
Expand All @@ -29,7 +31,8 @@ HandlerResult EndstoneActorGameplayHandler::handleEvent(const ActorGameplayEvent
{
auto visitor = [&](auto &&arg) -> HandlerResult {
using T = std::decay_t<decltype(arg)>;
if constexpr (std::is_same_v<T, Details::ValueOrRef<const ActorKilledEvent>>) {
if constexpr (std::is_same_v<T, Details::ValueOrRef<const ActorKilledEvent>> ||
std::is_same_v<T, Details::ValueOrRef<const ActorRemovedEvent>>) {
if (!handleEvent(arg.value())) {
return HandlerResult::BypassListeners;
}
Expand Down Expand Up @@ -61,4 +64,14 @@ bool EndstoneActorGameplayHandler::handleEvent(const ActorKilledEvent &event)
return true;
}

bool EndstoneActorGameplayHandler::handleEvent(const ActorRemovedEvent &event)
{
if (const auto *actor = WeakEntityRef(event.entity).tryUnwrap<::Actor>(); actor && !actor->isPlayer()) {
const auto &server = entt::locator<EndstoneServer>::value();
ActorRemoveEvent e{actor->getEndstoneActor()};
server.getPluginManager().callEvent(e);
}
return true;
}

} // namespace endstone::core
1 change: 1 addition & 0 deletions src/endstone/core/event/handlers/actor_gameplay_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class EndstoneActorGameplayHandler final : public ActorGameplayHandler {

private:
bool handleEvent(const ActorKilledEvent &event);
bool handleEvent(const ActorRemovedEvent &event);

std::unique_ptr<ActorGameplayHandler> handle_;
};
Expand Down
10 changes: 0 additions & 10 deletions src/endstone/runtime/bedrock_hooks/actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,6 @@ using endstone::core::EndstoneMob;
using endstone::core::EndstonePlayer;
using endstone::core::EndstoneServer;

void Actor::remove()
{
if (!isPlayer()) {
auto &server = entt::locator<EndstoneServer>::value();
endstone::ActorRemoveEvent e{getEndstoneActor()};
server.getPluginManager().callEvent(e);
}
ENDSTONE_HOOK_CALL_ORIGINAL(&Actor::remove, this);
}

void Actor::teleportTo(const Vec3 &pos, bool should_stop_riding, int cause, int entity_type, bool keep_velocity)
{
Vec3 position = pos;
Expand Down

0 comments on commit 8f5f6c4

Please sign in to comment.