minecraft-pi-reborn/mods/src/input/misc.cpp

92 lines
3.3 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>
2024-06-21 05:19:37 +00:00
#include <SDL/SDL.h>
2021-07-04 23:02:45 +00:00
2022-06-25 21:30:08 +00:00
#include "input-internal.h"
#include <mods/input/input.h>
#include <mods/feature/feature.h>
#include <mods/creative/creative.h>
2024-06-21 05:19:37 +00:00
#include <mods/misc/misc.h>
2021-07-04 23:02:45 +00:00
// Enable Miscellaneous Input Fixes
static int enable_misc = 0;
// Handle Back Button Presses
2024-01-06 11:30:23 +00:00
static void _handle_back(Minecraft *minecraft) {
2024-04-02 23:22:01 +00: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-21 03:58:40 +00:00
// Unable to safely run Minecraft::handleBack, deferring until safe.
return;
}
// Send Event
2024-06-21 05:19:37 +00:00
minecraft->handleBack(false);
2021-07-04 23:02:45 +00:00
}
// Fix OptionsScreen Ignoring The Back Button
static bool OptionsScreen_handleBackEvent_injection(OptionsScreen *screen, bool do_nothing) {
2021-07-05 02:32:19 +00:00
if (!do_nothing) {
2024-01-06 11:30:23 +00:00
Minecraft *minecraft = screen->minecraft;
2024-05-15 09:02:19 +00:00
minecraft->setScreen(nullptr);
2021-07-05 02:32:19 +00:00
}
2024-04-03 07:19:12 +00:00
return true;
2021-07-04 23:02:45 +00:00
}
2023-10-21 20:36:54 +00:00
// Fix "Sleeping Beauty" Bug
2024-04-03 07:19:12 +00:00
static bool InBedScreen_handleBackEvent_injection(InBedScreen *screen, bool do_nothing) {
2023-10-19 05:46:09 +00:00
if (!do_nothing) {
// Close Screen
2024-01-06 11:30:23 +00:00
Minecraft *minecraft = screen->minecraft;
2024-05-15 09:02:19 +00:00
minecraft->setScreen(nullptr);
2023-10-19 05:46:09 +00:00
// Stop Sleeping
2024-01-06 11:30:23 +00:00
LocalPlayer *player = minecraft->player;
2024-04-02 23:22:01 +00:00
if (player != nullptr) {
2024-06-21 05:19:37 +00:00
player->stopSleepInBed(true, true, true);
2023-10-19 05:46:09 +00:00
}
}
2024-04-03 07:19:12 +00:00
return true;
2023-10-19 05:46:09 +00:00
}
2021-07-04 23:02:45 +00:00
// Block UI Interaction When Mouse Is Locked
2024-01-06 11:30:23 +00:00
static bool Gui_tickItemDrop_Minecraft_isCreativeMode_call_injection(Minecraft *minecraft) {
2024-04-02 23:22:01 +00:00
bool is_in_game = minecraft->screen == nullptr || minecraft->screen->vtable == (Screen_vtable *) Touch_IngameBlockSelectionScreen_vtable_base;
2024-02-01 08:12:24 +00:00
if (!enable_misc || (SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_OFF && is_in_game)) {
2021-07-04 23:02:45 +00:00
// Call Original Method
2024-05-15 09:02:19 +00:00
return creative_is_restricted() && minecraft->isCreativeMode();
2021-07-04 23:02:45 +00:00
} else {
// Disable Item Drop Ticking
2024-06-21 05:19:37 +00:00
return true;
2021-07-04 23:02:45 +00:00
}
}
// Block UI Interaction When Mouse Is Locked
2024-04-03 07:19:12 +00:00
static void Gui_handleClick_injection(Gui_handleClick_t original, Gui *gui, int32_t param_2, int32_t param_3, int32_t param_4) {
2021-07-04 23:02:45 +00:00
if (SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_OFF) {
// Call Original Method
2024-04-03 07:19:12 +00:00
original(gui, param_2, param_3, param_4);
2021-07-04 23:02:45 +00:00
}
}
// 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
2024-05-05 00:46:15 +00:00
patch_vtable(OptionsScreen_handleBackEvent, OptionsScreen_handleBackEvent_injection);
2023-10-19 05:46:09 +00:00
// Fix "Sleeping Beauty" Bug
2024-05-05 00:46:15 +00:00
patch_vtable(InBedScreen_handleBackEvent, InBedScreen_handleBackEvent_injection);
2021-07-04 23:02:45 +00:00
// Disable Opening Inventory Using The Cursor When Cursor Is Hidden
2024-04-03 07:19:12 +00:00
overwrite_calls(Gui_handleClick, Gui_handleClick_injection);
2024-06-21 05:19:37 +00:00
// Proper Back Button Handling
misc_run_on_key_press([](Minecraft *mc, const int key) {
if (key == MC_KEY_ESCAPE) {
_handle_back(mc);
return true;
} else {
return false;
}
});
2021-07-04 23:02:45 +00:00
}
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-07-04 23:02:45 +00:00
}