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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • The mod I'm working on is in 1.19.2. The portal works correctly in Intellij but when I publish the jar, put it in the mods folder of the game it crashes with the following error whenever any entity collides with it: java.lang.IllegalAccessError: class com.github.warrentode.turtleblockacademy.blocks.TBAMiningPortalBlock tried to access protected field net.minecraft.world.entity.Entity.f_19819_ (com.github.warrentode.turtleblockacademy.blocks.TBAMiningPortalBlock is in module [email protected] of loader 'TRANSFORMER' @16c5b50a; net.minecraft.world.entity.Entity is in module [email protected] of loader 'TRANSFORMER' @16c5b50a)     at com.github.warrentode.turtleblockacademy.blocks.TBAMiningPortalBlock.m_7892_(TBAMiningPortalBlock.java:124) ~[turtleblockacademy-2024.2025-1.0.0.jar%23572!/:2024.2025-1.0.0] {re:classloading} The thing is, I have Entity.f_19819_ in my accessTransformer.cfg file in this line: public net.minecraft.world.entity.Entity f_19819_ # portalEntrancePos So what do I need to do to fix this error?
    • It will be about medeaival times
    • the mods are crashing and I'm not sure why so heres everything  crash log https://pastebin.com/RxLKbMNR  L2 Library (12library) has failed to load correctly java.lang.NoClassDefFoundError: org/antarcticgardens/newage/content/energiser/EnergiserBlock L2 Screen Tracker (12screentracker) has failed to load correctly java.lang.NoClassDefFoundError: Could not initialize class dev.xkmc.12library.base.L2Registrate Create: Interiors (interiors) has failed to load correctly java.lang.NoClassDefFoundError: Could not initialize class com.tterrag.registrate.AbstractRegistrate L2 Damage Tracker (12damagetracker) has failed to load correctly java.lang.NoClassDefFoundError: Could not initialize class dev.xkmc.l2library.base.L2Registrate Create Enchantment Industry (create_enchantment_industry) has failed to load correctly java.lang.NoClassDefFoundError: Could not initialize class com.simibubi.create.foundation.data.Createfiegistrate Create Crafts & Additions (createaddition) has failed to load correctly java.lang.NoClassDefFoundError: Could not initialize class com.simibubi.create.foundation.data.CreateRegistrate Create Slice & Dice (sliceanddice) has failed to load correctly java.lang.NoClassDefFoundError: Could not initialize class com.simibubi.create.foundation.data.CreateRegistrate L2 Tabs (12tabs) has failed to load correctly java.lang.NoClassDefFoundError: Could not initialize class dev.xkmc.l2library.base.L2Registrate Modular Golems (modulargolems) has failed to load correctly java.lang.NoClassDefFoundError: Could not initialize class dev.xkmc.l2library.base.L2Registrate Create: Steam 'n' FRails (railways) has failed to load correctly java.lang.NoClassDefFoundError : Could not initialize class com.simibubi.create.foundation.data.Createfregistrate Cuisine Delight (cuisinedelight) has failed to load correctly java.lang.NoClassDefFoundError: Could not initialize class dev.xkmc.12library.base.L2Registrate Create (create) has failed to load correctly java.lang.NoClassDefFoundError: Could not initialize class com.simibubi.create.Create Guardian Beam Defense (creategbd) has failed to load correctly java.lang.NoClassDefFoundError: Could not initialize class com.simibubi.create.foundation.data.CreateRegistrate L2 Item Selector (12itemselector) has failed to load correctly java.lang.NoClassDefFoundError: Could not initialize class dev.xkmc.l2library.base.L2Registrate
    • hey there, I have been using Forge for years without any problems, but for some time now I have been getting this error message when I click on “Installer” under the downloads. This happens on all versions. I have tried various things but have not gotten any results. If anyone has a solution, I would be very grateful!
    • I don't have mcreator installed.
  • Topics

×
×
  • Create New...

Important Information

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