| 1 | package net.minecraft.world.item;
|
| 2 |
|
| 3 | import java.util.Optional;
|
| 4 | import net.minecraft.core.Direction;
|
| 5 | import net.minecraft.core.Position;
|
| 6 | import net.minecraft.core.dispenser.BlockSource;
|
| 7 | import net.minecraft.world.entity.projectile.Projectile;
|
| 8 | import net.minecraft.world.level.Level;
|
| 9 | import net.minecraft.world.level.block.DispenserBlock;
|
| 10 | import net.minecraft.world.level.block.LevelEvent;
|
| 11 | import net.minecraft.world.phys.Vec3;
|
| 12 |
|
| 13 | public interface ProjectileItem {
|
| 14 | Projectile asProjectile(final Level level, final Position position, final ItemStack itemStack, final Direction direction);
|
| 15 |
|
| 16 | default ProjectileItem.DispenseConfig createDispenseConfig() {
|
| 17 | return ProjectileItem.DispenseConfig.DEFAULT;
|
| 18 | }
|
| 19 |
|
| 20 | default void shoot(final Projectile projectile, final double xd, final double yd, final double zd, final float pow, final float uncertainty) {
|
| 21 | projectile.shoot(xd, yd, zd, pow, uncertainty);
|
| 22 | }
|
| 23 |
|
| 24 | record DispenseConfig(
|
| 25 | ProjectileItem.PositionFunction positionFunction, float uncertainty, float power, Optional<@LevelEvent.Value Integer> overrideDispenseEvent
|
| 26 | ) {
|
| 27 | public static final ProjectileItem.DispenseConfig DEFAULT = builder().build();
|
| 28 |
|
| 29 | public static ProjectileItem.DispenseConfig.Builder builder() {
|
| 30 | return new ProjectileItem.DispenseConfig.Builder();
|
| 31 | }
|
| 32 |
|
| 33 | public static class Builder {
|
| 34 | private ProjectileItem.PositionFunction positionFunction = (source, direction) -> DispenserBlock.getDispensePosition(source, 0.7, new Vec3(0.0, 0.1, 0.0));
|
| 35 | private float uncertainty = 6.0F;
|
| 36 | private float power = 1.1F;
|
| 37 | private Optional<@LevelEvent.Value Integer> overrideDispenseEvent = Optional.empty();
|
| 38 |
|
| 39 | public ProjectileItem.DispenseConfig.Builder positionFunction(final ProjectileItem.PositionFunction positionFunction) {
|
| 40 | this.positionFunction = positionFunction;
|
| 41 | return this;
|
| 42 | }
|
| 43 |
|
| 44 | public ProjectileItem.DispenseConfig.Builder uncertainty(final float uncertainty) {
|
| 45 | this.uncertainty = uncertainty;
|
| 46 | return this;
|
| 47 | }
|
| 48 |
|
| 49 | public ProjectileItem.DispenseConfig.Builder power(final float power) {
|
| 50 | this.power = power;
|
| 51 | return this;
|
| 52 | }
|
| 53 |
|
| 54 | public ProjectileItem.DispenseConfig.Builder overrideDispenseEvent(final @LevelEvent.Value int dispenseEvent) {
|
| 55 | this.overrideDispenseEvent = Optional.of(dispenseEvent);
|
| 56 | return this;
|
| 57 | }
|
| 58 |
|
| 59 | public ProjectileItem.DispenseConfig build() {
|
| 60 | return new ProjectileItem.DispenseConfig(this.positionFunction, this.uncertainty, this.power, this.overrideDispenseEvent);
|
| 61 | }
|
| 62 | }
|
| 63 | }
|
| 64 |
|
| 65 | @FunctionalInterface
|
| 66 | interface PositionFunction {
|
| 67 | Position getDispensePosition(final BlockSource source, final Direction direction);
|
| 68 | }
|
| 69 | }
|
| 70 |
|