Jump to content

[1.7.2]Damaging one item when using/used another?


Numbers32

Recommended Posts

Hello,

 

I'm trying to create a new Bow that instead of using arrows, it consumes energy from a crystal orb (item from the mod). I've hit a roadblock trying to figure out how to "damage" the orb every time the bow shoots. I've searched practically everywhere, but I cannot find a good tutorial on how one might go about that. I'm wondering if anyone else out there has encountered this or if they could offer any advice.

 

Edit: I'm relatively new with modding, but have some programming experience, mostly C#.

 

Here is the Bow.class:

 

package aceofkings.rangermod;

import java.util.List;

import aceofkings.rangermod.RangerMain;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.projectile.EntityArrow;
import net.minecraft.init.Items;
import net.minecraft.item.EnumAction;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.IIcon;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.ArrowLooseEvent;
import net.minecraftforge.event.entity.player.ArrowNockEvent;

public class RangerBowMagic extends Item {

//Custom charge speed
public float charge;

//custom animation adjustments
public int anim_0;
public int anim_1;
public int anim_2;

//		public int b_type;

    public RangerBowMagic(float chargeTime, int anim_0, int anim_1, int anim_2)
    {
    	//You can't stack more than one bow.
        this.maxStackSize = 1;
        //Creative tab
        setCreativeTab(RangerMain.tabRangerMod);
        
        //allows custom chargeTime
        this.charge = chargeTime;
        
        //allows custom adjustment of animation
        this.anim_0 = anim_0;
        this.anim_1 = anim_1;
        this.anim_2 = anim_2;
        
		setMaxDamage(637);

     
    }
    
    public String[] bowPullIconNameArray;// = new String[] {this.t_0, this.t_1, this.t_2};
    @SideOnly(Side.CLIENT)
    private IIcon[] iconArray;
    
    //Fixes "FileNotFound...null.png"
    //allows custom texture for different bows
    public void getBowPullIconNameArray(){
    	
    	bowPullIconNameArray = new String[] {"bowCrystal_0", "bowCrystal_1", "bowCrystal_2" };
    }
// Not sure why i still have this... 
//	    public void onCreated(ItemStack par1ItemStack, World world, EntityPlayer entity){
//	    	
//	    	//Adds infinity to Crystal bow. Compensates for the Nether Star
//	    	if(b_type == 4){
//	    		par1ItemStack.addEnchantment(Enchantment.infinity, 1);
//	    	}
//	    }
    
    //
    public boolean hasEffect(ItemStack par1ItemStack){
    	return true;
    }
    
    public void addInformation(ItemStack par1ItemStack, EntityPlayer entityplayer, List list, boolean is){
    	
    }

    /**
     * called when the player releases the use item button. Args: itemstack, world, entityplayer, itemInUseCount
     */
    public void onPlayerStoppedUsing(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer, int par4)
    {
        int j = this.getMaxItemUseDuration(par1ItemStack) - par4;

        ArrowLooseEvent event = new ArrowLooseEvent(par3EntityPlayer, par1ItemStack, j);
        MinecraftForge.EVENT_BUS.post(event);
        if (event.isCanceled())
        {
            return;
        }
        j = event.charge;
        
        boolean flag = par3EntityPlayer.capabilities.isCreativeMode || EnchantmentHelper.getEnchantmentLevel(Enchantment.infinity.effectId, par1ItemStack) > 0;
        
        if (flag || par3EntityPlayer.inventory.hasItem(RangerMain.crystal_orb))
        {

        	//Change 20.0F to increase/decrease charge speed.
            float f = (float)j / charge;
            f = (f * f + f * 2.0F) / 3.0F;

            if ((double)f < 0.1D)
            {
                return;
            }

            if (f > 1.0F)
            {
                f = 1.0F;
            }

            EntityArrow entityarrow = new EntityArrow(par2World, par3EntityPlayer, f * 2.0F);

            if (f == 1.0F)
            {
                entityarrow.setIsCritical(true);
            }
            
            int k = EnchantmentHelper.getEnchantmentLevel(Enchantment.power.effectId, par1ItemStack);

            if (k > 0)
            {
            	entityarrow.setDamage(entityarrow.getDamage() + (double)k * 0.5D + 0.5D);
            }

            int l = EnchantmentHelper.getEnchantmentLevel(Enchantment.punch.effectId, par1ItemStack);

            if (l > 0)
            {
                entityarrow.setKnockbackStrength(l);
            }

            if (EnchantmentHelper.getEnchantmentLevel(Enchantment.flame.effectId, par1ItemStack) > 0)
            {
                entityarrow.setFire(100);
            }
            
            par1ItemStack.damageItem(1, par3EntityPlayer);
            par2World.playSoundAtEntity(par3EntityPlayer, "random.bow", 1.0F, 1.0F / (itemRand.nextFloat() * 0.4F + 1.2F) + f * 0.5F);

            if (flag)
            {
                entityarrow.canBePickedUp = 2;
            }
            else
            {
            	ItemStack itemstack = new ItemStack(RangerMain.crystal_orb);
            	int d = itemstack.getItemDamage();
            	
            	if(d < itemstack.getMaxDamage()){
            		itemstack.setItemDamage(d + 1);
            		
            	}
            	itemstack.getItemDamageForDisplay();
            	entityarrow.canBePickedUp = 2;         	
            }

            if (!par2World.isRemote)
            {
                par2World.spawnEntityInWorld(entityarrow);
            }
        	
        }
    }

    public ItemStack onEaten(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
    {
        return par1ItemStack;
    }

    /**
     * How long it takes to use or consume an item
     */
    public int getMaxItemUseDuration(ItemStack par1ItemStack)
    {
        return 72000;
    }

    /**
     * returns the action that specifies what animation to play when the items is being used
     */
    public EnumAction getItemUseAction(ItemStack par1ItemStack)
    {
        return EnumAction.bow;
    }

    /**
     * Called whenever this item is equipped and the right mouse button is pressed. Args: itemStack, world, entityPlayer
     */
    public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
    {
    	//debugging trying to figure out the getting and setting damage of the orb.
    	ItemStack itemstack = new ItemStack(RangerMain.crystal_orb);
    	int d = itemstack.getItemDamage();
    	
    	System.out.println(d);
    	
    	itemstack.setItemDamage(d + 1);
    	int dd = itemstack.getItemDamage();
    	
    	System.out.println(dd);
    	
    	
    	
    	
    	
    	
    	
    	
    	
        ArrowNockEvent event = new ArrowNockEvent(par3EntityPlayer, par1ItemStack);
        MinecraftForge.EVENT_BUS.post(event);
        if (event.isCanceled())
        {
            return event.result;
        }

        if (par3EntityPlayer.capabilities.isCreativeMode || par3EntityPlayer.inventory.hasItem(RangerMain.crystal_orb))
        {
            par3EntityPlayer.setItemInUse(par1ItemStack, this.getMaxItemUseDuration(par1ItemStack));
        }

        return par1ItemStack;
    }

    /**
     * Return the enchantability factor of the item, most of the time is based on material.
     */
    public int getItemEnchantability()
    {
    	return 1;
    }

    //Modified to allow custom texture use. maybe.
    @SideOnly(Side.CLIENT)
    public void registerIcons(IIconRegister par1IconRegister)
    {
    	//might change to (Ranger.MODID + ":" + this.t_s)
    	getBowPullIconNameArray(); //this solved the null texture!
        this.itemIcon = par1IconRegister.registerIcon(RangerMain.MODID + ":" + getUnlocalizedName().substring(5)); //.substring(5) removes "item."
        this.iconArray = new IIcon[bowPullIconNameArray.length];

        for (int i = 0; i < this.iconArray.length; ++i)
        {
            this.iconArray[i] = par1IconRegister.registerIcon(RangerMain.MODID + ":" + bowPullIconNameArray[i]);
        }
    }
    
    
    //Renders it in 3D, works, but not exactly like the regular bow. More like a sword.
    @SideOnly(Side.CLIENT)
    public boolean isFull3D()
    {
        return true;
    }

    /**
     * used to cycle through icons based on their used duration, i.e. for the bow
     */
    //Modified
    @SideOnly(Side.CLIENT)
    public IIcon getIcon(ItemStack stack, int renderPass, EntityPlayer player, ItemStack usingItem, int useRemaining)
{
    	//Bottom to top
	if(player.getItemInUse() == null) return this.itemIcon;
	int Pulling = stack.getMaxItemUseDuration() - useRemaining;
	if (Pulling >= this.anim_2)
	{
		return iconArray[2];
	}
	else if (Pulling > this.anim_1)
	{
		return iconArray[1];
	}
	else if (Pulling > this.anim_0)
	{
		return iconArray[0];
	}              
	return itemIcon;
}
}

 

 

And the Crystal Orb.class:

 

package aceofkings.rangermod;

import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;

public class RangerItemMagic extends Item {

public int m_type;
/**
 * @param maxStackSize is the max number items in a stack, ie 64
 * @param tab is the Creative tab ie, CreativeTabs.tabMisc
 * @param texture is the String for the item texture, ie "rangermod:item"
 * @param name is the String for the Unlocalized name, ie "itemName"
 * @param type is what kind of magic item, ie 0 for orb
 */
public RangerItemMagic(CreativeTabs tab, String texture, String name, int type){

	super();
	this.setMaxStackSize(1);
	this.setCreativeTab(tab);
	this.setUnlocalizedName(name);
	this.setTextureName(texture);
	this.setMaxDamage(320);
	this.isDamageable();
	}

}

 

 

Any help would be greatly appreciated! I've been stuck on this for a few days now.

Link to comment
Share on other sites

Put this in your onPlayerStoppedUsing method to damage the itemstack.

 

ItemStack itemstack;
for (int index = 0; index < player.inventory.getSizeInventory() && (itemstack != null ? itemstack.getItem() == YourModClass.youritem) ; index++) { //You could use 35 instead of the size here but i like it that way.
    itemstack = player.inventory.getStackInSlot(index);
}
if (itemstack != null && itemstack.getItem() == YourModClass.youritem) {
    if (itemStack.attemptDamageItem(1, rand)) {
        itemStack.stackSize--;
        if (itemStack.stackSize < 0)                  //This will consume your item once it breaks.
            itemStack.stackSize = 0;
        itemStack.setItemDamage(0);
        itemStack = null;
    }
}

 

EDIT: You might need to return the Item to the players Inverntory

PM's regarding modding questions should belong in the Modder Support sub-forum and won't be answered.

Link to comment
Share on other sites

Thanks! I'll try it out and see how it works.

 

Edit:

Okay, after modifying you solution to match my class

 

public void onPlayerStoppedUsing(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer, int par4)
    {
        int j = this.getMaxItemUseDuration(par1ItemStack) - par4;

        ArrowLooseEvent event = new ArrowLooseEvent(par3EntityPlayer, par1ItemStack, j);
        MinecraftForge.EVENT_BUS.post(event);
        if (event.isCanceled())
        {
            return;
        }
        j = event.charge;
        
        boolean flag = par3EntityPlayer.capabilities.isCreativeMode || EnchantmentHelper.getEnchantmentLevel(Enchantment.infinity.effectId, par1ItemStack) > 0;
        
        if (flag || par3EntityPlayer.inventory.hasItem(RangerMain.crystal_orb))
        {

        	//Change 20.0F to increase/decrease charge speed.
            float f = (float)j / charge;
            f = (f * f + f * 2.0F) / 3.0F;

            if ((double)f < 0.1D)
            {
                return;
            }

            if (f > 1.0F)
            {
                f = 1.0F;
            }

            EntityArrow entityarrow = new EntityArrow(par2World, par3EntityPlayer, f * 2.0F);

            if (f == 1.0F)
            {
                entityarrow.setIsCritical(true);
            }
            
            int k = EnchantmentHelper.getEnchantmentLevel(Enchantment.power.effectId, par1ItemStack);

            if (k > 0)
            {
            	entityarrow.setDamage(entityarrow.getDamage() + (double)k * 0.5D + 0.5D);
            }

            int l = EnchantmentHelper.getEnchantmentLevel(Enchantment.punch.effectId, par1ItemStack);

            if (l > 0)
            {
                entityarrow.setKnockbackStrength(l);
            }

            if (EnchantmentHelper.getEnchantmentLevel(Enchantment.flame.effectId, par1ItemStack) > 0)
            {
                entityarrow.setFire(100);
            }
            
            par1ItemStack.damageItem(1, par3EntityPlayer);
            par2World.playSoundAtEntity(par3EntityPlayer, "random.bow", 1.0F, 1.0F / (itemRand.nextFloat() * 0.4F + 1.2F) + f * 0.5F);

            if (flag)
            {
                entityarrow.canBePickedUp = 2;
            }
            else
            {
            	//New code
            	ItemStack itemstack;
            	for (int index = 0; index < par3EntityPlayer.inventory.getSizeInventory() && (itemstack != null ? itemstack.getItem() == RangerMain.crystal_orb : 35); index++) { //You could use 35 instead of the size here but i like it that way.
            	    itemstack = par3EntityPlayer.inventory.getStackInSlot(index);
            	}
            	
            	if (itemstack != null && itemstack.getItem() == RangerMain.crystal_orb) {
            	    if (itemstack.attemptDamageItem(1, itemRand)) {
            	        itemstack.stackSize--;
            	        if (itemstack.stackSize < 0)                  //This will consume your item once it breaks.
            	            itemstack.stackSize = 0;
            	        itemstack.setItemDamage(0);
            	        itemstack = null;
            	    }
            	} 
            	
            }

            if (!par2World.isRemote)
            {
                par2World.spawnEntityInWorld(entityarrow);
            }
        	
        }
    }

 

 

 

I get the following error:

 

"The operator && is undefined for the argument type(s) boolean, Object&Serializable&Comparable<?>"

 

This is what its underlining:

index < par3EntityPlayer.inventory.getSizeInventory() && (itemstack != null ? itemstack.getItem() == RangerMain.crystal_orb : 35);

 

I'm not 100% sure why either...

 

Link to comment
Share on other sites

Thanks again, that seemed to fix all of the errors... But one.

 

After I fixed the code so it would stop "yelling" at me, it was complaining that the local variable itemstack might not have been initialized. I'm assuming putting "new ItemStack(mainmod.myitem)" or even "null" as Eclipse suggessts would break it.

 

Once again thank you for your assistance!

 

EDIT: "ItemStack itemstack = null;" didnt break it. I put a print line on the damage of the item, and it goes up each time i use it (yay!). The only obstacle to pass is displaying the damage on the hot-bar, similar to a pickaxe or bow. You mentioned something earlier about returning it to inventory?

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



×
×
  • Create New...

Important Information

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