ItemWeapon.java 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package me.km.items;
  2. import com.google.common.collect.Multimap;
  3. import net.minecraft.block.state.IBlockState;
  4. import net.minecraft.creativetab.CreativeTabs;
  5. import net.minecraft.entity.EntityLivingBase;
  6. import net.minecraft.entity.SharedMonsterAttributes;
  7. import net.minecraft.entity.ai.attributes.AttributeModifier;
  8. import net.minecraft.inventory.EntityEquipmentSlot;
  9. import net.minecraft.item.Item;
  10. import net.minecraft.item.ItemStack;
  11. import net.minecraft.util.math.BlockPos;
  12. import net.minecraft.world.World;
  13. import net.minecraftforge.fml.relauncher.Side;
  14. import net.minecraftforge.fml.relauncher.SideOnly;
  15. public class ItemWeapon extends ItemBase
  16. {
  17. private final float attackDamage;
  18. private final double speed;
  19. private final Item.ToolMaterial material;
  20. public ItemWeapon(String name, String local, Item.ToolMaterial material, float attackDamage, double speed)
  21. {
  22. super(name, local);
  23. this.material = material;
  24. this.maxStackSize = 1;
  25. super.setMaxDamage(material.getMaxUses());
  26. super.setCreativeTab(CreativeTabs.COMBAT);
  27. this.attackDamage = attackDamage + material.getDamageVsEntity();
  28. this.speed = speed;
  29. }
  30. public ItemWeapon(String name, String local, Item.ToolMaterial material, float attackDamage)
  31. {
  32. this(name, local, material, attackDamage, -2.4d);
  33. }
  34. @Override
  35. public boolean hitEntity(ItemStack stack, EntityLivingBase target, EntityLivingBase attacker)
  36. {
  37. stack.damageItem(1, attacker);
  38. return true;
  39. }
  40. @Override
  41. public boolean onBlockDestroyed(ItemStack stack, World worldIn, IBlockState state, BlockPos pos, EntityLivingBase entityLiving)
  42. {
  43. if(state.getBlockHardness(worldIn, pos) != 0)
  44. {
  45. stack.damageItem(2, entityLiving);
  46. }
  47. return true;
  48. }
  49. @SideOnly(Side.CLIENT)
  50. @Override
  51. public boolean isFull3D()
  52. {
  53. return true;
  54. }
  55. @Override
  56. public int getItemEnchantability()
  57. {
  58. return this.material.getEnchantability();
  59. }
  60. @Override
  61. public boolean getIsRepairable(ItemStack toRepair, ItemStack repair)
  62. {
  63. ItemStack mat = this.material.getRepairItemStack();
  64. if (!mat.isEmpty() && net.minecraftforge.oredict.OreDictionary.itemMatches(mat, repair, false)) return true;
  65. return super.getIsRepairable(toRepair, repair);
  66. }
  67. @Override
  68. public Multimap<String, AttributeModifier> getItemAttributeModifiers(EntityEquipmentSlot equipmentSlot)
  69. {
  70. Multimap<String, AttributeModifier> multimap = super.getItemAttributeModifiers(equipmentSlot);
  71. if (equipmentSlot == EntityEquipmentSlot.MAINHAND)
  72. {
  73. multimap.put(SharedMonsterAttributes.ATTACK_DAMAGE.getName(), new AttributeModifier(ATTACK_DAMAGE_MODIFIER, "Weapon modifier", this.attackDamage, 0));
  74. multimap.put(SharedMonsterAttributes.ATTACK_SPEED.getName(), new AttributeModifier(ATTACK_SPEED_MODIFIER, "Weapon modifier", speed, 0));
  75. }
  76. return multimap;
  77. }
  78. }