Skip to content

Instantly share code, notes, and snippets.

@izzyaxel
Created September 4, 2016 21:52
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/dad0c5dd4f0aa79be21e9befca48d864 to your computer and use it in GitHub Desktop.
Save izzyaxel/dad0c5dd4f0aa79be21e9befca48d864 to your computer and use it in GitHub Desktop.
public interface IKeyReceiver
{
void onKeypress(EntityPlayer player, KeyBinding ... keyBindings);
}
public class KeyHandler
{
private static List<RegisteredKeybinding> registered = new ArrayList<RegisteredKeybinding>();
public static void registerKeybindsForNotification(IKeyReceiver callbackReceiver, KeyBinding... keyBindings)
{
registered.add(new RegisteredKeybinding(callbackReceiver, keyBindings));
}
@SubscribeEvent
public void clientKeyListener(TickEvent.PlayerTickEvent event)
{
if(event.side == Side.CLIENT && event.phase == TickEvent.Phase.START)
{
for(RegisteredKeybinding reg : registered)
{
for(KeyBinding k : reg.keyBindings)
{
if(k.isKeyDown())
{
reg.receiver.onKeypress(event.player, reg.keyBindings);
break;
}
}
}
}
}
}
public class RegisteredKeybinding
{
KeyBinding[] keyBindings;
IKeyReceiver receiver;
public RegisteredKeybinding(IKeyReceiver receiver, KeyBinding ... keyBindings)
{
this.keyBindings = keyBindings;
this.receiver = receiver;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment