Posted May 26, 201411 yr Hi! I've been trying to do some simple modding, but I've run into a wall trying to get a keybind to work. My mod displays the player's coordinates on screen, and the idea is to add a keybind that will change the display location of the coordinates. However, I can't get anything (not even a print statement) to happen on key input. Here's my KeyInputHandler class: package eum3.minecraft.simplecoords; import org.lwjgl.input.Keyboard; import net.minecraft.client.Minecraft; import net.minecraft.client.settings.KeyBinding; import cpw.mods.fml.client.FMLClientHandler; import cpw.mods.fml.client.registry.ClientRegistry; import cpw.mods.fml.common.eventhandler.EventPriority; import cpw.mods.fml.common.eventhandler.SubscribeEvent; import cpw.mods.fml.common.gameevent.InputEvent; import cpw.mods.fml.common.gameevent.InputEvent.KeyInputEvent; public class KeyInputHandler { //public static int coordloc = 0; private Minecraft mc; public KeyBinding Coordlocation; public KeyInputHandler() { Coordlocation = new KeyBinding("Coords location", Keyboard.KEY_RBRACKET, "SimpleCoords"); // Register KeyBindings to the ClientRegistry ClientRegistry.registerKeyBinding(Coordlocation); } @SubscribeEvent(priority = EventPriority.NORMAL) public void onKeyInput(KeyInputEvent event) { //System.out.println("A key was pressed."); if(mc.inGameHasFocus) { if(Coordlocation.isPressed()) { //coords.coordloc = 1; //System.out.println("] was pressed."); if(coords.coordloc == 2) coords.coordloc = 0; else coords.coordloc += 1; } } } } Here's the ClientProxy: package eum3.minecraft.simplecoords.client; import org.lwjgl.input.Keyboard; import cpw.mods.fml.client.registry.ClientRegistry; import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.event.FMLPreInitializationEvent; import net.minecraft.client.Minecraft; import net.minecraft.client.settings.KeyBinding; import net.minecraftforge.client.MinecraftForgeClient; import net.minecraftforge.common.MinecraftForge; import eum3.minecraft.simplecoords.CommonProxy; import eum3.minecraft.simplecoords.KeyInputHandler; import eum3.minecraft.simplecoords.coords; import org.lwjgl.input.Keyboard; public class ClientProxy extends CommonProxy { @Override public void preInit(FMLPreInitializationEvent event) { FMLCommonHandler.instance().bus().register(new KeyInputHandler()); } } Here's the main mod file: package eum3.minecraft.simplecoords; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.FontRenderer; import net.minecraft.client.gui.Gui; import net.minecraftforge.client.event.RenderGameOverlayEvent; import net.minecraftforge.client.event.RenderGameOverlayEvent.ElementType; //import net.minecraftforge.event.EventPriority; //import net.minecraftforge.event.ForgeSubscribe; import net.minecraftforge.event.*; import cpw.mods.fml.common.gameevent.InputEvent; import cpw.mods.fml.common.gameevent.InputEvent.KeyInputEvent; import org.lwjgl.input.Keyboard; import org.lwjgl.opengl.GL11; import cpw.mods.fml.common.eventhandler.EventPriority; import cpw.mods.fml.common.eventhandler.SubscribeEvent; public class coords extends Gui{ private Minecraft mc; public static int coordloc = 0; public coords(Minecraft mc) { super(); this.mc = mc; } /*@SubscribeEvent(priority = EventPriority.NORMAL) public void onKeyInput(KeyInputEvent event) { //System.out.println("A key was pressed."); int key = Keyboard.getEventKey(); //System.out.println(key); if(mc.inGameHasFocus) { if(key == Keyboard.KEY_RBRACKET) { //coords.coordloc = 1; //System.out.println("] was pressed."); if(coordloc == 2) coordloc = 0; else coordloc += 1; } } }*/ @SubscribeEvent(priority = EventPriority.NORMAL) public void onRenderCrosshairs(RenderGameOverlayEvent event) { if(event.isCancelable() || event.type != ElementType.CROSSHAIRS) { return; } int xPos = event.resolution.getScaledWidth()-40; int yPos = 10; int playerxint = 0; int playeryint = 0; int playerzint = 0; if(mc.thePlayer != null) { playerxint = (int) Math.floor(mc.thePlayer.posX); playeryint = (int) Math.floor(mc.thePlayer.posY); playerzint = (int) Math.floor(mc.thePlayer.posZ); playeryint -= 1; } FontRenderer fr = mc.fontRenderer; String s1 = Integer.toString(playerxint) + " " + Integer.toString(playerzint); String s2 = Integer.toString(playeryint) + " " + Integer.toString(coordloc); if(coordloc == 0) { this.drawCenteredString(fr, s1, 40, yPos, 0xFFFFFF); this.drawCenteredString(fr, s2, 40, yPos+10, 0xFFFFFF); } else if(coordloc == 1) { this.drawCenteredString(fr, s1, (xPos+40)/2, yPos, 0xFFFFFF); this.drawCenteredString(fr, s2, (xPos+40)/2, yPos+10, 0xFFFFFF); } else if(coordloc >= 2) { this.drawCenteredString(fr, s1, xPos, yPos, 0xFFFFFF); this.drawCenteredString(fr, s2, xPos, yPos+10, 0xFFFFFF); } } } Sorry for the large amount of commented out code, I've tried a lot of things to get this working. Any idea what I'm doing wrong?
May 26, 201411 yr Did you check if the keybinding appears in minecraft's controls gui? I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP) II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.
May 26, 201411 yr Author Yes. However, I managed to figure out my problems. The real problem was that I had my KeyInputHandler.java file outside of the client package. I've also made a number of other changes to get descriptions, saved settings, etc. working. Here's my fixed code: KeyInputHandler.java package eum3.minecraft.simplecoords.client; import org.lwjgl.input.Keyboard; import net.minecraft.client.Minecraft; import net.minecraft.client.settings.KeyBinding; import net.minecraft.util.StatCollector; import cpw.mods.fml.client.FMLClientHandler; import cpw.mods.fml.client.registry.ClientRegistry; import cpw.mods.fml.common.eventhandler.EventPriority; import cpw.mods.fml.common.eventhandler.SubscribeEvent; import cpw.mods.fml.common.gameevent.InputEvent; import cpw.mods.fml.common.gameevent.InputEvent.KeyInputEvent; import eum3.minecraft.simplecoords.coords; public class KeyInputHandler { //public static int coordloc = 0; private final Minecraft mc; public static final int loc = 0; private static final String[] desc = {"key.coordslocation.desc"}; private static final int[] keyValues = {Keyboard.KEY_RBRACKET}; public static final KeyBinding[] keys = new KeyBinding[desc.length]; //public KeyBinding Coordlocation; public KeyInputHandler() { mc = Minecraft.getMinecraft(); for(int i = 0; i < desc.length; ++i) { keys[i] = new KeyBinding(desc[i], keyValues[i], StatCollector.translateToLocal("key.SimpleCoords.label")); ClientRegistry.registerKeyBinding(keys[i]); Minecraft.getMinecraft().gameSettings.loadOptions(); } //Coordlocation = new KeyBinding("Coords location", Keyboard.KEY_RBRACKET, "SimpleCoords"); // Register KeyBindings to the ClientRegistry //ClientRegistry.registerKeyBinding(Coordlocation); } @SubscribeEvent(priority = EventPriority.NORMAL) public void onKeyInput(KeyInputEvent event) { //System.out.println("A key was pressed."); if(mc.inGameHasFocus) { if(keys[loc].isPressed()) { //coords.coordloc = 1; //System.out.println("] was pressed."); if(coords.coordloc == 2) coords.coordloc = 0; else coords.coordloc += 1; } } } } ClientProxy.java package eum3.minecraft.simplecoords.client; import org.lwjgl.input.Keyboard; import cpw.mods.fml.client.registry.ClientRegistry; import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.event.FMLPreInitializationEvent; import net.minecraft.client.Minecraft; import net.minecraft.client.settings.KeyBinding; import net.minecraftforge.client.MinecraftForgeClient; import net.minecraftforge.common.MinecraftForge; import eum3.minecraft.simplecoords.CommonProxy; import eum3.minecraft.simplecoords.coords; import org.lwjgl.input.Keyboard; public class ClientProxy extends CommonProxy { @Override public void registerRenderers() { FMLCommonHandler.instance().bus().register(new KeyInputHandler()); } } en_US.lang key.coordslocation.desc=Change Coords Location key.SimpleCoords.label=SimpleCoords The main mod file was not changed beyond altering the starting coordinate position.
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.