BlockDataAccessor .java

net.minecraft.server.commands.data.BlockDataAccessor (class): members: ERROR_NOT_A_BLOCK_ENTITY, getModifiedSuccess, getPrintSuccess, PROVIDER, setData.

net.minecraft.server.commands.dataclass78 linesserver3.0 KB

Implements

API Surface (5)

void setData(final CompoundTag tag)
CompoundTag getData()
Component getModifiedSuccess()
Component getPrintSuccess(final Tag data)
Component getPrintSuccess(final NbtPathArgument.NbtPath path, final double scale, final int value)

Fields & Constants (5)

LOGGER
ERROR_NOT_A_BLOCK_ENTITY
PROVIDER
entity
pos

String literals (5)

"commands.data.block.invalid"
"block"
"commands.data.block.modified"
"commands.data.block.query"
"commands.data.block.get"

Full Source

BlockDataAccessor.java78 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.server.commands.data;
2
3
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
4
import com.mojang.logging.LogUtils;
5
import java.util.Locale;
6
import net.minecraft.commands.Commands;
7
import net.minecraft.commands.arguments.NbtPathArgument;
8
import net.minecraft.commands.arguments.coordinates.BlockPosArgument;
9
import net.minecraft.core.BlockPos;
10
import net.minecraft.nbt.CompoundTag;
11
import net.minecraft.nbt.NbtUtils;
12
import net.minecraft.nbt.Tag;
13
import net.minecraft.network.chat.Component;
14
import net.minecraft.server.commands.ArgProvider;
15
import net.minecraft.util.ProblemReporter;
16
import net.minecraft.world.level.block.entity.BlockEntity;
17
import net.minecraft.world.level.block.state.BlockState;
18
import net.minecraft.world.level.storage.TagValueInput;
19
import org.slf4j.Logger;
20
21
public class BlockDataAccessor implements DataAccessor {
22
	private static final Logger LOGGER = LogUtils.getLogger();
23
	private static final SimpleCommandExceptionType ERROR_NOT_A_BLOCK_ENTITY = new SimpleCommandExceptionType(
24
		Component.translatable("commands.data.block.invalid")
25
	);
26
	public static final ArgProvider.Factory<DataAccessor> PROVIDER = arg -> ArgProvider.create(
27
		"block", () -> Commands.argument(arg, BlockPosArgument.blockPos()), context -> {
28
			BlockPos pos = BlockPosArgument.getLoadedBlockPos(context, arg);
29
			BlockEntity entity = context.getSource().getLevel().getBlockEntity(pos);
30
			if (entity == null) {
31
				throw ERROR_NOT_A_BLOCK_ENTITY.create();
32
			} else {
33
				return new BlockDataAccessor(entity, pos);
34
			}
35
		}
36
	);
37
	private final BlockEntity entity;
38
	private final BlockPos pos;
39
40
	public BlockDataAccessor(final BlockEntity entity, final BlockPos pos) {
41
		this.entity = entity;
42
		this.pos = pos;
43
	}
44
45
	@Override
46
	public void setData(final CompoundTag tag) {
47
		BlockState state = this.entity.getLevel().getBlockState(this.pos);
48
49
		try (ProblemReporter.ScopedCollector reporter = new ProblemReporter.ScopedCollector(this.entity.problemPath(), LOGGER)) {
50
			this.entity.loadWithComponents(TagValueInput.create(reporter, this.entity.getLevel().registryAccess(), tag));
51
			this.entity.setChanged();
52
			this.entity.getLevel().sendBlockUpdated(this.pos, state, state, 3);
53
		}
54
	}
55
56
	@Override
57
	public CompoundTag getData() {
58
		return this.entity.saveWithFullMetadata(this.entity.getLevel().registryAccess());
59
	}
60
61
	@Override
62
	public Component getModifiedSuccess() {
63
		return Component.translatable("commands.data.block.modified", this.pos.getX(), this.pos.getY(), this.pos.getZ());
64
	}
65
66
	@Override
67
	public Component getPrintSuccess(final Tag data) {
68
		return Component.translatable("commands.data.block.query", this.pos.getX(), this.pos.getY(), this.pos.getZ(), NbtUtils.toPrettyComponent(data));
69
	}
70
71
	@Override
72
	public Component getPrintSuccess(final NbtPathArgument.NbtPath path, final double scale, final int value) {
73
		return Component.translatable(
74
			"commands.data.block.get", path.asString(), this.pos.getX(), this.pos.getY(), this.pos.getZ(), String.format(Locale.ROOT, "%.2f", scale), value
75
		);
76
	}
77
}
78

Decompiled Minecraft 26.3 source of BlockDataAccessor (net/minecraft/server/commands/data/BlockDataAccessor.java) with Mojang official mappings, stored verbatim on the site.