[ChatCommandHandler] '/save' and '/load' commands removed. '/help' command added.

This commit is contained in:
Quentin Bazin 2020-06-22 15:50:12 +02:00
parent 785be03847
commit 21bafc046e
5 changed files with 57 additions and 70 deletions

View File

@ -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 <name>` and `/load <name>` 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

View File

@ -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<WorldCreationState>(m_titleScreen);
});

View File

@ -48,7 +48,7 @@ class WorldSelectionState : public InterfaceState {
TitleScreenState *m_titleScreen = nullptr;
MenuWidget m_menuWidget;
MenuWidget m_menuWidget{1, 8};
TextButton m_cancelButton;
};

View File

@ -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<std::string, decltype(&ChatCommandHandler::teleportationCommand)> 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<std::string> &command, ClientInfo &client) const {
if (command.size() != 4) {
m_server.sendChatMessage(0, "Usage: /tp x y z", &client);
void ChatCommandHandler::helpCommand(const std::vector<std::string> &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<std::string> &command, ClientInfo &client) const {
if (command.size() != 2) {
m_server.sendChatMessage(0, "Usage: /save <name>", &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<std::string> &command, ClientInfo &client) const {
if (command.size() != 2) {
m_server.sendChatMessage(0, "Usage: /load <name>", &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<std::string> &, ClientInfo &client) const {
m_server.sendChatMessage(0, "Stopping server...", &client);
m_server.stopServer();
}
#include "ServerConfig.hpp"
void ChatCommandHandler::optionCommand(const std::vector<std::string> &command, ClientInfo &client) const {
if (command.size() < 2 || command.size() > 3) {
m_server.sendChatMessage(0, "Usage: /option <name> [<value>]", &client);
@ -162,3 +112,31 @@ void ChatCommandHandler::optionCommand(const std::vector<std::string> &command,
}
}
}
void ChatCommandHandler::stopCommand(const std::vector<std::string> &, ClientInfo &client) const {
m_server.sendChatMessage(0, "Stopping server...", &client);
m_server.stopServer();
}
void ChatCommandHandler::teleportationCommand(const std::vector<std::string> &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);
}
}
}

View File

@ -27,6 +27,7 @@
#ifndef CHATCOMMANDHANDLER_HPP_
#define CHATCOMMANDHANDLER_HPP_
#include <map>
#include <string>
#include <vector>
@ -35,6 +36,8 @@ class ServerCommandHandler;
class WorldController;
class ChatCommandHandler {
using CommandCallback = void (ChatCommandHandler::*)(const std::vector<std::string> &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<std::string> &command, ClientInfo &client) const;
void saveCommand(const std::vector<std::string> &command, ClientInfo &client) const;
void loadCommand(const std::vector<std::string> &command, ClientInfo &client) const;
void stopCommand(const std::vector<std::string> &command, ClientInfo &client) const;
void helpCommand(const std::vector<std::string> &command, ClientInfo &client) const;
void optionCommand(const std::vector<std::string> &command, ClientInfo &client) const;
void stopCommand(const std::vector<std::string> &command, ClientInfo &client) const;
void teleportationCommand(const std::vector<std::string> &command, ClientInfo &client) const;
ServerCommandHandler &m_server;
WorldController &m_worldController;
std::map<std::string, CommandCallback> m_commands = {
{"help", &ChatCommandHandler::helpCommand},
{"option", &ChatCommandHandler::optionCommand},
{"stop", &ChatCommandHandler::stopCommand},
{"tp", &ChatCommandHandler::teleportationCommand},
};
};
#endif // CHATCOMMANDHANDLER_HPP_