ItemSling.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. private boolean isAmmo(ItemStack stack) {
  38. return stack.getItem() == Item.getItemFromBlock(Blocks.COBBLESTONE);
  39. }
  40. @Override
  41. public void onPlayerStoppedUsing(ItemStack stack, World w, LivingEntity liv, int timeLeft) {
  42. if(liv instanceof PlayerEntity) {
  43. int i = this.getUseDuration(stack) - timeLeft;
  44. if(i < 0) {
  45. return;
  46. }
  47. PlayerEntity p = (PlayerEntity) liv;
  48. ItemStack ammo = this.findAmmo(p);
  49. if(!ammo.isEmpty() || p.abilities.isCreativeMode) {
  50. if(ammo.isEmpty()) {
  51. ammo = new ItemStack(Blocks.COBBLESTONE);
  52. }
  53. float f = BowItem.getArrowVelocity(i);
  54. if(f >= 0.1) {
  55. if(!w.isRemote) {
  56. ItemStack ammoCopy = ammo.copy();
  57. ammoCopy.setCount(1);
  58. EntityItemProjectile proj = new EntityItemProjectile(p, ammoCopy, 2.0f);
  59. proj.setHeadingFromThrower(p, p.rotationPitch, p.rotationYaw, 0.0f, f, 1.0f);
  60. stack.damageItem(1, p, (c) -> {
  61. });
  62. w.addEntity(proj);
  63. }
  64. 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);
  65. if(!p.abilities.isCreativeMode) {
  66. ammo.shrink(1);
  67. if(ammo.isEmpty()) {
  68. p.inventory.deleteStack(ammo);
  69. }
  70. }
  71. p.addStat(Stats.ITEM_USED.get(this));
  72. }
  73. }
  74. }
  75. }
  76. @Override
  77. public int getUseDuration(ItemStack stack) {
  78. return 72000;
  79. }
  80. @Override
  81. public UseAction getUseAction(ItemStack stack) {
  82. return UseAction.BOW;
  83. }
  84. @Override
  85. public ActionResult<ItemStack> onItemRightClick(World w, PlayerEntity p, Hand hand) {
  86. ItemStack ammo = p.getHeldItem(hand);
  87. if(!p.abilities.isCreativeMode && this.findAmmo(p).isEmpty()) {
  88. return new ActionResult<>(ActionResultType.FAIL, ammo);
  89. }
  90. p.setActiveHand(hand);
  91. return new ActionResult<>(ActionResultType.SUCCESS, ammo);
  92. }
  93. @Override
  94. public int getItemEnchantability() {
  95. return 1;
  96. }
  97. }