| 1 | package net.minecraft.world.item;
|
| 2 |
|
| 3 | import net.minecraft.core.Direction;
|
| 4 | import net.minecraft.core.Position;
|
| 5 | import net.minecraft.core.dispenser.BlockSource;
|
| 6 | import net.minecraft.server.level.ServerLevel;
|
| 7 | import net.minecraft.sounds.SoundEvents;
|
| 8 | import net.minecraft.sounds.SoundSource;
|
| 9 | import net.minecraft.stats.Stats;
|
| 10 | import net.minecraft.world.InteractionHand;
|
| 11 | import net.minecraft.world.InteractionResult;
|
| 12 | import net.minecraft.world.entity.player.Player;
|
| 13 | import net.minecraft.world.entity.projectile.FireworkRocketEntity;
|
| 14 | import net.minecraft.world.entity.projectile.Projectile;
|
| 15 | import net.minecraft.world.item.context.UseOnContext;
|
| 16 | import net.minecraft.world.level.Level;
|
| 17 | import net.minecraft.world.phys.Vec3;
|
| 18 |
|
| 19 | public class FireworkRocketItem extends Item implements ProjectileItem {
|
| 20 | public static final byte[] CRAFTABLE_DURATIONS = new byte[]{1, 2, 3};
|
| 21 | public static final double ROCKET_PLACEMENT_OFFSET = 0.15;
|
| 22 |
|
| 23 | public FireworkRocketItem(final Item.Properties properties) {
|
| 24 | super(properties);
|
| 25 | }
|
| 26 |
|
| 27 | @Override
|
| 28 | public InteractionResult useOn(final UseOnContext context) {
|
| 29 | Level level = context.getLevel();
|
| 30 | Player player = context.getPlayer();
|
| 31 | if (player != null && player.isFallFlying()) {
|
| 32 | return InteractionResult.PASS;
|
| 33 | }
|
| 34 |
|
| 35 | if (level instanceof ServerLevel serverLevel) {
|
| 36 | ItemStack itemStack = context.getItemInHand();
|
| 37 | Vec3 clickLocation = context.getClickLocation();
|
| 38 | Direction direction = context.getClickedFace();
|
| 39 | Projectile.spawnProjectile(
|
| 40 | new FireworkRocketEntity(
|
| 41 | level,
|
| 42 | context.getPlayer(),
|
| 43 | clickLocation.x + direction.getStepX() * 0.15,
|
| 44 | clickLocation.y + direction.getStepY() * 0.15,
|
| 45 | clickLocation.z + direction.getStepZ() * 0.15,
|
| 46 | itemStack
|
| 47 | ),
|
| 48 | serverLevel,
|
| 49 | itemStack
|
| 50 | );
|
| 51 | itemStack.shrink(1);
|
| 52 | }
|
| 53 |
|
| 54 | return InteractionResult.SUCCESS;
|
| 55 | }
|
| 56 |
|
| 57 | @Override
|
| 58 | public InteractionResult use(final Level level, final Player player, final InteractionHand hand) {
|
| 59 | if (player.isFallFlying()) {
|
| 60 | ItemStack itemStack = player.getItemInHand(hand);
|
| 61 | if (level instanceof ServerLevel serverLevel) {
|
| 62 | if (player.dropAllLeashConnections(null)) {
|
| 63 | level.playSound(null, player, SoundEvents.LEAD_BREAK, SoundSource.NEUTRAL, 1.0F, 1.0F);
|
| 64 | }
|
| 65 |
|
| 66 | Projectile.spawnProjectile(new FireworkRocketEntity(level, itemStack, player), serverLevel, itemStack);
|
| 67 | itemStack.consume(1, player);
|
| 68 | player.awardStat(Stats.ITEM_USED.get(this));
|
| 69 | }
|
| 70 |
|
| 71 | return InteractionResult.SUCCESS;
|
| 72 | } else {
|
| 73 | return InteractionResult.PASS;
|
| 74 | }
|
| 75 | }
|
| 76 |
|
| 77 | @Override
|
| 78 | public Projectile asProjectile(final Level level, final Position position, final ItemStack itemStack, final Direction direction) {
|
| 79 | return new FireworkRocketEntity(level, itemStack.copyWithCount(1), position.x(), position.y(), position.z(), true);
|
| 80 | }
|
| 81 |
|
| 82 | @Override
|
| 83 | public ProjectileItem.DispenseConfig createDispenseConfig() {
|
| 84 | return ProjectileItem.DispenseConfig.builder()
|
| 85 | .positionFunction(FireworkRocketItem::getEntityJustOutsideOfBlockPos)
|
| 86 | .uncertainty(1.0F)
|
| 87 | .power(0.5F)
|
| 88 | .overrideDispenseEvent(1004)
|
| 89 | .build();
|
| 90 | }
|
| 91 |
|
| 92 | private static Vec3 getEntityJustOutsideOfBlockPos(final BlockSource source, final Direction direction) {
|
| 93 | return source.center().add(direction.getStepX() * 0.5000099999997474, direction.getStepY() * 0.5000099999997474, direction.getStepZ() * 0.5000099999997474);
|
| 94 | }
|
| 95 | }
|
| 96 |
|