| 1 | package net.minecraft.world.item.enchantment.effects;
|
| 2 |
|
| 3 | import com.mojang.serialization.MapCodec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import java.util.Optional;
|
| 6 | import net.minecraft.core.BlockPos;
|
| 7 | import net.minecraft.core.Holder;
|
| 8 | import net.minecraft.core.Vec3i;
|
| 9 | import net.minecraft.server.level.ServerLevel;
|
| 10 | import net.minecraft.world.entity.Entity;
|
| 11 | import net.minecraft.world.item.enchantment.EnchantedItemInUse;
|
| 12 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 13 | import net.minecraft.world.level.levelgen.blockpredicates.BlockPredicate;
|
| 14 | import net.minecraft.world.level.levelgen.feature.stateproviders.BlockStateProvider;
|
| 15 | import net.minecraft.world.phys.Vec3;
|
| 16 |
|
| 17 | public record ReplaceBlock(Vec3i offset, Optional<BlockPredicate> predicate, Holder<BlockStateProvider> blockState, Optional<Holder<GameEvent>> triggerGameEvent)
|
| 18 | implements EnchantmentEntityEffect {
|
| 19 | public static final MapCodec<ReplaceBlock> CODEC = RecordCodecBuilder.mapCodec(
|
| 20 | i -> i.group(
|
| 21 | Vec3i.CODEC.optionalFieldOf("offset", Vec3i.ZERO).forGetter(ReplaceBlock::offset),
|
| 22 | BlockPredicate.CODEC.optionalFieldOf("predicate").forGetter(ReplaceBlock::predicate),
|
| 23 | BlockStateProvider.CODEC.fieldOf("block_state").forGetter(ReplaceBlock::blockState),
|
| 24 | GameEvent.CODEC.optionalFieldOf("trigger_game_event").forGetter(ReplaceBlock::triggerGameEvent)
|
| 25 | )
|
| 26 | .apply(i, ReplaceBlock::new)
|
| 27 | );
|
| 28 |
|
| 29 | @Override
|
| 30 | public void apply(final ServerLevel serverLevel, final int enchantmentLevel, final EnchantedItemInUse item, final Entity entity, final Vec3 position) {
|
| 31 | BlockPos pos = BlockPos.containing(position).offset(this.offset);
|
| 32 | if (this.predicate.map(p -> p.test(serverLevel, pos)).orElse(true)
|
| 33 | && serverLevel.setBlockAndUpdate(pos, this.blockState.value().getState(serverLevel, entity.getRandom(), pos))) {
|
| 34 | this.triggerGameEvent.ifPresent(event -> serverLevel.gameEvent(entity, (Holder<GameEvent>)event, pos));
|
| 35 | }
|
| 36 | }
|
| 37 |
|
| 38 | @Override
|
| 39 | public MapCodec<ReplaceBlock> codec() {
|
| 40 | return CODEC;
|
| 41 | }
|
| 42 | }
|
| 43 |
|