Skip to content

Commit

Permalink
[ChatCommandHandler] '/save' and '/load' commands removed. '/help' co…
Browse files Browse the repository at this point in the history
…mmand added.
  • Loading branch information
Unarelith committed Jun 23, 2020
1 parent 785be03 commit 21bafc0
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 70 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,16 @@ 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))

### Missing features

- 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

Expand Down
1 change: 0 additions & 1 deletion source/client/states/WorldSelectionState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
2 changes: 1 addition & 1 deletion source/client/states/WorldSelectionState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class WorldSelectionState : public InterfaceState {

TitleScreenState *m_titleScreen = nullptr;

MenuWidget m_menuWidget;
MenuWidget m_menuWidget{1, 8};

TextButton m_cancelButton;
};
Expand Down
102 changes: 40 additions & 62 deletions source/server/network/ChatCommandHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
}
}

17 changes: 13 additions & 4 deletions source/server/network/ChatCommandHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#ifndef CHATCOMMANDHANDLER_HPP_
#define CHATCOMMANDHANDLER_HPP_

#include <map>
#include <string>
#include <vector>

Expand All @@ -35,21 +36,29 @@ 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) {}

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_

0 comments on commit 21bafc0

Please sign in to comment.