Jump to content

[Solved][1.7.2] Problem with keybinding


Eum3

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.