Skip to content

Commit

Permalink
feat: add new PlayerStateManager
Browse files Browse the repository at this point in the history
  • Loading branch information
OEOTYAN committed Apr 7, 2024
1 parent 4a23cd1 commit 9675c9d
Show file tree
Hide file tree
Showing 31 changed files with 669 additions and 123 deletions.
14 changes: 7 additions & 7 deletions old/core/command/RegionCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,55 +94,55 @@ void regionCommandSetup() {
}
switch (do_hash2(action)) {
case do_hash2("cuboid"):
playerData.region = worldedit::Region::createRegion(
playerData.region = worldedit::Region::create(
worldedit::CUBOID,
tmpbox,
origin.getPlayer()->getDimensionId()
);
output.trSuccess("worldedit.region.set.cuboid");
break;
case do_hash2("extend"):
playerData.region = worldedit::Region::createRegion(
playerData.region = worldedit::Region::create(
worldedit::EXPAND,
tmpbox,
origin.getPlayer()->getDimensionId()
);
output.trSuccess("worldedit.region.set.extend");
break;
case do_hash2("poly"):
playerData.region = worldedit::Region::createRegion(
playerData.region = worldedit::Region::create(
worldedit::POLY,
tmpbox,
origin.getPlayer()->getDimensionId()
);
output.trSuccess("worldedit.region.set.poly");
break;
case do_hash2("sphere"):
playerData.region = worldedit::Region::createRegion(
playerData.region = worldedit::Region::create(
worldedit::SPHERE,
tmpbox,
origin.getPlayer()->getDimensionId()
);
output.trSuccess("worldedit.region.set.sphere");
break;
case do_hash2("cylinder"):
playerData.region = worldedit::Region::createRegion(
playerData.region = worldedit::Region::create(
worldedit::CYLINDER,
tmpbox,
origin.getPlayer()->getDimensionId()
);
output.trSuccess("worldedit.region.set.cylinder");
break;
case do_hash2("convex"):
playerData.region = worldedit::Region::createRegion(
playerData.region = worldedit::Region::create(
worldedit::CONVEX,
tmpbox,
origin.getPlayer()->getDimensionId()
);
output.trSuccess("worldedit.region.set.convex");
break;
case do_hash2("loft"):
playerData.region = worldedit::Region::createRegion(
playerData.region = worldedit::Region::create(
worldedit::LOFT,
tmpbox,
origin.getPlayer()->getDimensionId()
Expand Down
4 changes: 2 additions & 2 deletions old/core/data/PlayerData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ bool PlayerData::changeVicePos(BlockInstance blockInstance, bool output) {
return false;
}
if (region == nullptr) {
region = Region::createRegion(RegionType::CUBOID);
region = Region::create(RegionType::CUBOID);
}
auto pos = blockInstance.getPosition();
if (player->getDimensionId() != region->getDimensionID()) {
Expand Down Expand Up @@ -69,7 +69,7 @@ bool PlayerData::changeMainPos(BlockInstance blockInstance, bool output) {
return false;
}
if (region == nullptr) {
region = Region::createRegion(RegionType::CUBOID);
region = Region::create(RegionType::CUBOID);
}
auto pos = blockInstance.getPosition();
if (player->getDimensionId() != region->getDimensionID()) {
Expand Down
42 changes: 21 additions & 21 deletions old/core/subscribe/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ bool playerLeftClick(
const bool isLong,
class ItemStack* item,
BlockInstance& blockInstance,
FaceID mFace
FacingID mFace
) {
static phmap::flat_hash_map<std::string, long long> tickMap;

Expand Down Expand Up @@ -51,22 +51,22 @@ bool playerLeftClick(
if (brush->lneedFace && blockInstance != BlockInstance::Null) {
BlockPos bPos = blockInstance.getPosition();
switch (mFace) {
case FaceID::Down:
case FacingID::Down:
bPos.y -= 1;
break;
case FaceID::Up:
case FacingID::Up:
bPos.y += 1;
break;
case FaceID::North:
case FacingID::North:
bPos.z -= 1;
break;
case FaceID::South:
case FacingID::South:
bPos.z += 1;
break;
case FaceID::West:
case FacingID::West:
bPos.x -= 1;
break;
case FaceID::East:
case FacingID::East:
bPos.x += 1;
break;
default:
Expand All @@ -89,7 +89,7 @@ bool playerRightClick(
const bool isLong,
class ItemStack* item,
BlockInstance& blockInstance,
FaceID mFace
FacingID mFace
) {
static phmap::flat_hash_map<std::string, long long> tickMap;

Expand Down Expand Up @@ -128,22 +128,22 @@ bool playerRightClick(
if (brush->needFace && blockInstance != BlockInstance::Null) {
BlockPos bPos = blockInstance.getPosition();
switch (mFace) {
case FaceID::Down:
case FacingID::Down:
bPos.y -= 1;
break;
case FaceID::Up:
case FacingID::Up:
bPos.y += 1;
break;
case FaceID::North:
case FacingID::North:
bPos.z -= 1;
break;
case FaceID::South:
case FacingID::South:
bPos.z += 1;
break;
case FaceID::West:
case FacingID::West:
bPos.x -= 1;
break;
case FaceID::East:
case FacingID::East:
bPos.x += 1;
break;
default:
Expand Down Expand Up @@ -171,7 +171,7 @@ void playerSubscribe() {
false,
ev.mItemStack,
*const_cast<BlockInstance*>(&ev.mBlockInstance),
static_cast<FaceID>(ev.mFace)
static_cast<FacingID>(ev.mFace)
);
});
Event::PlayerUseItemEvent::subscribe([](const Event::PlayerUseItemEvent& ev) {
Expand All @@ -186,7 +186,7 @@ void playerSubscribe() {
!= BedrockBlocks::mAir) {
requiereWater = false;
}
FaceID face;
FacingID face;
BlockInstance blockInstance =
ev.mPlayer->getBlockFromViewVector(face, requiereWater, false, 2048.0f);
return playerRightClick(ev.mPlayer, true, ev.mItemStack, blockInstance, face);
Expand All @@ -205,7 +205,7 @@ void playerSubscribe() {
!= BedrockBlocks::mAir) {
requiereWater = false;
}
FaceID face;
FacingID face;
ev.mPlayer->getBlockFromViewVector(face, requiereWater);
bool res = playerLeftClick(
ev.mPlayer,
Expand Down Expand Up @@ -256,7 +256,7 @@ void playerSubscribe() {
!= BedrockBlocks::mAir) {
requiereWater = false;
}
FaceID face;
FacingID face;
BlockInstance blockInstance =
ev.mPlayer->getBlockFromViewVector(face, requiereWater);
return playerRightClick(
Expand All @@ -281,7 +281,7 @@ void playerSubscribe() {
!= BedrockBlocks::mAir) {
requiereWater = false;
}
FaceID face;
FacingID face;
BlockInstance blockInstance =
ev.mPlayer->getBlockFromViewVector(face, requiereWater);
return playerRightClick(
Expand All @@ -303,7 +303,7 @@ void playerSubscribe() {
// != BedrockBlocks::mAir) {
// requiereWater = false;
// }
// FaceID face;
// FacingID face;
// BlockInstance blockInstance = player->getBlockFromViewVector(face, requiereWater,
// false, 2048.0f); worldedit::playerLeftClick(player, true, player->getHandSlot(),
// blockInstance, face);
Expand All @@ -330,7 +330,7 @@ THook(
!= BedrockBlocks::mAir) {
requiereWater = false;
}
FaceID face;
FacingID face;
BlockInstance blockInstance =
player->getBlockFromViewVector(face, requiereWater, false, 2048.0f);
worldedit::playerLeftClick(
Expand Down
4 changes: 2 additions & 2 deletions old/core/subscribe/player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ bool playerLeftClick(
const bool isLong,
class ItemStack* item,
BlockInstance& blockInstance,
FaceID mFace
FacingID mFace
);
bool playerRightClick(
Player* player,
const bool isLong,
class ItemStack* item,
BlockInstance& blockInstance,
FaceID mFace
FacingID mFace
);
} // namespace we
13 changes: 12 additions & 1 deletion src/worldedit/Config.h → src/data/Config.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "Global.h"
#include "region/RegionType.h"
#include "worldedit/Global.h"

#include <ll/api/reflection/Dispatcher.h>
#include <mc/server/commands/CommandPermissionLevel.h>
Expand All @@ -18,7 +19,17 @@ struct Config {
struct {
mce::Color region_line_color{"#FFEC27"};
mce::Color region_point_color{"#10E436"};
mce::Color main_hand_color{"#FF3040"};
mce::Color off_hand_color{"#29ADFF"};
} colors{};
struct {
double maximum_brush_length = 2048;
int minimum_response_tick = 3;
} player_state;

struct PlayerConfig {
RegionType default_region_type{RegionType::Cuboid};
} player_default_config;
};
} // namespace we

Expand Down
84 changes: 84 additions & 0 deletions src/data/PlayerState.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include "PlayerState.h"
#include "utils/Serialize.h"
#include "worldedit/WorldEdit.h"

namespace we {

PlayerState::PlayerState(mce::UUID const& uuid) : uuid(uuid) {}

void PlayerState::setMainPosInternal() {
if (mainPos) {
auto& we = WorldEdit::getInstance();
mainPos->geo = we.getGeo().box(
mainPos->data.dim,
AABB{mainPos->data.pos}.shrink(-0.07),
we.getConfig().colors.main_hand_color
);
}
}
void PlayerState::setVicePosInternal() {
if (vicePos) {
auto& we = WorldEdit::getInstance();
vicePos->geo = we.getGeo().box(
vicePos->data.dim,
AABB{vicePos->data.pos}.shrink(-0.07),
we.getConfig().colors.off_hand_color
);
}
}
Region& PlayerState::getOrCreateRegion(WithDim<BlockPos> const& v) {
if (!region || region->getDim() != v.dim) {
region =
Region::create(regionType.value_or(config.default_region_type), v.dim, v.pos);
regionType = region->getType();
mDirty = true;
}
return *region;
}
bool PlayerState::setMainPos(WithDim<BlockPos> const& v) {
if (auto& r = getOrCreateRegion(v); r.setMainPos(v.pos)) {
mainPos.emplace(v);
setMainPosInternal();
if (r.needResetVice()) {
vicePos.reset();
}
mDirty = true;
return true;
}
return false;
}
bool PlayerState::setVicePos(WithDim<BlockPos> const& v) {
if (getOrCreateRegion(v).setVicePos(v.pos)) {
vicePos.emplace(v);
setVicePosInternal();
mDirty = true;
return true;
}
return false;
}

void PlayerState::serialize(CompoundTag& nbt) const {
if (mainPos) doSerialize(mainPos->data, nbt["mainPos"]);
if (vicePos) doSerialize(vicePos->data, nbt["vicePos"]);
if (regionType) doSerialize(*regionType, nbt["regionType"]);
if (region) region->serialize(nbt["region"].emplace<CompoundTag>());
}
void PlayerState::deserialize(CompoundTag const& nbt) {
if (nbt.contains("mainPos")) {
mainPos.emplace();
ll::reflection::deserialize(mainPos->data, nbt["mainPos"]);
setMainPosInternal();
}
if (nbt.contains("vicePos")) {
vicePos.emplace();
ll::reflection::deserialize(vicePos->data, nbt["vicePos"]);
setVicePosInternal();
}
if (nbt.contains("regionType")) {
ll::reflection::deserialize(*regionType, nbt["regionType"]);
}
if (nbt.contains("region")) {
region = Region::create(nbt["region"].get<CompoundTag>());
}
}
} // namespace we
45 changes: 45 additions & 0 deletions src/data/PlayerState.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#pragma once

#include "region/Region.h"
#include "Config.h"

#include <mc/deps/core/mce/UUID.h>
#include <mc/world/level/Tick.h>

namespace we {
class PlayerStateManager;
class PlayerState {
friend PlayerStateManager;
std::atomic_bool mutable mDirty;
std::atomic<std::chrono::system_clock::time_point> mutable lastUsedTime;

std::atomic<Tick> mutable lastLeftClick;
std::atomic<Tick> mutable lastRightClick;

mce::UUID uuid;

std::optional<WithGeo<WithDim<BlockPos>>> mainPos;
std::optional<WithGeo<WithDim<BlockPos>>> vicePos;

std::optional<RegionType> regionType;
std::shared_ptr<Region> region;

void setMainPosInternal();
void setVicePosInternal();

Region& getOrCreateRegion(WithDim<BlockPos> const&);

public:
Config::PlayerConfig config;

bool dirty() const { return mDirty; }

PlayerState(mce::UUID const& uuid);

bool setMainPos(WithDim<BlockPos> const&);
bool setVicePos(WithDim<BlockPos> const&);

void serialize(CompoundTag&) const;
void deserialize(CompoundTag const&);
};
} // namespace we
Loading

0 comments on commit 9675c9d

Please sign in to comment.