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.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • They were already updated, and just to double check I even did a cleanup and fresh update from that same page. I'm quite sure drivers are not the problem here. 
    • i tried downloading the drivers but it says no AMD graphics hardware has been detected    
    • Update your AMD/ATI drivers - get the drivers from their website - do not update via system  
    • As the title says i keep on crashing on forge 1.20.1 even without any mods downloaded, i have the latest drivers (nvidia) and vanilla minecraft works perfectly fine for me logs: https://pastebin.com/5UR01yG9
    • Hello everyone, I'm making this post to seek help for my modded block, It's a special block called FrozenBlock supposed to take the place of an old block, then after a set amount of ticks, it's supposed to revert its Block State, Entity, data... to the old block like this :  The problem I have is that the system breaks when handling multi blocks (I tried some fix but none of them worked) :  The bug I have identified is that the function "setOldBlockFields" in the item's "setFrozenBlock" function gets called once for the 1st block of multiblock getting frozen (as it should), but gets called a second time BEFORE creating the first FrozenBlock with the data of the 1st block, hence giving the same data to the two FrozenBlock :   Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=head] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@73681674 BlockEntityData : id:"minecraft:bed",x:3,y:-60,z:-6} Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=3, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=2, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} here is the code inside my custom "freeze" item :    @Override     public @NotNull InteractionResult useOn(@NotNull UseOnContext pContext) {         if (!pContext.getLevel().isClientSide() && pContext.getHand() == InteractionHand.MAIN_HAND) {             BlockPos blockPos = pContext.getClickedPos();             BlockPos secondBlockPos = getMultiblockPos(blockPos, pContext.getLevel().getBlockState(blockPos));             if (secondBlockPos != null) {                 createFrozenBlock(pContext, secondBlockPos);             }             createFrozenBlock(pContext, blockPos);             return InteractionResult.SUCCESS;         }         return super.useOn(pContext);     }     public static void createFrozenBlock(UseOnContext pContext, BlockPos blockPos) {         BlockState oldState = pContext.getLevel().getBlockState(blockPos);         BlockEntity oldBlockEntity = oldState.hasBlockEntity() ? pContext.getLevel().getBlockEntity(blockPos) : null;         CompoundTag oldBlockEntityData = oldState.hasBlockEntity() ? oldBlockEntity.serializeNBT() : null;         if (oldBlockEntity != null) {             pContext.getLevel().removeBlockEntity(blockPos);         }         BlockState FrozenBlock = setFrozenBlock(oldState, oldBlockEntity, oldBlockEntityData);         pContext.getLevel().setBlockAndUpdate(blockPos, FrozenBlock);     }     public static BlockState setFrozenBlock(BlockState blockState, @Nullable BlockEntity blockEntity, @Nullable CompoundTag blockEntityData) {         BlockState FrozenBlock = BlockRegister.FROZEN_BLOCK.get().defaultBlockState();         ((FrozenBlock) FrozenBlock.getBlock()).setOldBlockFields(blockState, blockEntity, blockEntityData);         return FrozenBlock;     }  
  • Topics

×
×
  • Create New...

Important Information

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