Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

So I am developing a mod which requires an item with cooldown, I need to have the item be damaged by 99 when used (right clicked) but the onItemRightCick entityplayer variable I think is not letting the item be damaged.

 

Code:

 

 

package jcm2606.sorcerycraft.common.items;

import jcm2606.sorcerycraft.common.core.reference.SCItemShine;
import net.minecraft.src.Entity;
import net.minecraft.src.EntityLiving;
import net.minecraft.src.EntityPlayer;
import net.minecraft.src.ItemStack;
import net.minecraft.src.World;

public class ItemWandFire extends SCItemShine{

public ItemWandFire(int par1) {
	super(par1);
	this.setMaxDamage(100);
}

/**
     * Called each tick as long the item is on a player inventory. Uses by maps to check if is on a player hand and
     * update it's contents.
     */
    public void onUpdate(ItemStack par1ItemStack, World par2World, Entity par3Entity, int par4, boolean par5) {
    	if(par1ItemStack.getItemDamage() > 0)
    	{
    		//par1ItemStack.damageItem(-1, (EntityLiving) par3Entity); Again commented this line out for testing
    	}
    }
    
    /**
     * Callback for item usage. If the item does something special on right clicking, he will have one of those. Return
     * True if something happen and false if it don't. This is for ITEMS, not BLOCKS
     */
    @Override
public boolean onItemUse(ItemStack itemStack, EntityPlayer entityPlayer, World world, int x, int y, int z, int l, float f1, float f2, float f3) {
	itemStack.damageItem(1, entityPlayer);
	System.out.println(this.isDamageable());  //Testing
	return true;
    }
}

 

 

  • Author

What about:

System.out.println(itemStack.getItemDamage());

Tried it, kept returning 0, thus the item ain't being damaged.

Hey,

 

I had the same problem as you, try to change your gamemode to survival maybe?

 

Panderoli ;D

Sorry for my bad english...

  • Author

Wait... I thought item damage being issued straight up wasn't affected by gamemode... and your suggestion made me laugh xD

I think you're possibly looking for this -

    public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer) {
        if (!par3EntityPlayer.capabilities.isCreativeMode) {
                par1ItemStack.damageItem(99, par3EntityPlayer);
        }

On every right click it'll damage the item by '99'.

 

Another example, if you want it to shoot a snowball -

    public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer) {
        if (!par3EntityPlayer.capabilities.isCreativeMode) {
                par1ItemStack.damageItem(99, par3EntityPlayer);
        }

        par2World.playSoundAtEntity(par3EntityPlayer, "random.bow", 0.5F, 0.4F / (itemRand.nextFloat() * 0.4F + 0.8F));

        if (!par2World.isRemote) {
        	par2World.spawnEntityInWorld(new EntitySnowball(par2World, par3EntityPlayer));
        }

        return par1ItemStack;
    }

 

Just a note, I've got the code for making it not 'work' if the damage isn't 0, if you want it ask, unless you want the fun of working it out yourself ;)

Might the problem be in onItemUse? 

    @Override
public boolean onItemUse(ItemStack itemStack, EntityPlayer entityPlayer, World world, int x, int y, int z, int l, float f1, float f2, float f3) {
	itemStack.damageItem(1, entityPlayer);
	System.out.println(this.isDamageable());  //Testing
	[b]return true;[/b]
    }

 

I've noticed that when that method returns true clientside, it is never called serverside.  Try adding an if (world.isRemote){return false;} statement near the beginning of the method body.

  • Author

Zedicus: Since I don't have that it should still be working in creative mode, but I have scrapped and restarted the mod so I'll check soon

 

Quantum: That method is called twice so it must be getting called on the server aswell...

If you're talking about -

if (!par3EntityPlayer.capabilities.isCreativeMode) {

And I understand you correctly, that just means if you're not in creative mode then run this, basically how it doesn't damage the item when you're in creative mode. (Apologies if I misunderstood.)

  • Author

I have fixed it, it turns out the damageItem method has a built-in gamemode check to see whether the player s in creative or survival, I did not know this so I will now be locking the topic.

Guest
This topic is now closed to further replies.

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.