minecraft-pi-reborn/mods/src/death/death.cpp

62 lines
2.1 KiB
C++
Raw Normal View History

#include <string>
#include <libreborn/libreborn.h>
2021-09-12 03:18:12 +00:00
#include <symbols/minecraft.h>
2022-06-25 21:30:08 +00:00
#include <mods/init/init.h>
#include <mods/feature/feature.h>
// Death Messages
2024-01-06 11:30:23 +00:00
static std::string get_death_message(Player *player) {
// Get Username
2024-01-06 11:30:23 +00:00
std::string *username = &player->username;
// Prepare Death Message
std::string message;
2021-12-01 02:54:43 +00:00
message.append(username->c_str());
message.append(" has died");
// Return
return message;
}
2024-01-06 11:30:23 +00:00
// Death Message Logic
#define Player_actuallyHurt_injection(type) \
static void type##Player_actuallyHurt_injection(type##Player *player, int32_t damage) { \
/* Store Old Health */ \
int32_t old_health = player->health; \
\
/* Call Original Method */ \
2024-01-07 08:23:43 +00:00
type##Player_actuallyHurt_non_virtual(player, damage); \
2024-01-06 11:30:23 +00:00
\
/* Store New Health */ \
int32_t new_health = player->health; \
\
/* Get Variables */ \
Minecraft *minecraft = player->minecraft; \
RakNetInstance *rak_net_instance = minecraft->rak_net_instance; \
/* Only Run On Server-Side */ \
if (rak_net_instance->vtable->isServer(rak_net_instance)) { \
/* Check Health */ \
if (new_health < 1 && old_health >= 1) { \
/* Get Death Message */ \
std::string message = get_death_message((Player *) player); \
\
/* Post Death Message */ \
ServerSideNetworkHandler *server_side_network_handler = (ServerSideNetworkHandler *) minecraft->network_handler; \
2024-01-07 08:23:43 +00:00
ServerSideNetworkHandler_displayGameMessage(server_side_network_handler, &message); \
2024-01-06 11:30:23 +00:00
} \
} \
}
2024-01-06 11:30:23 +00:00
Player_actuallyHurt_injection(Local)
Player_actuallyHurt_injection(Server)
// Init
void init_death() {
// Death Messages
if (feature_has("Implement Death Messages", server_auto)) {
2021-07-04 23:02:45 +00:00
patch_address(ServerPlayer_actuallyHurt_vtable_addr, (void *) ServerPlayer_actuallyHurt_injection);
patch_address(LocalPlayer_actuallyHurt_vtable_addr, (void *) LocalPlayer_actuallyHurt_injection);
}
}