Jump to content

Recommended Posts

Posted

I'm trying the spawn in an arrow and this is the error:

 

 

  Reveal hidden contents

 

 

Here is the method:

 

@Override
public void onKeyPress(EntityPlayer player) {
	if(!player.worldObj.isRemote){
		EntityArrow arrow = new EntityArrow(player.worldObj, player, 2);
		player.worldObj.spawnEntityInWorld(arrow);
	}

}

 

This method is called by my KeyEventHandler which provides an EntityPlayerMP for the player. What am I doing wrong?

 

 

Creator of the MyFit, MagiCraft, Tesseract gun, and Papa's Wingeria mod.

Posted

Ok I have a key handler that sends a packet to the server and the server receives that packet and calls the onKeyPress method. This keyPress method is part of the module classes we were talking about earlier

Creator of the MyFit, MagiCraft, Tesseract gun, and Papa's Wingeria mod.

Posted

Ok did some testing it turns out that when I press the key (only once) the method is called a LOT and keeps getting called over and over. What could make this happen?

Creator of the MyFit, MagiCraft, Tesseract gun, and Papa's Wingeria mod.

Posted
  On 11/17/2015 at 1:36 AM, Asweez said:

Ok did some testing it turns out that when I press the key (only once) the method is called a LOT and keeps getting called over and over. What could make this happen?

Hard to say without seeing your code, but I bet you're doing something like setting a field saying the key was pressed, then sending a packet as long as that is true.

 

What are you using to handle key presses? KeyInputEvent or one of the TickEvents? Show your whole class (or at least every piece of it related to handling keys) and any related classes.

Posted
  On 11/17/2015 at 1:36 AM, Asweez said:

Ok did some testing it turns out that when I press the key (only once) the method is called a LOT and keeps getting called over and over. What could make this happen?

 

Depending on what keyboard event your listening to, you're likely asking

isKeyDown(key)

which will be true for the entire duration that the key is pressed.  Which as the code runs 60+ times a second* and a press lasts a tenth of a second...that's at least 6 calls.

 

*Pretty much as fast as the program runs, not tied to game ticks in any way.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted

Code:

package com.apmods.hta.handler;

import com.apmods.hta.key.HTAKeys;
import com.apmods.hta.network.AttackKeyPackage;
import com.apmods.hta.network.HTANetwork;

import net.minecraft.client.Minecraft;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.InputEvent.KeyInputEvent;

public class HTAKeyHandler {
@SubscribeEvent
public void keyDown(KeyInputEvent event){
	if(HTAKeys.attack1.isPressed()){
		if(Minecraft.getMinecraft().inGameHasFocus){
			HTANetwork.net.sendToServer(new AttackKeyPackage.KeyMessage(1));
		}
	}
	else if(HTAKeys.attack2.isPressed()){
		if(Minecraft.getMinecraft().inGameHasFocus){
			HTANetwork.net.sendToServer(new AttackKeyPackage.KeyMessage(2));
		}
	}
}
}

 

And when I press it and let go of the key, it just keeps calling the method

Creator of the MyFit, MagiCraft, Tesseract gun, and Papa's Wingeria mod.

Posted

KeyInputEvent fires both when a key is pressed and when it is released, AND it fires for every single key on the keyboard, not just ones you may want to listen to.

 

You need to check Keyboard.getEventKey() and compare it to the keyCodes of your KeyBindings, and also check Keyboard.getEventState() which is true when the key is pressed.

Posted

Ok this is really weird. When I press the key, even with your code:

if(Keyboard.getEventKey() == HTAKeys.attack1.getKeyCode() && Keyboard.getEventKeyState()){
		if(Minecraft.getMinecraft().inGameHasFocus){
			HTANetwork.net.sendToServer(new AttackKeyPackage.KeyMessage(1));
		}
	}

 

It will constantly put out "key pressed" in the console, even when I quit the world it keeps going. What is going on?

Creator of the MyFit, MagiCraft, Tesseract gun, and Papa's Wingeria mod.

Posted

Oh wow all it was was that there was a for loop that was making it run multiple times. My bad. Thank you for helping, though.

Creator of the MyFit, MagiCraft, Tesseract gun, and Papa's Wingeria mod.

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.