| 1 | package net.minecraft.world.item;
|
| 2 |
|
| 3 | import java.util.ArrayList;
|
| 4 | import java.util.List;
|
| 5 | import java.util.function.Predicate;
|
| 6 | import net.minecraft.core.component.DataComponents;
|
| 7 | import net.minecraft.server.level.ServerLevel;
|
| 8 | import net.minecraft.tags.ItemTags;
|
| 9 | import net.minecraft.util.Unit;
|
| 10 | import net.minecraft.world.InteractionHand;
|
| 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.entity.projectile.arrow.AbstractArrow;
|
| 15 | import net.minecraft.world.item.enchantment.EnchantmentHelper;
|
| 16 | import net.minecraft.world.level.Level;
|
| 17 | import org.jspecify.annotations.Nullable;
|
| 18 |
|
| 19 | public abstract class ProjectileWeaponItem extends Item {
|
| 20 | public static final Predicate<ItemStack> ARROW_ONLY = itemStack -> itemStack.is(ItemTags.ARROWS);
|
| 21 | public static final Predicate<ItemStack> ARROW_OR_FIREWORK = ARROW_ONLY.or(itemStack -> itemStack.is(Items.FIREWORK_ROCKET));
|
| 22 |
|
| 23 | public ProjectileWeaponItem(final Item.Properties properties) {
|
| 24 | super(properties);
|
| 25 | }
|
| 26 |
|
| 27 | public Predicate<ItemStack> getSupportedHeldProjectiles() {
|
| 28 | return this.getAllSupportedProjectiles();
|
| 29 | }
|
| 30 |
|
| 31 | public abstract Predicate<ItemStack> getAllSupportedProjectiles();
|
| 32 |
|
| 33 | public static ItemStack getHeldProjectile(final LivingEntity entity, final Predicate<ItemStack> valid) {
|
| 34 | if (valid.test(entity.getItemInHand(InteractionHand.OFF_HAND))) {
|
| 35 | return entity.getItemInHand(InteractionHand.OFF_HAND);
|
| 36 | } else {
|
| 37 | return valid.test(entity.getItemInHand(InteractionHand.MAIN_HAND)) ? entity.getItemInHand(InteractionHand.MAIN_HAND) : ItemStack.EMPTY;
|
| 38 | }
|
| 39 | }
|
| 40 |
|
| 41 | public abstract int getDefaultProjectileRange();
|
| 42 |
|
| 43 | protected void shoot(
|
| 44 | final ServerLevel level,
|
| 45 | final LivingEntity shooter,
|
| 46 | final InteractionHand hand,
|
| 47 | final ItemStack weapon,
|
| 48 | final List<ItemStack> projectiles,
|
| 49 | final float power,
|
| 50 | final float uncertainty,
|
| 51 | final boolean isCrit,
|
| 52 | final @Nullable LivingEntity targetOverride
|
| 53 | ) {
|
| 54 | float maxAngle = EnchantmentHelper.processProjectileSpread(level, weapon, shooter, 0.0F);
|
| 55 | float angleStep = projectiles.size() == 1 ? 0.0F : 2.0F * maxAngle / (projectiles.size() - 1);
|
| 56 | float angleOffset = (projectiles.size() - 1) % 2 * angleStep / 2.0F;
|
| 57 | float direction = 1.0F;
|
| 58 |
|
| 59 | for (int i = 0; i < projectiles.size(); i++) {
|
| 60 | ItemStack projectile = projectiles.get(i);
|
| 61 | if (!projectile.isEmpty()) {
|
| 62 | float angle = angleOffset + direction * ((i + 1) / 2) * angleStep;
|
| 63 | direction = -direction;
|
| 64 | int index = i;
|
| 65 | Projectile.spawnProjectile(
|
| 66 | this.createProjectile(level, shooter, weapon, projectile, isCrit),
|
| 67 | level,
|
| 68 | projectile,
|
| 69 | projectileEntity -> this.shootProjectile(shooter, projectileEntity, index, power, uncertainty, angle, targetOverride)
|
| 70 | );
|
| 71 | weapon.hurtAndBreak(this.getDurabilityUse(projectile), shooter, hand.asEquipmentSlot());
|
| 72 | if (weapon.isEmpty()) {
|
| 73 | break;
|
| 74 | }
|
| 75 | }
|
| 76 | }
|
| 77 | }
|
| 78 |
|
| 79 | protected int getDurabilityUse(final ItemStack projectile) {
|
| 80 | return 1;
|
| 81 | }
|
| 82 |
|
| 83 | protected abstract void shootProjectile(
|
| 84 | final LivingEntity shooter,
|
| 85 | final Projectile projectileEntity,
|
| 86 | final int index,
|
| 87 | final float power,
|
| 88 | final float uncertainty,
|
| 89 | final float angle,
|
| 90 | final @Nullable LivingEntity targetOverrride
|
| 91 | );
|
| 92 |
|
| 93 | protected Projectile createProjectile(final Level level, final LivingEntity shooter, final ItemStack weapon, final ItemStack projectile, final boolean isCrit) {
|
| 94 | ArrowItem arrowItem = projectile.getItem() instanceof ArrowItem arrow ? arrow : (ArrowItem)Items.ARROW;
|
| 95 | AbstractArrow arrow = arrowItem.createArrow(level, projectile, shooter, weapon);
|
| 96 | if (isCrit) {
|
| 97 | arrow.setCritArrow(true);
|
| 98 | }
|
| 99 |
|
| 100 | return arrow;
|
| 101 | }
|
| 102 |
|
| 103 | protected static List<ItemStack> draw(final ItemStack weapon, final ItemStack projectile, final LivingEntity shooter) {
|
| 104 | if (projectile.isEmpty()) {
|
| 105 | return List.of();
|
| 106 | }
|
| 107 |
|
| 108 | int numProjectiles = shooter.level() instanceof ServerLevel serverLevel ? EnchantmentHelper.processProjectileCount(serverLevel, weapon, shooter, 1) : 1;
|
| 109 | List<ItemStack> drawn = new ArrayList<>(numProjectiles);
|
| 110 | ItemStack projectileCopy = projectile.copy();
|
| 111 |
|
| 112 | for (int i = 0; i < numProjectiles; i++) {
|
| 113 | ItemStack drawnStack = useAmmo(weapon, i == 0 ? projectile : projectileCopy, shooter, i > 0);
|
| 114 | if (!drawnStack.isEmpty()) {
|
| 115 | drawn.add(drawnStack);
|
| 116 | }
|
| 117 | }
|
| 118 |
|
| 119 | return drawn;
|
| 120 | }
|
| 121 |
|
| 122 | protected static ItemStack useAmmo(final ItemStack weapon, final ItemStack projectile, final LivingEntity holder, final boolean forceInfinite) {
|
| 123 | int ammoToUse = !forceInfinite && !holder.hasInfiniteMaterials() && holder.level() instanceof ServerLevel serverLevel
|
| 124 | ? EnchantmentHelper.processAmmoUse(serverLevel, weapon, projectile, 1)
|
| 125 | : 0;
|
| 126 | if (ammoToUse > projectile.getCount()) {
|
| 127 | return ItemStack.EMPTY;
|
| 128 | }
|
| 129 |
|
| 130 | if (ammoToUse == 0) {
|
| 131 | ItemStack copy = projectile.copyWithCount(1);
|
| 132 | copy.set(DataComponents.INTANGIBLE_PROJECTILE, Unit.INSTANCE);
|
| 133 | return copy;
|
| 134 | }
|
| 135 |
|
| 136 | ItemStack used = projectile.split(ammoToUse);
|
| 137 | if (projectile.isEmpty() && holder instanceof Player player) {
|
| 138 | player.getInventory().removeItem(projectile);
|
| 139 | }
|
| 140 |
|
| 141 | return used;
|
| 142 | }
|
| 143 | }
|
| 144 |
|