ItemSling.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package me.km.items;
  2. import me.km.entities.EntityItemProjectile;
  3. import net.minecraft.block.Blocks;
  4. import net.minecraft.entity.LivingEntity;
  5. import net.minecraft.entity.player.PlayerEntity;
  6. import net.minecraft.item.BowItem;
  7. import net.minecraft.item.Item;
  8. import net.minecraft.item.ItemGroup;
  9. import net.minecraft.item.ItemStack;
  10. import net.minecraft.item.UseAction;
  11. import net.minecraft.stats.Stats;
  12. import net.minecraft.util.ActionResult;
  13. import net.minecraft.util.ActionResultType;
  14. import net.minecraft.util.Hand;
  15. import net.minecraft.util.ResourceLocation;
  16. import net.minecraft.util.SoundCategory;
  17. import net.minecraft.util.SoundEvents;
  18. import net.minecraft.world.World;
  19. public class ItemSling extends Item
  20. {
  21. public ItemSling(int maxDamage)
  22. {
  23. super(new Properties().defaultMaxDamage(maxDamage).group(ItemGroup.COMBAT));
  24. this.addPropertyOverride(new ResourceLocation("pull"), (stack, w, ent) ->
  25. {
  26. if(ent == null)
  27. {
  28. return 0.0f;
  29. }
  30. else
  31. {
  32. return ent.getActiveItemStack().getItem() != ModItems.sling ? 0.0f : (stack.getUseDuration() - ent.getItemInUseCount()) / 20.0F;
  33. }
  34. });
  35. this.addPropertyOverride(new ResourceLocation("pulling"), (stack, w, ent) -> ent != null && ent.isHandActive() && ent.getActiveItemStack() == stack ? 1.0f : 0.0f);
  36. }
  37. private ItemStack findAmmo(PlayerEntity p)
  38. {
  39. if(this.isAmmo(p.getHeldItem(Hand.OFF_HAND)))
  40. {
  41. return p.getHeldItem(Hand.OFF_HAND);
  42. }
  43. else if(this.isAmmo(p.getHeldItem(Hand.MAIN_HAND)))
  44. {
  45. return p.getHeldItem(Hand.MAIN_HAND);
  46. }
  47. else
  48. {
  49. for(int i = 0; i < p.inventory.getSizeInventory(); i++)
  50. {
  51. ItemStack stack = p.inventory.getStackInSlot(i);
  52. if(this.isAmmo(stack))
  53. {
  54. return stack;
  55. }
  56. }
  57. return ItemStack.EMPTY;
  58. }
  59. }
  60. private boolean isAmmo(ItemStack stack)
  61. {
  62. return stack.getItem() == Item.getItemFromBlock(Blocks.COBBLESTONE);
  63. }
  64. @Override
  65. public void onPlayerStoppedUsing(ItemStack stack, World w, LivingEntity liv, int timeLeft)
  66. {
  67. if(liv instanceof PlayerEntity)
  68. {
  69. int i = this.getUseDuration(stack) - timeLeft;
  70. if(i < 0)
  71. {
  72. return;
  73. }
  74. PlayerEntity p = (PlayerEntity) liv;
  75. ItemStack ammo = this.findAmmo(p);
  76. if(!ammo.isEmpty() || p.abilities.isCreativeMode)
  77. {
  78. if(ammo.isEmpty())
  79. {
  80. ammo = new ItemStack(Blocks.COBBLESTONE);
  81. }
  82. float f = BowItem.getArrowVelocity(i);
  83. if(f >= 0.1)
  84. {
  85. if(!w.isRemote)
  86. {
  87. ItemStack ammoCopy = ammo.copy();
  88. ammoCopy.setCount(1);
  89. EntityItemProjectile proj = new EntityItemProjectile(p, ammoCopy, 2.0f);
  90. proj.setHeadingFromThrower(p, p.rotationPitch, p.rotationYaw, 0.0f, f, 1.0f);
  91. stack.damageItem(1, p, (c) -> {});
  92. w.addEntity(proj);
  93. }
  94. w.playSound(null, p.getPosX(), p.getPosY(), p.getPosZ(), SoundEvents.ENTITY_ARROW_SHOOT, SoundCategory.PLAYERS, 1.0f, 1.0f / (random.nextFloat() * 0.4f + 1.2f) + f * 0.5f);
  95. if(!p.abilities.isCreativeMode)
  96. {
  97. ammo.shrink(1);
  98. if(ammo.isEmpty())
  99. {
  100. p.inventory.deleteStack(ammo);
  101. }
  102. }
  103. p.addStat(Stats.ITEM_USED.get(this));
  104. }
  105. }
  106. }
  107. }
  108. @Override
  109. public int getUseDuration(ItemStack stack)
  110. {
  111. return 72000;
  112. }
  113. @Override
  114. public UseAction getUseAction(ItemStack stack)
  115. {
  116. return UseAction.BOW;
  117. }
  118. @Override
  119. public ActionResult<ItemStack> onItemRightClick(World w, PlayerEntity p, Hand hand)
  120. {
  121. ItemStack ammo = p.getHeldItem(hand);
  122. if(!p.abilities.isCreativeMode && this.findAmmo(p).isEmpty())
  123. {
  124. return new ActionResult<>(ActionResultType.FAIL, ammo);
  125. }
  126. p.setActiveHand(hand);
  127. return new ActionResult<>(ActionResultType.SUCCESS, ammo);
  128. }
  129. @Override
  130. public int getItemEnchantability()
  131. {
  132. return 1;
  133. }
  134. }