Jump to content

Recommended Posts

Posted (edited)

I am attempting to repair the sword that the player is holding on a client event.

Here is my code:

PlayerEntity player = Minecraft.getInstance().player;
ItemStack heldItem = player.getHeldItemMainhand();

if(heldItem.getItem() == Items.DIAMOND_SWORD){
    LOGGER.info(heldItem.getDamage());

    //repair item
    heldItem.damageItem(-10, player,PlayerEntity::livingTick);

    LOGGER.info(heldItem.getDamage());
}

 

This outputs:

14

14

into the console, showing that the damage value is not being changed. This doesn't work for positive damage values either. 

 

I assume that I am just using the wrong thing for the 3rd parameter (of damageItem), which wants a Consumer<PlayerEntity> type. I am not sure what  a Consumer<PlayerEntity> is or how to use it. As such, I am not sure if this is a problem of my Java ability, or my understanding of the Forge API.

 

Thanks for any help!

 

Edited by NathaFred
Posted

Okay, I assume there is a way to transfer the task to the server. Or in other words modify the inventory from the server, when a client event runs. Or would I have to use a server event?

Posted
1 hour ago, NathaFred said:

Or would I have to use a server event?

Use a server event. Do not trust the client. The client is a lying cheating bastard.

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 (edited)

So it is! XD So after a number of iterations I finally fixed part of my problem. I found that any Event not located under the .client namespace is a server event. So I just had to make sure event.getWorld().isRemote was false. The problem I have now is that item.damageItem() does not work whereas item.setDamage does. I am currently using setDamage and just adding the value that way, which is arguably simpler, but I would like to know why damageItem was failing. My updated code is here running on a server event:

PlayerEntity player = event.getPlayer();
if(player.getHeldItemMainhand().isDamageable()) {

    Consumer<PlayerEntity> pl = (p) -> LOGGER.info(p);

    LOGGER.info(player.getHeldItemMainhand().getDamage());

    //this line of code works
    //player.getHeldItemMainhand().setDamage(player.getHeldItemMainhand().getDamage() - 8);
    
    //this one does not
    player.getHeldItemMainhand().damageItem(-8,player,pl);

    LOGGER.info(player.getHeldItemMainhand().getDamage());
}
Edited by NathaFred
Posted (edited)

1. Yeah I figured that was wrong. I should get into the habit of searching the vanilla code for usage examples.

2. I am repairing the item, so that was intended.

 

It still does nothing even with the same implementation as vanilla...

if (heldItem.isDamageable()) {

    heldItem.damageItem(8, player,
            (p_220017_1_) -> {p_220017_1_.sendBreakAnimation(Hand.MAIN_HAND);}
            );

    LOGGER.info(heldItem.getDamage());
}

 

I kept the same Consumer name as vanilla just in case but that doesn't change anything. I am not sure what is wrong, as it was working for a few minutes seemingly. However, I now cannot get it to do anything... 

Edited by NathaFred
Everything I said in this message before was wrong...

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.