ItemSling.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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.SoundCategory;
  16. import net.minecraft.util.SoundEvents;
  17. import net.minecraft.world.World;
  18. public class ItemSling extends Item {
  19. public ItemSling(int maxDamage) {
  20. super(new Properties().defaultMaxDamage(maxDamage).group(ItemGroup.COMBAT));
  21. }
  22. private ItemStack findAmmo(PlayerEntity p) {
  23. if(this.isAmmo(p.getHeldItem(Hand.OFF_HAND))) {
  24. return p.getHeldItem(Hand.OFF_HAND);
  25. } else if(this.isAmmo(p.getHeldItem(Hand.MAIN_HAND))) {
  26. return p.getHeldItem(Hand.MAIN_HAND);
  27. } else {
  28. for(int i = 0; i < p.inventory.getSizeInventory(); i++) {
  29. ItemStack stack = p.inventory.getStackInSlot(i);
  30. if(this.isAmmo(stack)) {
  31. return stack;
  32. }
  33. }
  34. return ItemStack.EMPTY;
  35. }
  36. }
  37. @SuppressWarnings("deprecation")
  38. private boolean isAmmo(ItemStack stack) {
  39. return stack.getItem() == Item.getItemFromBlock(Blocks.COBBLESTONE);
  40. }
  41. @Override
  42. public void onPlayerStoppedUsing(ItemStack stack, World w, LivingEntity liv, int timeLeft) {
  43. if(liv instanceof PlayerEntity) {
  44. int i = this.getUseDuration(stack) - timeLeft;
  45. if(i < 0) {
  46. return;
  47. }
  48. PlayerEntity p = (PlayerEntity) liv;
  49. ItemStack ammo = this.findAmmo(p);
  50. if(!ammo.isEmpty() || p.abilities.isCreativeMode) {
  51. if(ammo.isEmpty()) {
  52. ammo = new ItemStack(Blocks.COBBLESTONE);
  53. }
  54. float f = BowItem.getArrowVelocity(i);
  55. if(f >= 0.1) {
  56. if(!w.isRemote) {
  57. ItemStack ammoCopy = ammo.copy();
  58. ammoCopy.setCount(1);
  59. EntityItemProjectile proj = new EntityItemProjectile(p, ammoCopy, 2.0f);
  60. proj.setHeadingFromThrower(p, p.rotationPitch, p.rotationYaw, 0.0f, f,
  61. 1.0f);
  62. stack.damageItem(1, p, (c) -> {
  63. });
  64. w.addEntity(proj);
  65. }
  66. w.playSound(null, p.getPosX(), p.getPosY(), p.getPosZ(),
  67. SoundEvents.ENTITY_ARROW_SHOOT, SoundCategory.PLAYERS, 1.0f,
  68. 1.0f / (random.nextFloat() * 0.4f + 1.2f) + f * 0.5f);
  69. if(!p.abilities.isCreativeMode) {
  70. ammo.shrink(1);
  71. if(ammo.isEmpty()) {
  72. p.inventory.deleteStack(ammo);
  73. }
  74. }
  75. p.addStat(Stats.ITEM_USED.get(this));
  76. }
  77. }
  78. }
  79. }
  80. @Override
  81. public int getUseDuration(ItemStack stack) {
  82. return 72000;
  83. }
  84. @Override
  85. public UseAction getUseAction(ItemStack stack) {
  86. return UseAction.BOW;
  87. }
  88. @Override
  89. public ActionResult<ItemStack> onItemRightClick(World w, PlayerEntity p, Hand hand) {
  90. ItemStack ammo = p.getHeldItem(hand);
  91. if(!p.abilities.isCreativeMode && this.findAmmo(p).isEmpty()) {
  92. return new ActionResult<>(ActionResultType.FAIL, ammo);
  93. }
  94. p.setActiveHand(hand);
  95. return new ActionResult<>(ActionResultType.SUCCESS, ammo);
  96. }
  97. @Override
  98. public int getItemEnchantability() {
  99. return 1;
  100. }
  101. }