Jump to content

[1.7.10] Key Troubles


SebasTheGreat

Recommended Posts

Hey so I'm trying to make it so when I press a key and the player is wearing a certain type of armor, it opens a GUI. I have the key set up and when you press the key the GUI opens but when I add the if statement:

if(Minecraft.getMinecraft().thePlayer.getEquipmentInSlot(1) == new ItemStack("Armor I want"))
				{
					Minecraft.getMinecraft().thePlayer.openGui...
				}

into the if statement that tests to see if the key is pressed, when I press the key in the game the GUI never opens, even if I have the armor specified on.

 

So to re-put that so people can understand it, I press key - GUI opens, I add statement to see if specific armor is on, and press key - GUI doesn't open at all.

 

I think I'm understanding the getEquipmentInSlot Method wrong so any help would be appreciated.

Link to comment
Share on other sites

So I've changed my code to what you guys said:

if(Minecraft.getMinecraft().thePlayer.getEquipmentInSlot(1) != null)
			{
				if(ItemStack.areItemStacksEqual(Minecraft.getMinecraft().thePlayer.getEquipmentInSlot(1), new ItemStack("Armor I want")))
					{
						Minecraft.getMinecraft().thePlayer.openGui...
					}

			}

but it still doesn't work

 

You aren't creating a new anything.

The armor piece I am checking is only an Item and I need an ItemStack to compare them so I make a new ItemStack.

Link to comment
Share on other sites

1. Can I use FMLClientHandler.instance().getClientPlayerEntity() instead.

2. I can't grasp what you're talking about with the ItemStack.getItem(), everytime I try to use it I get the error:

Cannot make a static reference to the non-static method getItem() from the type ItemStack

*facepalm* He meant to call whatever the itemstack was. For example, if the itemstack was named cheese, it would be, cheese.getItem();

 

I'm back from being gone for... I think its been about a year. I'm pretty sure nobody remembers me, but hello anybody who does!

Link to comment
Share on other sites

1. Can I use FMLClientHandler.instance().getClientPlayerEntity() instead.

2. I can't grasp what you're talking about with the ItemStack.getItem(), everytime I try to use it I get the error:

Cannot make a static reference to the non-static method getItem() from the type ItemStack

 

1. Read what I just said

2. Basic java. Learn it.

Link to comment
Share on other sites

Try this:

 

if(Minecraft.getMinecraft().thePlayer.getCurrentArmor(0) != null && Minecraft.getMinecraft().thePlayer.getCurrentArmor(0).getItem().equals(YourMod.yourArmorBoots))
if(Minecraft.getMinecraft().thePlayer.getCurrentArmor(1) != null && Minecraft.getMinecraft().thePlayer.getCurrentArmor(1).getItem().equals(YourMod.yourArmorLeggings))
if(Minecraft.getMinecraft().thePlayer.getCurrentArmor(2) != null && Minecraft.getMinecraft().thePlayer.getCurrentArmor(2).getItem().equals(YourMod.yourArmorChestplate))
if(Minecraft.getMinecraft().thePlayer.getCurrentArmor(3) != null && Minecraft.getMinecraft().thePlayer.getCurrentArmor(3).getItem().equals(YourMod.yourArmorHelmet))
{
	Minecraft.getMinecraft().thePlayer.openGui...
}

 

Quick explanation: first of all, we want to check the player's ARMOR, so we use getCurrentArmor.

second of all, we want to make sure the correct armor slots are non-null, AKA empty, or we will get a NullPointerException.

third of all, if you want to check someone's armor, you want to make sure they are wearing the full set right? well that's what i'm doing here. If that's not what you want, simply remove the lines about the specific armor pieces.

finnaly, I am using Object.equals(Object) instead of Object == Object, because .equals is safer, and is better practice.

 

also, one more thing to remember, tracking key presses is safe, but you need to be careful with Minecraft.getMinecraft(), because if this is running on the internal/external server instead of the client, that will cause a crash.

If I ever say something stupid, or simply incorrect, please excuse me. I don't know anything about 1.8 modding, and I don't know much about entities either, But I try to help when I can.

Link to comment
Share on other sites

Try this:

 

if(Minecraft.getMinecraft().thePlayer.getCurrentArmor(0) != null && Minecraft.getMinecraft().thePlayer.getCurrentArmor(0).getItem().equals(YourMod.yourArmorBoots))
if(Minecraft.getMinecraft().thePlayer.getCurrentArmor(1) != null && Minecraft.getMinecraft().thePlayer.getCurrentArmor(1).getItem().equals(YourMod.yourArmorLeggings))
if(Minecraft.getMinecraft().thePlayer.getCurrentArmor(2) != null && Minecraft.getMinecraft().thePlayer.getCurrentArmor(2).getItem().equals(YourMod.yourArmorChestplate))
if(Minecraft.getMinecraft().thePlayer.getCurrentArmor(3) != null && Minecraft.getMinecraft().thePlayer.getCurrentArmor(3).getItem().equals(YourMod.yourArmorHelmet))
{
	Minecraft.getMinecraft().thePlayer.openGui...
}

 

Quick explanation: first of all, we want to check the player's ARMOR, so we use getCurrentArmor.

second of all, we want to make sure the correct armor slots are non-null, AKA empty, or we will get a NullPointerException.

third of all, if you want to check someone's armor, you want to make sure they are wearing the full set right? well that's what i'm doing here. If that's not what you want, simply remove the lines about the specific armor pieces.

finnaly, I am using Object.equals(Object) instead of Object == Object, because .equals is safer, and is better practice.

 

also, one more thing to remember, tracking key presses is safe, but you need to be careful with Minecraft.getMinecraft(), because if this is running on the internal/external server instead of the client, that will cause a crash.

Don't compare Items and Blocks using

.equals()

! You can safely use

==

, because item and block classes are singletons.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Link to comment
Share on other sites

Ok so in order for it to work on a server, I need to use packets. I assume I need a packet where the server sends a message to the client telling it to open the GUI, because GUI's are client side only.

 

I've made the packet but I have the issue of

"ModName".network.sendTo(new "Packet", "EntityPlayerMP");

What do I put in for the "EntityPlayerMP"?

This is in the onKeyInput method which doesn't simply have a parameter for EntityPlayerMP. Is there a method that gets which player is pressing the key?

Sorry for my obvious stupidity on this subject, I'm kinda new to it.

Link to comment
Share on other sites

Yeah and this:

 

// Sending packets:

MyMod.network.sendToServer(new MyMessage("foobar"));

MyMod.network.sendTo(new SomeMessage(), somePlayer);

 

is what it says on that page. That's what I'm doing in that code in my previous post.

I need to use the sendTo method to send the packet to the client telling him to open the GUI if the key is pressed and have the packet check to see if the correct armor is being worn.

I'm sorry but linking me to a page I've seen a hundred times ins't going to help me here, I'm not asking for any copy and paste but I would like an explanation of what I'm supposed to do with this situation.

Link to comment
Share on other sites

Yeah so I have this in my onKeyInput:

"ModName".network.sendToServer(new "Packet");

and this in my onMessage method in my packet:

if(ctx.getServerHandler().playerEntity.getCurrentArmor(0) != null 
				&& ctx.getServerHandler().playerEntity.getCurrentArmor(0).getItem().equals("armorBoots"))
			if(ctx.getServerHandler().playerEntity.getCurrentArmor(1) != null 
			&& ctx.getServerHandler().playerEntity.getCurrentArmor(1).getItem().equals("armorLeggings"))
			if(ctx.getServerHandler().playerEntity.getCurrentArmor(2) != null 
			&& ctx.getServerHandler().playerEntity.getCurrentArmor(2).getItem().equals("armorChestplate"))
			if(ctx.getServerHandler().playerEntity.getCurrentArmor(3) != null 
			&& ctx.getServerHandler().playerEntity.getCurrentArmor(3).getItem().equals("armorHelmet"))
			{
				ctx.getServerHandler().playerEntity.openGui...
			}
        	return null;

but when I press the key in game nothing happens.

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.



×
×
  • Create New...

Important Information

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