KeyManager.java 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package me.km.networking;
  2. import net.minecraft.client.settings.KeyBinding;
  3. import net.minecraftforge.fml.client.registry.ClientRegistry;
  4. import net.minecraftforge.event.TickEvent;
  5. import net.minecraftforge.api.distmarker.Dist;
  6. import net.minecraftforge.api.distmarker.OnlyIn;
  7. import net.minecraftforge.eventbus.api.SubscribeEvent;
  8. import org.lwjgl.glfw.GLFW;
  9. @OnlyIn(Dist.CLIENT)
  10. public class KeyManager
  11. {
  12. private final KeyBinding[] functionKeys;
  13. public KeyManager()
  14. {
  15. functionKeys = new KeyBinding[9];
  16. functionKeys[0] = register(1, GLFW.GLFW_KEY_G);
  17. functionKeys[1] = register(2, GLFW.GLFW_KEY_H);
  18. functionKeys[2] = register(3, GLFW.GLFW_KEY_J);
  19. functionKeys[3] = register(4, GLFW.GLFW_KEY_K);
  20. functionKeys[4] = register(5, GLFW.GLFW_KEY_V);
  21. functionKeys[5] = register(6, GLFW.GLFW_KEY_B);
  22. functionKeys[6] = register(7, GLFW.GLFW_KEY_N);
  23. functionKeys[7] = register(8, GLFW.GLFW_KEY_M);
  24. functionKeys[8] = register(9, GLFW.GLFW_KEY_COMMA);
  25. }
  26. public String getKeyDescription(int index)
  27. {
  28. return functionKeys[index].getLocalizedName();
  29. }
  30. private KeyBinding register(int id, int key)
  31. {
  32. KeyBinding keyBind = new KeyBinding("key.function." + id, key, "key.km.function");
  33. ClientRegistry.registerKeyBinding(keyBind);
  34. return keyBind;
  35. }
  36. @SubscribeEvent
  37. public void onRenderGui(TickEvent.ClientTickEvent e)
  38. {
  39. for(int i = 0; i < functionKeys.length; i++)
  40. {
  41. if(functionKeys[i].isPressed())
  42. {
  43. ModPacketHandler.sendFunctionKey(i + 1);
  44. }
  45. }
  46. }
  47. }