diff --git a/README.md b/README.md index d94059782..9b9abed49 100644 --- a/README.md +++ b/README.md @@ -94,7 +94,7 @@ This list is non exhaustive. - Block metadata - Player model display (without animation) - Dimensions (like the Nether or the Ender in Minecraft) ([#80](https://github.com/Unarelith/OpenMiner/pull/80)) -- World loading/saving (using `/save ` and `/load ` commands, see [#26](https://github.com/Unarelith/OpenMiner/issues/26)) +- World loading/saving (see [#26](https://github.com/Unarelith/OpenMiner/issues/26)) - Texture pack system (partially implemented, see [#34](https://github.com/Unarelith/OpenMiner/issues/34)) - Entities ([#90](https://github.com/Unarelith/OpenMiner/pull/90)) @@ -102,7 +102,8 @@ This list is non exhaustive. - Fluid propagation ([#62](https://github.com/Unarelith/OpenMiner/issues/62)) - Day/night cycle with sun/moon display ([#73](https://github.com/Unarelith/OpenMiner/issues/73)) -- Real worldgen (seed-based, cave tunnels) ([#79](https://github.com/Unarelith/OpenMiner/issues/79)) +- Seed-based worldgen ([#116](https://github.com/Unarelith/OpenMiner/issues/116)) +- Cave tunnels ([#118](https://github.com/Unarelith/OpenMiner/issues/118)) - Clouds ([#52](https://github.com/Unarelith/OpenMiner/pull/52)) - Particle system diff --git a/source/client/states/WorldSelectionState.cpp b/source/client/states/WorldSelectionState.cpp index 7cc1b9978..6711105fd 100644 --- a/source/client/states/WorldSelectionState.cpp +++ b/source/client/states/WorldSelectionState.cpp @@ -40,7 +40,6 @@ WorldSelectionState::WorldSelectionState(TitleScreenState *titleScreen) : InterfaceState(titleScreen), m_titleScreen(titleScreen) { m_menuWidget.setScale(Config::guiScale, Config::guiScale); - m_menuWidget.addButton("New world", [this](TextButton &) { m_stateStack->push(m_titleScreen); }); diff --git a/source/client/states/WorldSelectionState.hpp b/source/client/states/WorldSelectionState.hpp index cd32a75c0..41c3190e7 100644 --- a/source/client/states/WorldSelectionState.hpp +++ b/source/client/states/WorldSelectionState.hpp @@ -48,7 +48,7 @@ class WorldSelectionState : public InterfaceState { TitleScreenState *m_titleScreen = nullptr; - MenuWidget m_menuWidget; + MenuWidget m_menuWidget{1, 8}; TextButton m_cancelButton; }; diff --git a/source/server/network/ChatCommandHandler.cpp b/source/server/network/ChatCommandHandler.cpp index 5df071924..f17411ff0 100644 --- a/source/server/network/ChatCommandHandler.cpp +++ b/source/server/network/ChatCommandHandler.cpp @@ -30,6 +30,7 @@ #include "ChatCommandHandler.hpp" #include "ClientInfo.hpp" #include "ServerCommandHandler.hpp" +#include "ServerConfig.hpp" #include "WorldController.hpp" void ChatCommandHandler::parseCommand(const std::string &str, ClientInfo &client) const { @@ -42,89 +43,38 @@ void ChatCommandHandler::parseCommand(const std::string &str, ClientInfo &client command.emplace_back(line); } - std::unordered_map commands = { - {"tp", &ChatCommandHandler::teleportationCommand}, - {"save", &ChatCommandHandler::saveCommand}, - {"load", &ChatCommandHandler::loadCommand}, - {"stop", &ChatCommandHandler::stopCommand}, - {"option", &ChatCommandHandler::optionCommand}, - }; - if (!command.empty()) { - auto it = commands.find(command.at(0)); - if (it != commands.end()) { + auto it = m_commands.find(command.at(0)); + if (it != m_commands.end()) { (this->*it->second)(command, client); } else { - m_server.sendChatMessage(0, "Unrecognized command: " + command.at(0)); + m_server.sendChatMessage(0, "Unrecognized command: " + command.at(0), &client); std::stringstream commandList; - for (auto &it : commands) { + for (auto &it : m_commands) { if (commandList.tellp() != std::streampos(0)) commandList << ", "; commandList << it.first; } - m_server.sendChatMessage(0, "Available commands are: " + commandList.str()); + m_server.sendChatMessage(0, "Available commands are: " + commandList.str(), &client); } } } -void ChatCommandHandler::teleportationCommand(const std::vector &command, ClientInfo &client) const { - if (command.size() != 4) { - m_server.sendChatMessage(0, "Usage: /tp x y z", &client); +void ChatCommandHandler::helpCommand(const std::vector &command, ClientInfo &client) const { + if (command.size() > 1) { + m_server.sendChatMessage(0, "Usage: /help", &client); } else { - try { - s32 x = std::stoi(command.at(1)); - s32 y = std::stoi(command.at(2)); - s32 z = std::stoi(command.at(3)); - - m_server.setPlayerPosition(client.id, x, y, z); - - m_server.sendPlayerPosUpdate(client.id, true); - - m_server.sendChatMessage(0, "Teleported to " + std::to_string(x) + " " + std::to_string(y) + " " + std::to_string(z), &client); - } - catch (std::out_of_range &e) { - m_server.sendChatMessage(0, "Invalid coordinates", &client); + m_server.sendChatMessage(0, "Available commands are:", &client); + for (auto &it : m_commands) { + m_server.sendChatMessage(0, "/" + it.first, &client); } } } -void ChatCommandHandler::saveCommand(const std::vector &command, ClientInfo &client) const { - if (command.size() != 2) { - m_server.sendChatMessage(0, "Usage: /save ", &client); - } - else { - std::string name = command.at(1); - - m_worldController.save(name); - - m_server.sendChatMessage(0, "Saved '" + name + "'", &client); - } -} - -void ChatCommandHandler::loadCommand(const std::vector &command, ClientInfo &client) const { - if (command.size() != 2) { - m_server.sendChatMessage(0, "Usage: /load ", &client); - } - else { - std::string name = command.at(1); - - m_worldController.load(name); - - m_server.sendChatMessage(0, "Loaded '" + name + "'", &client); - } -} - -void ChatCommandHandler::stopCommand(const std::vector &, ClientInfo &client) const { - m_server.sendChatMessage(0, "Stopping server...", &client); - m_server.stopServer(); -} - -#include "ServerConfig.hpp" - void ChatCommandHandler::optionCommand(const std::vector &command, ClientInfo &client) const { if (command.size() < 2 || command.size() > 3) { m_server.sendChatMessage(0, "Usage: /option []", &client); @@ -162,3 +112,31 @@ void ChatCommandHandler::optionCommand(const std::vector &command, } } } + +void ChatCommandHandler::stopCommand(const std::vector &, ClientInfo &client) const { + m_server.sendChatMessage(0, "Stopping server...", &client); + m_server.stopServer(); +} + +void ChatCommandHandler::teleportationCommand(const std::vector &command, ClientInfo &client) const { + if (command.size() != 4) { + m_server.sendChatMessage(0, "Usage: /tp x y z", &client); + } + else { + try { + s32 x = std::stoi(command.at(1)); + s32 y = std::stoi(command.at(2)); + s32 z = std::stoi(command.at(3)); + + m_server.setPlayerPosition(client.id, x, y, z); + + m_server.sendPlayerPosUpdate(client.id, true); + + m_server.sendChatMessage(0, "Teleported to " + std::to_string(x) + " " + std::to_string(y) + " " + std::to_string(z), &client); + } + catch (std::out_of_range &e) { + m_server.sendChatMessage(0, "Invalid coordinates", &client); + } + } +} + diff --git a/source/server/network/ChatCommandHandler.hpp b/source/server/network/ChatCommandHandler.hpp index 204467159..7428fef76 100644 --- a/source/server/network/ChatCommandHandler.hpp +++ b/source/server/network/ChatCommandHandler.hpp @@ -27,6 +27,7 @@ #ifndef CHATCOMMANDHANDLER_HPP_ #define CHATCOMMANDHANDLER_HPP_ +#include #include #include @@ -35,6 +36,8 @@ class ServerCommandHandler; class WorldController; class ChatCommandHandler { + using CommandCallback = void (ChatCommandHandler::*)(const std::vector &command, ClientInfo &client) const; + public: ChatCommandHandler(ServerCommandHandler &server, WorldController &worldController) : m_server(server), m_worldController(worldController) {} @@ -42,14 +45,20 @@ class ChatCommandHandler { void parseCommand(const std::string &str, ClientInfo &client) const; private: - void teleportationCommand(const std::vector &command, ClientInfo &client) const; - void saveCommand(const std::vector &command, ClientInfo &client) const; - void loadCommand(const std::vector &command, ClientInfo &client) const; - void stopCommand(const std::vector &command, ClientInfo &client) const; + void helpCommand(const std::vector &command, ClientInfo &client) const; void optionCommand(const std::vector &command, ClientInfo &client) const; + void stopCommand(const std::vector &command, ClientInfo &client) const; + void teleportationCommand(const std::vector &command, ClientInfo &client) const; ServerCommandHandler &m_server; WorldController &m_worldController; + + std::map m_commands = { + {"help", &ChatCommandHandler::helpCommand}, + {"option", &ChatCommandHandler::optionCommand}, + {"stop", &ChatCommandHandler::stopCommand}, + {"tp", &ChatCommandHandler::teleportationCommand}, + }; }; #endif // CHATCOMMANDHANDLER_HPP_