2021-07-04 19:02:45 -04:00
|
|
|
#include <libreborn/libreborn.h>
|
2021-09-11 23:18:12 -04:00
|
|
|
#include <symbols/minecraft.h>
|
2021-07-04 19:02:45 -04:00
|
|
|
|
2022-06-25 17:30:08 -04:00
|
|
|
#include "input-internal.h"
|
|
|
|
#include <mods/input/input.h>
|
|
|
|
#include <mods/feature/feature.h>
|
|
|
|
#include <mods/creative/creative.h>
|
2021-07-04 19:02:45 -04: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
|
2024-01-06 06:30:23 -05:00
|
|
|
static void _handle_back(Minecraft *minecraft) {
|
2024-04-02 19:22:01 -04:00
|
|
|
// If Minecraft's Level property is initialized, but Minecraft's Player property is nullptr, then Minecraft::handleBack may crash.
|
|
|
|
if (minecraft->level != nullptr && minecraft->player == nullptr) {
|
2022-10-20 23:58:40 -04:00
|
|
|
// Unable to safely run Minecraft::handleBack, deferring until safe.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Send Event
|
2021-07-04 19:02:45 -04:00
|
|
|
for (int i = 0; i < back_button_presses; i++) {
|
2024-01-06 06:30:23 -05:00
|
|
|
minecraft->vtable->handleBack(minecraft, 0);
|
2021-07-04 19:02:45 -04:00
|
|
|
}
|
|
|
|
back_button_presses = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fix OptionsScreen Ignoring The Back Button
|
2024-02-02 04:20:34 -05:00
|
|
|
static bool OptionsScreen_handleBackEvent_injection(OptionsScreen *screen, bool do_nothing) {
|
2021-07-04 22:32:19 -04:00
|
|
|
if (!do_nothing) {
|
2024-01-06 06:30:23 -05:00
|
|
|
Minecraft *minecraft = screen->minecraft;
|
2024-04-02 19:22:01 -04:00
|
|
|
Minecraft_setScreen(minecraft, nullptr);
|
2021-07-04 22:32:19 -04:00
|
|
|
}
|
2021-07-04 19:02:45 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-10-21 16:36:54 -04:00
|
|
|
// Fix "Sleeping Beauty" Bug
|
2024-01-06 06:30:23 -05:00
|
|
|
static int32_t InBedScreen_handleBackEvent_injection(InBedScreen *screen, bool do_nothing) {
|
2023-10-19 01:46:09 -04:00
|
|
|
if (!do_nothing) {
|
|
|
|
// Close Screen
|
2024-01-06 06:30:23 -05:00
|
|
|
Minecraft *minecraft = screen->minecraft;
|
2024-04-02 19:22:01 -04:00
|
|
|
Minecraft_setScreen(minecraft, nullptr);
|
2023-10-19 01:46:09 -04:00
|
|
|
// Stop Sleeping
|
2024-01-06 06:30:23 -05:00
|
|
|
LocalPlayer *player = minecraft->player;
|
2024-04-02 19:22:01 -04:00
|
|
|
if (player != nullptr) {
|
2024-01-06 06:30:23 -05:00
|
|
|
player->vtable->stopSleepInBed(player, 1, 1, 1);
|
2023-10-19 01:46:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-07-04 19:02:45 -04:00
|
|
|
// 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
|
2024-01-06 06:30:23 -05:00
|
|
|
static void _handle_mouse_grab(Minecraft *minecraft) {
|
2021-07-04 19:02:45 -04:00
|
|
|
if (mouse_grab_state == -1) {
|
|
|
|
// Grab
|
2024-01-07 03:23:43 -05:00
|
|
|
Minecraft_grabMouse(minecraft);
|
2021-07-04 19:02:45 -04:00
|
|
|
} else if (mouse_grab_state == 1) {
|
|
|
|
// Un-Grab
|
2024-01-07 03:23:43 -05:00
|
|
|
Minecraft_releaseMouse(minecraft);
|
2021-07-04 19:02:45 -04:00
|
|
|
}
|
|
|
|
mouse_grab_state = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#include <SDL/SDL.h>
|
|
|
|
|
|
|
|
// Block UI Interaction When Mouse Is Locked
|
2024-01-06 06:30:23 -05:00
|
|
|
static bool Gui_tickItemDrop_Minecraft_isCreativeMode_call_injection(Minecraft *minecraft) {
|
2024-04-02 19:22:01 -04:00
|
|
|
bool is_in_game = minecraft->screen == nullptr || minecraft->screen->vtable == (Screen_vtable *) Touch_IngameBlockSelectionScreen_vtable_base;
|
2024-02-01 03:12:24 -05:00
|
|
|
if (!enable_misc || (SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_OFF && is_in_game)) {
|
2021-07-04 19:02:45 -04:00
|
|
|
// Call Original Method
|
2024-01-07 03:23:43 -05:00
|
|
|
return creative_is_restricted() && Minecraft_isCreativeMode(minecraft);
|
2021-07-04 19:02:45 -04:00
|
|
|
} else {
|
|
|
|
// Disable Item Drop Ticking
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Block UI Interaction When Mouse Is Locked
|
2024-01-06 06:30:23 -05:00
|
|
|
static void Gui_handleClick_injection(Gui *gui, int32_t param_2, int32_t param_3, int32_t param_4) {
|
2021-07-04 19:02:45 -04:00
|
|
|
if (SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_OFF) {
|
|
|
|
// Call Original Method
|
2024-01-07 03:23:43 -05:00
|
|
|
Gui_handleClick(gui, param_2, param_3, param_4);
|
2021-07-04 19:02:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init
|
|
|
|
void _init_misc() {
|
2022-04-09 20:01:16 -04:00
|
|
|
enable_misc = feature_has("Miscellaneous Input Fixes", server_disabled);
|
2021-07-04 19:02:45 -04:00
|
|
|
if (enable_misc) {
|
|
|
|
// Fix OptionsScreen Ignoring The Back Button
|
|
|
|
patch_address(OptionsScreen_handleBackEvent_vtable_addr, (void *) OptionsScreen_handleBackEvent_injection);
|
2023-10-19 01:46:09 -04:00
|
|
|
// Fix "Sleeping Beauty" Bug
|
|
|
|
patch_address(InBedScreen_handleBackEvent_vtable_addr, (void *) InBedScreen_handleBackEvent_injection);
|
2021-07-04 19:02:45 -04:00
|
|
|
// Disable Opening Inventory Using The Cursor When Cursor Is Hidden
|
|
|
|
overwrite_calls((void *) Gui_handleClick, (void *) Gui_handleClick_injection);
|
|
|
|
}
|
2021-11-13 23:29:48 -05: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 19:42:55 -04:00
|
|
|
input_run_on_tick(_handle_back);
|
|
|
|
input_run_on_tick(_handle_mouse_grab);
|
2021-07-04 19:02:45 -04:00
|
|
|
}
|