Jump to content

[SOLVED]How to set damage on bow hit (left click)


CAJlO

Recommended Posts

I try to make a bow, which could set damage like swords are, here is my code, why it is not working?

 

package NOWBows.items;

import NOWBows.Bows;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.ai.attributes.AttributeModifier;
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.ItemBow;
import net.minecraft.item.ItemStack;
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;

import org.lwjgl.opengl.GL11;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;

public class ItemBattleBow extends ItemBow {

   public static final String[] bowPullIconNameArray = new String[]{"pulling_0", "pulling_1", "pulling_2"};
   @SideOnly(Side.CLIENT)
   private IIcon[] iconArray;
   private static final String __OBFID = "CL_00001777";
   public static IIcon BattleBow;
   public static IIcon BattleBow1;
   public static IIcon BattleBow2;
   public static IIcon BattleBow3;

   private float damageVsEntity;
   private final Item.ToolMaterial field_150933_b;
   
   public ItemBattleBow(Item.ToolMaterial MATERIAL) {
      this.field_150933_b = MATERIAL;
  this.maxStackSize = 1;
  this.setMaxDamage(MATERIAL.getMaxUses());//this.setMaxDamage(484);
  this.setCreativeTab(Bows.tabNOWBows);
      this.bFull3D = true;
      this.damageVsEntity = 20.0F + MATERIAL.getDamageVsEntity();/*trying to set damage*/
   }
   /**
    * Gets a map of item attribute modifiers, used by ItemSword to increase hit damage.
    */
   public Multimap getItemAttributeModifiers()
   {
       Multimap multimap = HashMultimap.create(); // this part, you need to create a new hash-multimap!
       multimap.put(SharedMonsterAttributes.attackDamage.getAttributeUnlocalizedName(), new AttributeModifier(field_111210_e, "Weapon modifier", (double)this.damageVsEntity, 0));
       return multimap;
   }

   @Override
   public void onPlayerStoppedUsing(ItemStack p_77615_1_, World p_77615_2_, EntityPlayer p_77615_3_, int p_77615_4_) {
      int j = this.getMaxItemUseDuration(p_77615_1_) - p_77615_4_;
      ArrowLooseEvent event = new ArrowLooseEvent(p_77615_3_, p_77615_1_, j);
      MinecraftForge.EVENT_BUS.post(event);
      if(!event.isCanceled()) {
         j = event.charge;
         boolean flag = p_77615_3_.capabilities.isCreativeMode || EnchantmentHelper.getEnchantmentLevel(Enchantment.infinity.effectId, p_77615_1_) > 0;
         if(flag || p_77615_3_.inventory.hasItem(Items.arrow)) {
            float f = (float)j / 17.0F;
            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(p_77615_2_, p_77615_3_, f * 2.0F);
            if(f == 1.0F) {
               entityarrow.setIsCritical(true);
            }

            int k = EnchantmentHelper.getEnchantmentLevel(Enchantment.power.effectId, p_77615_1_);
            if(k > 0) {
               entityarrow.setDamage(entityarrow.getDamage() + (double)k * 0.5D + 0.5D);
            }

            int l = EnchantmentHelper.getEnchantmentLevel(Enchantment.punch.effectId, p_77615_1_);
            if(l > 0) {
               entityarrow.setKnockbackStrength(l);
            }

            if(EnchantmentHelper.getEnchantmentLevel(Enchantment.flame.effectId, p_77615_1_) > 0) {
               entityarrow.setFire(100);
            }

            p_77615_1_.damageItem(1, p_77615_3_);
            p_77615_2_.playSoundAtEntity(p_77615_3_, "random.bow", 1.0F, 1.0F / (itemRand.nextFloat() * 0.4F + 1.2F) + f * 0.5F);
            if(flag) {
               entityarrow.canBePickedUp = 2;
            } else {
               p_77615_3_.inventory.consumeInventoryItem(Items.arrow);
            }

            if(!p_77615_2_.isRemote) {
               p_77615_2_.spawnEntityInWorld(entityarrow);
               entityarrow.setDamage(entityarrow.getDamage() * 1.15D);
            }
         }

      }
   }
   @Override
   public ItemStack onEaten(ItemStack p_77654_1_, World p_77654_2_, EntityPlayer p_77654_3_) {
      return p_77654_1_;
   }
   @Override
   public int getMaxItemUseDuration(ItemStack p_77626_1_) {
      return 72000;
   }
   @Override
   public EnumAction getItemUseAction(ItemStack p_77661_1_) {
      return EnumAction.bow;
   }
   @Override
   public ItemStack onItemRightClick(ItemStack p_77659_1_, World p_77659_2_, EntityPlayer p_77659_3_) {
      ArrowNockEvent event = new ArrowNockEvent(p_77659_3_, p_77659_1_);
      MinecraftForge.EVENT_BUS.post(event);
      if(event.isCanceled()) {
         return event.result;
      } else {
         if(p_77659_3_.capabilities.isCreativeMode || p_77659_3_.inventory.hasItem(Items.arrow)) {
            p_77659_3_.setItemInUse(p_77659_1_, this.getMaxItemUseDuration(p_77659_1_));
         }

         return p_77659_1_;
      }
   }
   @Override
   public int getItemEnchantability() {
      return 1;
   }
   @Override
   @SideOnly(Side.CLIENT)
   public void registerIcons(IIconRegister p_94581_1_) {
      this.itemIcon = p_94581_1_.registerIcon(Bows.MODID + ":BattleBow");
      BattleBow = p_94581_1_.registerIcon(Bows.MODID + ":BattleBow");
      BattleBow1 = p_94581_1_.registerIcon(Bows.MODID + ":BattleBow2");
      BattleBow2 = p_94581_1_.registerIcon(Bows.MODID + ":BattleBow3");
      BattleBow3 = p_94581_1_.registerIcon(Bows.MODID + ":BattleBow4");
      this.iconArray = new IIcon[bowPullIconNameArray.length];

      for(int i = 0; i < this.iconArray.length; ++i) {
         this.iconArray[i] = p_94581_1_.registerIcon(this.getIconString() + "_" + bowPullIconNameArray[i]);
      }

   }
   @Override
   public IIcon getIcon(ItemStack itemStack, int renderPass, EntityPlayer player, ItemStack usingItem, int useRemaining) {
      if(Minecraft.getMinecraft().gameSettings.thirdPersonView != 0) {
         GL11.glTranslatef(0.0F, -0.6F, -0.025F);
         GL11.glRotatef(-17.0F, 0.0F, 0.0F, 1.0F);
         GL11.glRotatef(14.0F, 1.0F, 0.0F, 0.0F);
         GL11.glRotatef(4.5F, 0.0F, 1.0F, 0.0F);
      }

      if(player.getItemInUse() == null) {
         return this.itemIcon;
      } else {
         int var8 = itemStack.getMaxItemUseDuration() - useRemaining;
         return var8 >= 18 && this == Bows.BattleBow?BattleBow3:(var8 > 13 && this == Bows.BattleBow?BattleBow2:(var8 > 0 && this == Bows.BattleBow?BattleBow1:this.itemIcon));
      }
   }

}

 

Link to comment
Share on other sites

    public Multimap getItemAttributeModifiers()
    {
        Multimap multimap = super.getItemAttributeModifiers();
        multimap.put(SharedMonsterAttributes.attackDamage.getAttributeUnlocalizedName(), new AttributeModifier(itemModifierUUID, "Bow modifier", (double)2.0F, 0));
        return multimap;
    }

Link to comment
Share on other sites

@Override

public boolean onEntitySwing(EntityLivingBase entityLiving, ItemStack camael)

{

return false;

}

 

@Override

public boolean hitEntity(ItemStack stack, EntityLivingBase target, EntityLivingBase attacker)

{

   

    target.attackEntityFrom(DamageSource.generic, 1F);

    stack.damageItem(1, attacker);

    return true;

}

 

Link to comment
Share on other sites

    public Multimap getItemAttributeModifiers()
    {
        Multimap multimap = super.getItemAttributeModifiers();
        multimap.put(SharedMonsterAttributes.attackDamage.getAttributeUnlocalizedName(), new AttributeModifier(itemModifierUUID, "Bow modifier", (double)2.0F, 0));
        return multimap;
    }

@Override

public boolean onEntitySwing(EntityLivingBase entityLiving, ItemStack camael)

{

return false;

}

 

@Override

public boolean hitEntity(ItemStack stack, EntityLivingBase target, EntityLivingBase attacker)

{

   

    target.attackEntityFrom(DamageSource.generic, 1F);

    stack.damageItem(1, attacker);

    return true;

}

Please sorry me guys, you both are right with your code, both are working fine.
Link to comment
Share on other sites

thi is a little out of topic

i little extressed whit the thing of the arrow textures,

i can see you are making a bow way more complex than mines

 

can you borrow me the code from an working arrow_entity  and a their render class  to compare them agains the mine

 

this is not the firs time imake this question and i could find whats is wrong

http://www.minecraftforge.net/forum/index.php?topic=29503.0

 

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.