SetBlockProperties .java

net.minecraft.world.item.enchantment.effects.SetBlockProperties (record, 47 lines): members: properties, offset, triggerGameEvent, apply, codec, CODEC.

net.minecraft.world.item.enchantment.effectsrecord47 linescommon2.0 KB

Implements

API Surface (2)

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

Fields & Constants (1)

CODEC

String literals (3)

"properties"
"offset"
"trigger_game_event"

Full Source

SetBlockProperties.java47 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.world.entity.Entity;
11
import net.minecraft.world.item.component.BlockItemStateProperties;
12
import net.minecraft.world.item.enchantment.EnchantedItemInUse;
13
import net.minecraft.world.level.block.state.BlockState;
14
import net.minecraft.world.level.gameevent.GameEvent;
15
import net.minecraft.world.phys.Vec3;
16
17
public record SetBlockProperties(BlockItemStateProperties properties, Vec3i offset, Optional<Holder<GameEvent>> triggerGameEvent)
18
	implements EnchantmentEntityEffect {
19
	public static final MapCodec<SetBlockProperties> CODEC = RecordCodecBuilder.mapCodec(
20
		i -> i.group(
21
				BlockItemStateProperties.CODEC.fieldOf("properties").forGetter(SetBlockProperties::properties),
22
				Vec3i.CODEC.optionalFieldOf("offset", Vec3i.ZERO).forGetter(SetBlockProperties::offset),
23
				GameEvent.CODEC.optionalFieldOf("trigger_game_event").forGetter(SetBlockProperties::triggerGameEvent)
24
			)
25
			.apply(i, SetBlockProperties::new)
26
	);
27
28
	public SetBlockProperties(final BlockItemStateProperties properties) {
29
		this(properties, Vec3i.ZERO, Optional.of(GameEvent.BLOCK_CHANGE));
30
	}
31
32
	@Override
33
	public void apply(final ServerLevel serverLevel, final int enchantmentLevel, final EnchantedItemInUse item, final Entity entity, final Vec3 position) {
34
		BlockPos blockPos = BlockPos.containing(position).offset(this.offset);
35
		BlockState state = entity.level().getBlockState(blockPos);
36
		BlockState modified = this.properties.apply(state);
37
		if (state != modified && entity.level().setBlockAndUpdate(blockPos, modified)) {
38
			this.triggerGameEvent.ifPresent(event -> serverLevel.gameEvent(entity, (Holder<GameEvent>)event, blockPos));
39
		}
40
	}
41
42
	@Override
43
	public MapCodec<SetBlockProperties> codec() {
44
		return CODEC;
45
	}
46
}
47

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