2 #include "CLIContext.hpp"
3 #include "commands.hpp"
5 #include "../io/TokenStreamReader.hpp"
6 #include "../world/Entity.hpp"
7 #include "../world/Player.hpp"
11 #include <glm/gtx/io.hpp>
18 CLI::CLI(World &world)
21 AddCommand("tp", new TeleportCommand);
25 for (auto &entry : commands) {
30 void CLI::AddCommand(const string &name, Command *cmd) {
34 void CLI::Execute(CLIContext &ctx, const string &line) {
36 TokenStreamReader args(s);
37 if (!args.HasMore()) {
38 // ignore empty command line
41 if (args.Peek().type != Token::IDENTIFIER) {
42 ctx.Error("I don't understand");
46 args.ReadIdentifier(name);
47 auto entry = commands.find(name);
48 if (entry == commands.end()) {
49 ctx.Error(name + ": command not found");
53 entry->second->Execute(*this, ctx, args);
54 } catch (exception &e) {
55 ctx.Error(name + ": " + e.what());
57 ctx.Error(name + ": unknown execution error");
61 CLI::Command::~Command() {
66 void TeleportCommand::Execute(CLI &cli, CLIContext &ctx, TokenStreamReader &args) {
67 glm::vec3 pos(args.GetFloat(), args.GetFloat(), args.GetFloat());
68 EntityState state = ctx.GetPlayer().GetEntity().GetState();
69 state.pos = ExactLocation(pos).Sanitize();
70 ctx.GetPlayer().GetEntity().SetState(state);
73 msg << ctx.GetPlayer().Name() << " teleported to " << pos;
74 ctx.Broadcast(msg.str());