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 CLIContext::CLIContext(Player *p, Entity *e)
70 , effective_entity(e) {
72 original_entity = effective_entity = &p->GetEntity();
77 void TeleportCommand::Execute(CLI &, CLIContext &ctx, TokenStreamReader &args) {
78 if (!ctx.HasEntity()) {
79 ctx.Error("teleport needs entity to operate on");
83 glm::vec3 pos(args.GetFloat(), args.GetFloat(), args.GetFloat());
84 EntityState state = ctx.GetEntity().GetState();
85 state.pos = ExactLocation(pos).Sanitize();
86 ctx.GetEntity().SetState(state);
89 msg << ctx.GetEntity().Name() << " teleported to " << pos;
90 ctx.Broadcast(msg.str());