TestBlockEntity .java

net.minecraft.world.level.block.entity.TestBlockEntity (class): members: DEFAULT_MESSAGE, hasTriggered, triggered, setMode, setPowered, powered, getMode.

net.minecraft.world.level.block.entityclass128 linescommonextends BlockEntity3.6 KB

API Surface (13)

void saveAdditional(final ValueOutput output)
void loadAdditional(final ValueInput input)
CompoundTag getUpdateTag(final HolderLookup.Provider registries)
boolean isPowered()
void setPowered(final boolean powered)
TestBlockMode getMode()
void setMode(final TestBlockMode mode)
void reset()
void trigger()
void log()
boolean hasTriggered()
String getMessage()
void setMessage(final String message)

Fields & Constants (7)

LOGGER
DEFAULT_MESSAGE
DEFAULT_POWERED
mode
message
powered
triggered

String literals (2)

"mode"
"powered"

Full Source

TestBlockEntity.java128 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.block.entity;
2
3
import com.mojang.logging.LogUtils;
4
import net.minecraft.core.BlockPos;
5
import net.minecraft.core.HolderLookup;
6
import net.minecraft.nbt.CompoundTag;
7
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
8
import net.minecraft.world.level.block.Block;
9
import net.minecraft.world.level.block.Blocks;
10
import net.minecraft.world.level.block.TestBlock;
11
import net.minecraft.world.level.block.state.BlockState;
12
import net.minecraft.world.level.block.state.properties.TestBlockMode;
13
import net.minecraft.world.level.storage.ValueInput;
14
import net.minecraft.world.level.storage.ValueOutput;
15
import org.jspecify.annotations.Nullable;
16
import org.slf4j.Logger;
17
18
public class TestBlockEntity extends BlockEntity {
19
	private static final Logger LOGGER = LogUtils.getLogger();
20
	private static final String DEFAULT_MESSAGE = "";
21
	private static final boolean DEFAULT_POWERED = false;
22
	private TestBlockMode mode;
23
	private String message = "";
24
	private boolean powered = false;
25
	private boolean triggered;
26
27
	public TestBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
28
		super(BlockEntityTypes.TEST_BLOCK, worldPosition, blockState);
29
		this.mode = blockState.getValue(TestBlock.MODE);
30
	}
31
32
	@Override
33
	protected void saveAdditional(final ValueOutput output) {
34
		output.store("mode", TestBlockMode.CODEC, this.mode);
35
		output.putString("message", this.message);
36
		output.putBoolean("powered", this.powered);
37
	}
38
39
	@Override
40
	protected void loadAdditional(final ValueInput input) {
41
		this.mode = input.read("mode", TestBlockMode.CODEC).orElse(TestBlockMode.FAIL);
42
		this.message = input.getStringOr("message", "");
43
		this.powered = input.getBooleanOr("powered", false);
44
	}
45
46
	private void updateBlockState() {
47
		if (this.level != null) {
48
			BlockPos pos = this.getBlockPos();
49
			BlockState blockState = this.level.getBlockState(pos);
50
			if (blockState.is(Blocks.TEST_BLOCK)) {
51
				this.level.setBlock(pos, blockState.setValue(TestBlock.MODE, this.mode), 2);
52
			}
53
		}
54
	}
55
56
	public @Nullable ClientboundBlockEntityDataPacket getUpdatePacket() {
57
		return ClientboundBlockEntityDataPacket.create(this);
58
	}
59
60
	@Override
61
	public CompoundTag getUpdateTag(final HolderLookup.Provider registries) {
62
		return this.saveCustomOnly(registries);
63
	}
64
65
	public boolean isPowered() {
66
		return this.powered;
67
	}
68
69
	public void setPowered(final boolean powered) {
70
		this.powered = powered;
71
	}
72
73
	public TestBlockMode getMode() {
74
		return this.mode;
75
	}
76
77
	public void setMode(final TestBlockMode mode) {
78
		this.mode = mode;
79
		this.updateBlockState();
80
	}
81
82
	private Block getBlockType() {
83
		return this.getBlockState().getBlock();
84
	}
85
86
	public void reset() {
87
		this.triggered = false;
88
		if (this.mode == TestBlockMode.START && this.level != null) {
89
			this.setPowered(false);
90
			this.level.updateNeighborsAt(this.getBlockPos(), this.getBlockType());
91
		}
92
	}
93
94
	public void trigger() {
95
		if (this.mode == TestBlockMode.START && this.level != null) {
96
			this.setPowered(true);
97
			BlockPos pos = this.getBlockPos();
98
			this.level.updateNeighborsAt(pos, this.getBlockType());
99
			this.level.getBlockTicks().willTickThisTick(pos, this.getBlockType());
100
			this.log();
101
		} else {
102
			if (this.mode == TestBlockMode.LOG) {
103
				this.log();
104
			}
105
106
			this.triggered = true;
107
		}
108
	}
109
110
	public void log() {
111
		if (!this.message.isBlank()) {
112
			LOGGER.info("Test {} (at {}): {}", this.mode.getSerializedName(), this.getBlockPos(), this.message);
113
		}
114
	}
115
116
	public boolean hasTriggered() {
117
		return this.triggered;
118
	}
119
120
	public String getMessage() {
121
		return this.message;
122
	}
123
124
	public void setMessage(final String message) {
125
		this.message = message;
126
	}
127
}
128

Decompiled Minecraft 26.3 source of TestBlockEntity (net/minecraft/world/level/block/entity/TestBlockEntity.java) with Mojang official mappings, stored verbatim on the site.