diff --git a/src/main/java/com/replaymod/core/KeyBindingRegistry.java b/src/main/java/com/replaymod/core/KeyBindingRegistry.java index 24a3807b..65acb418 100644 --- a/src/main/java/com/replaymod/core/KeyBindingRegistry.java +++ b/src/main/java/com/replaymod/core/KeyBindingRegistry.java @@ -9,6 +9,7 @@ import net.minecraft.util.ReportedException; import net.minecraftforge.fml.client.registry.ClientRegistry; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import net.minecraftforge.fml.common.gameevent.InputEvent; +import org.lwjgl.input.Keyboard; import java.util.Collection; import java.util.Collections; @@ -19,6 +20,7 @@ import java.util.concurrent.Callable; public class KeyBindingRegistry { private Map keyBindings = new HashMap(); private Multimap keyBindingHandlers = ArrayListMultimap.create(); + private Multimap rawHandlers = ArrayListMultimap.create(); public void registerKeyBinding(String name, int keyCode, Runnable whenPressed) { KeyBinding keyBinding = keyBindings.get(name); @@ -30,12 +32,21 @@ public class KeyBindingRegistry { keyBindingHandlers.put(keyBinding, whenPressed); } + public void registerRaw(int keyCode, Runnable whenPressed) { + rawHandlers.put(keyCode, whenPressed); + } + public Map getKeyBindings() { return Collections.unmodifiableMap(keyBindings); } @SubscribeEvent public void onKeyInput(InputEvent.KeyInputEvent event) { + handleKeyBindings(); + handleRaw(); + } + + public void handleKeyBindings() { for (Map.Entry> entry : keyBindingHandlers.asMap().entrySet()) { while (entry.getKey().isPressed()) { for (final Runnable runnable : entry.getValue()) { @@ -57,4 +68,24 @@ public class KeyBindingRegistry { } } } + + public void handleRaw() { + int keyCode = Keyboard.getEventKey() == 0 ? Keyboard.getEventCharacter() + 256 : Keyboard.getEventKey(); + for (final Runnable runnable : rawHandlers.get(keyCode)) { + try { + runnable.run(); + } catch (Throwable cause) { + CrashReport crashReport = CrashReport.makeCrashReport(cause, "Handling Raw Key Binding"); + CrashReportCategory category = crashReport.makeCategory("Key Binding"); + category.addCrashSection("Key Code", keyCode); + category.addCrashSectionCallable("Handler", new Callable() { + @Override + public Object call() throws Exception { + return runnable; + } + }); + throw new ReportedException(crashReport); + } + } + } } diff --git a/src/main/java/com/replaymod/extras/HotkeyButtons.java b/src/main/java/com/replaymod/extras/HotkeyButtons.java index a36f9798..e3c29f30 100644 --- a/src/main/java/com/replaymod/extras/HotkeyButtons.java +++ b/src/main/java/com/replaymod/extras/HotkeyButtons.java @@ -1,5 +1,6 @@ package com.replaymod.extras; +import com.replaymod.core.KeyBindingRegistry; import com.replaymod.core.ReplayMod; import com.replaymod.replay.events.ReplayOpenEvent; import com.replaymod.replay.gui.overlay.GuiReplayOverlay; @@ -66,7 +67,8 @@ public class HotkeyButtons implements Extra { } }.setLayout(panelLayout = new GridLayout().setSpacingX(5).setSpacingY(5).setColumns(1)); - for (final KeyBinding keyBinding : mod.getKeyBindingRegistry().getKeyBindings().values()) { + final KeyBindingRegistry keyBindingRegistry = mod.getKeyBindingRegistry(); + for (final KeyBinding keyBinding : keyBindingRegistry.getKeyBindings().values()) { String keyName = "???"; try { keyName = Keyboard.getKeyName(keyBinding.getKeyCode()); @@ -79,7 +81,7 @@ public class HotkeyButtons implements Extra { @Override public void run() { keyBinding.pressTime++; - FMLCommonHandler.instance().fireKeyInput(); + keyBindingRegistry.handleKeyBindings(); } }), new GuiLabel().setI18nText(keyBinding.getKeyDescription())