From ddf9a7fa20113cf61341424b2a2aee0e07b8efc8 Mon Sep 17 00:00:00 2001 From: Alpyne Date: Wed, 4 Nov 2020 16:32:44 -0800 Subject: [PATCH] Functional block breaking! --- src/DedicatedServer.cpp | 13 +++++++++++++ src/world/World.cpp | 16 +++++++++++++++- src/world/World.h | 13 +++++++++++-- 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/src/DedicatedServer.cpp b/src/DedicatedServer.cpp index 5160fe4..5f22011 100644 --- a/src/DedicatedServer.cpp +++ b/src/DedicatedServer.cpp @@ -328,6 +328,19 @@ namespace Feather template <> void DedicatedServer::HandlePacket(MinecraftClient& client, const Play::ServerboundPlayerAction& action) { + using Action = Play::ServerboundPlayerAction::Action; + switch (action.action) + { + case Action::StartBreakBlock: break; + case Action::AbortBreakBlock: break; + case Action::FinishedBreakBlock: + m_worldManager.GetOverworld()->SetBlock(action.pos, World::GLOBAL_PALETTE.GetDefault()); + break; + case Action::DropItemStack: break; + case Action::DropItem: break; + case Action::ReleaseUseItem: break; + case Action::SwapItemHands: break; + } } } diff --git a/src/world/World.cpp b/src/world/World.cpp index 3b6518e..a42e893 100644 --- a/src/world/World.cpp +++ b/src/world/World.cpp @@ -83,6 +83,20 @@ namespace Feather return chunks[pos].get(); } + bool World::SetBlock(const BlockPos& pos, const BlockState& state) + { + if (IsOutsideBuildHeight(pos)) return false; + + Chunk* chunk = GetChunk(ChunkPos(pos)); + + Log::Debug("World::SetBlock at BlockPos({}, {}, {}) -> ChunkPos({}, {})", + pos.x, pos.y, pos.z, + chunk->pos.x, chunk->pos.z + ); + + return chunk->SetBlock(pos, state); + } + std::unique_ptr World::LoadChunk(ChunkPos pos) { fs::path worldPath = fs::path(m_name); @@ -136,4 +150,4 @@ namespace Feather return std::make_unique(pos, std::move(nbt)); } -}; \ No newline at end of file +}; diff --git a/src/world/World.h b/src/world/World.h index 74a0496..ad77a51 100644 --- a/src/world/World.h +++ b/src/world/World.h @@ -19,6 +19,10 @@ namespace Feather class World { + public: + // Min and max build height, inclusive + static constexpr int MIN_BUILD_HEIGHT = 0, MAX_BUILD_HEIGHT = 255; + public: World(std::string name); @@ -29,16 +33,21 @@ namespace Feather Chunk* GetChunk(ChunkPos pos); + bool SetBlock(const BlockPos& pos, const BlockState& state); + LevelData GetLevelData() const { return m_levelData; } + inline bool IsOutsideBuildHeight(BlockPos pos) { return IsOutsideBuildHeight(pos.y); } + inline bool IsOutsideBuildHeight(int y) { return y < MIN_BUILD_HEIGHT || y > MAX_BUILD_HEIGHT; } + private: std::unique_ptr LoadChunk(ChunkPos pos); - Lockable>> m_chunks; + LockableMap> m_chunks; LevelData m_levelData; // TODO nuke this shit std::string m_name; }; -} \ No newline at end of file +}