Jump to content

[1.7.2]Any key holding method? or any suggestion on how to do this?


lorizz

Recommended Posts

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 =)

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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)
                }
            }

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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
}

Link to comment
Share on other sites

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++;
}

Link to comment
Share on other sites

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
}
}

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.