From c93f6f87bc829338556e00eb660ac5828f66e306 Mon Sep 17 00:00:00 2001 From: Bigjango13 Date: Tue, 29 Oct 2024 14:12:03 -0700 Subject: [PATCH] Fixup some APIs --- mods/include/mods/misc/misc.h | 2 +- mods/src/api/api.cpp | 93 +++++++++---------- mods/src/f3/f3.cpp | 16 +++- mods/src/misc/api.cpp | 7 +- mods/src/server/server.cpp | 2 +- mods/src/title-screen/splashes.txt | 3 +- symbols/CMakeLists.txt | 10 +- .../src/game/mode/{ => creator}/Creator.def | 0 .../mode/{ => creator}/EventList_Item.def | 0 .../{ => creator}/EventList_TileEvent.def | 0 .../src/game/mode/{ => creator}/ICreator.def | 0 .../src/game/mode/{ => creator}/TileEvent.def | 0 12 files changed, 70 insertions(+), 63 deletions(-) rename symbols/src/game/mode/{ => creator}/Creator.def (100%) rename symbols/src/game/mode/{ => creator}/EventList_Item.def (100%) rename symbols/src/game/mode/{ => creator}/EventList_TileEvent.def (100%) rename symbols/src/game/mode/{ => creator}/ICreator.def (100%) rename symbols/src/game/mode/{ => creator}/TileEvent.def (100%) diff --git a/mods/include/mods/misc/misc.h b/mods/include/mods/misc/misc.h index 477a7398..731a8b42 100644 --- a/mods/include/mods/misc/misc.h +++ b/mods/include/mods/misc/misc.h @@ -27,7 +27,7 @@ void misc_run_on_game_key_press(const std::function &fun void misc_run_on_key_press(const std::function &func); void misc_run_on_creative_inventory_setup(const std::function &function); void misc_run_on_swap_buffers(const std::function &function); -std::string misc_get_player_username(Player *player); +std::string misc_get_player_username(Player *player, bool convert_to_utf = false); std::map &misc_get_entity_names(); std::string misc_get_entity_name(Entity *entity); std::string misc_get_entity_name_upper(Entity *entity); diff --git a/mods/src/api/api.cpp b/mods/src/api/api.cpp index 7b9719a6..e47cc165 100644 --- a/mods/src/api/api.cpp +++ b/mods/src/api/api.cpp @@ -13,7 +13,7 @@ #include #include -bool compat_mode = true; +static bool compat_mode = true; static std::string getBlocks(CommandServer *commandserver, Vec3 start, Vec3 end) { int startx = start.x, starty = start.y, startz = start.z; int endx = end.x, endy = end.y, endz = end.z; @@ -60,6 +60,11 @@ static Vec3 getDir(Entity *entity) { return Vec3{x, y, z}; } +static std::string empty_fallback(std::string str, int i) { + if (str == "") return std::to_string(i); + return str; +} + static std::string getEntityData(CommandServer *commandserver, Entity *entity) { float x = entity->x, y = entity->y - entity->height_offset, z = entity->z; commandserver->pos_translator.to(x, y, z); @@ -67,7 +72,7 @@ static std::string getEntityData(CommandServer *commandserver, Entity *entity) { // type std::to_string(entity->getEntityTypeId()) + "," + // name - misc_get_entity_name_upper(entity) + "," + + empty_fallback(misc_get_entity_name_upper(entity), entity->id) + "," + // x std::to_string(x) + "," + // y @@ -104,15 +109,15 @@ struct ProjectileHitEvent { struct ChatEvent { std::string message = ""; - int from_player = 0; + int owner_id = 0; bool invalid = false; - std::string toString() { - return message; + std::string toString(__attribute__((unused)) CommandServer *commandserver) { + return std::to_string(owner_id) + "," + message; } }; -constexpr int EVENT_SIZE = 50; +static constexpr int EVENT_SIZE = 50; template struct CircularQueue { int at = 0; @@ -139,10 +144,17 @@ struct CircularQueue { i %= EVENT_SIZE; } }; -CircularQueue hitEvents{}; -CircularQueue chatEvents{}; +static CircularQueue hitEvents{}; +static CircularQueue chatEvents{}; -void clearEvents(CommandServer *commandserver, int id) { +static void clearNewEvents() { + // Clear chat events + chatEvents.at = chatEvents.start; + // Clear projectile events + if (!compat_mode) hitEvents.at = hitEvents.start; +} + +static void clearEvents(CommandServer *commandserver, int id) { // Clear block hit events if (commandserver->minecraft->isCreativeMode()) { EventList_TileEvent &tileEvents = commandserver->minecraft->getCreator()->getTileEvents(); @@ -156,7 +168,7 @@ void clearEvents(CommandServer *commandserver, int id) { // Clear chat events int i = chatEvents.start; while (chatEvents.at != i) { - if (chatEvents.buf[i].from_player == id) { + if (chatEvents.buf[i].owner_id == id) { chatEvents.buf[i].invalid = true; } } @@ -169,7 +181,7 @@ void clearEvents(CommandServer *commandserver, int id) { } } -std::string getBlockHits(CommandServer *commandserver, ConnectedClient &client, int id) { +static std::string getBlockHits(CommandServer *commandserver, ConnectedClient &client, int id) { std::string ret = ""; if (commandserver->minecraft->isCreativeMode()) { EventList_TileEvent &tileEvents = commandserver->minecraft->getCreator()->getTileEvents(); @@ -194,35 +206,20 @@ std::string getBlockHits(CommandServer *commandserver, ConnectedClient &client, return ret + "\n"; } -std::string getChatPosts(int id) { +template +static std::string getEventsOfId(CircularQueue events, CommandServer *commandserver, int id) { std::string ret = ""; - int i = chatEvents.start; - while (chatEvents.at != i) { - ChatEvent ce = chatEvents.buf[i]; - if (ce.from_player == id && !ce.invalid) { - chatEvents.buf[i].invalid = true; - std::string message = ce.toString(); + int i = events.start; + while (events.at != i) { + T event = events.buf[i]; + if (event.owner_id == id && !event.invalid) { + events.buf[i].invalid = true; + std::string message = event.toString(commandserver); if (compat_mode) std::replace(message.begin(), message.end(), '|', '\\'); else message = misc_base64_encode(message); - ret += std::to_string(ce.from_player) + "," + message + "|"; - } - chatEvents.advance_counter(i); - } - if (ret.size() > 1) ret.pop_back(); - return ret + "\n"; -} - -std::string getProjectileHits(CommandServer *commandserver, int id) { - std::string ret = ""; - int i = hitEvents.start; - while (hitEvents.at != i) { - ProjectileHitEvent he = hitEvents.buf[i]; - if (he.owner_id == id && !he.invalid) { - hitEvents.buf[i].invalid = true; - std::string message = he.toString(commandserver); ret += message + "|"; } - hitEvents.advance_counter(i); + events.advance_counter(i); } if (ret.size() > 1) ret.pop_back(); return ret + "\n"; @@ -281,7 +278,7 @@ std::string CommandServer_parse_injection(CommandServer_parse_t old, CommandServ if (entity == NULL) { WARN("Player (or Entity) [%i] not found in entity.getName.", id); } - return misc_get_entity_name_upper(entity) + "\n"; + return empty_fallback(misc_get_entity_name_upper(entity), entity->id) + "\n"; } else if (cmd == "world.getEntities") { int type; int ret = sscanf(args.c_str(), "%d", &type); @@ -326,22 +323,24 @@ std::string CommandServer_parse_injection(CommandServer_parse_t old, CommandServ while (chatEvents.at != chatEvents.start) { ChatEvent ce = chatEvents.pop(); if (!ce.invalid) { - std::string message = ce.toString(); + std::string message = ce.toString(commandserver); if (compat_mode) std::replace(message.begin(), message.end(), '|', '\\'); else message = misc_base64_encode(message); - ret += std::to_string(ce.from_player) + "," + message + "|"; + ret += message + "|"; } } if (ret.size() > 1) ret.pop_back(); return ret + "\n"; } else if (cmd == "player.events.chat.posts") { - return getChatPosts(commandserver->minecraft->player->id); + if (commandserver->minecraft->player == NULL) return fail; + return getEventsOfId(chatEvents, commandserver, commandserver->minecraft->player->id); } else if (cmd == "entity.events.chat.posts") { int id; int ret = sscanf(args.c_str(), "%d", &id); if (ret != 1) return fail; - return getChatPosts(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") { int id; @@ -358,13 +357,15 @@ std::string CommandServer_parse_injection(CommandServer_parse_t old, CommandServ if (result.size() > 1) result.pop_back(); return result + "\n"; } else if (cmd == "player.events.projectile.hits") { - return getProjectileHits(commandserver, commandserver->minecraft->player->id); + if (commandserver->minecraft->player == NULL) return fail; + return getEventsOfId(hitEvents, commandserver, commandserver->minecraft->player->id); } else if (cmd == "entity.events.projectile.hits") { int id; int ret = sscanf(args.c_str(), "%d", &id); if (ret != 1) return fail; - return getProjectileHits(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 ""; @@ -565,6 +566,7 @@ std::string CommandServer_parse_injection(CommandServer_parse_t old, CommandServ + std::to_string(commandserver->minecraft->player->y) + "," + 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") { int id; @@ -572,11 +574,8 @@ std::string CommandServer_parse_injection(CommandServer_parse_t old, CommandServ if (ret != 1) return fail; clearEvents(commandserver, id); } else if (cmd == "events.clear") { - // Clear chat events - chatEvents.at = chatEvents.start; - // Clear projectile events - if (!compat_mode) hitEvents.at = hitEvents.start; - // Extended the original, so now call the original + clearNewEvents(); + // Clear the other events as well return old(commandserver, client, command); } else if (cmd == "reborn.disableCompatMode") { compat_mode = false; diff --git a/mods/src/f3/f3.cpp b/mods/src/f3/f3.cpp index 43a507f1..1be17e94 100644 --- a/mods/src/f3/f3.cpp +++ b/mods/src/f3/f3.cpp @@ -104,7 +104,7 @@ static std::vector get_debug_info_right(const Minecraft *minecraft) type = "Tile"; if (minecraft->level) { const int id = minecraft->level->getTile(x, y, z); - std::string id_info = "ID: " + std::to_string(id); + std::string id_info = "Type: "; if (Tile *tile = Tile::tiles[id]) { const std::string description_id = tile->getDescriptionId(); std::string name = description_id + ".name"; @@ -113,10 +113,9 @@ static std::vector get_debug_info_right(const Minecraft *minecraft) } else { name = description_id; } - if (!name.empty()) { - id_info += " (" + name + ')'; - } + id_info += name; } + id_info += " (" + std::to_string(id) + ")"; type_info.push_back(id_info); type_info.push_back("Data: " + std::to_string(minecraft->level->getData(x, y, z))); } @@ -129,7 +128,14 @@ static std::vector get_debug_info_right(const Minecraft *minecraft) z = entity->z; type = "Entity"; type_info.push_back("ID: " + std::to_string(entity->id)); - type_info.push_back("Type: " + misc_get_entity_names()[entity->getEntityTypeId()] + " (" + std::to_string(entity->getEntityTypeId()) + ")"); + std::string type_str = "Type: "; + if (entity->isPlayer()) { + type_str += "Player"; + } else { + type_str += misc_get_entity_names()[entity->getEntityTypeId()]; + } + type_str += " (" + std::to_string(entity->getEntityTypeId()) + ")"; + type_info.push_back(type_str); if (entity->isMob()) { Mob *mob = (Mob *) entity; type_info.push_back("Health: " + std::to_string(mob->health) + '/' + std::to_string(mob->getMaxHealth())); diff --git a/mods/src/misc/api.cpp b/mods/src/misc/api.cpp index 96ba4ee0..7caca8e9 100644 --- a/mods/src/misc/api.cpp +++ b/mods/src/misc/api.cpp @@ -135,7 +135,8 @@ void misc_render_background(int color, const Minecraft *minecraft, const int x, } // Get Player's Username -std::string misc_get_player_username(Player *player) { +std::string misc_get_player_username(Player *player, bool convert_to_utf) { + if (!convert_to_utf) return player->username; const std::string *username = &player->username; char *safe_username_c = from_cp437(username->c_str()); std::string safe_username = safe_username_c; @@ -169,14 +170,14 @@ std::string misc_get_entity_name(Entity *entity) { int type = entity->getEntityTypeId(); std::map &names = misc_get_entity_names(); if (names.find(type) != names.end()) return names[type]; - return std::to_string(entity->id); + return ""; } } std::string misc_get_entity_name_upper(Entity *entity) { std::string ret = misc_get_entity_name(entity); if (entity != NULL && !entity->isPlayer()) { - for (auto &c : ret) c = toupper(c); + for (char &c : ret) c = toupper(c); } return ret; } diff --git a/mods/src/server/server.cpp b/mods/src/server/server.cpp index b9a05176..eaf59bbd 100644 --- a/mods/src/server/server.cpp +++ b/mods/src/server/server.cpp @@ -129,7 +129,7 @@ static void find_players(Minecraft *minecraft, const std::string &target_usernam for (std::size_t i = 0; i < players.size(); i++) { // Iterate Players Player *player = players[i]; - std::string username = misc_get_player_username(player); + std::string username = misc_get_player_username(player, true); if (all_players || username == target_username) { // Run Callback callback(minecraft, username, player); diff --git a/mods/src/title-screen/splashes.txt b/mods/src/title-screen/splashes.txt index 00fb2813..a4fa653e 100644 --- a/mods/src/title-screen/splashes.txt +++ b/mods/src/title-screen/splashes.txt @@ -62,4 +62,5 @@ Python API! Raspberry Pi! It's alive! Now with cake! -The bug attractor! \ No newline at end of file +The bug attractor! +I promise, this is the last time! \ No newline at end of file diff --git a/symbols/CMakeLists.txt b/symbols/CMakeLists.txt index 458919e4..bea7c21b 100644 --- a/symbols/CMakeLists.txt +++ b/symbols/CMakeLists.txt @@ -8,11 +8,11 @@ set(SRC src/game/Minecraft.def src/game/mode/GameMode.def src/game/mode/CreatorMode.def - src/game/mode/ICreator.def - src/game/mode/Creator.def - src/game/mode/EventList_Item.def - src/game/mode/EventList_TileEvent.def - src/game/mode/TileEvent.def + src/game/mode/creator/ICreator.def + src/game/mode/creator/Creator.def + src/game/mode/creator/EventList_Item.def + src/game/mode/creator/EventList_TileEvent.def + src/game/mode/creator/TileEvent.def src/game/mode/SurvivalMode.def src/game/NinecraftApp.def src/game/GameRenderer.def diff --git a/symbols/src/game/mode/Creator.def b/symbols/src/game/mode/creator/Creator.def similarity index 100% rename from symbols/src/game/mode/Creator.def rename to symbols/src/game/mode/creator/Creator.def diff --git a/symbols/src/game/mode/EventList_Item.def b/symbols/src/game/mode/creator/EventList_Item.def similarity index 100% rename from symbols/src/game/mode/EventList_Item.def rename to symbols/src/game/mode/creator/EventList_Item.def diff --git a/symbols/src/game/mode/EventList_TileEvent.def b/symbols/src/game/mode/creator/EventList_TileEvent.def similarity index 100% rename from symbols/src/game/mode/EventList_TileEvent.def rename to symbols/src/game/mode/creator/EventList_TileEvent.def diff --git a/symbols/src/game/mode/ICreator.def b/symbols/src/game/mode/creator/ICreator.def similarity index 100% rename from symbols/src/game/mode/ICreator.def rename to symbols/src/game/mode/creator/ICreator.def diff --git a/symbols/src/game/mode/TileEvent.def b/symbols/src/game/mode/creator/TileEvent.def similarity index 100% rename from symbols/src/game/mode/TileEvent.def rename to symbols/src/game/mode/creator/TileEvent.def