BrushableBlockEntity .java

net.minecraft.world.level.block.entity.BrushableBlockEntity (class, 248 lines): members: REQUIRED_BRUSHES_TO_BREAK, brushCountResetsAtTick, brushCount.

net.minecraft.world.level.block.entityclass248 linescommonextends BlockEntity8.9 KB

API Surface (8)

boolean brush(final long gameTime, final ServerLevel level, final LivingEntity user, final Direction direction, final ItemStack brush)
void checkReset(final ServerLevel level)
CompoundTag getUpdateTag(final HolderLookup.Provider registries)
ClientboundBlockEntityDataPacket getUpdatePacket()
void loadAdditional(final ValueInput input)
void saveAdditional(final ValueOutput output)
void setLootTable(final ResourceKey<LootTable> lootTable, final long seed)
ItemStack getItem()

Fields & Constants (15)

LOGGER
LOOT_TABLE_TAG
LOOT_TABLE_SEED_TAG
HIT_DIRECTION_TAG
ITEM_TAG
BRUSH_COOLDOWN_TICKS
BRUSH_RESET_TICKS
REQUIRED_BRUSHES_TO_BREAK
brushCount
brushCountResetsAtTick
coolDownEndsAtTick
item
hitDirection
lootTable
lootTableSeed

String literals (4)

"LootTable"
"LootTableSeed"
"hit_direction"
"item"

Full Source

BrushableBlockEntity.java248 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.block.entity;
2
3
import com.mojang.logging.LogUtils;
4
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
5
import java.util.Objects;
6
import net.minecraft.advancements.triggers.CriteriaTriggers;
7
import net.minecraft.core.BlockPos;
8
import net.minecraft.core.Direction;
9
import net.minecraft.core.HolderLookup;
10
import net.minecraft.nbt.CompoundTag;
11
import net.minecraft.nbt.NbtOps;
12
import net.minecraft.nbt.Tag;
13
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
14
import net.minecraft.resources.RegistryOps;
15
import net.minecraft.resources.ResourceKey;
16
import net.minecraft.server.level.ServerLevel;
17
import net.minecraft.server.level.ServerPlayer;
18
import net.minecraft.world.entity.EntityTypes;
19
import net.minecraft.world.entity.LivingEntity;
20
import net.minecraft.world.entity.item.ItemEntity;
21
import net.minecraft.world.item.ItemInstance;
22
import net.minecraft.world.item.ItemStack;
23
import net.minecraft.world.level.block.Block;
24
import net.minecraft.world.level.block.Blocks;
25
import net.minecraft.world.level.block.BrushableBlock;
26
import net.minecraft.world.level.block.state.BlockState;
27
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
28
import net.minecraft.world.level.storage.ValueInput;
29
import net.minecraft.world.level.storage.ValueOutput;
30
import net.minecraft.world.level.storage.loot.LootParams;
31
import net.minecraft.world.level.storage.loot.LootTable;
32
import net.minecraft.world.level.storage.loot.parameters.LootContextParamSets;
33
import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
34
import net.minecraft.world.phys.Vec3;
35
import org.jspecify.annotations.Nullable;
36
import org.slf4j.Logger;
37
38
public class BrushableBlockEntity extends BlockEntity {
39
	private static final Logger LOGGER = LogUtils.getLogger();
40
	private static final String LOOT_TABLE_TAG = "LootTable";
41
	private static final String LOOT_TABLE_SEED_TAG = "LootTableSeed";
42
	private static final String HIT_DIRECTION_TAG = "hit_direction";
43
	private static final String ITEM_TAG = "item";
44
	private static final int BRUSH_COOLDOWN_TICKS = 10;
45
	private static final int BRUSH_RESET_TICKS = 40;
46
	private static final int REQUIRED_BRUSHES_TO_BREAK = 10;
47
	private int brushCount;
48
	private long brushCountResetsAtTick;
49
	private long coolDownEndsAtTick;
50
	private ItemStack item = ItemStack.EMPTY;
51
	private @Nullable Direction hitDirection;
52
	private @Nullable ResourceKey<LootTable> lootTable;
53
	private long lootTableSeed;
54
55
	public BrushableBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
56
		super(BlockEntityTypes.BRUSHABLE_BLOCK, worldPosition, blockState);
57
	}
58
59
	public boolean brush(final long gameTime, final ServerLevel level, final LivingEntity user, final Direction direction, final ItemStack brush) {
60
		if (this.hitDirection == null) {
61
			this.hitDirection = direction;
62
		}
63
64
		this.brushCountResetsAtTick = gameTime + 40L;
65
		if (gameTime < this.coolDownEndsAtTick) {
66
			return false;
67
		}
68
69
		this.coolDownEndsAtTick = gameTime + 10L;
70
		this.unpackLootTable(level, user, brush);
71
		int previousCompletionState = this.getCompletionState();
72
		if (++this.brushCount >= 10) {
73
			this.brushingCompleted(level, user, brush);
74
			return true;
75
		}
76
77
		level.scheduleTick(this.getBlockPos(), this.getBlockState().getBlock(), 2);
78
		int completionState = this.getCompletionState();
79
		if (previousCompletionState != completionState) {
80
			BlockState previousState = this.getBlockState();
81
			BlockState state = previousState.setValue(BlockStateProperties.DUSTED, completionState);
82
			level.setBlockAndUpdate(this.getBlockPos(), state);
83
		}
84
85
		return false;
86
	}
87
88
	private void unpackLootTable(final ServerLevel level, final LivingEntity user, final ItemInstance brush) {
89
		if (this.lootTable != null) {
90
			LootTable lootTable = level.getServer().reloadableRegistries().getLootTable(this.lootTable);
91
			if (user instanceof ServerPlayer serverPlayer) {
92
				CriteriaTriggers.GENERATE_LOOT.trigger(serverPlayer, this.lootTable);
93
			}
94
95
			LootParams params = new LootParams.Builder(level)
96
				.withParameter(LootContextParams.ORIGIN, Vec3.atCenterOf(this.worldPosition))
97
				.withLuck(user.getLuck())
98
				.withParameter(LootContextParams.THIS_ENTITY, user)
99
				.withParameter(LootContextParams.TOOL, brush)
100
				.create(LootContextParamSets.ARCHAEOLOGY);
101
			ObjectArrayList<ItemStack> loot = lootTable.getRandomItems(params, this.lootTableSeed);
102
103
			this.item = switch (loot.size()) {
104
				case 0 -> ItemStack.EMPTY;
105
				case 1 -> (ItemStack)loot.getFirst();
106
				default -> {
107
					LOGGER.warn("Expected max 1 loot from loot table {}, but got {}", this.lootTable.identifier(), loot.size());
108
					yield loot.getFirst();
109
				}
110
			};
111
			this.lootTable = null;
112
			this.setChanged();
113
		}
114
	}
115
116
	private void brushingCompleted(final ServerLevel level, final LivingEntity user, final ItemStack brush) {
117
		this.dropContent(level, user, brush);
118
		BlockState blockState = this.getBlockState();
119
		level.levelEvent(3008, this.getBlockPos(), Block.getId(blockState));
120
		Block turnsInto;
121
		if (this.getBlockState().getBlock() instanceof BrushableBlock brushableBlock) {
122
			turnsInto = brushableBlock.getTurnsInto();
123
		} else {
124
			turnsInto = Blocks.AIR;
125
		}
126
127
		level.setBlockAndUpdate(this.worldPosition, turnsInto.defaultBlockState());
128
	}
129
130
	private void dropContent(final ServerLevel level, final LivingEntity user, final ItemStack brush) {
131
		this.unpackLootTable(level, user, brush);
132
		if (!this.item.isEmpty()) {
133
			double size = EntityTypes.ITEM.getWidth();
134
			double centerRange = 1.0 - size;
135
			double halfSize = size / 2.0;
136
			Direction dropDirection = Objects.requireNonNullElse(this.hitDirection, Direction.UP);
137
			BlockPos dropPos = this.worldPosition.relative(dropDirection, 1);
138
			double xo = dropPos.getX() + 0.5 * centerRange + halfSize;
139
			double yo = dropPos.getY() + 0.5 + EntityTypes.ITEM.getHeight() / 2.0F;
140
			double zo = dropPos.getZ() + 0.5 * centerRange + halfSize;
141
			ItemEntity entity = new ItemEntity(level, xo, yo, zo, this.item.split(level.getRandom().nextInt(21) + 10));
142
			entity.setDeltaMovement(Vec3.ZERO);
143
			level.addFreshEntity(entity);
144
			this.item = ItemStack.EMPTY;
145
		}
146
	}
147
148
	public void checkReset(final ServerLevel level) {
149
		if (this.brushCount != 0 && level.getGameTime() >= this.brushCountResetsAtTick) {
150
			int previousCompletionState = this.getCompletionState();
151
			this.brushCount = Math.max(0, this.brushCount - 2);
152
			int completionState = this.getCompletionState();
153
			if (previousCompletionState != completionState) {
154
				level.setBlockAndUpdate(this.getBlockPos(), this.getBlockState().setValue(BlockStateProperties.DUSTED, completionState));
155
			}
156
157
			int retractionSpeed = 4;
158
			this.brushCountResetsAtTick = level.getGameTime() + 4L;
159
		}
160
161
		if (this.brushCount == 0) {
162
			this.hitDirection = null;
163
			this.brushCountResetsAtTick = 0L;
164
			this.coolDownEndsAtTick = 0L;
165
		} else {
166
			level.scheduleTick(this.getBlockPos(), this.getBlockState().getBlock(), 2);
167
		}
168
	}
169
170
	private boolean tryLoadLootTable(final ValueInput input) {
171
		this.lootTable = input.read("LootTable", LootTable.KEY_CODEC).orElse(null);
172
		this.lootTableSeed = input.getLongOr("LootTableSeed", 0L);
173
		return this.lootTable != null;
174
	}
175
176
	private boolean trySaveLootTable(final ValueOutput base) {
177
		if (this.lootTable == null) {
178
			return false;
179
		}
180
181
		base.store("LootTable", LootTable.KEY_CODEC, this.lootTable);
182
		if (this.lootTableSeed != 0L) {
183
			base.putLong("LootTableSeed", this.lootTableSeed);
184
		}
185
186
		return true;
187
	}
188
189
	@Override
190
	public CompoundTag getUpdateTag(final HolderLookup.Provider registries) {
191
		CompoundTag tag = super.getUpdateTag(registries);
192
		tag.storeNullable("hit_direction", Direction.LEGACY_ID_CODEC, this.hitDirection);
193
		if (!this.item.isEmpty()) {
194
			RegistryOps<Tag> ops = registries.createSerializationContext(NbtOps.INSTANCE);
195
			tag.store("item", ItemStack.CODEC, ops, this.item);
196
		}
197
198
		return tag;
199
	}
200
201
	public ClientboundBlockEntityDataPacket getUpdatePacket() {
202
		return ClientboundBlockEntityDataPacket.create(this);
203
	}
204
205
	@Override
206
	protected void loadAdditional(final ValueInput input) {
207
		super.loadAdditional(input);
208
		if (!this.tryLoadLootTable(input)) {
209
			this.item = input.read("item", ItemStack.CODEC).orElse(ItemStack.EMPTY);
210
		} else {
211
			this.item = ItemStack.EMPTY;
212
		}
213
214
		this.hitDirection = input.read("hit_direction", Direction.LEGACY_ID_CODEC).orElse(null);
215
	}
216
217
	@Override
218
	protected void saveAdditional(final ValueOutput output) {
219
		super.saveAdditional(output);
220
		if (!this.trySaveLootTable(output) && !this.item.isEmpty()) {
221
			output.store("item", ItemStack.CODEC, this.item);
222
		}
223
	}
224
225
	public void setLootTable(final ResourceKey<LootTable> lootTable, final long seed) {
226
		this.lootTable = lootTable;
227
		this.lootTableSeed = seed;
228
	}
229
230
	private int getCompletionState() {
231
		if (this.brushCount == 0) {
232
			return 0;
233
		} else if (this.brushCount < 3) {
234
			return 1;
235
		} else {
236
			return this.brushCount < 6 ? 2 : 3;
237
		}
238
	}
239
240
	public @Nullable Direction getHitDirection() {
241
		return this.hitDirection;
242
	}
243
244
	public ItemStack getItem() {
245
		return this.item;
246
	}
247
}
248

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