2019-03-17 17:22:53 +01:00
|
|
|
/*
|
|
|
|
* =====================================================================================
|
|
|
|
*
|
2020-02-08 18:34:26 +09:00
|
|
|
* OpenMiner
|
2020-02-25 01:42:10 +09:00
|
|
|
*
|
2020-02-08 18:34:26 +09:00
|
|
|
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <openminer@unarelith.net>
|
2020-02-25 01:42:10 +09:00
|
|
|
* Copyright (C) 2019-2020 the OpenMiner contributors (see CONTRIBUTORS.md)
|
|
|
|
*
|
|
|
|
* This file is part of OpenMiner.
|
2019-03-17 17:22:53 +01:00
|
|
|
*
|
2020-02-25 01:42:10 +09:00
|
|
|
* OpenMiner is free software; you can redistribute it and/or
|
2020-02-08 18:34:26 +09:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2019-03-17 17:22:53 +01:00
|
|
|
*
|
2020-02-25 01:42:10 +09:00
|
|
|
* OpenMiner is distributed in the hope that it will be useful,
|
2020-02-08 18:34:26 +09:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
2019-03-17 17:22:53 +01:00
|
|
|
*
|
2020-02-08 18:34:26 +09:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
2020-02-25 01:42:10 +09:00
|
|
|
* along with OpenMiner; if not, write to the Free Software Foundation, Inc.,
|
2020-02-08 18:34:26 +09:00
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2019-03-17 17:22:53 +01:00
|
|
|
*
|
|
|
|
* =====================================================================================
|
|
|
|
*/
|
2020-02-11 15:00:03 +09:00
|
|
|
#include "BlockData.hpp"
|
2019-03-17 17:22:53 +01:00
|
|
|
#include "Registry.hpp"
|
2019-04-07 18:20:15 +02:00
|
|
|
#include "ScriptEngine.hpp"
|
2019-03-17 17:22:53 +01:00
|
|
|
#include "Server.hpp"
|
|
|
|
#include "ServerBlock.hpp"
|
|
|
|
#include "ServerPlayer.hpp"
|
|
|
|
#include "ServerCommandHandler.hpp"
|
2020-03-08 14:44:23 +01:00
|
|
|
#include "WorldController.hpp"
|
2019-03-17 17:22:53 +01:00
|
|
|
|
2020-03-04 14:38:31 +01:00
|
|
|
void ServerCommandHandler::sendBlockDataUpdate(s32 x, s32 y, s32 z, const BlockData *blockData, const ClientInfo *client) const {
|
2020-02-11 15:00:03 +09:00
|
|
|
sf::Packet packet;
|
|
|
|
packet << Network::Command::BlockDataUpdate << x << y << z
|
|
|
|
<< blockData->meta << blockData->useAltTiles;
|
|
|
|
|
|
|
|
if (!client)
|
|
|
|
m_server.sendToAllClients(packet);
|
|
|
|
else
|
|
|
|
client->tcpSocket->send(packet);
|
|
|
|
}
|
|
|
|
|
2020-03-04 14:38:31 +01:00
|
|
|
void ServerCommandHandler::sendBlockInvUpdate(s32 x, s32 y, s32 z, const Inventory &inventory, const ClientInfo *client) const {
|
2020-02-11 15:00:03 +09:00
|
|
|
sf::Packet packet;
|
|
|
|
packet << Network::Command::BlockInvUpdate << x << y << z << inventory;
|
|
|
|
|
|
|
|
if (!client)
|
|
|
|
m_server.sendToAllClients(packet);
|
|
|
|
else
|
|
|
|
client->tcpSocket->send(packet);
|
|
|
|
}
|
|
|
|
|
2020-03-04 14:38:31 +01:00
|
|
|
void ServerCommandHandler::sendPlayerPosUpdate(u16 clientID, bool isTeleportation, const ClientInfo *client) const {
|
2020-02-22 01:44:00 +09:00
|
|
|
const ServerPlayer &player = m_players.at(clientID);
|
|
|
|
|
2020-02-11 15:00:03 +09:00
|
|
|
sf::Packet packet;
|
|
|
|
packet << Network::Command::PlayerPosUpdate;
|
|
|
|
packet << clientID;
|
|
|
|
packet << player.x() << player.y() << player.z();
|
2020-02-22 01:44:00 +09:00
|
|
|
packet << isTeleportation;
|
|
|
|
|
|
|
|
if (!client)
|
|
|
|
m_server.sendToAllClients(packet);
|
|
|
|
else
|
|
|
|
client->tcpSocket->send(packet);
|
|
|
|
}
|
|
|
|
|
2020-03-08 15:28:46 +01:00
|
|
|
void ServerCommandHandler::sendPlayerChangeDimension(u16 clientID, s32 x, s32 y, s32 z, u16 dimension, const ClientInfo *client) const {
|
|
|
|
sf::Packet packet;
|
|
|
|
packet << Network::Command::PlayerChangeDimension;
|
|
|
|
packet << clientID << x << y << z << dimension;
|
|
|
|
|
|
|
|
if (!client)
|
|
|
|
m_server.sendToAllClients(packet);
|
|
|
|
else
|
|
|
|
client->tcpSocket->send(packet);
|
|
|
|
}
|
|
|
|
|
2020-03-04 14:38:31 +01:00
|
|
|
void ServerCommandHandler::sendChatMessage(u16 clientID, const std::string &message, const ClientInfo *client) const {
|
2020-02-22 01:44:00 +09:00
|
|
|
sf::Packet packet;
|
|
|
|
packet << Network::Command::ChatMessage << clientID << message;
|
2020-02-11 15:00:03 +09:00
|
|
|
|
|
|
|
if (!client)
|
|
|
|
m_server.sendToAllClients(packet);
|
|
|
|
else
|
|
|
|
client->tcpSocket->send(packet);
|
|
|
|
}
|
|
|
|
|
2019-03-17 17:22:53 +01:00
|
|
|
void ServerCommandHandler::setupCallbacks() {
|
2020-03-04 14:38:31 +01:00
|
|
|
m_server.setConnectionCallback([this](ClientInfo &client) {
|
2019-03-17 17:22:53 +01:00
|
|
|
sf::Packet packet;
|
|
|
|
packet << Network::Command::RegistryData;
|
|
|
|
m_registry.serialize(packet);
|
|
|
|
client.tcpSocket->send(packet);
|
|
|
|
|
2019-12-30 20:19:16 +09:00
|
|
|
// FIXME: Duplicated below, why?
|
2020-01-10 16:08:57 +09:00
|
|
|
// Here the new client is not part of m_players though
|
2019-04-08 15:29:19 +02:00
|
|
|
for (auto &it : m_players) {
|
|
|
|
sf::Packet spawnPacket;
|
|
|
|
spawnPacket << Network::Command::PlayerSpawn << it.first;
|
|
|
|
spawnPacket << it.second.x() << it.second.y() << it.second.z();
|
|
|
|
client.tcpSocket->send(spawnPacket);
|
|
|
|
}
|
2019-04-07 18:20:15 +02:00
|
|
|
|
2020-01-19 18:22:50 +09:00
|
|
|
m_players.emplace(client.id, client);
|
2019-04-08 15:29:19 +02:00
|
|
|
|
|
|
|
auto &player = m_players.at(client.id);
|
2020-01-17 14:39:29 +09:00
|
|
|
player.setPosition(m_spawnPosition.x, m_spawnPosition.y, m_spawnPosition.z);
|
2019-04-08 15:29:19 +02:00
|
|
|
|
2020-02-15 13:54:02 +09:00
|
|
|
// FIXME: Find a better way to give starting items
|
2019-04-08 15:29:19 +02:00
|
|
|
m_scriptEngine.lua()["init"](player);
|
2019-04-07 18:20:15 +02:00
|
|
|
|
2019-03-17 17:22:53 +01:00
|
|
|
sf::Packet invPacket;
|
|
|
|
invPacket << Network::Command::PlayerInvUpdate << client.id;
|
2019-04-07 18:20:15 +02:00
|
|
|
invPacket << m_players.at(client.id).inventory();
|
2019-03-17 17:22:53 +01:00
|
|
|
client.tcpSocket->send(invPacket);
|
|
|
|
|
2019-12-30 20:19:16 +09:00
|
|
|
// FIXME: Duplicated above, why?
|
2019-03-17 17:22:53 +01:00
|
|
|
sf::Packet spawnPacket;
|
|
|
|
spawnPacket << Network::Command::PlayerSpawn << client.id;
|
|
|
|
spawnPacket << m_spawnPosition.x << m_spawnPosition.y << m_spawnPosition.z;
|
|
|
|
m_server.sendToAllClients(spawnPacket);
|
|
|
|
|
2020-01-19 18:22:50 +09:00
|
|
|
// m_world.sendSpawnData(client, player);
|
2019-03-17 17:22:53 +01:00
|
|
|
});
|
|
|
|
|
2020-03-04 14:38:31 +01:00
|
|
|
m_server.setCommandCallback(Network::Command::ClientDisconnect, [this](ClientInfo &client, sf::Packet &) {
|
2020-02-24 17:20:49 +09:00
|
|
|
m_players.erase(client.id);
|
|
|
|
});
|
|
|
|
|
2020-03-04 14:38:31 +01:00
|
|
|
m_server.setCommandCallback(Network::Command::ChunkRequest, [this](ClientInfo &client, sf::Packet &packet) {
|
2020-01-20 13:40:07 +09:00
|
|
|
s32 cx, cy, cz;
|
|
|
|
packet >> cx >> cy >> cz;
|
|
|
|
|
2020-03-08 14:44:23 +01:00
|
|
|
getWorldForClient(client.id).sendRequestedData(client, cx, cy, cz);
|
2020-01-20 13:40:07 +09:00
|
|
|
});
|
|
|
|
|
2020-03-04 14:38:31 +01:00
|
|
|
m_server.setCommandCallback(Network::Command::PlayerInvUpdate, [this](ClientInfo &client, sf::Packet &packet) {
|
2019-03-17 17:22:53 +01:00
|
|
|
u16 clientId;
|
|
|
|
packet >> clientId;
|
|
|
|
if (clientId == client.id) {
|
2019-04-07 18:20:15 +02:00
|
|
|
packet >> m_players.at(client.id).inventory();
|
2019-03-17 17:22:53 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-03-04 14:38:31 +01:00
|
|
|
m_server.setCommandCallback(Network::Command::PlayerPosUpdate, [this](ClientInfo &client, sf::Packet &packet) {
|
2020-03-02 20:41:47 +01:00
|
|
|
double x, y, z;
|
2019-04-07 17:20:35 +02:00
|
|
|
u16 clientId;
|
|
|
|
packet >> clientId;
|
|
|
|
packet >> x >> y >> z;
|
2019-03-17 17:22:53 +01:00
|
|
|
|
2019-04-07 17:20:35 +02:00
|
|
|
if (clientId == client.id)
|
2019-04-07 18:20:15 +02:00
|
|
|
m_players.at(client.id).setPosition(x, y, z);
|
2019-03-17 17:22:53 +01:00
|
|
|
});
|
|
|
|
|
2020-03-08 14:44:23 +01:00
|
|
|
m_server.setCommandCallback(Network::Command::PlayerPlaceBlock, [this](ClientInfo &client, sf::Packet &packet) {
|
2019-03-17 17:22:53 +01:00
|
|
|
s32 x, y, z;
|
|
|
|
u32 block;
|
|
|
|
packet >> x >> y >> z >> block;
|
2020-03-08 14:44:23 +01:00
|
|
|
|
|
|
|
ServerWorld &world = getWorldForClient(client.id);
|
|
|
|
world.setBlock(x, y, z, block & 0xffff);
|
|
|
|
world.setData(x, y, z, block >> 16);
|
2019-03-17 17:22:53 +01:00
|
|
|
|
|
|
|
sf::Packet answer;
|
|
|
|
answer << Network::Command::BlockUpdate << x << y << z << block;
|
|
|
|
m_server.sendToAllClients(answer);
|
|
|
|
});
|
|
|
|
|
2020-03-08 14:44:23 +01:00
|
|
|
m_server.setCommandCallback(Network::Command::PlayerDigBlock, [this](ClientInfo &client, sf::Packet &packet) {
|
2019-03-17 17:22:53 +01:00
|
|
|
s32 x, y, z;
|
|
|
|
packet >> x >> y >> z;
|
2020-03-08 14:44:23 +01:00
|
|
|
getWorldForClient(client.id).setBlock(x, y, z, 0);
|
2019-03-17 17:22:53 +01:00
|
|
|
|
|
|
|
sf::Packet answer;
|
|
|
|
answer << Network::Command::BlockUpdate << x << y << z << u32(0);
|
|
|
|
m_server.sendToAllClients(answer);
|
|
|
|
});
|
|
|
|
|
2020-03-04 14:38:31 +01:00
|
|
|
m_server.setCommandCallback(Network::Command::PlayerInventory, [this](ClientInfo &client, sf::Packet &packet) {
|
2020-02-15 14:42:29 +09:00
|
|
|
u16 screenWidth, screenHeight;
|
|
|
|
u8 guiScale;
|
|
|
|
packet >> screenWidth >> screenHeight >> guiScale;
|
|
|
|
|
2020-03-07 15:41:19 +01:00
|
|
|
sol::unsafe_function func = m_scriptEngine.lua()["show_inventory"];
|
|
|
|
|
|
|
|
try {
|
|
|
|
func(client, screenWidth, screenHeight, guiScale);
|
|
|
|
}
|
|
|
|
catch (const sol::error &error) {
|
|
|
|
DEBUG("Failed to send inventory GUI\n", error.what());
|
|
|
|
}
|
2020-02-08 02:48:39 +09:00
|
|
|
});
|
|
|
|
|
2020-03-04 14:38:31 +01:00
|
|
|
m_server.setCommandCallback(Network::Command::PlayerCreativeWindow, [this](ClientInfo &client, sf::Packet &packet) {
|
2020-02-29 18:07:01 +01:00
|
|
|
u16 screenWidth, screenHeight;
|
|
|
|
u8 guiScale;
|
|
|
|
packet >> screenWidth >> screenHeight >> guiScale;
|
|
|
|
|
2020-03-07 15:41:19 +01:00
|
|
|
sol::unsafe_function func = m_scriptEngine.lua()["show_creative_window"];
|
|
|
|
|
|
|
|
try {
|
|
|
|
func(client, screenWidth, screenHeight, guiScale);
|
|
|
|
}
|
|
|
|
catch (const sol::error &error) {
|
|
|
|
DEBUG("Failed to send creative window GUI\n", error.what());
|
|
|
|
}
|
2020-02-29 18:07:01 +01:00
|
|
|
});
|
|
|
|
|
2020-03-04 14:38:31 +01:00
|
|
|
m_server.setCommandCallback(Network::Command::BlockActivated, [this](ClientInfo &client, sf::Packet &packet) {
|
2019-03-17 17:22:53 +01:00
|
|
|
s32 x, y, z;
|
2020-02-15 14:42:29 +09:00
|
|
|
u16 screenWidth, screenHeight;
|
|
|
|
u8 guiScale;
|
|
|
|
packet >> x >> y >> z >> screenWidth >> screenHeight >> guiScale;
|
2019-03-17 17:22:53 +01:00
|
|
|
|
2020-03-08 14:44:23 +01:00
|
|
|
ServerWorld &world = getWorldForClient(client.id);
|
|
|
|
|
|
|
|
u16 id = world.getBlock(x, y, z);
|
2020-03-08 15:28:46 +01:00
|
|
|
ServerBlock &block = (ServerBlock &)(m_registry.getBlock(id));
|
|
|
|
block.onBlockActivated({x, y, z}, m_players.at(client.id), world, client, *this, screenWidth, screenHeight, guiScale);
|
2019-03-17 17:22:53 +01:00
|
|
|
});
|
|
|
|
|
2020-03-08 14:44:23 +01:00
|
|
|
m_server.setCommandCallback(Network::Command::BlockInvUpdate, [this](ClientInfo &client, sf::Packet &packet) {
|
2019-03-17 17:22:53 +01:00
|
|
|
gk::Vector3<s32> pos;
|
|
|
|
packet >> pos.x >> pos.y >> pos.z;
|
|
|
|
|
2020-03-08 14:44:23 +01:00
|
|
|
BlockData *data = getWorldForClient(client.id).getBlockData(pos.x, pos.y, pos.z);
|
2019-03-17 17:22:53 +01:00
|
|
|
if (data)
|
|
|
|
packet >> data->inventory;
|
|
|
|
else
|
|
|
|
DEBUG("BlockInvUpdate: No block data found at", pos.x, pos.y, pos.z);
|
|
|
|
});
|
|
|
|
|
2020-03-08 14:44:23 +01:00
|
|
|
m_server.setCommandCallback(Network::Command::BlockDataUpdate, [this](ClientInfo &client, sf::Packet &packet) {
|
2019-03-17 17:22:53 +01:00
|
|
|
gk::Vector3<s32> pos;
|
|
|
|
packet >> pos.x >> pos.y >> pos.z;
|
|
|
|
|
2020-03-08 14:44:23 +01:00
|
|
|
BlockData *data = getWorldForClient(client.id).getBlockData(pos.x, pos.y, pos.z);
|
2019-03-17 17:22:53 +01:00
|
|
|
if (data) {
|
2020-02-08 17:45:20 +09:00
|
|
|
packet >> data->meta >> data->useAltTiles;
|
2019-03-17 17:22:53 +01:00
|
|
|
}
|
|
|
|
});
|
2020-02-21 17:25:56 +09:00
|
|
|
|
2020-03-04 14:38:31 +01:00
|
|
|
m_server.setCommandCallback(Network::Command::ChatMessage, [this](ClientInfo &client, sf::Packet &packet) {
|
2020-02-22 01:44:00 +09:00
|
|
|
u16 clientID;
|
|
|
|
std::string message;
|
|
|
|
packet >> clientID >> message;
|
2020-02-21 17:25:56 +09:00
|
|
|
|
2020-02-22 02:21:42 +09:00
|
|
|
if (message[0] != '/' || (message.length() > 1 && message[1] == '/')) {
|
|
|
|
if (message[0] == '/' && message.length() > 1 && message[1] == '/')
|
|
|
|
sendChatMessage(clientID, message.substr(1));
|
|
|
|
else
|
|
|
|
sendChatMessage(clientID, message);
|
2020-02-22 01:44:00 +09:00
|
|
|
}
|
|
|
|
// FIXME: Do a proper implementation later
|
|
|
|
else {
|
|
|
|
std::stringstream sstream;
|
|
|
|
sstream << message.substr(1);
|
|
|
|
|
|
|
|
std::vector<std::string> command;
|
|
|
|
std::string line;
|
|
|
|
while (std::getline(sstream, line, ' ')) {
|
|
|
|
command.emplace_back(line);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!command.empty()) {
|
|
|
|
if (command.at(0) == "tp") {
|
|
|
|
if (command.size() != 4) {
|
|
|
|
// FIXME: ID 0 should be server messages
|
|
|
|
sendChatMessage(0, "Usage: /tp x y z", &client);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
s32 x = std::stoi(command.at(1));
|
|
|
|
s32 y = std::stoi(command.at(2));
|
|
|
|
s32 z = std::stoi(command.at(3));
|
|
|
|
|
|
|
|
m_players.at(clientID).setPosition(x, y, z);
|
|
|
|
|
|
|
|
sendPlayerPosUpdate(clientID, true);
|
|
|
|
|
|
|
|
sendChatMessage(0, "Teleported to " + std::to_string(x) + " " + std::to_string(y) + " " + std::to_string(z), &client);
|
|
|
|
}
|
|
|
|
}
|
2020-02-22 02:21:42 +09:00
|
|
|
else {
|
|
|
|
sendChatMessage(0, "Unrecognized command: " + command.at(0));
|
|
|
|
sendChatMessage(0, "Available commands are: tp");
|
|
|
|
}
|
2020-02-22 01:44:00 +09:00
|
|
|
}
|
|
|
|
}
|
2020-02-21 17:25:56 +09:00
|
|
|
});
|
2019-03-17 17:22:53 +01:00
|
|
|
}
|
|
|
|
|
2020-03-08 14:44:23 +01:00
|
|
|
inline ServerWorld &ServerCommandHandler::getWorldForClient(u16 clientID) {
|
|
|
|
auto it = m_players.find(clientID);
|
|
|
|
if (it == m_players.end())
|
|
|
|
throw EXCEPTION("Player instance not found for client", clientID);
|
|
|
|
|
|
|
|
return m_worldController.getWorld(it->second.dimension());
|
|
|
|
}
|
|
|
|
|