From ae359de188d70838f9f699e14efa414a561e0cd9 Mon Sep 17 00:00:00 2001 From: Bigjango13 Date: Wed, 30 Oct 2024 00:45:35 -0700 Subject: [PATCH] Add entity.get/setAbsPos --- mods/src/api/README.md | 5 ++++- mods/src/api/api.cpp | 24 +++++++++++++++--------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/mods/src/api/README.md b/mods/src/api/README.md index ded01a34..f0bba383 100644 --- a/mods/src/api/README.md +++ b/mods/src/api/README.md @@ -103,7 +103,10 @@ Egdecases: Extras: - Compatibility mode, this is enabled by default, and can be changed with `reborn.disableCompatMode` or `reborn.enableCompatMode`. - - `events.clear` +- `entity.getAbsPos(id: int) -> {x: int, y: int, z: int}` + - Same as `player.getAbsPos`, but for the entity +- `entity.setAbsPos(id: int, x: int, y: int, z: int)` + - Same as `player.setAbsPos`, but for the entity ## How does it work? diff --git a/mods/src/api/api.cpp b/mods/src/api/api.cpp index 1ccea176..88e39bb9 100644 --- a/mods/src/api/api.cpp +++ b/mods/src/api/api.cpp @@ -257,7 +257,7 @@ std::string CommandServer_parse_injection(CommandServer_parse_t old, CommandServ // Redirect player commands to the entity command if (commandserver->minecraft->player != NULL) { - if (cmd.starts_with(player_namespace) && cmd != "player.setting" && command != "player.getAbsPos" && command != "player.setAbsPos") { + if (cmd.starts_with(player_namespace) && cmd != "player.setting") { cmd = "entity." + cmd.substr(player_namespace.size()); args = std::to_string(commandserver->minecraft->player->id) + "," + args; } @@ -521,15 +521,21 @@ std::string CommandServer_parse_injection(CommandServer_parse_t old, CommandServ if (result.size() > 1) result.pop_back(); for (auto &c : result) c = toupper(c); return result + "\n"; - } else if (cmd == "player.setAbsPos" && commandserver->minecraft->player) { + } else if (cmd == "entity.setAbsPos") { + int id; float x, y, z; - int ret = sscanf(args.c_str(), "%f,%f,%f", &x, &y, &z); - if (ret != 3) return fail; - commandserver->minecraft->player->moveTo(x, y, z, commandserver->minecraft->player->yaw, commandserver->minecraft->player->pitch); - } else if (cmd == "player.getAbsPos" && commandserver->minecraft->player) { - return std::to_string(commandserver->minecraft->player->x) + "," - + std::to_string(commandserver->minecraft->player->y) + "," - + std::to_string(commandserver->minecraft->player->z) + "\n"; + int ret = sscanf(args.c_str(), "%d,%f,%f,%f", &id, &x, &y, &z); + Entity *entity = commandserver->minecraft->level->getEntity(id); + if (ret != 4 || entity == NULL) return fail; + entity->moveTo(x, y, z, entity->yaw, entity->pitch); + } else if (cmd == "entity.getAbsPos") { + int id; + int ret = sscanf(args.c_str(), "%d", &id); + Entity *entity = commandserver->minecraft->level->getEntity(id); + if (ret != 1 || entity == NULL) return fail; + return std::to_string(entity->x) + "," + + std::to_string(entity->y) + "," + + std::to_string(entity->z) + "\n"; } else if (cmd == "entity.events.clear") { int id; int ret = sscanf(args.c_str(), "%d", &id);