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_creative_inventory_setup(const std::function<void(FillingContainer *)> &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::string misc_get_entity_name(Entity *entity);
std::string misc_get_entity_name_upper(Entity *entity);

View File

@ -13,7 +13,7 @@
#include <mods/server/server.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) {
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 <typename T, int SIZE = EVENT_SIZE>
struct CircularQueue {
int at = 0;
@ -139,10 +144,17 @@ struct CircularQueue {
i %= EVENT_SIZE;
}
};
CircularQueue<ProjectileHitEvent> hitEvents{};
CircularQueue<ChatEvent> chatEvents{};
static CircularQueue<ProjectileHitEvent> hitEvents{};
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
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<typename T>
static std::string getEventsOfId(CircularQueue<T> 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;

View File

@ -104,7 +104,7 @@ static std::vector<std::string> 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<std::string> 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<std::string> 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()));

View File

@ -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<int, std::string> &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;
}

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++) {
// 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);

View File

@ -62,4 +62,5 @@ Python API!
Raspberry Pi!
It's alive!
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/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