BrewingStandBlockEntity .java

net.minecraft.world.level.block.entity.BrewingStandBlockEntity (class): members: BREWING_TIME_SECONDS, DATA_TOTAL_BREW_TIME, DATA_TOTAL_FUEL_USES, getUses.

net.minecraft.world.level.block.entityclass320 linescommonextends BaseContainerBlockEntity10.7 KB

Implements

API Surface (17)

int get(final int dataId)
void set(final int dataId, final int value)
int getCount()
Component getDefaultName()
int getContainerSize()
NonNullList<ItemStack> getItems()
void setItems(final NonNullList<ItemStack> items)
int getUses(final ServerLevel level, final BrewingFuel brewingFuel)
float getSpeedMultiplier(final ServerLevel level, final BrewingFuel brewingFuel)
void serverTick(final ServerLevel level, final BlockPos pos, final BlockState selfState, final BrewingStandBlockEntity entity)
void loadAdditional(final ValueInput input)
void saveAdditional(final ValueOutput output)
boolean canPlaceItem(final int slot, final ItemStack itemStack)
int[] getSlotsForFace(final Direction direction)
boolean canPlaceItemThroughFace(final int slot, final ItemStack itemStack, final @Nullable Direction direction)
boolean canTakeItemThroughFace(final int slot, final ItemStack itemStack, final Direction direction)
AbstractContainerMenu createMenu(final int containerId, final Inventory inventory)

Fields & Constants (26)

INGREDIENT_SLOT
FUEL_SLOT
SLOTS_FOR_UP
SLOTS_FOR_DOWN
SLOTS_FOR_SIDES
DATA_BREW_TIME
DATA_FUEL_USES
DATA_TOTAL_BREW_TIME
DATA_TOTAL_FUEL_USES
NUM_DATA_VALUES
DEFAULT_BREW_TIME
BREWING_TIME_SECONDS
DEFAULT_FUEL
DEFAULT_SPEED_MULTIPLIER
DEFAULT_FUEL_USES
DEFAULT_NAME
items
brewTime
totalBrewTime
lastPotionCount
ingredient
fuel
totalFuel
speedMultiplier
dataAccess
quickCheck

String literals (6)

"container.brewing"
"BrewTime"
"total_brew_time"
"Fuel"
"total_fuel"
"speed_multiplier"

Full Source

BrewingStandBlockEntity.java320 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.block.entity;
2
3
import java.util.Arrays;
4
import java.util.Optional;
5
import net.minecraft.core.BlockPos;
6
import net.minecraft.core.Direction;
7
import net.minecraft.core.NonNullList;
8
import net.minecraft.core.component.DataComponents;
9
import net.minecraft.network.chat.Component;
10
import net.minecraft.server.level.ServerLevel;
11
import net.minecraft.world.ContainerHelper;
12
import net.minecraft.world.Containers;
13
import net.minecraft.world.WorldlyContainer;
14
import net.minecraft.world.entity.player.Inventory;
15
import net.minecraft.world.inventory.AbstractContainerMenu;
16
import net.minecraft.world.inventory.BrewingStandMenu;
17
import net.minecraft.world.inventory.ContainerData;
18
import net.minecraft.world.item.Item;
19
import net.minecraft.world.item.ItemStack;
20
import net.minecraft.world.item.ItemStackTemplate;
21
import net.minecraft.world.item.Items;
22
import net.minecraft.world.item.component.BrewingFuel;
23
import net.minecraft.world.item.crafting.BrewingInput;
24
import net.minecraft.world.item.crafting.BrewingRecipe;
25
import net.minecraft.world.item.crafting.PotionIngredient;
26
import net.minecraft.world.item.crafting.RecipeAccess;
27
import net.minecraft.world.item.crafting.RecipeHolder;
28
import net.minecraft.world.item.crafting.RecipeManager;
29
import net.minecraft.world.item.crafting.RecipePropertySet;
30
import net.minecraft.world.item.crafting.RecipeType;
31
import net.minecraft.world.level.block.BrewingStandBlock;
32
import net.minecraft.world.level.block.state.BlockState;
33
import net.minecraft.world.level.storage.ValueInput;
34
import net.minecraft.world.level.storage.ValueOutput;
35
import org.jspecify.annotations.Nullable;
36
37
public class BrewingStandBlockEntity extends BaseContainerBlockEntity implements WorldlyContainer {
38
	private static final int INGREDIENT_SLOT = 3;
39
	private static final int FUEL_SLOT = 4;
40
	private static final int[] SLOTS_FOR_UP = new int[]{3};
41
	private static final int[] SLOTS_FOR_DOWN = new int[]{0, 1, 2, 3};
42
	private static final int[] SLOTS_FOR_SIDES = new int[]{0, 1, 2, 4};
43
	public static final int DATA_BREW_TIME = 0;
44
	public static final int DATA_FUEL_USES = 1;
45
	public static final int DATA_TOTAL_BREW_TIME = 2;
46
	public static final int DATA_TOTAL_FUEL_USES = 3;
47
	public static final int NUM_DATA_VALUES = 4;
48
	private static final int DEFAULT_BREW_TIME = 0;
49
	public static final int BREWING_TIME_SECONDS = 20;
50
	private static final int DEFAULT_FUEL = 0;
51
	private static final float DEFAULT_SPEED_MULTIPLIER = 1.0F;
52
	private static final int DEFAULT_FUEL_USES = 20;
53
	private static final Component DEFAULT_NAME = Component.translatable("container.brewing");
54
	private NonNullList<ItemStack> items = NonNullList.withSize(5, ItemStack.EMPTY);
55
	private int brewTime;
56
	private int totalBrewTime;
57
	private boolean[] lastPotionCount;
58
	private Item ingredient;
59
	private int fuel;
60
	private int totalFuel;
61
	private float speedMultiplier = 1.0F;
62
	protected final ContainerData dataAccess = new ContainerData() {
63
		@Override
64
		public int get(final int dataId) {
65
			return switch (dataId) {
66
				case 0 -> BrewingStandBlockEntity.this.brewTime;
67
				case 1 -> BrewingStandBlockEntity.this.fuel;
68
				case 2 -> BrewingStandBlockEntity.this.totalBrewTime;
69
				case 3 -> BrewingStandBlockEntity.this.totalFuel;
70
				default -> 0;
71
			};
72
		}
73
74
		@Override
75
		public void set(final int dataId, final int value) {
76
			switch (dataId) {
77
				case 0:
78
					BrewingStandBlockEntity.this.brewTime = value;
79
					break;
80
				case 1:
81
					BrewingStandBlockEntity.this.fuel = value;
82
					break;
83
				case 2:
84
					BrewingStandBlockEntity.this.totalBrewTime = value;
85
					break;
86
				case 3:
87
					BrewingStandBlockEntity.this.totalFuel = value;
88
			}
89
		}
90
91
		@Override
92
		public int getCount() {
93
			return 4;
94
		}
95
	};
96
	private final RecipeManager.CachedCheck<BrewingInput, BrewingRecipe> quickCheck = RecipeManager.createCheck(RecipeType.BREWING);
97
98
	public BrewingStandBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
99
		super(BlockEntityTypes.BREWING_STAND, worldPosition, blockState);
100
	}
101
102
	@Override
103
	protected Component getDefaultName() {
104
		return DEFAULT_NAME;
105
	}
106
107
	@Override
108
	public int getContainerSize() {
109
		return this.items.size();
110
	}
111
112
	@Override
113
	protected NonNullList<ItemStack> getItems() {
114
		return this.items;
115
	}
116
117
	@Override
118
	protected void setItems(final NonNullList<ItemStack> items) {
119
		this.items = items;
120
	}
121
122
	protected int getUses(final ServerLevel level, final BrewingFuel brewingFuel) {
123
		return brewingFuel.uses().get(this.getLootContext(level), 0);
124
	}
125
126
	protected float getSpeedMultiplier(final ServerLevel level, final BrewingFuel brewingFuel) {
127
		return brewingFuel.speedMultiplier().get(this.getLootContext(level), 1.0F);
128
	}
129
130
	public static void serverTick(final ServerLevel level, final BlockPos pos, final BlockState selfState, final BrewingStandBlockEntity entity) {
131
		NonNullList<ItemStack> items = entity.getItems();
132
		ItemStack fuel = items.get(4);
133
		BrewingFuel brewingFuel = fuel.get(DataComponents.BREWING_FUEL);
134
		if (entity.fuel <= 0 && brewingFuel != null) {
135
			entity.fuel = entity.getUses(level, brewingFuel);
136
			entity.totalFuel = entity.fuel;
137
			entity.speedMultiplier = entity.getSpeedMultiplier(level, brewingFuel);
138
			ItemStackTemplate fuelRemainder = fuel.getItem().getCraftingRemainder();
139
			ItemStack newFuel = fuel;
140
			fuel.shrink(1);
141
			if (fuelRemainder != null) {
142
				if (fuel.isEmpty()) {
143
					newFuel = fuelRemainder.create();
144
				} else {
145
					Containers.dropItemStack(level, pos.getX(), pos.getY(), pos.getZ(), fuelRemainder.create());
146
				}
147
			}
148
149
			items.set(4, newFuel);
150
			setChanged(level, pos, selfState);
151
		}
152
153
		boolean brewable = isBrewable(level, entity);
154
		boolean isBrewing = entity.brewTime > 0;
155
		ItemStack ingredient = entity.items.get(3);
156
		if (isBrewing) {
157
			entity.brewTime--;
158
			boolean isDoneBrewing = entity.brewTime == 0;
159
			if (isDoneBrewing && brewable) {
160
				doBrew(level, pos, entity);
161
			} else if (!brewable || !ingredient.is(entity.ingredient)) {
162
				entity.brewTime = 0;
163
			}
164
165
			setChanged(level, pos, selfState);
166
		} else if (brewable && entity.fuel > 0) {
167
			float speedMutliplier = entity.speedMultiplier > 0.0F ? entity.speedMultiplier : 1.0F;
168
			entity.fuel--;
169
			entity.brewTime = (int)Math.ceil(400.0F / speedMutliplier);
170
			entity.totalBrewTime = entity.brewTime;
171
			entity.ingredient = ingredient.getItem();
172
			setChanged(level, pos, selfState);
173
		}
174
175
		boolean[] newCount = entity.getPotionBits();
176
		if (!Arrays.equals(newCount, entity.lastPotionCount)) {
177
			entity.lastPotionCount = newCount;
178
			BlockState state = selfState;
179
			if (!(state.getBlock() instanceof BrewingStandBlock)) {
180
				return;
181
			}
182
183
			for (int i = 0; i < BrewingStandBlock.HAS_BOTTLE.length; i++) {
184
				state = state.setValue(BrewingStandBlock.HAS_BOTTLE[i], newCount[i]);
185
			}
186
187
			level.setBlock(pos, state, 2);
188
		}
189
	}
190
191
	private boolean[] getPotionBits() {
192
		boolean[] result = new boolean[3];
193
194
		for (int potion = 0; potion < 3; potion++) {
195
			if (!this.items.get(potion).isEmpty()) {
196
				result[potion] = true;
197
			}
198
		}
199
200
		return result;
201
	}
202
203
	private static boolean isBrewable(final ServerLevel serverLevel, final BrewingStandBlockEntity entity) {
204
		NonNullList<ItemStack> items = entity.getItems();
205
		ItemStack ingredient = items.get(3);
206
		if (ingredient.isEmpty()) {
207
			return false;
208
		}
209
210
		RecipeManager recipeManager = serverLevel.recipeAccess();
211
		if (!recipeManager.propertySet(RecipePropertySet.BREWING_REAGENTS).test(ingredient)) {
212
			return false;
213
		}
214
215
		for (int dest = 0; dest < 3; dest++) {
216
			ItemStack itemStack = items.get(dest);
217
			if (!itemStack.isEmpty()) {
218
				Optional<RecipeHolder<BrewingRecipe>> recipe = entity.quickCheck.getRecipeFor(new BrewingInput(itemStack, ingredient), serverLevel);
219
				if (recipe.isPresent()) {
220
					return true;
221
				}
222
			}
223
		}
224
225
		return false;
226
	}
227
228
	private static void doBrew(final ServerLevel level, final BlockPos pos, final BrewingStandBlockEntity entity) {
229
		NonNullList<ItemStack> items = entity.getItems();
230
		ItemStack ingredient = items.get(3);
231
232
		for (int dest = 0; dest < 3; dest++) {
233
			ItemStack container = items.get(dest);
234
			BrewingInput input = new BrewingInput(container, ingredient);
235
			Optional<RecipeHolder<BrewingRecipe>> recipe = entity.quickCheck.getRecipeFor(input, level);
236
			items.set(dest, recipe.isPresent() ? recipe.get().value().assemble(input) : container);
237
		}
238
239
		ItemStackTemplate remainder = ingredient.getItem().getCraftingRemainder();
240
		ingredient.shrink(1);
241
		if (remainder != null) {
242
			if (ingredient.isEmpty()) {
243
				ingredient = remainder.create();
244
			} else {
245
				Containers.dropItemStack(level, pos.getX(), pos.getY(), pos.getZ(), remainder.create());
246
			}
247
		}
248
249
		items.set(3, ingredient);
250
		level.levelEvent(1035, pos, 0);
251
	}
252
253
	@Override
254
	protected void loadAdditional(final ValueInput input) {
255
		super.loadAdditional(input);
256
		this.items = NonNullList.withSize(this.getContainerSize(), ItemStack.EMPTY);
257
		ContainerHelper.loadAllItems(input, this.items);
258
		this.brewTime = input.getIntOr("BrewTime", 0);
259
		this.totalBrewTime = input.getIntOr("total_brew_time", 400);
260
		if (this.brewTime > 0) {
261
			this.ingredient = this.items.get(3).getItem();
262
		}
263
264
		this.fuel = input.getIntOr("Fuel", 0);
265
		this.totalFuel = input.getIntOr("total_fuel", 20);
266
		this.speedMultiplier = input.getFloatOr("speed_multiplier", 1.0F);
267
	}
268
269
	@Override
270
	protected void saveAdditional(final ValueOutput output) {
271
		super.saveAdditional(output);
272
		output.putInt("BrewTime", this.brewTime);
273
		output.putInt("total_brew_time", this.totalBrewTime);
274
		ContainerHelper.saveAllItems(output, this.items);
275
		output.putInt("Fuel", this.fuel);
276
		output.putInt("total_fuel", this.totalFuel);
277
		output.putFloat("speed_multiplier", this.speedMultiplier);
278
	}
279
280
	@Override
281
	public boolean canPlaceItem(final int slot, final ItemStack itemStack) {
282
		if (slot == 4) {
283
			return itemStack.has(DataComponents.BREWING_FUEL);
284
		}
285
286
		if (this.level == null) {
287
			return false;
288
		}
289
290
		RecipeAccess recipeAccess = this.level.recipeAccess();
291
		return slot == 3
292
			? recipeAccess.propertySet(RecipePropertySet.BREWING_REAGENTS).test(itemStack)
293
			: PotionIngredient.isPotionInput(itemStack, recipeAccess) && this.getItem(slot).isEmpty();
294
	}
295
296
	@Override
297
	public int[] getSlotsForFace(final Direction direction) {
298
		if (direction == Direction.UP) {
299
			return SLOTS_FOR_UP;
300
		} else {
301
			return direction == Direction.DOWN ? SLOTS_FOR_DOWN : SLOTS_FOR_SIDES;
302
		}
303
	}
304
305
	@Override
306
	public boolean canPlaceItemThroughFace(final int slot, final ItemStack itemStack, final @Nullable Direction direction) {
307
		return this.canPlaceItem(slot, itemStack);
308
	}
309
310
	@Override
311
	public boolean canTakeItemThroughFace(final int slot, final ItemStack itemStack, final Direction direction) {
312
		return slot == 3 ? itemStack.is(Items.GLASS_BOTTLE) : true;
313
	}
314
315
	@Override
316
	protected AbstractContainerMenu createMenu(final int containerId, final Inventory inventory) {
317
		return new BrewingStandMenu(containerId, inventory, this, this.dataAccess);
318
	}
319
}
320

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