Skip to content

Commit

Permalink
feat: add Player::isFlying
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed May 24, 2024
1 parent b08c2f9 commit 8ccd5d8
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 4 deletions.
2 changes: 1 addition & 1 deletion include/bedrock/command/command_origin.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include "bedrock/memory.h"
#include "bedrock/network/network_identifier.h"
#include "bedrock/network/protocol/sub_client_id.h"
#include "bedrock/world/actor/abilities/abilities_index.h"
#include "bedrock/world/actor/player/abilities/abilities_index.h"
#include "bedrock/world/level/dimension/dimension.h"
#include "bedrock/world/math/vec2.h"
#include "bedrock/world/math/vec3.h"
Expand Down
6 changes: 3 additions & 3 deletions include/bedrock/world/actor/components/abilities_component.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

#pragma once

#include "bedrock/world/actor/player/abilities/layered_abilities.h"
#include "bedrock/world/actor/player/permissions_handler.h"

class AbilitiesComponent {
public:
PermissionsHandler permissions_handler;
struct AbilitiesComponent {
LayeredAbilities abilities;
};
5 changes: 5 additions & 0 deletions include/bedrock/world/actor/player/abilities/abilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@

class Abilities {
public:
[[nodiscard]] const Ability &getAbility(AbilitiesIndex index) const
{
return abilities_.at(static_cast<std::size_t>(index));
}

private:
std::array<Ability, static_cast<std::size_t>(AbilitiesIndex::AbilityCount)> abilities_;
};
Expand Down
15 changes: 15 additions & 0 deletions include/bedrock/world/actor/player/abilities/ability.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ class Ability {

Ability() = default;

[[nodiscard]] Type getType() const
{
return type_;
}

[[nodiscard]] bool getBool() const
{
return type_ == Type::Bool ? value_.b : false;
}

[[nodiscard]] float getFloat() const
{
return type_ == Type::Float ? value_.f : 0.0F;
}

private:
Type type_;
union {
Expand Down
23 changes: 23 additions & 0 deletions include/bedrock/world/actor/player/abilities/layered_abilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,29 @@

class LayeredAbilities {
public:
[[nodiscard]] const Ability &getAbility(AbilitiesIndex index) const
{
const Ability *ability;
for (auto i = static_cast<int>(AbilitiesLayer::LayerCount) - 1; i >= 0; i--) {
const auto &abilities = layers_[i];
ability = &abilities.getAbility(index);
if (ability->getType() != Ability::Type::NotSet) {
break;
}
}
return *ability;
}

[[nodiscard]] bool getBool(AbilitiesIndex index) const
{
return getAbility(index).getBool();
}

[[nodiscard]] float getFloat(AbilitiesIndex index) const
{
return getAbility(index).getFloat();
}

private:
PermissionsHandler permissions_;
std::array<Abilities, static_cast<std::size_t>(AbilitiesLayer::LayerCount)> layers_;
Expand Down
1 change: 1 addition & 0 deletions include/bedrock/world/actor/player/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ class Player : public Mob {
[[nodiscard]] BEDROCK_API const std::string &getName() const;
[[nodiscard]] GameType getPlayerGameType() const;
[[nodiscard]] bool isEmoting() const;
[[nodiscard]] bool isFlying() const;

private:
public:
Expand Down
7 changes: 7 additions & 0 deletions src/endstone_runtime/bedrock/world/actor/player/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "bedrock/network/protocol/game/available_commands_packet.h"
#include "bedrock/world/actor/actor_flags.h"
#include "bedrock/world/actor/components/abilities_component.h"
#include "bedrock/world/actor/components/actor_game_type_component.h"
#include "bedrock/world/level/level.h"
#include "endstone/detail/hook.h"
Expand Down Expand Up @@ -57,3 +58,9 @@ bool Player::isEmoting() const
{
return getStatusFlag(ActorFlags::EMOTING);
}

bool Player::isFlying() const
{
auto component = getPersistentComponent<AbilitiesComponent>();
return component->abilities.getBool(AbilitiesIndex::Flying);
}

0 comments on commit 8ccd5d8

Please sign in to comment.