ReplaceDisk .java

net.minecraft.world.item.enchantment.effects.ReplaceDisk (record): members: radius, height, offset, predicate, blockState, triggerGameEvent, apply, codec.

net.minecraft.world.item.enchantment.effectsrecord61 linescommon2.7 KB

Implements

API Surface (2)

void apply(final ServerLevel serverLevel, final int enchantmentLevel, final EnchantedItemInUse item, final Entity entity, final Vec3 position)
MapCodec<ReplaceDisk> codec()

Fields & Constants (1)

CODEC

String literals (6)

"radius"
"height"
"offset"
"predicate"
"block_state"
"trigger_game_event"

Full Source

ReplaceDisk.java61 lines · Minecraft 26.3 · verbatim
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.util.Mth;
11
import net.minecraft.util.RandomSource;
12
import net.minecraft.world.entity.Entity;
13
import net.minecraft.world.item.enchantment.EnchantedItemInUse;
14
import net.minecraft.world.item.enchantment.LevelBasedValue;
15
import net.minecraft.world.level.gameevent.GameEvent;
16
import net.minecraft.world.level.levelgen.blockpredicates.BlockPredicate;
17
import net.minecraft.world.level.levelgen.feature.stateproviders.BlockStateProvider;
18
import net.minecraft.world.phys.Vec3;
19
20
public record ReplaceDisk(
21
	LevelBasedValue radius,
22
	LevelBasedValue height,
23
	Vec3i offset,
24
	Optional<BlockPredicate> predicate,
25
	Holder<BlockStateProvider> blockState,
26
	Optional<Holder<GameEvent>> triggerGameEvent
27
) implements EnchantmentEntityEffect {
28
	public static final MapCodec<ReplaceDisk> CODEC = RecordCodecBuilder.mapCodec(
29
		i -> i.group(
30
				LevelBasedValue.CODEC.fieldOf("radius").forGetter(ReplaceDisk::radius),
31
				LevelBasedValue.CODEC.fieldOf("height").forGetter(ReplaceDisk::height),
32
				Vec3i.CODEC.optionalFieldOf("offset", Vec3i.ZERO).forGetter(ReplaceDisk::offset),
33
				BlockPredicate.CODEC.optionalFieldOf("predicate").forGetter(ReplaceDisk::predicate),
34
				BlockStateProvider.CODEC.fieldOf("block_state").forGetter(ReplaceDisk::blockState),
35
				GameEvent.CODEC.optionalFieldOf("trigger_game_event").forGetter(ReplaceDisk::triggerGameEvent)
36
			)
37
			.apply(i, ReplaceDisk::new)
38
	);
39
40
	@Override
41
	public void apply(final ServerLevel serverLevel, final int enchantmentLevel, final EnchantedItemInUse item, final Entity entity, final Vec3 position) {
42
		BlockPos centerBlock = BlockPos.containing(position).offset(this.offset);
43
		RandomSource random = entity.getRandom();
44
		int dist = (int)this.radius.calculate(enchantmentLevel);
45
		int height = (int)this.height.calculate(enchantmentLevel);
46
47
		for (BlockPos pos : BlockPos.betweenClosed(centerBlock.offset(-dist, 0, -dist), centerBlock.offset(dist, Math.min(height - 1, 0), dist))) {
48
			if (pos.distToCenterSqr(position.x(), pos.getY() + 0.5, position.z()) < Mth.square(dist)
49
				&& this.predicate.map(p -> p.test(serverLevel, pos)).orElse(true)
50
				&& serverLevel.setBlockAndUpdate(pos, this.blockState.value().getState(serverLevel, random, pos))) {
51
				this.triggerGameEvent.ifPresent(event -> serverLevel.gameEvent(entity, (Holder<GameEvent>)event, pos));
52
			}
53
		}
54
	}
55
56
	@Override
57
	public MapCodec<ReplaceDisk> codec() {
58
		return CODEC;
59
	}
60
}
61

Decompiled Minecraft 26.3 source of ReplaceDisk (net/minecraft/world/item/enchantment/effects/ReplaceDisk.java) with Mojang official mappings, stored verbatim on the site.