Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close the narrator menu if the F4 key is pressed while the menu is opening #83

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class NarratorMenu {
private static boolean isMenuKeyPressedPreviousTick = false;
private static boolean isHotKeyPressedPreviousTick = false;
private static boolean isHotKeySwitchedPreviousTick = false;
private static boolean isNarratorMenuJustClosed = false;
private int hotKeyFunctionIndex = 0;
private static final boolean[] LAST_RUN_HAS_DONE_FLAG = new boolean[10];

Expand Down Expand Up @@ -87,8 +88,18 @@ public void update() {
if (minecraftClient == null) return;
if (minecraftClient.player == null) return;

KeyBindingsHandler kbh = KeyBindingsHandler.getInstance();
boolean isNarratorMenuKeyPressed = KeyUtils.isAnyPressed(kbh.narratorMenuKey);

// With Narrator Menu opened, listen to number keys pressing for executing corresponding functions
if (minecraftClient.currentScreen instanceof NarratorMenuGUI) {
// Close the menu if the F4 key is pressed while the menu is opening
if (isNarratorMenuKeyPressed) {
isNarratorMenuJustClosed = true;
minecraftClient.currentScreen.close();
return;
}

// for the little performance improvement, will not use KeyUtils here.
long handle = minecraftClient.getWindow().getHandle();
Stream.of(MENU_FUNCTIONS)
Expand All @@ -100,16 +111,14 @@ public void update() {

if (minecraftClient.currentScreen != null) return;

KeyBindingsHandler kbh = KeyBindingsHandler.getInstance();
boolean isNarratorMenuKeyPressed = KeyUtils.isAnyPressed(kbh.narratorMenuKey);
boolean isNarratorMenuHotKeyPressed = KeyUtils.isAnyPressed(kbh.narratorMenuHotKey);

// F3 + F4 triggers game mode changing function in vanilla game, will not open the menu under this situation.
boolean isF3KeyNotPressed = !KeyUtils.isF3Pressed();

// The F4 is pressed before and released at current tick
// To make the narrator menu open AFTER release the F4 key
boolean openTheMenuScreen = !isNarratorMenuKeyPressed && isMenuKeyPressedPreviousTick;
boolean openTheMenuScreen = !isNarratorMenuKeyPressed && isMenuKeyPressedPreviousTick && !isNarratorMenuJustClosed;

// Opposite to menu open, executes immediately,
// but will not execute twice until release and press the key again
Expand Down Expand Up @@ -142,8 +151,10 @@ public void update() {
// update the states for next tick
isMenuKeyPressedPreviousTick = isNarratorMenuKeyPressed;
isHotKeyPressedPreviousTick = isNarratorMenuHotKeyPressed;
// clean the state when F4 is released

// clean the states when F4 is released
if (openTheMenuScreen) isHotKeySwitchedPreviousTick = false;
if (!isNarratorMenuKeyPressed) isNarratorMenuJustClosed = false;

} catch (Exception e) {
MainClass.errorLog("An error occurred in NarratorMenu.");
Expand Down