#include #include "common/network/Packets.h" #include "server/commands/CommandManager.h" static void commandTest(ServerState&, const CommandArguments& args) { std::cout << "test command" << std::endl; for(int i = 0; i < args.getLength(); i++) { std::cout << " - " << args[i] << std::endl; } } static void commandStop(ServerState& sc, const CommandArguments&) { sc.running = false; } static void commandSay(ServerState& sc, const CommandArguments& args) { StringBuffer<256> s; for(int i = 1; i < args.getLength(); i++) { s.append(args[i]); s.append(' '); } OutPacket out = OutPacket::reliable(260); out.writeU16(ServerPacket::CHAT); out.writeString(s); sc.server.send(out); std::cout << s << '\n'; } CommandManager::CommandManager() { commands.add("test", commandTest); commands.add("stop", commandStop); commands.add("say", commandSay); } void CommandManager::execute(ServerState& sc, const RawCommand& rawCommand) { CommandArguments args(rawCommand); if(args.getLength() == 0) { std::cout << "Invalid command syntax: '" << rawCommand << "'\n"; return; } CommandName command(args[0]); Command* c = commands.search(command); if(c == nullptr) { std::cout << "Unknown command: '" << command << "'" << std::endl; return; } (*c)(sc, args); }