Jump to content

[1.7.10] How would I damage the player's held item from a keybind?


Recommended Posts

Posted

If the player is holding a magic rod when a key is pressed it should cast a fireball and damage the rod.

 

But I'm not sure how to go about doing that.  I'm assuming the code I have won't work (I haven't made the projectile or packets yet so I haven't tested it) since it's getting the equipped item of the client player? If not, how do I get the regular EntityPlayer from that in a packet? Would simply passing the client player through the packet parameters and casting it to EntityPlayer be enough?

 

	@SubscribeEvent
public void onKeyPress(KeyInputEvent event)
{
	if (Keybinds.fire.isPressed())
	{
		ItemStack stack = Minecraft.getMinecraft().thePlayer.getCurrentEquippedItem();
		if (stack.getItem() instanceof Rod)
		{
			if (stack.getItemDamage() < stack.getMaxDamage() - 1)
			{
				// TODO packet
				stack.damageItem(1, Minecraft.getMinecraft().thePlayer);
			}
		}
	}
}

Posted

Just send a packet to the server saying "the player pressed the fire key", you don't actually need to send any data in it. On the server side, you'll have access to the player that sent the packet so you can check if they're holding a rod and then spawn the fireball and damage the rod (I'd recommend creating a method to do this in your

Rod

class).

 

Forge's Read the Docs site has a section on networking here. This includes the Simple Network Implementation, which is what you should use to send the packet.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

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.