| 1 | package net.minecraft.world.item.consume_effects;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.MapCodec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import net.minecraft.core.BlockPos;
|
| 7 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 8 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 9 | import net.minecraft.network.codec.StreamCodec;
|
| 10 | import net.minecraft.server.level.ServerLevel;
|
| 11 | import net.minecraft.sounds.SoundEvent;
|
| 12 | import net.minecraft.sounds.SoundEvents;
|
| 13 | import net.minecraft.sounds.SoundSource;
|
| 14 | import net.minecraft.tags.BlockTags;
|
| 15 | import net.minecraft.util.BlockUtil;
|
| 16 | import net.minecraft.util.ExtraCodecs;
|
| 17 | import net.minecraft.util.Mth;
|
| 18 | import net.minecraft.world.entity.LivingEntity;
|
| 19 | import net.minecraft.world.entity.animal.fox.Fox;
|
| 20 | import net.minecraft.world.item.ItemStack;
|
| 21 | import net.minecraft.world.level.Level;
|
| 22 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 23 | import net.minecraft.world.phys.Vec3;
|
| 24 |
|
| 25 | public record TeleportRandomlyConsumeEffect(float diameter, boolean directionalParticles) implements ConsumeEffect {
|
| 26 | private static final float DEFAULT_DIAMETER = 16.0F;
|
| 27 | public static final MapCodec<TeleportRandomlyConsumeEffect> CODEC = RecordCodecBuilder.mapCodec(
|
| 28 | i -> i.group(
|
| 29 | ExtraCodecs.POSITIVE_FLOAT.optionalFieldOf("diameter", 16.0F).forGetter(TeleportRandomlyConsumeEffect::diameter),
|
| 30 | Codec.BOOL.optionalFieldOf("directional_particles", true).forGetter(TeleportRandomlyConsumeEffect::directionalParticles)
|
| 31 | )
|
| 32 | .apply(i, TeleportRandomlyConsumeEffect::new)
|
| 33 | );
|
| 34 | public static final StreamCodec<RegistryFriendlyByteBuf, TeleportRandomlyConsumeEffect> STREAM_CODEC = StreamCodec.composite(
|
| 35 | ByteBufCodecs.FLOAT,
|
| 36 | TeleportRandomlyConsumeEffect::diameter,
|
| 37 | ByteBufCodecs.BOOL,
|
| 38 | TeleportRandomlyConsumeEffect::directionalParticles,
|
| 39 | TeleportRandomlyConsumeEffect::new
|
| 40 | );
|
| 41 |
|
| 42 | public TeleportRandomlyConsumeEffect() {
|
| 43 | this(16.0F, true);
|
| 44 | }
|
| 45 |
|
| 46 | @Override
|
| 47 | public ConsumeEffect.Type<TeleportRandomlyConsumeEffect> getType() {
|
| 48 | return ConsumeEffect.Type.TELEPORT_RANDOMLY;
|
| 49 | }
|
| 50 |
|
| 51 | @Override
|
| 52 | public boolean apply(final Level level, final ItemStack stack, final LivingEntity user) {
|
| 53 | for (int attempt = 0; attempt < 16; attempt++) {
|
| 54 | double xx = user.getX() + (user.getRandom().nextDouble() - 0.5) * this.diameter;
|
| 55 | double yy = Mth.clamp(
|
| 56 | user.getY() + (user.getRandom().nextDouble() - 0.5) * this.diameter, level.getMinY(), level.getMinY() + ((ServerLevel)level).getLogicalHeight() - 1
|
| 57 | );
|
| 58 | double zz = user.getZ() + (user.getRandom().nextDouble() - 0.5) * this.diameter;
|
| 59 | if (user.isPassenger()) {
|
| 60 | user.stopRiding();
|
| 61 | }
|
| 62 |
|
| 63 | Vec3 oldPos = user.position();
|
| 64 | if (user.randomTeleport(xx, yy, zz, true, BlockTags.CONSUMABLE_DOES_NOT_TELEPORT_TO)) {
|
| 65 | level.gameEvent(GameEvent.TELEPORT, oldPos, GameEvent.Context.of(user));
|
| 66 | SoundSource soundSource;
|
| 67 | SoundEvent soundEvent;
|
| 68 | if (user instanceof Fox) {
|
| 69 | soundEvent = SoundEvents.FOX_TELEPORT;
|
| 70 | soundSource = SoundSource.NEUTRAL;
|
| 71 | } else {
|
| 72 | soundEvent = SoundEvents.CHORUS_FRUIT_TELEPORT;
|
| 73 | soundSource = SoundSource.PLAYERS;
|
| 74 | }
|
| 75 |
|
| 76 | level.playSound(null, user.getX(), user.getY(), user.getZ(), soundEvent, soundSource);
|
| 77 | if (this.directionalParticles) {
|
| 78 | BlockPos origin = BlockPos.containing(oldPos);
|
| 79 | BlockPos target = user.blockPosition();
|
| 80 | level.levelEvent(2017, origin, BlockUtil.clampedPackDifferenceInPosition(origin, target, 127, 127, 127));
|
| 81 | }
|
| 82 |
|
| 83 | user.resetFallDistance();
|
| 84 | user.resetCurrentImpulseContext();
|
| 85 | return true;
|
| 86 | }
|
| 87 | }
|
| 88 |
|
| 89 | return false;
|
| 90 | }
|
| 91 | }
|
| 92 |
|