| 1 | package net.minecraft.world.item.enchantment.effects;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.DataResult;
|
| 5 | import com.mojang.serialization.MapCodec;
|
| 6 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 7 | import net.minecraft.core.particles.ParticleOptions;
|
| 8 | import net.minecraft.core.particles.ParticleTypes;
|
| 9 | import net.minecraft.server.level.ServerLevel;
|
| 10 | import net.minecraft.util.ExtraCodecs;
|
| 11 | import net.minecraft.util.RandomSource;
|
| 12 | import net.minecraft.util.StringRepresentable;
|
| 13 | import net.minecraft.util.valueproviders.ConstantFloat;
|
| 14 | import net.minecraft.util.valueproviders.FloatProvider;
|
| 15 | import net.minecraft.util.valueproviders.FloatProviders;
|
| 16 | import net.minecraft.world.entity.Entity;
|
| 17 | import net.minecraft.world.item.enchantment.EnchantedItemInUse;
|
| 18 | import net.minecraft.world.phys.Vec3;
|
| 19 |
|
| 20 | public record SpawnParticlesEffect(
|
| 21 | ParticleOptions particle,
|
| 22 | SpawnParticlesEffect.PositionSource horizontalPosition,
|
| 23 | SpawnParticlesEffect.PositionSource verticalPosition,
|
| 24 | SpawnParticlesEffect.VelocitySource horizontalVelocity,
|
| 25 | SpawnParticlesEffect.VelocitySource verticalVelocity,
|
| 26 | FloatProvider speed
|
| 27 | ) implements EnchantmentEntityEffect {
|
| 28 | public static final MapCodec<SpawnParticlesEffect> CODEC = RecordCodecBuilder.mapCodec(
|
| 29 | i -> i.group(
|
| 30 | ParticleTypes.CODEC.fieldOf("particle").forGetter(SpawnParticlesEffect::particle),
|
| 31 | SpawnParticlesEffect.PositionSource.CODEC.fieldOf("horizontal_position").forGetter(SpawnParticlesEffect::horizontalPosition),
|
| 32 | SpawnParticlesEffect.PositionSource.CODEC.fieldOf("vertical_position").forGetter(SpawnParticlesEffect::verticalPosition),
|
| 33 | SpawnParticlesEffect.VelocitySource.CODEC.fieldOf("horizontal_velocity").forGetter(SpawnParticlesEffect::horizontalVelocity),
|
| 34 | SpawnParticlesEffect.VelocitySource.CODEC.fieldOf("vertical_velocity").forGetter(SpawnParticlesEffect::verticalVelocity),
|
| 35 | FloatProviders.CODEC.optionalFieldOf("speed", ConstantFloat.ZERO).forGetter(SpawnParticlesEffect::speed)
|
| 36 | )
|
| 37 | .apply(i, SpawnParticlesEffect::new)
|
| 38 | );
|
| 39 |
|
| 40 | public static SpawnParticlesEffect.PositionSource offsetFromEntityPosition(final float offset) {
|
| 41 | return new SpawnParticlesEffect.PositionSource(SpawnParticlesEffect.PositionSourceType.ENTITY_POSITION, offset, 1.0F);
|
| 42 | }
|
| 43 |
|
| 44 | public static SpawnParticlesEffect.PositionSource inBoundingBox() {
|
| 45 | return new SpawnParticlesEffect.PositionSource(SpawnParticlesEffect.PositionSourceType.BOUNDING_BOX, 0.0F, 1.0F);
|
| 46 | }
|
| 47 |
|
| 48 | public static SpawnParticlesEffect.VelocitySource movementScaled(final float scale) {
|
| 49 | return new SpawnParticlesEffect.VelocitySource(scale, ConstantFloat.ZERO);
|
| 50 | }
|
| 51 |
|
| 52 | public static SpawnParticlesEffect.VelocitySource fixedVelocity(final FloatProvider provider) {
|
| 53 | return new SpawnParticlesEffect.VelocitySource(0.0F, provider);
|
| 54 | }
|
| 55 |
|
| 56 | @Override
|
| 57 | public void apply(final ServerLevel serverLevel, final int enchantmentLevel, final EnchantedItemInUse item, final Entity entity, final Vec3 position) {
|
| 58 | RandomSource random = entity.getRandom();
|
| 59 | Vec3 movement = entity.getKnownMovement();
|
| 60 | float bbWidth = entity.getBbWidth();
|
| 61 | float bbHeight = entity.getBbHeight();
|
| 62 | serverLevel.sendParticles(
|
| 63 | this.particle,
|
| 64 | this.horizontalPosition.getCoordinate(position.x(), position.x(), bbWidth, random),
|
| 65 | this.verticalPosition.getCoordinate(position.y(), position.y() + bbHeight / 2.0F, bbHeight, random),
|
| 66 | this.horizontalPosition.getCoordinate(position.z(), position.z(), bbWidth, random),
|
| 67 | 0,
|
| 68 | this.horizontalVelocity.getVelocity(movement.x(), random),
|
| 69 | this.verticalVelocity.getVelocity(movement.y(), random),
|
| 70 | this.horizontalVelocity.getVelocity(movement.z(), random),
|
| 71 | this.speed.sample(random)
|
| 72 | );
|
| 73 | }
|
| 74 |
|
| 75 | @Override
|
| 76 | public MapCodec<SpawnParticlesEffect> codec() {
|
| 77 | return CODEC;
|
| 78 | }
|
| 79 |
|
| 80 | public record PositionSource(SpawnParticlesEffect.PositionSourceType type, float offset, float scale) {
|
| 81 | public static final MapCodec<SpawnParticlesEffect.PositionSource> CODEC = RecordCodecBuilder.<SpawnParticlesEffect.PositionSource>mapCodec(
|
| 82 | i -> i.group(
|
| 83 | SpawnParticlesEffect.PositionSourceType.CODEC.fieldOf("type").forGetter(SpawnParticlesEffect.PositionSource::type),
|
| 84 | Codec.FLOAT.optionalFieldOf("offset", 0.0F).forGetter(SpawnParticlesEffect.PositionSource::offset),
|
| 85 | ExtraCodecs.POSITIVE_FLOAT.optionalFieldOf("scale", 1.0F).forGetter(SpawnParticlesEffect.PositionSource::scale)
|
| 86 | )
|
| 87 | .apply(i, SpawnParticlesEffect.PositionSource::new)
|
| 88 | )
|
| 89 | .validate(
|
| 90 | positioning -> positioning.type() == SpawnParticlesEffect.PositionSourceType.ENTITY_POSITION && positioning.scale() != 1.0F
|
| 91 | ? DataResult.error(() -> "Cannot scale an entity position coordinate source")
|
| 92 | : DataResult.success(positioning)
|
| 93 | );
|
| 94 |
|
| 95 | public double getCoordinate(final double position, final double center, final float boundingBoxSpan, final RandomSource random) {
|
| 96 | return this.type.getCoordinate(position, center, boundingBoxSpan * this.scale, random) + this.offset;
|
| 97 | }
|
| 98 | }
|
| 99 |
|
| 100 | public enum PositionSourceType implements StringRepresentable {
|
| 101 | ENTITY_POSITION("entity_position", (pos, center, bbSpan, random) -> pos),
|
| 102 | BOUNDING_BOX("in_bounding_box", (pos, center, bbSpan, random) -> center + (random.nextDouble() - 0.5) * bbSpan);
|
| 103 |
|
| 104 | public static final Codec<SpawnParticlesEffect.PositionSourceType> CODEC = StringRepresentable.fromEnum(SpawnParticlesEffect.PositionSourceType::values);
|
| 105 | private final String id;
|
| 106 | private final SpawnParticlesEffect.PositionSourceType.CoordinateSource source;
|
| 107 |
|
| 108 | PositionSourceType(final String id, final SpawnParticlesEffect.PositionSourceType.CoordinateSource source) {
|
| 109 | this.id = id;
|
| 110 | this.source = source;
|
| 111 | }
|
| 112 |
|
| 113 | public double getCoordinate(final double position, final double center, final float boundingBoxSpan, final RandomSource random) {
|
| 114 | return this.source.getCoordinate(position, center, boundingBoxSpan, random);
|
| 115 | }
|
| 116 |
|
| 117 | @Override
|
| 118 | public String getSerializedName() {
|
| 119 | return this.id;
|
| 120 | }
|
| 121 |
|
| 122 | @FunctionalInterface
|
| 123 | private interface CoordinateSource {
|
| 124 | double getCoordinate(double pos, double center, float boundingBoxSpan, RandomSource random);
|
| 125 | }
|
| 126 | }
|
| 127 |
|
| 128 | public record VelocitySource(float movementScale, FloatProvider base) {
|
| 129 | public static final MapCodec<SpawnParticlesEffect.VelocitySource> CODEC = RecordCodecBuilder.mapCodec(
|
| 130 | i -> i.group(
|
| 131 | Codec.FLOAT.optionalFieldOf("movement_scale", 0.0F).forGetter(SpawnParticlesEffect.VelocitySource::movementScale),
|
| 132 | FloatProviders.CODEC.optionalFieldOf("base", ConstantFloat.ZERO).forGetter(SpawnParticlesEffect.VelocitySource::base)
|
| 133 | )
|
| 134 | .apply(i, SpawnParticlesEffect.VelocitySource::new)
|
| 135 | );
|
| 136 |
|
| 137 | public double getVelocity(final double movement, final RandomSource random) {
|
| 138 | return movement * this.movementScale + this.base.sample(random);
|
| 139 | }
|
| 140 | }
|
| 141 | }
|
| 142 |
|