minecraft-pi-docker/mods/src/input/misc.c

95 lines
3.0 KiB
C
Raw Normal View History

2021-07-04 23:02:45 +00:00
#include <libreborn/libreborn.h>
2021-09-12 03:18:12 +00:00
#include <symbols/minecraft.h>
2021-07-04 23:02:45 +00:00
#include "input.h"
#include "../feature/feature.h"
2021-11-14 04:29:48 +00:00
#include "../creative/creative.h"
2021-07-04 23:02:45 +00:00
// Enable Miscellaneous Input Fixes
static int enable_misc = 0;
// Store Back Button Presses
static int back_button_presses = 0;
int input_back() {
if (enable_misc) {
back_button_presses++;
return 1; // Handled
} else {
return 0; // Not Handled
}
}
// Handle Back Button Presses
2021-10-04 23:42:55 +00:00
static void _handle_back(unsigned char *minecraft) {
2021-07-04 23:02:45 +00:00
unsigned char *minecraft_vtable = *(unsigned char **) minecraft;
Minecraft_handleBack_t Minecraft_handleBack = *(Minecraft_handleBack_t *) (minecraft_vtable + Minecraft_handleBack_vtable_offset);
for (int i = 0; i < back_button_presses; i++) {
(*Minecraft_handleBack)(minecraft, 0);
}
back_button_presses = 0;
}
// Fix OptionsScreen Ignoring The Back Button
2021-07-05 02:32:19 +00:00
static int32_t OptionsScreen_handleBackEvent_injection(unsigned char *screen, bool do_nothing) {
if (!do_nothing) {
unsigned char *minecraft = *(unsigned char **) (screen + Screen_minecraft_property_offset);
(*Minecraft_setScreen)(minecraft, NULL);
}
2021-07-04 23:02:45 +00:00
return 1;
}
// Set Mouse Grab State
static int mouse_grab_state = 0;
void input_set_mouse_grab_state(int state) {
mouse_grab_state = state;
}
// Grab/Un-Grab Mouse
2021-10-04 23:42:55 +00:00
static void _handle_mouse_grab(unsigned char *minecraft) {
2021-07-04 23:02:45 +00:00
if (mouse_grab_state == -1) {
// Grab
(*Minecraft_grabMouse)(minecraft);
} else if (mouse_grab_state == 1) {
// Un-Grab
(*Minecraft_releaseMouse)(minecraft);
}
mouse_grab_state = 0;
}
#include <SDL/SDL.h>
// Block UI Interaction When Mouse Is Locked
static bool Gui_tickItemDrop_Minecraft_isCreativeMode_call_injection(unsigned char *minecraft) {
2021-11-14 04:29:48 +00:00
if (!enable_misc || SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_OFF) {
2021-07-04 23:02:45 +00:00
// Call Original Method
2021-11-14 04:29:48 +00:00
return creative_is_restricted() && (*Minecraft_isCreativeMode)(minecraft);
2021-07-04 23:02:45 +00:00
} else {
// Disable Item Drop Ticking
return 1;
}
}
// Block UI Interaction When Mouse Is Locked
static void Gui_handleClick_injection(unsigned char *gui, int32_t param_2, int32_t param_3, int32_t param_4) {
if (SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_OFF) {
// Call Original Method
(*Gui_handleClick)(gui, param_2, param_3, param_4);
}
}
// Init
void _init_misc() {
2022-04-10 00:01:16 +00:00
enable_misc = feature_has("Miscellaneous Input Fixes", server_disabled);
2021-07-04 23:02:45 +00:00
if (enable_misc) {
// Fix OptionsScreen Ignoring The Back Button
patch_address(OptionsScreen_handleBackEvent_vtable_addr, (void *) OptionsScreen_handleBackEvent_injection);
// Disable Opening Inventory Using The Cursor When Cursor Is Hidden
overwrite_calls((void *) Gui_handleClick, (void *) Gui_handleClick_injection);
}
2021-11-14 04:29:48 +00:00
// Disable Item Dropping Using The Cursor When Cursor Is Hidden
overwrite_call((void *) 0x27800, (void *) Gui_tickItemDrop_Minecraft_isCreativeMode_call_injection);
2021-10-04 23:42:55 +00:00
input_run_on_tick(_handle_back);
input_run_on_tick(_handle_mouse_grab);
2021-07-04 23:02:45 +00:00
}