Skip to content

Instantly share code, notes, and snippets.

@izzyaxel
Created September 4, 2016 10:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save izzyaxel/0891aaf1a58e407c20e5315c02db1510 to your computer and use it in GitHub Desktop.
Save izzyaxel/0891aaf1a58e407c20e5315c02db1510 to your computer and use it in GitHub Desktop.
public class KeyHandler
{
private static Map<KeyBinding, List<IKeyReceiver>> callbacks = new HashMap<KeyBinding, List<IKeyReceiver>>();
public static void registerKeybindForNotification(KeyBinding keyBinding, IKeyReceiver callbackReceiver)
{
if(!callbacks.containsKey(keyBinding))
{
List<IKeyReceiver> receivers = new ArrayList<IKeyReceiver>();
receivers.add(callbackReceiver);
callbacks.put(keyBinding, receivers);
}
else
{
List<IKeyReceiver> receivers = callbacks.get(keyBinding);
receivers.add(callbackReceiver);
callbacks.put(keyBinding, receivers);
}
}
@SubscribeEvent
public void clientKeyListener(TickEvent.PlayerTickEvent event)
{
if(event.side == Side.CLIENT && event.phase == TickEvent.Phase.START)
{
for(KeyBinding k : Minecraft.getMinecraft().gameSettings.keyBindings)
{
if(callbacks.containsKey(k) && k.isKeyDown())
{
List<IKeyReceiver> receivers = callbacks.get(k);
for(IKeyReceiver receiver : receivers)
{
receiver.onKeypress(event.player, k);
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment