Posted April 11, 201411 yr Hi guys, it's me TheLorizz and I found this version a little harder and confusing than the others. I miss the old keyDown and keyUp methods and the hold KeyHandler from forge, but how do I do similiar things in forge 1.7.2? That's my class: KeyHandler (FMLEvents): package com.thelorizz.protocraft.handler; import net.minecraft.client.Minecraft; import net.minecraft.client.settings.KeyBinding; import net.minecraft.entity.player.EntityPlayer; import org.lwjgl.input.Keyboard; import cpw.mods.fml.client.registry.ClientRegistry; import cpw.mods.fml.common.eventhandler.SubscribeEvent; import cpw.mods.fml.common.gameevent.InputEvent.KeyInputEvent; import cpw.mods.fml.common.gameevent.TickEvent.ClientTickEvent; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; public class ProtoKeyHandler { @SideOnly(Side.CLIENT) public KeyBinding jump = new KeyBinding("Proto Jump", Keyboard.KEY_SPACE, "Protocraft"); //On mod loading, automatically the normal jump will be bound to Keyboard.KEY_L //com.thelorizz.protocraft.handler.gameupdater.updateKeybinds:line 32; public ProtoKeyHandler() { ClientRegistry.registerKeyBinding(jump); } public boolean isHeld = false; /** * KeyInputEvent is in the FML package, so we must register to the FML event * bus */ @SubscribeEvent public void onKeyInput(KeyInputEvent event) { // Salto EntityPlayer player = (EntityPlayer) Minecraft.getMinecraft().thePlayer; if (jump.isPressed() && this.isHeld == false) { this.isHeld = true; double motionY = player.motionY; System.out.println("Caricando salto!"); do { System.out.println(motionY); if (motionY >= 1) { player.motionY = motionY; player.jump(); this.isHeld = false; } else { motionY += 0.1D; player.motionY = motionY; } } while (this.isHeld); // player.jump(); this.isHeld = false; } } } How can I do a "keyHold" method like I did in 1.5.2 with keyUp and keyDown methods? Ps: and possibly, can I use "ClientTickEvent" for doing a key's code every tick? Thanks, lorizz =)
April 13, 201411 yr Here ya go: https://github.com/coolAlias/Tutorial-1.7.2/blob/master/src/main/java/tutorial/client/KeyHandler.java There is now KeyInputEvent, and you can check if your KeyBinding or any vanilla KeyBinding is pressed or not. http://i.imgur.com/NdrFdld.png[/img]
April 13, 201411 yr bump thats...unfair. I bumped my post 3 times and nobody answered. You bumped your post 1 time and get an answer... and thx for the link, could be useful for me too Here could be your advertisement!
April 13, 201411 yr Author Here ya go: https://github.com/coolAlias/Tutorial-1.7.2/blob/master/src/main/java/tutorial/client/KeyHandler.java There is now KeyInputEvent, and you can check if your KeyBinding or any vanilla KeyBinding is pressed or not. But any method like this? if (isKeyPressed) { print("pressed"); } else { print("released"); } With KeyInputEvent you can't because anytime you release a key, it stops there, no more calling.
April 13, 201411 yr Author Made it on my own. Created a new EventHandler wich contains a new FMLEvent (no CPW's modifies) that is called in there: In the Minecraft runTick method: while (Keyboard.next()) { [...] if (Keyboard.getEventKeyState()) { [...] FMLCommonHandler.instance().fireKeyInput(); //KeyInputEvent (on key press) } else { ProtoEventHandler.instance().fireKeyOutput(); //My KeyOutputEvent (on key release) } }
April 13, 201411 yr Made it on my own. Created a new EventHandler wich contains a new FMLEvent (no CPW's modifies) that is called in there: In the Minecraft runTick method: while (Keyboard.next()) { [...] if (Keyboard.getEventKeyState()) { [...] FMLCommonHandler.instance().fireKeyInput(); //KeyInputEvent (on key press) } else { ProtoEventHandler.instance().fireKeyOutput(); //My KeyOutputEvent (on key release) } } Does this work in mp without packets? Here could be your advertisement!
April 13, 201411 yr The server has never access to keys without packets. What do you expect? Magic? Thats what confused me. I thought that maybe there now is an easy to use 1-line build in feature... Here could be your advertisement!
April 13, 201411 yr Author The server has never access to keys without packets. What do you expect? Magic? Thats what confused me. I thought that maybe there now is an easy to use 1-line build in feature... What I did is only for Single Player and it works perfectly. Diesen is there a build in minecraft wait method, or something that can make it? Because I tried with the Timers, Thread-classes and ScheduledExecutorService but my minecraft crashes. This is what I want to do: double counter = 0.4D; while(keys[PROTOJUMP].getIsKeyPressed) { counter += 0.015D; if(counter >= 1.0D) { player.motionY = counter; counter = 0.4D; } //There I want the timer, because without it the thread crashes }
April 13, 201411 yr Author Without an update function how can I do it? 20 ticks = 1 second but in this case it isn't: private int ticks = 0; public void onTick() { if (ticks % 20 == 0) { System.out.println("One second!"); } ticks++; } I'm so confused
April 13, 201411 yr Author You probably want a TickHandler. Eheh but this is 1.7.2, I didn't understand TickEvents at all. This is the problem do I need to do something like: @SubscribeEvent public void keyDown(ClientTickEvent event) { if (event.phase == Phase.START) { final EntityPlayer player = (EntityPlayer) Minecraft.getMinecraft().thePlayer; if (mc.inGameHasFocus) { while (keys[PROTOJUMP].getIsKeyPressed()) { jumpCounter += 0.015D; if (jumpCounter >= 1.0D) { player.motionY = jumpCounter; jumpCounter = 0.4D; } jumpStarted = true; onTick(); } } } } private int ticks = 0; public void onTick() { if(ticks % 20 == 0) { System.out.println("1 second passed"); } ticks++; }
April 13, 201411 yr Author Looks good to me on a quick glance. Uhmm that doesn't work, I'm out of ideas
April 14, 201411 yr If you need to check that a key is pressed constantly, don't do that inside the key input event, and not with a while loop. I'm pretty sure that the key input event is called when a key is pressed, or released, but not any time in between. It is not a tick event. I suggest using some kind of onUpdate method, be it in a custom item, onLivingUpdate for players, a tick event for clients, or whatever, then you can check if your key is pressed just like you did, and perform the action every tick: public void someMethodCalledEveryTick(EntityPlayer player) { // make sure you are on the client! if (player.worldObj.isRemote && YourKeyClass.keys[PROTOJUMP].getIsKeyPressed()) { // jump code } } http://i.imgur.com/NdrFdld.png[/img]
April 14, 201411 yr Author It finally works, I used onUpdate from an extended EntityPlayer class =) Thanks all!
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.