| 1 | package net.minecraft.world.item;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import java.util.function.Predicate;
|
| 5 | import net.minecraft.server.level.ServerLevel;
|
| 6 | import net.minecraft.sounds.SoundEvents;
|
| 7 | import net.minecraft.sounds.SoundSource;
|
| 8 | import net.minecraft.stats.Stats;
|
| 9 | import net.minecraft.world.InteractionHand;
|
| 10 | import net.minecraft.world.InteractionResult;
|
| 11 | import net.minecraft.world.entity.LivingEntity;
|
| 12 | import net.minecraft.world.entity.player.Player;
|
| 13 | import net.minecraft.world.entity.projectile.Projectile;
|
| 14 | import net.minecraft.world.level.Level;
|
| 15 | import org.jspecify.annotations.Nullable;
|
| 16 |
|
| 17 | public class BowItem extends ProjectileWeaponItem {
|
| 18 | public static final int MAX_DRAW_DURATION = 20;
|
| 19 | public static final int DEFAULT_RANGE = 15;
|
| 20 |
|
| 21 | public BowItem(final Item.Properties properties) {
|
| 22 | super(properties);
|
| 23 | }
|
| 24 |
|
| 25 | @Override
|
| 26 | public boolean releaseUsing(final ItemStack itemStack, final Level level, final LivingEntity entity, final int remainingTime) {
|
| 27 | if (entity instanceof Player player) {
|
| 28 | ItemStack projectile = player.getProjectile(itemStack);
|
| 29 | if (projectile.isEmpty()) {
|
| 30 | return false;
|
| 31 | }
|
| 32 |
|
| 33 | int timeHeld = this.getUseDuration(itemStack, entity) - remainingTime;
|
| 34 | float pow = getPowerForTime(timeHeld);
|
| 35 | if (pow < 0.1) {
|
| 36 | return false;
|
| 37 | }
|
| 38 |
|
| 39 | List<ItemStack> firedProjectiles = draw(itemStack, projectile, player);
|
| 40 | if (level instanceof ServerLevel serverLevel && !firedProjectiles.isEmpty()) {
|
| 41 | this.shoot(serverLevel, player, player.getUsedItemHand(), itemStack, firedProjectiles, pow * 3.0F, 1.0F, pow == 1.0F, null);
|
| 42 | }
|
| 43 |
|
| 44 | level.playSound(
|
| 45 | null,
|
| 46 | player.getX(),
|
| 47 | player.getY(),
|
| 48 | player.getZ(),
|
| 49 | SoundEvents.ARROW_SHOOT,
|
| 50 | SoundSource.PLAYERS,
|
| 51 | 1.0F,
|
| 52 | 1.0F / (level.getRandom().nextFloat() * 0.4F + 1.2F) + pow * 0.5F
|
| 53 | );
|
| 54 | player.awardStat(Stats.ITEM_USED.get(this));
|
| 55 | return true;
|
| 56 | } else {
|
| 57 | return false;
|
| 58 | }
|
| 59 | }
|
| 60 |
|
| 61 | @Override
|
| 62 | protected void shootProjectile(
|
| 63 | final LivingEntity shooter,
|
| 64 | final Projectile projectileEntity,
|
| 65 | final int index,
|
| 66 | final float power,
|
| 67 | final float uncertainty,
|
| 68 | final float angle,
|
| 69 | final @Nullable LivingEntity targetOverrride
|
| 70 | ) {
|
| 71 | projectileEntity.shootFromRotation(shooter, shooter.getXRot(), shooter.getYRot() + angle, 0.0F, power, uncertainty);
|
| 72 | }
|
| 73 |
|
| 74 | public static float getPowerForTime(final int timeHeld) {
|
| 75 | float pow = timeHeld / 20.0F;
|
| 76 | pow = (pow * pow + pow * 2.0F) / 3.0F;
|
| 77 | if (pow > 1.0F) {
|
| 78 | pow = 1.0F;
|
| 79 | }
|
| 80 |
|
| 81 | return pow;
|
| 82 | }
|
| 83 |
|
| 84 | @Override
|
| 85 | public int getUseDuration(final ItemStack itemStack, final LivingEntity user) {
|
| 86 | return 72000;
|
| 87 | }
|
| 88 |
|
| 89 | @Override
|
| 90 | public ItemUseAnimation getUseAnimation(final ItemStack itemStack) {
|
| 91 | return ItemUseAnimation.BOW;
|
| 92 | }
|
| 93 |
|
| 94 | @Override
|
| 95 | public InteractionResult use(final Level level, final Player player, final InteractionHand hand) {
|
| 96 | ItemStack itemStack = player.getItemInHand(hand);
|
| 97 | boolean foundProjectile = !player.getProjectile(itemStack).isEmpty();
|
| 98 | if (!player.hasInfiniteMaterials() && !foundProjectile) {
|
| 99 | return InteractionResult.FAIL;
|
| 100 | }
|
| 101 |
|
| 102 | player.startUsingItem(hand);
|
| 103 | return InteractionResult.CONSUME;
|
| 104 | }
|
| 105 |
|
| 106 | @Override
|
| 107 | public Predicate<ItemStack> getAllSupportedProjectiles() {
|
| 108 | return ARROW_ONLY;
|
| 109 | }
|
| 110 |
|
| 111 | @Override
|
| 112 | public int getDefaultProjectileRange() {
|
| 113 | return 15;
|
| 114 | }
|
| 115 | }
|
| 116 |
|