Skip to content

Commit 5689c2d

Browse files
committed
Update to v149 - Binding refactor fix
1 parent 669b53a commit 5689c2d

1 file changed

Lines changed: 51 additions & 2 deletions

File tree

src/schembrowser/SchematicBrowser.java

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
public class SchematicBrowser extends Mod {
1818
public Fi schematicRepoDirectory;
1919
public SchematicBrowserDialog schematicBrowserDialog;
20+
private KeyCode browserKeycode = null;
21+
private int prevBrowserKeycode = -1;
22+
2023
public SchematicBrowser() {
2124
Log.info("Loaded Schematic Browser");
2225
}
@@ -39,9 +42,12 @@ public void init() {
3942
Core.scene.addListener(new InputListener() { // FINISHME: Make it work for mobile?
4043
@Override
4144
public boolean keyDown(InputEvent event, KeyCode keycode) {
42-
if (!state.isMenu() && !ui.chatfrag.shown() && !ui.schematics.isShown() && !ui.database.isShown()
45+
// This is terrible
46+
if (browserKeycode == null || keycodeChanged()) getBoundKeycode();
47+
if (keycode == SchematicBrowser.this.browserKeycode
48+
&& !state.isMenu() && !ui.chatfrag.shown() && !ui.schematics.isShown() && !ui.database.isShown()
4349
&& !ui.consolefrag.shown() && !ui.content.isShown() && !Core.scene.hasKeyboard()
44-
&& keycode == Core.keybinds.get(Binding.schematic_menu).key && Core.input.shift()
50+
&& Core.input.shift()
4551
) {
4652
ui.schematics.hide();
4753
schematicBrowserDialog.show();
@@ -66,6 +72,49 @@ public static SettingsMenuDialog.SettingsCategory settingsCategory() {
6672
public static void settingsMenuTable(SettingsMenuDialog.SettingsTable table) {
6773
table.checkPref("schematicbrowserimporttags", true);
6874
}
75+
76+
private boolean keycodeChanged(){
77+
// Known issue: Keycode settings do not write immediately after changing the keybind
78+
// But the performance hit for detecting changes immediately is not worth.
79+
return prevBrowserKeycode != (prevBrowserKeycode = Core.settings.getInt("keybind-default-keyboard-schematic_menu-key", -1));
80+
}
81+
82+
private void getBoundKeycode(){
83+
boolean oldBindings = false;
84+
try {
85+
Binding.class.getDeclaredField("schematicMenu").get(null);
86+
} catch (Exception ignored) {
87+
oldBindings = true;
88+
}
89+
try {
90+
if (!oldBindings) {
91+
// Binding.schematicMenu.value.key
92+
browserKeycode = (KeyCode)Reflect.get(
93+
Class.forName("arc.input.KeyBind$Axis"),
94+
Reflect.get(
95+
Class.forName("arc.input.KeyBind"),
96+
Binding.class.getDeclaredField("schematicMenu").get(null),
97+
"value"
98+
),
99+
"key"
100+
);
101+
} else {
102+
// Core.keybinds.get(Binding.schematic_menu).key
103+
browserKeycode = (KeyCode)Reflect.get(
104+
Class.forName("arc.KeyBinds$Axis"),
105+
Reflect.invoke(
106+
Core.class.getDeclaredField("keybinds").get(null),
107+
"get",
108+
new Object[]{Binding.class.getDeclaredField("schematic_menu").get(null)},
109+
Class.forName("arc.KeyBinds$KeyBind")
110+
),
111+
"key"
112+
);
113+
}
114+
} catch (Exception ex) {
115+
throw new RuntimeException(ex);
116+
}
117+
}
69118
}
70119

71120

0 commit comments

Comments
 (0)