From f3dc145d4a581d76f3dca0c6d63207f433e230db Mon Sep 17 00:00:00 2001 From: TheBrokenRail Date: Fri, 7 Oct 2022 00:06:50 -0400 Subject: [PATCH] Allow Opening Crafting With Controller --- docs/CONTROLS.md | 5 +++-- media-layer/core/src/media.c | 7 +++++++ media-layer/include/SDL/SDL_keysym.h | 3 ++- mods/CMakeLists.txt | 2 +- mods/include/mods/input/input.h | 1 + mods/src/compat/compat.c | 4 ++++ mods/src/input/crafting.cpp | 30 ++++++++++++++++++++++++++++ mods/src/input/input-internal.h | 1 + mods/src/input/input.cpp | 3 +++ symbols/include/symbols/minecraft.h | 7 +++++++ 10 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 mods/src/input/crafting.cpp diff --git a/docs/CONTROLS.md b/docs/CONTROLS.md index c71b3c461..bef9e5149 100644 --- a/docs/CONTROLS.md +++ b/docs/CONTROLS.md @@ -29,10 +29,11 @@ | Action | Function | | --- | --- | | A | Jump | -| D-Pad Down | Drop Item | | Y | Open Inventory | -| D-Pad Up | Change Perspective | | B | Sneak[^1] | +| X | Open Crafting | +| D-Pad Up | Change Perspective | +| D-Pad Down | Drop Item | | D-Pad Right | Open Chat | | Left/Right Bumper | Cycle Selected Item In Toolbar | | Left Trigger | Attack/Destroy | diff --git a/media-layer/core/src/media.c b/media-layer/core/src/media.c index 2c9b2a9e0..b57a53063 100644 --- a/media-layer/core/src/media.c +++ b/media-layer/core/src/media.c @@ -60,6 +60,7 @@ static void character_event(char c) { } // Convert GLFW Key To SDL Key +#define IMAGINARY_GLFW_CRAFTING_KEY GLFW_KEY_LAST static SDLKey glfw_key_to_sdl_key(int key) { switch (key) { // Movement @@ -136,6 +137,9 @@ static SDLKey glfw_key_to_sdl_key(int key) { // Chat case GLFW_KEY_T: return SDLK_t; + // Crafting + case IMAGINARY_GLFW_CRAFTING_KEY: + return SDLK_WORLD_0; // Unknown default: return SDLK_UNKNOWN; @@ -293,6 +297,9 @@ static SDLKey glfw_controller_button_to_key(int button) { case GLFW_GAMEPAD_BUTTON_START: case GLFW_GAMEPAD_BUTTON_BACK: return GLFW_KEY_ESCAPE; + // Crafting + case GLFW_GAMEPAD_BUTTON_X: + return IMAGINARY_GLFW_CRAFTING_KEY; // Unknown default: return GLFW_KEY_UNKNOWN; diff --git a/media-layer/include/SDL/SDL_keysym.h b/media-layer/include/SDL/SDL_keysym.h index a489b9352..a6397c521 100644 --- a/media-layer/include/SDL/SDL_keysym.h +++ b/media-layer/include/SDL/SDL_keysym.h @@ -38,7 +38,8 @@ typedef enum { SDLK_F11 = 292, SDLK_F12 = 293, SDLK_RSHIFT = 303, - SDLK_LSHIFT = 304 + SDLK_LSHIFT = 304, + SDLK_WORLD_0 = 160 // Used For Controller Crafting Button } SDLKey; typedef enum { diff --git a/mods/CMakeLists.txt b/mods/CMakeLists.txt index b1d393ccb..ab2ac3b8e 100644 --- a/mods/CMakeLists.txt +++ b/mods/CMakeLists.txt @@ -62,7 +62,7 @@ else() target_link_libraries(camera screenshot) endif() - add_library(input SHARED src/input/input.cpp src/input/bow.c src/input/attack.c src/input/toggle.c src/input/misc.c src/input/drop.cpp) + add_library(input SHARED src/input/input.cpp src/input/bow.c src/input/attack.c src/input/toggle.c src/input/misc.c src/input/drop.cpp src/input/crafting.cpp) target_link_libraries(input mods-headers reborn-patch symbols creative feature misc media-layer-core) add_library(sign SHARED src/sign/sign.cpp) diff --git a/mods/include/mods/input/input.h b/mods/include/mods/input/input.h index 1771a2faf..782221640 100644 --- a/mods/include/mods/input/input.h +++ b/mods/include/mods/input/input.h @@ -12,6 +12,7 @@ void input_hide_gui(); void input_third_person(); int input_back(); void input_drop(int drop_slot); +void input_open_crafting(); void input_set_is_left_click(int val); diff --git a/mods/src/compat/compat.c b/mods/src/compat/compat.c index 29e8f5f7b..06ccd57b0 100644 --- a/mods/src/compat/compat.c +++ b/mods/src/compat/compat.c @@ -84,6 +84,10 @@ HOOK(SDL_PollEvent, int, (SDL_Event *event)) { // Drop Item input_drop((event->key.keysym.mod & KMOD_CTRL) != 0); handled = 1; + } else if (event->key.keysym.sym == SDLK_WORLD_0) { + // Crafting + input_open_crafting(); + handled = 1; } break; } diff --git a/mods/src/input/crafting.cpp b/mods/src/input/crafting.cpp new file mode 100644 index 000000000..b3975a7fd --- /dev/null +++ b/mods/src/input/crafting.cpp @@ -0,0 +1,30 @@ +#include +#include + +#include "input-internal.h" +#include +#include + +// Store Should Open Crafting Menu +static int should_open_crafting = 0; +void input_open_crafting() { + should_open_crafting = 1; +} +static void _handle_open_crafting(unsigned char *minecraft) { + if (should_open_crafting) { + should_open_crafting = 0; + + // Set Screen + if (!creative_is_restricted() || !(*Minecraft_isCreativeMode)(minecraft)) { + unsigned char *screen = (unsigned char *) ::operator new(WORKBENCH_SCREEN_SIZE); + ALLOC_CHECK(screen); + screen = (*WorkbenchScreen)(screen, 0); + (*Minecraft_setScreen)(minecraft, screen); + } + } +} + +// Init +void _init_crafting() { + input_run_on_tick(_handle_open_crafting); +} diff --git a/mods/src/input/input-internal.h b/mods/src/input/input-internal.h index 2b081c8a0..f7943ec5e 100644 --- a/mods/src/input/input-internal.h +++ b/mods/src/input/input-internal.h @@ -9,6 +9,7 @@ __attribute__((visibility("internal"))) void _init_bow(); __attribute__((visibility("internal"))) void _init_misc(); __attribute__((visibility("internal"))) void _init_toggle(); __attribute__((visibility("internal"))) void _init_drop(); +__attribute__((visibility("internal"))) void _init_crafting(); #ifdef __cplusplus } diff --git a/mods/src/input/input.cpp b/mods/src/input/input.cpp index 34303b520..3cc410f4b 100644 --- a/mods/src/input/input.cpp +++ b/mods/src/input/input.cpp @@ -49,6 +49,9 @@ void init_input() { // Allow Attacking Mobs _init_attack(); + // Allow Opening Crafting With Controller + _init_crafting(); + // Disable Raw Mouse Motion if (feature_has("Disable Raw Mouse Motion (Not Recommended)", server_disabled)) { media_set_raw_mouse_motion_enabled(0); diff --git a/symbols/include/symbols/minecraft.h b/symbols/include/symbols/minecraft.h index f32f85e3e..dec17e3cb 100644 --- a/symbols/include/symbols/minecraft.h +++ b/symbols/include/symbols/minecraft.h @@ -854,6 +854,13 @@ static uint32_t HumanoidModel_is_sneaking_property_offset = 0x236; // bool static void *PlayerRenderer_render_vtable_addr = (void *) 0x107f08; +// WorkbenchScreen + +#define WORKBENCH_SCREEN_SIZE 0x180 + +typedef unsigned char *(*WorkbenchScreen_t)(unsigned char *screen, int32_t param_1); +static WorkbenchScreen_t WorkbenchScreen = (WorkbenchScreen_t) 0x301cc; + // Method That Require C++ Types #ifdef __cplusplus