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

69 lines
2.2 KiB
C++
Raw Normal View History

2020-12-02 23:18:49 +00:00
#include <vector>
2022-07-31 03:52:50 +00:00
#include <SDL/SDL.h>
2021-01-27 21:26:19 +00:00
#include <libreborn/libreborn.h>
2021-09-12 03:18:12 +00:00
#include <symbols/minecraft.h>
2020-12-02 23:18:49 +00:00
2022-06-25 21:30:08 +00:00
#include <mods/init/init.h>
#include <mods/feature/feature.h>
#include <mods/input/input.h>
#include <mods/sign/sign.h>
2020-12-02 23:18:49 +00:00
2022-07-31 03:52:50 +00:00
// Handle Backspace
static int32_t sdl_key_to_minecraft_key_injection(int32_t sdl_key) {
if (sdl_key == SDLK_BACKSPACE) {
return 8;
} else {
// Call Original Method
return (*sdl_key_to_minecraft_key)(sdl_key);
}
}
2020-12-02 23:18:49 +00:00
// Open Sign Screen
static void LocalPlayer_openTextEdit_injection(unsigned char *local_player, unsigned char *sign) {
2020-12-18 03:22:56 +00:00
if (*(int32_t *) (sign + TileEntity_id_property_offset) == 4) {
unsigned char *minecraft = *(unsigned char **) (local_player + LocalPlayer_minecraft_property_offset);
unsigned char *screen = (unsigned char *) ::operator new(TEXT_EDIT_SCREEN_SIZE);
2021-02-16 17:26:40 +00:00
ALLOC_CHECK(screen);
2020-12-02 23:18:49 +00:00
screen = (*TextEditScreen)(screen, sign);
(*Minecraft_setScreen)(minecraft, screen);
}
}
// Store Text Input
std::vector<char> input;
2021-06-29 02:59:24 +00:00
void sign_key_press(char key) {
2022-07-31 03:52:50 +00:00
input.push_back(key);
2020-12-02 23:18:49 +00:00
}
2021-07-04 23:02:45 +00:00
static void clear_input(__attribute__((unused)) unsigned char *minecraft) {
2020-12-02 23:18:49 +00:00
input.clear();
}
// Handle Text Input
static void TextEditScreen_updateEvents_injection(unsigned char *screen) {
// Call Original Method
(*Screen_updateEvents)(screen);
2022-07-31 03:52:50 +00:00
if (!*(bool *)(screen + Screen_passthrough_input_property_offset)) {
unsigned char *vtable = *(unsigned char **) screen;
2020-12-02 23:18:49 +00:00
for (char key : input) {
2022-07-31 03:52:50 +00:00
// Handle Normal Key
(*(Screen_keyboardNewChar_t *) (vtable + Screen_keyboardNewChar_vtable_offset))(screen, key);
2020-12-02 23:18:49 +00:00
}
}
2021-07-04 23:02:45 +00:00
clear_input(NULL);
2020-12-02 23:18:49 +00:00
}
2021-06-29 02:59:24 +00:00
// Init
void init_sign() {
2022-04-10 00:01:16 +00:00
if (feature_has("Fix Sign Placement", server_disabled)) {
2022-07-31 03:52:50 +00:00
// Handle Backspace
overwrite_calls((void *) sdl_key_to_minecraft_key, (void *) sdl_key_to_minecraft_key_injection);
2020-12-02 23:18:49 +00:00
// Fix Signs
patch_address(LocalPlayer_openTextEdit_vtable_addr, (void *) LocalPlayer_openTextEdit_injection);
patch_address(TextEditScreen_updateEvents_vtable_addr, (void *) TextEditScreen_updateEvents_injection);
2022-07-31 03:52:50 +00:00
// Clear Input On Input Tick
2021-07-04 23:02:45 +00:00
input_run_on_tick(clear_input);
2020-12-02 23:18:49 +00:00
}
2021-06-29 02:59:24 +00:00
}