Redirect "player." commands to "entity."

This commit is contained in:
Bigjango13 2024-10-30 00:14:17 -07:00
parent 5c264736de
commit b7deac1389

View File

@ -226,6 +226,7 @@ static std::string getEventsOfId(CircularQueue<T> events, CommandServer *command
} }
static const std::string fail = "Fail\n"; static const std::string fail = "Fail\n";
static const std::string player_namespace = "player.";
std::unordered_map<int, int> modern_entity_id_mapping = { std::unordered_map<int, int> modern_entity_id_mapping = {
{93, 10}, /* Chicken */ {93, 10}, /* Chicken */
{92, 11}, /* Cow */ {92, 11}, /* Cow */
@ -254,6 +255,14 @@ std::string CommandServer_parse_injection(CommandServer_parse_t old, CommandServ
if (cmd_end == std::string::npos) return fail; if (cmd_end == std::string::npos) return fail;
std::string args = command.substr(arg_start + 1, cmd_end - arg_start - 1); std::string args = command.substr(arg_start + 1, cmd_end - arg_start - 1);
// 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") {
cmd = "entity." + cmd.substr(player_namespace.size());
args = std::to_string(commandserver->minecraft->player->id) + "," + args;
}
}
// And now the big if-else chain // And now the big if-else chain
if (cmd == "world.getBlocks") { if (cmd == "world.getBlocks") {
int x0, y0, z0, x1, y1, z1; int x0, y0, z0, x1, y1, z1;
@ -331,17 +340,11 @@ std::string CommandServer_parse_injection(CommandServer_parse_t old, CommandServ
} }
if (ret.size() > 1) ret.pop_back(); if (ret.size() > 1) ret.pop_back();
return ret + "\n"; return ret + "\n";
} else if (cmd == "player.events.chat.posts") {
if (commandserver->minecraft->player == NULL) return fail;
return getEventsOfId(chatEvents, commandserver, commandserver->minecraft->player->id);
} else if (cmd == "entity.events.chat.posts") { } else if (cmd == "entity.events.chat.posts") {
int id; int id;
int ret = sscanf(args.c_str(), "%d", &id); int ret = sscanf(args.c_str(), "%d", &id);
if (ret != 1) return fail; if (ret != 1) return fail;
return getEventsOfId(chatEvents, commandserver, id); return getEventsOfId(chatEvents, commandserver, id);
} else if (cmd == "player.events.block.hits") {
if (commandserver->minecraft->player == NULL) return fail;
return getBlockHits(commandserver, client, commandserver->minecraft->player->id);
} else if (cmd == "entity.events.block.hits") { } else if (cmd == "entity.events.block.hits") {
int id; int id;
int ret = sscanf(args.c_str(), "%d", &id); int ret = sscanf(args.c_str(), "%d", &id);
@ -356,20 +359,11 @@ std::string CommandServer_parse_injection(CommandServer_parse_t old, CommandServ
} }
if (result.size() > 1) result.pop_back(); if (result.size() > 1) result.pop_back();
return result + "\n"; return result + "\n";
} else if (cmd == "player.events.projectile.hits") {
if (commandserver->minecraft->player == NULL) return fail;
return getEventsOfId(hitEvents, commandserver, commandserver->minecraft->player->id);
} else if (cmd == "entity.events.projectile.hits") { } else if (cmd == "entity.events.projectile.hits") {
int id; int id;
int ret = sscanf(args.c_str(), "%d", &id); int ret = sscanf(args.c_str(), "%d", &id);
if (ret != 1) return fail; if (ret != 1) return fail;
return getEventsOfId(hitEvents, commandserver, id); return getEventsOfId(hitEvents, commandserver, id);
} else if (cmd == "player.setDirection" && commandserver->minecraft->player) {
if (commandserver->minecraft->player == NULL) return fail;
float x, y, z;
int ret = sscanf(args.c_str(), "%f,%f,%f", &x, &y, &z);
if (ret != 3) return "";
setDir((Entity *) commandserver->minecraft->player, x, y, z);
} else if (cmd == "entity.setDirection") { } else if (cmd == "entity.setDirection") {
int id; int id;
float x, y, z; float x, y, z;
@ -381,9 +375,6 @@ std::string CommandServer_parse_injection(CommandServer_parse_t old, CommandServ
} else { } else {
setDir(entity, x, y, z); setDir(entity, x, y, z);
} }
} else if (cmd == "player.getDirection" && commandserver->minecraft->player) {
Vec3 vec = getDir((Entity *) commandserver->minecraft->player);
return std::to_string(vec.x) + "," + std::to_string(vec.y) + "," + std::to_string(vec.z) + "\n";
} else if (cmd == "entity.getDirection") { } else if (cmd == "entity.getDirection") {
int id; int id;
int ret = sscanf(args.c_str(), "%d", &id); int ret = sscanf(args.c_str(), "%d", &id);
@ -396,11 +387,6 @@ std::string CommandServer_parse_injection(CommandServer_parse_t old, CommandServ
Vec3 vec = getDir(entity); Vec3 vec = getDir(entity);
return std::to_string(vec.x) + "," + std::to_string(vec.y) + "," + std::to_string(vec.z) + "\n"; return std::to_string(vec.x) + "," + std::to_string(vec.y) + "," + std::to_string(vec.z) + "\n";
} }
} else if (cmd == "player.setRotation" && commandserver->minecraft->player) {
float yaw;
int ret = sscanf(args.c_str(), "%f", &yaw);
if (ret != 1) return "";
commandserver->minecraft->player->yaw = yaw;
} else if (cmd == "entity.setRotation") { } else if (cmd == "entity.setRotation") {
int id; int id;
float yaw; float yaw;
@ -412,11 +398,6 @@ std::string CommandServer_parse_injection(CommandServer_parse_t old, CommandServ
} else { } else {
entity->yaw = yaw; entity->yaw = yaw;
} }
} else if (cmd == "player.setPitch" && commandserver->minecraft->player) {
float pitch;
int ret = sscanf(args.c_str(), "%f", &pitch);
if (ret != 1) return "";
commandserver->minecraft->player->pitch = pitch;
} else if (cmd == "entity.setPitch") { } else if (cmd == "entity.setPitch") {
int id; int id;
float pitch; float pitch;
@ -428,8 +409,6 @@ std::string CommandServer_parse_injection(CommandServer_parse_t old, CommandServ
} else { } else {
entity->pitch = pitch; entity->pitch = pitch;
} }
} else if (cmd == "player.getRotation" && commandserver->minecraft->player) {
return std::to_string(commandserver->minecraft->player->yaw) + "\n";
} else if (cmd == "entity.getRotation") { } else if (cmd == "entity.getRotation") {
int id; int id;
int ret = sscanf(args.c_str(), "%d", &id); int ret = sscanf(args.c_str(), "%d", &id);
@ -441,8 +420,6 @@ std::string CommandServer_parse_injection(CommandServer_parse_t old, CommandServ
} else { } else {
return std::to_string(entity->yaw) + "\n"; return std::to_string(entity->yaw) + "\n";
} }
} else if (cmd == "player.getPitch" && commandserver->minecraft->player) {
return std::to_string(commandserver->minecraft->player->pitch) + "\n";
} else if (cmd == "entity.getPitch") { } else if (cmd == "entity.getPitch") {
int id; int id;
int ret = sscanf(args.c_str(), "%d", &id); int ret = sscanf(args.c_str(), "%d", &id);
@ -454,23 +431,17 @@ std::string CommandServer_parse_injection(CommandServer_parse_t old, CommandServ
} else { } else {
return std::to_string(entity->pitch) + "\n"; return std::to_string(entity->pitch) + "\n";
} }
} else if (cmd == "player.getEntities" || cmd == "entity.getEntities") { } else if (cmd == "entity.getEntities") {
// Parse // Parse
int dist, type; int dist, type;
Entity *src = NULL; Entity *src = NULL;
if (cmd == "player.getEntities") { int id = 0;
int ret = sscanf(args.c_str(), "%d,%d", &dist, &type); int ret = sscanf(args.c_str(), "%d,%d,%d", &id, &dist, &type);
if (ret != 2 || commandserver->minecraft->player == NULL) return ""; if (ret != 3) return "";
src = (Entity *) commandserver->minecraft->player; src = commandserver->minecraft->level->getEntity(id);
} else { if (src == NULL) {
int id = 0; WARN("Entity [%i] not found.", id);
int ret = sscanf(args.c_str(), "%d,%d,%d", &id, &dist, &type); return fail;
if (ret != 3) return "";
src = commandserver->minecraft->level->getEntity(id);
if (src == NULL) {
WARN("Entity [%i] not found.", id);
return fail;
}
} }
// Run // Run
std::string result = ""; std::string result = "";
@ -481,23 +452,17 @@ std::string CommandServer_parse_injection(CommandServer_parse_t old, CommandServ
} }
if (result.size() > 1) result.pop_back(); if (result.size() > 1) result.pop_back();
return result + "\n"; return result + "\n";
} else if (cmd == "player.removeEntities" || cmd == "entity.removeEntities") { } else if (cmd == "entity.removeEntities") {
// Parse // Parse
int dist, type; int dist, type;
Entity *src = NULL; Entity *src = NULL;
if (cmd == "player.removeEntities") { int id = 0;
int ret = sscanf(args.c_str(), "%d,%d", &dist, &type); int ret = sscanf(args.c_str(), "%d,%d,%d", &id, &dist, &type);
if (ret != 2 || commandserver->minecraft->player == NULL) return ""; if (ret != 3) return "";
src = (Entity *) commandserver->minecraft->player; src = commandserver->minecraft->level->getEntity(id);
} else { if (src == NULL) {
int id = 0; WARN("Entity [%i] not found.", id);
int ret = sscanf(args.c_str(), "%d,%d,%d", &id, &dist, &type); return fail;
if (ret != 3) return "";
src = commandserver->minecraft->level->getEntity(id);
if (src == NULL) {
WARN("Entity [%i] not found.", id);
return fail;
}
} }
// Run // Run
int removed = 0; int removed = 0;
@ -565,9 +530,6 @@ std::string CommandServer_parse_injection(CommandServer_parse_t old, CommandServ
return std::to_string(commandserver->minecraft->player->x) + "," return std::to_string(commandserver->minecraft->player->x) + ","
+ std::to_string(commandserver->minecraft->player->y) + "," + std::to_string(commandserver->minecraft->player->y) + ","
+ std::to_string(commandserver->minecraft->player->z) + "\n"; + std::to_string(commandserver->minecraft->player->z) + "\n";
} else if (cmd == "player.events.clear") {
if (commandserver->minecraft->player == NULL) return "";
clearEvents(commandserver, commandserver->minecraft->player->id);
} else if (cmd == "entity.events.clear") { } else if (cmd == "entity.events.clear") {
int id; int id;
int ret = sscanf(args.c_str(), "%d", &id); int ret = sscanf(args.c_str(), "%d", &id);