| 1 | package net.minecraft.world.item.enchantment.effects;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.MapCodec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import java.util.Optional;
|
| 7 | import net.minecraft.core.Holder;
|
| 8 | import net.minecraft.core.HolderSet;
|
| 9 | import net.minecraft.core.particles.ExplosionParticleInfo;
|
| 10 | import net.minecraft.core.particles.ParticleOptions;
|
| 11 | import net.minecraft.core.particles.ParticleTypes;
|
| 12 | import net.minecraft.core.registries.Registries;
|
| 13 | import net.minecraft.core.registries.codec.RegistryCodecs;
|
| 14 | import net.minecraft.server.level.ServerLevel;
|
| 15 | import net.minecraft.sounds.SoundEvent;
|
| 16 | import net.minecraft.util.random.WeightedList;
|
| 17 | import net.minecraft.world.damagesource.DamageSource;
|
| 18 | import net.minecraft.world.damagesource.DamageType;
|
| 19 | import net.minecraft.world.entity.Entity;
|
| 20 | import net.minecraft.world.item.enchantment.EnchantedItemInUse;
|
| 21 | import net.minecraft.world.item.enchantment.LevelBasedValue;
|
| 22 | import net.minecraft.world.level.Level;
|
| 23 | import net.minecraft.world.level.SimpleExplosionDamageCalculator;
|
| 24 | import net.minecraft.world.level.block.Block;
|
| 25 | import net.minecraft.world.phys.Vec3;
|
| 26 | import org.jspecify.annotations.Nullable;
|
| 27 |
|
| 28 | public record ExplodeEffect(
|
| 29 | boolean attributeToUser,
|
| 30 | Optional<Holder<DamageType>> damageType,
|
| 31 | Optional<LevelBasedValue> knockbackMultiplier,
|
| 32 | Optional<HolderSet<Block>> immuneBlocks,
|
| 33 | Vec3 offset,
|
| 34 | LevelBasedValue radius,
|
| 35 | boolean createFire,
|
| 36 | Level.ExplosionInteraction blockInteraction,
|
| 37 | ParticleOptions smallParticle,
|
| 38 | ParticleOptions largeParticle,
|
| 39 | WeightedList<ExplosionParticleInfo> blockParticles,
|
| 40 | Holder<SoundEvent> sound
|
| 41 | ) implements EnchantmentEntityEffect {
|
| 42 | public static final MapCodec<ExplodeEffect> CODEC = RecordCodecBuilder.mapCodec(
|
| 43 | i -> i.group(
|
| 44 | Codec.BOOL.optionalFieldOf("attribute_to_user", false).forGetter(ExplodeEffect::attributeToUser),
|
| 45 | DamageType.CODEC.optionalFieldOf("damage_type").forGetter(ExplodeEffect::damageType),
|
| 46 | LevelBasedValue.CODEC.optionalFieldOf("knockback_multiplier").forGetter(ExplodeEffect::knockbackMultiplier),
|
| 47 | RegistryCodecs.holderSet(Registries.BLOCK).optionalFieldOf("immune_blocks").forGetter(ExplodeEffect::immuneBlocks),
|
| 48 | Vec3.CODEC.optionalFieldOf("offset", Vec3.ZERO).forGetter(ExplodeEffect::offset),
|
| 49 | LevelBasedValue.CODEC.fieldOf("radius").forGetter(ExplodeEffect::radius),
|
| 50 | Codec.BOOL.optionalFieldOf("create_fire", false).forGetter(ExplodeEffect::createFire),
|
| 51 | Level.ExplosionInteraction.CODEC.fieldOf("block_interaction").forGetter(ExplodeEffect::blockInteraction),
|
| 52 | ParticleTypes.CODEC.fieldOf("small_particle").forGetter(ExplodeEffect::smallParticle),
|
| 53 | ParticleTypes.CODEC.fieldOf("large_particle").forGetter(ExplodeEffect::largeParticle),
|
| 54 | WeightedList.codec(ExplosionParticleInfo.CODEC).optionalFieldOf("block_particles", WeightedList.of()).forGetter(ExplodeEffect::blockParticles),
|
| 55 | SoundEvent.CODEC.fieldOf("sound").forGetter(ExplodeEffect::sound)
|
| 56 | )
|
| 57 | .apply(i, ExplodeEffect::new)
|
| 58 | );
|
| 59 |
|
| 60 | @Override
|
| 61 | public void apply(final ServerLevel serverLevel, final int enchantmentLevel, final EnchantedItemInUse item, final Entity entity, final Vec3 position) {
|
| 62 | Vec3 pos = position.add(this.offset);
|
| 63 | serverLevel.explode(
|
| 64 | this.attributeToUser ? entity : null,
|
| 65 | this.getDamageSource(entity, pos),
|
| 66 | new SimpleExplosionDamageCalculator(
|
| 67 | this.blockInteraction != Level.ExplosionInteraction.NONE,
|
| 68 | this.damageType.isPresent(),
|
| 69 | this.knockbackMultiplier.map(value -> value.calculate(enchantmentLevel)),
|
| 70 | this.immuneBlocks
|
| 71 | ),
|
| 72 | pos.x(),
|
| 73 | pos.y(),
|
| 74 | pos.z(),
|
| 75 | Math.max(this.radius.calculate(enchantmentLevel), 0.0F),
|
| 76 | this.createFire,
|
| 77 | this.blockInteraction,
|
| 78 | this.smallParticle,
|
| 79 | this.largeParticle,
|
| 80 | this.blockParticles,
|
| 81 | this.sound
|
| 82 | );
|
| 83 | }
|
| 84 |
|
| 85 | private @Nullable DamageSource getDamageSource(final Entity entity, final Vec3 position) {
|
| 86 | if (this.damageType.isEmpty()) {
|
| 87 | return null;
|
| 88 | } else {
|
| 89 | return this.attributeToUser ? new DamageSource(this.damageType.get(), entity) : new DamageSource(this.damageType.get(), position);
|
| 90 | }
|
| 91 | }
|
| 92 |
|
| 93 | @Override
|
| 94 | public MapCodec<ExplodeEffect> codec() {
|
| 95 | return CODEC;
|
| 96 | }
|
| 97 | }
|
| 98 |
|