Fixup some APIs

This commit is contained in:
Bigjango13 2024-10-29 14:12:03 -07:00
parent 245cc4dea5
commit c93f6f87bc
12 changed files with 70 additions and 63 deletions

View File

@ -27,7 +27,7 @@ void misc_run_on_game_key_press(const std::function<bool(Minecraft *, int)> &fun
void misc_run_on_key_press(const std::function<bool(Minecraft *, int)> &func); void misc_run_on_key_press(const std::function<bool(Minecraft *, int)> &func);
void misc_run_on_creative_inventory_setup(const std::function<void(FillingContainer *)> &function); void misc_run_on_creative_inventory_setup(const std::function<void(FillingContainer *)> &function);
void misc_run_on_swap_buffers(const std::function<void()> &function); void misc_run_on_swap_buffers(const std::function<void()> &function);
std::string misc_get_player_username(Player *player); std::string misc_get_player_username(Player *player, bool convert_to_utf = false);
std::map<int, std::string> &misc_get_entity_names(); std::map<int, std::string> &misc_get_entity_names();
std::string misc_get_entity_name(Entity *entity); std::string misc_get_entity_name(Entity *entity);
std::string misc_get_entity_name_upper(Entity *entity); std::string misc_get_entity_name_upper(Entity *entity);

View File

@ -13,7 +13,7 @@
#include <mods/server/server.h> #include <mods/server/server.h>
#include <mods/feature/feature.h> #include <mods/feature/feature.h>
bool compat_mode = true; static bool compat_mode = true;
static std::string getBlocks(CommandServer *commandserver, Vec3 start, Vec3 end) { static std::string getBlocks(CommandServer *commandserver, Vec3 start, Vec3 end) {
int startx = start.x, starty = start.y, startz = start.z; int startx = start.x, starty = start.y, startz = start.z;
int endx = end.x, endy = end.y, endz = end.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}; 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) { static std::string getEntityData(CommandServer *commandserver, Entity *entity) {
float x = entity->x, y = entity->y - entity->height_offset, z = entity->z; float x = entity->x, y = entity->y - entity->height_offset, z = entity->z;
commandserver->pos_translator.to(x, y, z); commandserver->pos_translator.to(x, y, z);
@ -67,7 +72,7 @@ static std::string getEntityData(CommandServer *commandserver, Entity *entity) {
// type // type
std::to_string(entity->getEntityTypeId()) + "," + std::to_string(entity->getEntityTypeId()) + "," +
// name // name
misc_get_entity_name_upper(entity) + "," + empty_fallback(misc_get_entity_name_upper(entity), entity->id) + "," +
// x // x
std::to_string(x) + "," + std::to_string(x) + "," +
// y // y
@ -104,15 +109,15 @@ struct ProjectileHitEvent {
struct ChatEvent { struct ChatEvent {
std::string message = ""; std::string message = "";
int from_player = 0; int owner_id = 0;
bool invalid = false; bool invalid = false;
std::string toString() { std::string toString(__attribute__((unused)) CommandServer *commandserver) {
return message; return std::to_string(owner_id) + "," + message;
} }
}; };
constexpr int EVENT_SIZE = 50; static constexpr int EVENT_SIZE = 50;
template <typename T, int SIZE = EVENT_SIZE> template <typename T, int SIZE = EVENT_SIZE>
struct CircularQueue { struct CircularQueue {
int at = 0; int at = 0;
@ -139,10 +144,17 @@ struct CircularQueue {
i %= EVENT_SIZE; i %= EVENT_SIZE;
} }
}; };
CircularQueue<ProjectileHitEvent> hitEvents{}; static CircularQueue<ProjectileHitEvent> hitEvents{};
CircularQueue<ChatEvent> chatEvents{}; static CircularQueue<ChatEvent> 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 // Clear block hit events
if (commandserver->minecraft->isCreativeMode()) { if (commandserver->minecraft->isCreativeMode()) {
EventList_TileEvent &tileEvents = commandserver->minecraft->getCreator()->getTileEvents(); EventList_TileEvent &tileEvents = commandserver->minecraft->getCreator()->getTileEvents();
@ -156,7 +168,7 @@ void clearEvents(CommandServer *commandserver, int id) {
// Clear chat events // Clear chat events
int i = chatEvents.start; int i = chatEvents.start;
while (chatEvents.at != i) { while (chatEvents.at != i) {
if (chatEvents.buf[i].from_player == id) { if (chatEvents.buf[i].owner_id == id) {
chatEvents.buf[i].invalid = true; 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 = ""; std::string ret = "";
if (commandserver->minecraft->isCreativeMode()) { if (commandserver->minecraft->isCreativeMode()) {
EventList_TileEvent &tileEvents = commandserver->minecraft->getCreator()->getTileEvents(); EventList_TileEvent &tileEvents = commandserver->minecraft->getCreator()->getTileEvents();
@ -194,35 +206,20 @@ std::string getBlockHits(CommandServer *commandserver, ConnectedClient &client,
return ret + "\n"; return ret + "\n";
} }
std::string getChatPosts(int id) { template<typename T>
static std::string getEventsOfId(CircularQueue<T> events, CommandServer *commandserver, int id) {
std::string ret = ""; std::string ret = "";
int i = chatEvents.start; int i = events.start;
while (chatEvents.at != i) { while (events.at != i) {
ChatEvent ce = chatEvents.buf[i]; T event = events.buf[i];
if (ce.from_player == id && !ce.invalid) { if (event.owner_id == id && !event.invalid) {
chatEvents.buf[i].invalid = true; events.buf[i].invalid = true;
std::string message = ce.toString(); std::string message = event.toString(commandserver);
if (compat_mode) std::replace(message.begin(), message.end(), '|', '\\'); if (compat_mode) std::replace(message.begin(), message.end(), '|', '\\');
else message = misc_base64_encode(message); 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 + "|"; ret += message + "|";
} }
hitEvents.advance_counter(i); events.advance_counter(i);
} }
if (ret.size() > 1) ret.pop_back(); if (ret.size() > 1) ret.pop_back();
return ret + "\n"; return ret + "\n";
@ -281,7 +278,7 @@ std::string CommandServer_parse_injection(CommandServer_parse_t old, CommandServ
if (entity == NULL) { if (entity == NULL) {
WARN("Player (or Entity) [%i] not found in entity.getName.", id); 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") { } else if (cmd == "world.getEntities") {
int type; int type;
int ret = sscanf(args.c_str(), "%d", &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) { while (chatEvents.at != chatEvents.start) {
ChatEvent ce = chatEvents.pop(); ChatEvent ce = chatEvents.pop();
if (!ce.invalid) { if (!ce.invalid) {
std::string message = ce.toString(); std::string message = ce.toString(commandserver);
if (compat_mode) std::replace(message.begin(), message.end(), '|', '\\'); if (compat_mode) std::replace(message.begin(), message.end(), '|', '\\');
else message = misc_base64_encode(message); else message = misc_base64_encode(message);
ret += std::to_string(ce.from_player) + "," + message + "|"; ret += message + "|";
} }
} }
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") { } 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") { } 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 getChatPosts(id); return getEventsOfId(chatEvents, commandserver, id);
} else if (cmd == "player.events.block.hits") { } else if (cmd == "player.events.block.hits") {
if (commandserver->minecraft->player == NULL) return fail;
return getBlockHits(commandserver, client, commandserver->minecraft->player->id); 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;
@ -358,13 +357,15 @@ 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") { } 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") { } 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 getProjectileHits(commandserver, id); return getEventsOfId(hitEvents, commandserver, id);
} else if (cmd == "player.setDirection" && commandserver->minecraft->player) { } else if (cmd == "player.setDirection" && commandserver->minecraft->player) {
if (commandserver->minecraft->player == NULL) return fail;
float x, y, z; float x, y, z;
int ret = sscanf(args.c_str(), "%f,%f,%f", &x, &y, &z); int ret = sscanf(args.c_str(), "%f,%f,%f", &x, &y, &z);
if (ret != 3) return ""; 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->y) + ","
+ std::to_string(commandserver->minecraft->player->z) + "\n"; + std::to_string(commandserver->minecraft->player->z) + "\n";
} else if (cmd == "player.events.clear") { } else if (cmd == "player.events.clear") {
if (commandserver->minecraft->player == NULL) return "";
clearEvents(commandserver, commandserver->minecraft->player->id); clearEvents(commandserver, commandserver->minecraft->player->id);
} else if (cmd == "entity.events.clear") { } else if (cmd == "entity.events.clear") {
int id; int id;
@ -572,11 +574,8 @@ std::string CommandServer_parse_injection(CommandServer_parse_t old, CommandServ
if (ret != 1) return fail; if (ret != 1) return fail;
clearEvents(commandserver, id); clearEvents(commandserver, id);
} else if (cmd == "events.clear") { } else if (cmd == "events.clear") {
// Clear chat events clearNewEvents();
chatEvents.at = chatEvents.start; // Clear the other events as well
// Clear projectile events
if (!compat_mode) hitEvents.at = hitEvents.start;
// Extended the original, so now call the original
return old(commandserver, client, command); return old(commandserver, client, command);
} else if (cmd == "reborn.disableCompatMode") { } else if (cmd == "reborn.disableCompatMode") {
compat_mode = false; compat_mode = false;

View File

@ -104,7 +104,7 @@ static std::vector<std::string> get_debug_info_right(const Minecraft *minecraft)
type = "Tile"; type = "Tile";
if (minecraft->level) { if (minecraft->level) {
const int id = minecraft->level->getTile(x, y, z); 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]) { if (Tile *tile = Tile::tiles[id]) {
const std::string description_id = tile->getDescriptionId(); const std::string description_id = tile->getDescriptionId();
std::string name = description_id + ".name"; std::string name = description_id + ".name";
@ -113,10 +113,9 @@ static std::vector<std::string> get_debug_info_right(const Minecraft *minecraft)
} else { } else {
name = description_id; 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(id_info);
type_info.push_back("Data: " + std::to_string(minecraft->level->getData(x, y, z))); type_info.push_back("Data: " + std::to_string(minecraft->level->getData(x, y, z)));
} }
@ -129,7 +128,14 @@ static std::vector<std::string> get_debug_info_right(const Minecraft *minecraft)
z = entity->z; z = entity->z;
type = "Entity"; type = "Entity";
type_info.push_back("ID: " + std::to_string(entity->id)); 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()) { if (entity->isMob()) {
Mob *mob = (Mob *) entity; Mob *mob = (Mob *) entity;
type_info.push_back("Health: " + std::to_string(mob->health) + '/' + std::to_string(mob->getMaxHealth())); type_info.push_back("Health: " + std::to_string(mob->health) + '/' + std::to_string(mob->getMaxHealth()));

View File

@ -135,7 +135,8 @@ void misc_render_background(int color, const Minecraft *minecraft, const int x,
} }
// Get Player's Username // 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; const std::string *username = &player->username;
char *safe_username_c = from_cp437(username->c_str()); char *safe_username_c = from_cp437(username->c_str());
std::string safe_username = safe_username_c; std::string safe_username = safe_username_c;
@ -169,14 +170,14 @@ std::string misc_get_entity_name(Entity *entity) {
int type = entity->getEntityTypeId(); int type = entity->getEntityTypeId();
std::map<int, std::string> &names = misc_get_entity_names(); std::map<int, std::string> &names = misc_get_entity_names();
if (names.find(type) != names.end()) return names[type]; 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 misc_get_entity_name_upper(Entity *entity) {
std::string ret = misc_get_entity_name(entity); std::string ret = misc_get_entity_name(entity);
if (entity != NULL && !entity->isPlayer()) { if (entity != NULL && !entity->isPlayer()) {
for (auto &c : ret) c = toupper(c); for (char &c : ret) c = toupper(c);
} }
return ret; return ret;
} }

View File

@ -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++) { for (std::size_t i = 0; i < players.size(); i++) {
// Iterate Players // Iterate Players
Player *player = players[i]; 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) { if (all_players || username == target_username) {
// Run Callback // Run Callback
callback(minecraft, username, player); callback(minecraft, username, player);

View File

@ -63,3 +63,4 @@ Raspberry Pi!
It's alive! It's alive!
Now with cake! Now with cake!
The bug attractor! The bug attractor!
I promise, this is the last time!

View File

@ -8,11 +8,11 @@ set(SRC
src/game/Minecraft.def src/game/Minecraft.def
src/game/mode/GameMode.def src/game/mode/GameMode.def
src/game/mode/CreatorMode.def src/game/mode/CreatorMode.def
src/game/mode/ICreator.def src/game/mode/creator/ICreator.def
src/game/mode/Creator.def src/game/mode/creator/Creator.def
src/game/mode/EventList_Item.def src/game/mode/creator/EventList_Item.def
src/game/mode/EventList_TileEvent.def src/game/mode/creator/EventList_TileEvent.def
src/game/mode/TileEvent.def src/game/mode/creator/TileEvent.def
src/game/mode/SurvivalMode.def src/game/mode/SurvivalMode.def
src/game/NinecraftApp.def src/game/NinecraftApp.def
src/game/GameRenderer.def src/game/GameRenderer.def