Skip to content

Commit

Permalink
Server is now aware of each player's held item. Closed #110.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Jun 2, 2020
1 parent 67bf226 commit 301d33f
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 25 deletions.
1 change: 1 addition & 0 deletions docs/lua-api-cpp.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
## ServerPlayer

- `const ClientInfo &client()`
- `ItemStack held_item_stack()`

## ServerWorld

Expand Down
10 changes: 5 additions & 5 deletions source/client/hud/BlockCursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ void BlockCursor::onEvent(const sf::Event &event, const Hotbar &hotbar) {

u32 blockId = m_world.getBlock(m_selectedBlock.x, m_selectedBlock.y, m_selectedBlock.z);
const Block &block = Registry::getInstance().getBlock(blockId);
const Item &item = Registry::getInstance().getItem(hotbar.currentItem());
const Item &item = hotbar.currentItem();

bool blockActivationSent = false;
if (block.id() && block.canBeActivated() && !gk::GamePad::isKeyPressed(GameKey::Sneak)) {
m_client.sendBlockActivated(m_selectedBlock);
blockActivationSent = true;
}

if (block.id() && !blockActivationSent && hotbar.currentItem() && item.isBlock()) {
if (block.id() && !blockActivationSent && hotbar.currentItem().id() && item.isBlock()) {
s8 face = m_selectedBlock.w;

s32 x = m_selectedBlock.x;
Expand All @@ -87,19 +87,19 @@ void BlockCursor::onEvent(const sf::Event &event, const Hotbar &hotbar) {
const Block &block = Registry::getInstance().getBlock(blockId);
if (!blockId || block.drawType() == BlockDrawType::Liquid) {
// Second, we check if the new block is not inside the player
const Block &newBlock = Registry::getInstance().getBlock(hotbar.currentItem());
const Block &newBlock = Registry::getInstance().getBlock(hotbar.currentItem().id());
gk::FloatBox boundingBox = newBlock.boundingBox() + gk::Vector3f(x - m_player.x(), y - m_player.y(), z - m_player.z());
gk::FloatBox playerBoundingBox = m_player.hitbox();
if (!boundingBox.intersects(playerBoundingBox)) {
u32 block = hotbar.currentItem();
u32 block = hotbar.currentItem().id();
if (newBlock.isRotatable()) {
u16 data = m_player.getOppositeDirection() & 0x3;
m_world.setData(x, y, z, data);

block |= data << 16;
}

m_world.setBlock(x, y, z, hotbar.currentItem());
m_world.setBlock(x, y, z, hotbar.currentItem().id());

m_client.sendPlayerPlaceBlock(x, y, z, block);

Expand Down
2 changes: 1 addition & 1 deletion source/client/hud/HUD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#include "HUD.hpp"

HUD::HUD(ClientPlayer &player, ClientWorld &world, ClientCommandHandler &client)
: m_hotbar(player.inventory()),
: m_hotbar(player.inventory(), client),
m_blockCursor(player, world, client),
m_debugOverlay(player, world),
m_chat(client.client())
Expand Down
13 changes: 10 additions & 3 deletions source/client/hud/Hotbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@
*
* =====================================================================================
*/
#include "ClientCommandHandler.hpp"
#include "Config.hpp"
#include "Hotbar.hpp"

Hotbar::Hotbar(Inventory &inventory, Widget *parent) : Widget(182, 22, parent), m_inventory(inventory) {
Hotbar::Hotbar(Inventory &inventory, ClientCommandHandler &client, Widget *parent)
: Widget(182, 22, parent), m_inventory(inventory), m_client(client)
{
m_background.load("texture-widgets");
m_background.setClipRect(0, 0, 182, 22);
m_background.setPosition(0, 0, 0);
Expand All @@ -39,10 +42,14 @@ Hotbar::Hotbar(Inventory &inventory, Widget *parent) : Widget(182, 22, parent),

void Hotbar::onEvent(const sf::Event &event) {
if (event.type == sf::Event::MouseWheelScrolled) {
if (event.mouseWheelScroll.delta < 0)
if (event.mouseWheelScroll.delta < 0) {
m_cursorPos = (m_cursorPos + 1) % 9;
else if (event.mouseWheelScroll.delta > 0)
m_client.sendPlayerHeldItemChanged(m_cursorPos, currentItem().id());
}
else if (event.mouseWheelScroll.delta > 0) {
m_cursorPos = (m_cursorPos == 0) ? 8 : m_cursorPos - 1;
m_client.sendPlayerHeldItemChanged(m_cursorPos, currentItem().id());
}

m_cursor.setPosition(-1 + 20 * m_cursorPos, -1, 0);
}
Expand Down
8 changes: 6 additions & 2 deletions source/client/hud/Hotbar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,18 @@
#include "Inventory.hpp"
#include "ItemWidget.hpp"

class ClientCommandHandler;

class Hotbar : public Widget {
public:
Hotbar(Inventory &inventory, Widget *parent = nullptr);
Hotbar(Inventory &inventory, ClientCommandHandler &client, Widget *parent = nullptr);

void onEvent(const sf::Event &event) override;

void update() override;

int cursorPos() const { return m_cursorPos; }
u16 currentItem() const { return m_inventory.getStack(m_cursorPos, 0).item().id(); }
const Item &currentItem() const { return m_inventory.getStack(m_cursorPos, 0).item(); }

private:
void draw(gk::RenderTarget &target, gk::RenderStates states) const override;
Expand All @@ -53,6 +55,8 @@ class Hotbar : public Widget {

Inventory &m_inventory;

ClientCommandHandler &m_client;

std::vector<ItemWidget> m_items;
};

Expand Down
7 changes: 7 additions & 0 deletions source/client/network/ClientCommandHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ void ClientCommandHandler::sendPlayerCreativeWindowRequest() {
m_client.send(packet);
}

void ClientCommandHandler::sendPlayerHeldItemChanged(u8 hotbarSlot, u16 itemID) {
Network::Packet packet;
packet << Network::Command::PlayerHeldItemChanged
<< hotbarSlot << itemID;
m_client.send(packet);
}

void ClientCommandHandler::sendBlockActivated(const glm::ivec4 &selectedBlock) {
Network::Packet packet;
packet << Network::Command::BlockActivated
Expand Down
1 change: 1 addition & 0 deletions source/client/network/ClientCommandHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class ClientCommandHandler {
void sendPlayerPlaceBlock(s32 x, s32 y, s32 z, u32 block);
void sendPlayerInventoryRequest();
void sendPlayerCreativeWindowRequest();
void sendPlayerHeldItemChanged(u8 hotbarSlot, u16 itemID);
void sendBlockActivated(const glm::ivec4 &selectedBlock);
void sendBlockInvUpdate(Inventory &inventory);
void sendChunkRequest(s32 chunkX, s32 chunkY, s32 chunkZ);
Expand Down
1 change: 1 addition & 0 deletions source/common/network/Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ std::string Network::commandToString(Network::Command command) {
{Network::Command::PlayerInventory, "PlayerInventory"},
{Network::Command::PlayerCreativeWindow, "PlayerCreativeWindow"},
{Network::Command::PlayerChangeDimension, "PlayerChangeDimension"},
{Network::Command::PlayerHeldItemChanged, "PlayerHeldItemChanged"},

{Network::Command::BlockUpdate, "BlockUpdate"},
{Network::Command::BlockActivated, "BlockActivated"},
Expand Down
27 changes: 14 additions & 13 deletions source/common/network/Network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,27 +55,28 @@ namespace Network {
PlayerInventory = 0x0c, // <TCP> [NetworkCommand][u16 screenWidth, screenHeight][u8 guiScale] (from Client only)
PlayerCreativeWindow = 0x0d, // <TCP> [NetworkCommand][u16 screenWidth, screenHeight][u8 guiScale] (from Client only)
PlayerChangeDimension = 0x0e, // <TCP> [NetworkCommand][u16 client id][s32 x, y, z][u16 dimension] (from Server only)
PlayerHeldItemChanged = 0x0f, // <TCP> [NetworkCommand][u8 hotbar slot][u16 item id (to check match with server)] (from Client only)

// Block commands
BlockUpdate = 0x0f, // <TCP> [NetworkCommand][s32 x, y, z][u32 block] (from Server only)
BlockActivated = 0x10, // <TCP> [NetworkCommand][s32 x, y, z][u16 screenWidth, screenHeight][u8 guiScale] (from Client only)
BlockGUIData = 0x11, // <TCP> [NetworkCommand][LuaGUIData data] (from Server only)
BlockInvUpdate = 0x12, // <TCP> [NetworkCommand][s32 x, y, z][[std::string item][u16 amount][u8 x, y]...] (both) [FIXME]
BlockDataUpdate = 0x13, // <TCP> [NetworkCommand][s32 x, y, z][u64 data] (both) [FIXME]
BlockUpdate = 0x10, // <TCP> [NetworkCommand][s32 x, y, z][u32 block] (from Server only)
BlockActivated = 0x11, // <TCP> [NetworkCommand][s32 x, y, z][u16 screenWidth, screenHeight][u8 guiScale] (from Client only)
BlockGUIData = 0x12, // <TCP> [NetworkCommand][LuaGUIData data] (from Server only)
BlockInvUpdate = 0x13, // <TCP> [NetworkCommand][s32 x, y, z][[std::string item][u16 amount][u8 x, y]...] (both) [FIXME]
BlockDataUpdate = 0x14, // <TCP> [NetworkCommand][s32 x, y, z][u64 data] (both) [FIXME]

// Registry commands
RegistryData = 0x14, // <TCP> [NetworkCommand][Block block] (from Server only)
RegistryData = 0x15, // <TCP> [NetworkCommand][Block block] (from Server only)

// Chat commands
ChatMessage = 0x15, // <TCP> [NetworkCommand][u16 client id][std::string message] (both)
ChatMessage = 0x16, // <TCP> [NetworkCommand][u16 client id][std::string message] (both)

// Entity commands
EntitySpawn = 0x16, // <TCP> [NetworkCommand][u32 entity id] (from Server only)
EntityDespawn = 0x17, // <TCP> [NetworkCommand][u32 entity id] (from Server only)
EntityPosition = 0x18, // <TCP> [NetworkCommand][u32 entity id][double x, double y, double z] (from Server only)
EntityRotation = 0x19, // <TCP> [NetworkCommand][u32 entity id][float w, float x, float y, float z] (from Server only)
EntityAnimation = 0x1a, // <TCP> [NetworkCommand][u32 entity id][AnimationComponent anim] (from Server only)
EntityDrawableDef = 0x1b, // <TCP> [NetworkCommand][u32 entity id][DrawableDef def] (from Server only)
EntitySpawn = 0x17, // <TCP> [NetworkCommand][u32 entity id] (from Server only)
EntityDespawn = 0x18, // <TCP> [NetworkCommand][u32 entity id] (from Server only)
EntityPosition = 0x19, // <TCP> [NetworkCommand][u32 entity id][double x, double y, double z] (from Server only)
EntityRotation = 0x1a, // <TCP> [NetworkCommand][u32 entity id][float w, float x, float y, float z] (from Server only)
EntityAnimation = 0x1b, // <TCP> [NetworkCommand][u32 entity id][AnimationComponent anim] (from Server only)
EntityDrawableDef = 0x1c, // <TCP> [NetworkCommand][u32 entity id][DrawableDef def] (from Server only)
};

std::string commandToString(Command command);
Expand Down
15 changes: 15 additions & 0 deletions source/server/network/ServerCommandHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,21 @@ void ServerCommandHandler::setupCallbacks() {
}
});

m_server.setCommandCallback(Network::Command::PlayerHeldItemChanged, [this](ClientInfo &client, Network::Packet &packet) {
ServerPlayer *player = m_players.getPlayer(client.id);
if (player) {
u8 hotbarSlot;
u16 itemID;
packet >> hotbarSlot >> itemID;
if (player->inventory().getStack(hotbarSlot, 0).item().id() != itemID)
gkWarning() << "PlayerHeldItemChanged:" << "Desync of item ID between client and server";

player->setHeldItemSlot(hotbarSlot);
}
else
gkError() << ("Failed to change held item of player " + std::to_string(client.id) + ": Player not found").c_str();
});

m_server.setCommandCallback(Network::Command::BlockActivated, [this](ClientInfo &client, Network::Packet &packet) {
ServerPlayer *player = m_players.getPlayer(client.id);
if (player) {
Expand Down
3 changes: 2 additions & 1 deletion source/server/world/ServerPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ ServerPlayer::ServerPlayer(ClientInfo &client) : m_client(client) {
void ServerPlayer::initUsertype(sol::state &lua) {
lua.new_usertype<ServerPlayer>("ServerPlayer",
sol::base_classes, sol::bases<Player>(),
"client", &ServerPlayer::client
"client", &ServerPlayer::client,
"held_item_stack", &ServerPlayer::heldItemStack
);
}

7 changes: 7 additions & 0 deletions source/server/world/ServerPlayer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#ifndef SERVERPLAYER_HPP_
#define SERVERPLAYER_HPP_

#include <gk/core/Debug.hpp>

#include "ClientInfo.hpp"
#include "Player.hpp"

Expand All @@ -36,10 +38,15 @@ class ServerPlayer : public Player {

const ClientInfo &client() const { return m_client; }

const ItemStack &heldItemStack() { return m_inventory.getStack(m_heldItemSlot, 0); }
void setHeldItemSlot(u8 heldItemSlot) { m_heldItemSlot = heldItemSlot; }

static void initUsertype(sol::state &lua);

private:
ClientInfo &m_client;

u8 m_heldItemSlot = 0;
};

#endif // SERVERPLAYER_HPP_

0 comments on commit 301d33f

Please sign in to comment.