CauldronInteractions .java

net.minecraft.core.cauldron.CauldronInteractions (class): members: addDefaultInteractions, emptyBucket, fillBucket, canFill, newItem, itemInHand, newState.

net.minecraft.core.cauldronclass281 linescommon11.1 KB

API Surface (4)

void bootStrap()
void addDefaultInteractions(final CauldronInteraction.Dispatcher interactionMap)
InteractionResult fillBucket(final BlockState state, final Level level, final BlockPos pos, final Player player, final InteractionHand hand, final ItemStack itemInHand, final ItemStack newItem, final Predicate<BlockState> canFill, final SoundEvent soundEvent)
InteractionResult emptyBucket(final Level level, final BlockPos pos, final Player player, final InteractionHand hand, final ItemStack itemInHand, final BlockState newState, final SoundEvent soundEvent)

Fields & Constants (15)

ID_MAPPER
CODEC
EMPTY
WATER
LAVA
POWDER_SNOW
state
level
pos
player
hand
itemInHand
newItem
canFill
newState

String literals (4)

"empty"
"water"
"lava"
"powder_snow"

Full Source

CauldronInteractions.java281 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.core.cauldron;
2
3
import com.mojang.serialization.Codec;
4
import java.util.function.Predicate;
5
import net.minecraft.core.BlockPos;
6
import net.minecraft.core.component.DataComponents;
7
import net.minecraft.sounds.SoundEvent;
8
import net.minecraft.sounds.SoundEvents;
9
import net.minecraft.sounds.SoundSource;
10
import net.minecraft.stats.Stats;
11
import net.minecraft.tags.FluidTags;
12
import net.minecraft.tags.ItemTags;
13
import net.minecraft.util.ExtraCodecs;
14
import net.minecraft.world.InteractionHand;
15
import net.minecraft.world.InteractionResult;
16
import net.minecraft.world.entity.player.Player;
17
import net.minecraft.world.item.Item;
18
import net.minecraft.world.item.ItemStack;
19
import net.minecraft.world.item.ItemUtils;
20
import net.minecraft.world.item.Items;
21
import net.minecraft.world.item.alchemy.PotionContents;
22
import net.minecraft.world.item.alchemy.Potions;
23
import net.minecraft.world.level.Level;
24
import net.minecraft.world.level.block.Block;
25
import net.minecraft.world.level.block.Blocks;
26
import net.minecraft.world.level.block.LayeredCauldronBlock;
27
import net.minecraft.world.level.block.ShulkerBoxBlock;
28
import net.minecraft.world.level.block.entity.BannerPatternLayers;
29
import net.minecraft.world.level.block.state.BlockState;
30
import net.minecraft.world.level.gameevent.GameEvent;
31
import net.minecraft.world.level.material.FluidState;
32
33
public class CauldronInteractions {
34
	private static final ExtraCodecs.LateBoundIdMapper<String, CauldronInteraction.Dispatcher> ID_MAPPER = new ExtraCodecs.LateBoundIdMapper<>();
35
	public static final Codec<CauldronInteraction.Dispatcher> CODEC = ID_MAPPER.codec(Codec.STRING);
36
	public static final CauldronInteraction.Dispatcher EMPTY = newDispatcher("empty");
37
	public static final CauldronInteraction.Dispatcher WATER = newDispatcher("water");
38
	public static final CauldronInteraction.Dispatcher LAVA = newDispatcher("lava");
39
	public static final CauldronInteraction.Dispatcher POWDER_SNOW = newDispatcher("powder_snow");
40
41
	private static CauldronInteraction.Dispatcher newDispatcher(final String name) {
42
		CauldronInteraction.Dispatcher result = new CauldronInteraction.Dispatcher();
43
		ID_MAPPER.put(name, result);
44
		return result;
45
	}
46
47
	public static void bootStrap() {
48
		addDefaultInteractions(EMPTY);
49
		EMPTY.put(Items.POTION, (var0, level, pos, player, hand, itemInHand) -> {
50
			PotionContents potion = itemInHand.get(DataComponents.POTION_CONTENTS);
51
			if (potion != null && potion.is(Potions.WATER)) {
52
				if (!level.isClientSide()) {
53
					Item usedItem = itemInHand.getItem();
54
					player.setItemInHand(hand, ItemUtils.createFilledResult(itemInHand, player, new ItemStack(Items.GLASS_BOTTLE)));
55
					player.awardStat(Stats.USE_CAULDRON);
56
					player.awardStat(Stats.ITEM_USED.get(usedItem));
57
					level.setBlockAndUpdate(pos, Blocks.WATER_CAULDRON.defaultBlockState());
58
					level.playSound(null, pos, SoundEvents.BOTTLE_EMPTY, SoundSource.BLOCKS, 1.0F, 1.0F);
59
					level.gameEvent(null, GameEvent.FLUID_PLACE, pos);
60
				}
61
62
				return InteractionResult.SUCCESS;
63
			} else {
64
				return InteractionResult.TRY_WITH_EMPTY_HAND;
65
			}
66
		});
67
		addDefaultInteractions(WATER);
68
		WATER.put(
69
			Items.BUCKET,
70
			(state, level, pos, player, hand, itemInHand) -> fillBucket(
71
				state, level, pos, player, hand, itemInHand, new ItemStack(Items.WATER_BUCKET), s -> s.getValue(LayeredCauldronBlock.LEVEL) == 3, SoundEvents.BUCKET_FILL
72
			)
73
		);
74
		WATER.put(Items.GLASS_BOTTLE, (state, level, pos, player, hand, itemInHand) -> {
75
			if (!level.isClientSide()) {
76
				Item usedItem = itemInHand.getItem();
77
				player.setItemInHand(hand, ItemUtils.createFilledResult(itemInHand, player, PotionContents.createItemStack(Items.POTION, Potions.WATER)));
78
				player.awardStat(Stats.USE_CAULDRON);
79
				player.awardStat(Stats.ITEM_USED.get(usedItem));
80
				LayeredCauldronBlock.lowerFillLevel(state, level, pos);
81
				level.playSound(null, pos, SoundEvents.BOTTLE_FILL, SoundSource.BLOCKS, 1.0F, 1.0F);
82
				level.gameEvent(null, GameEvent.FLUID_PICKUP, pos);
83
			}
84
85
			return InteractionResult.SUCCESS;
86
		});
87
		WATER.put(Items.POTION, (state, level, pos, player, hand, itemInHand) -> {
88
			if (state.getValue(LayeredCauldronBlock.LEVEL) == 3) {
89
				return InteractionResult.TRY_WITH_EMPTY_HAND;
90
			}
91
92
			PotionContents potion = itemInHand.get(DataComponents.POTION_CONTENTS);
93
			if (potion != null && potion.is(Potions.WATER)) {
94
				if (!level.isClientSide()) {
95
					player.setItemInHand(hand, ItemUtils.createFilledResult(itemInHand, player, new ItemStack(Items.GLASS_BOTTLE)));
96
					player.awardStat(Stats.USE_CAULDRON);
97
					player.awardStat(Stats.ITEM_USED.get(itemInHand.getItem()));
98
					level.setBlockAndUpdate(pos, state.cycle(LayeredCauldronBlock.LEVEL));
99
					level.playSound(null, pos, SoundEvents.BOTTLE_EMPTY, SoundSource.BLOCKS, 1.0F, 1.0F);
100
					level.gameEvent(null, GameEvent.FLUID_PLACE, pos);
101
				}
102
103
				return InteractionResult.SUCCESS;
104
			} else {
105
				return InteractionResult.TRY_WITH_EMPTY_HAND;
106
			}
107
		});
108
		WATER.put(ItemTags.CAULDRON_CAN_REMOVE_DYE, CauldronInteractions::dyedItemIteration);
109
		Items.BANNER.forEach(banner -> WATER.put(banner, CauldronInteractions::bannerInteraction));
110
		Items.DYED_SHULKER_BOX.forEach(dyedShulkerBox -> WATER.put(dyedShulkerBox, CauldronInteractions::shulkerBoxInteraction));
111
		LAVA.put(
112
			Items.BUCKET,
113
			(state, level, pos, player, hand, itemInHand) -> fillBucket(
114
				state, level, pos, player, hand, itemInHand, new ItemStack(Items.LAVA_BUCKET), var0x -> true, SoundEvents.BUCKET_FILL_LAVA
115
			)
116
		);
117
		addDefaultInteractions(LAVA);
118
		POWDER_SNOW.put(
119
			Items.BUCKET,
120
			(state, level, pos, player, hand, itemInHand) -> fillBucket(
121
				state,
122
				level,
123
				pos,
124
				player,
125
				hand,
126
				itemInHand,
127
				new ItemStack(Items.POWDER_SNOW_BUCKET),
128
				s -> s.getValue(LayeredCauldronBlock.LEVEL) == 3,
129
				SoundEvents.BUCKET_FILL_POWDER_SNOW
130
			)
131
		);
132
		addDefaultInteractions(POWDER_SNOW);
133
	}
134
135
	public static void addDefaultInteractions(final CauldronInteraction.Dispatcher interactionMap) {
136
		interactionMap.put(Items.LAVA_BUCKET, CauldronInteractions::fillLavaInteraction);
137
		interactionMap.put(Items.WATER_BUCKET, CauldronInteractions::fillWaterInteraction);
138
		interactionMap.put(Items.POWDER_SNOW_BUCKET, CauldronInteractions::fillPowderSnowInteraction);
139
	}
140
141
	public static InteractionResult fillBucket(
142
		final BlockState state,
143
		final Level level,
144
		final BlockPos pos,
145
		final Player player,
146
		final InteractionHand hand,
147
		final ItemStack itemInHand,
148
		final ItemStack newItem,
149
		final Predicate<BlockState> canFill,
150
		final SoundEvent soundEvent
151
	) {
152
		if (!canFill.test(state)) {
153
			return InteractionResult.TRY_WITH_EMPTY_HAND;
154
		}
155
156
		if (!level.isClientSide()) {
157
			Item itemUsed = itemInHand.getItem();
158
			player.setItemInHand(hand, ItemUtils.createFilledResult(itemInHand, player, newItem));
159
			player.awardStat(Stats.USE_CAULDRON);
160
			player.awardStat(Stats.ITEM_USED.get(itemUsed));
161
			level.setBlockAndUpdate(pos, Blocks.CAULDRON.defaultBlockState());
162
			level.playSound(null, pos, soundEvent, SoundSource.BLOCKS, 1.0F, 1.0F);
163
			level.gameEvent(null, GameEvent.FLUID_PICKUP, pos);
164
		}
165
166
		return InteractionResult.SUCCESS;
167
	}
168
169
	public static InteractionResult emptyBucket(
170
		final Level level,
171
		final BlockPos pos,
172
		final Player player,
173
		final InteractionHand hand,
174
		final ItemStack itemInHand,
175
		final BlockState newState,
176
		final SoundEvent soundEvent
177
	) {
178
		if (!level.isClientSide()) {
179
			Item itemUsed = itemInHand.getItem();
180
			player.setItemInHand(hand, ItemUtils.createFilledResult(itemInHand, player, new ItemStack(Items.BUCKET)));
181
			player.awardStat(Stats.FILL_CAULDRON);
182
			player.awardStat(Stats.ITEM_USED.get(itemUsed));
183
			level.setBlockAndUpdate(pos, newState);
184
			level.playSound(null, pos, soundEvent, SoundSource.BLOCKS, 1.0F, 1.0F);
185
			level.gameEvent(null, GameEvent.FLUID_PLACE, pos);
186
		}
187
188
		return InteractionResult.SUCCESS;
189
	}
190
191
	private static InteractionResult fillWaterInteraction(
192
		final BlockState state, final Level level, final BlockPos pos, final Player player, final InteractionHand hand, final ItemStack itemInHand
193
	) {
194
		return emptyBucket(
195
			level, pos, player, hand, itemInHand, Blocks.WATER_CAULDRON.defaultBlockState().setValue(LayeredCauldronBlock.LEVEL, 3), SoundEvents.BUCKET_EMPTY
196
		);
197
	}
198
199
	private static InteractionResult fillLavaInteraction(
200
		final BlockState state, final Level level, final BlockPos pos, final Player player, final InteractionHand hand, final ItemStack itemInHand
201
	) {
202
		return isUnderWater(level, pos)
203
			? InteractionResult.CONSUME
204
			: emptyBucket(level, pos, player, hand, itemInHand, Blocks.LAVA_CAULDRON.defaultBlockState(), SoundEvents.BUCKET_EMPTY_LAVA);
205
	}
206
207
	private static InteractionResult fillPowderSnowInteraction(
208
		final BlockState state, final Level level, final BlockPos pos, final Player player, final InteractionHand hand, final ItemStack itemInHand
209
	) {
210
		return isUnderWater(level, pos)
211
			? InteractionResult.CONSUME
212
			: emptyBucket(
213
				level,
214
				pos,
215
				player,
216
				hand,
217
				itemInHand,
218
				Blocks.POWDER_SNOW_CAULDRON.defaultBlockState().setValue(LayeredCauldronBlock.LEVEL, 3),
219
				SoundEvents.BUCKET_EMPTY_POWDER_SNOW
220
			);
221
	}
222
223
	private static InteractionResult shulkerBoxInteraction(
224
		final BlockState state, final Level level, final BlockPos pos, final Player player, final InteractionHand hand, final ItemStack itemInHand
225
	) {
226
		Block block = Block.byItem(itemInHand.getItem());
227
		if (!(block instanceof ShulkerBoxBlock)) {
228
			return InteractionResult.TRY_WITH_EMPTY_HAND;
229
		}
230
231
		if (!level.isClientSide()) {
232
			ItemStack cleanedShulkerBox = itemInHand.transmuteCopy(Blocks.SHULKER_BOX, 1);
233
			player.setItemInHand(hand, ItemUtils.createFilledResult(itemInHand, player, cleanedShulkerBox, false));
234
			player.awardStat(Stats.CLEAN_SHULKER_BOX);
235
			LayeredCauldronBlock.lowerFillLevel(state, level, pos);
236
		}
237
238
		return InteractionResult.SUCCESS;
239
	}
240
241
	private static InteractionResult bannerInteraction(
242
		final BlockState state, final Level level, final BlockPos pos, final Player player, final InteractionHand hand, final ItemStack itemInHand
243
	) {
244
		BannerPatternLayers patterns = itemInHand.getOrDefault(DataComponents.BANNER_PATTERNS, BannerPatternLayers.EMPTY);
245
		if (patterns.layers().isEmpty()) {
246
			return InteractionResult.TRY_WITH_EMPTY_HAND;
247
		}
248
249
		if (!level.isClientSide()) {
250
			ItemStack cleanedBanner = itemInHand.copyWithCount(1);
251
			cleanedBanner.set(DataComponents.BANNER_PATTERNS, patterns.removeLast());
252
			player.setItemInHand(hand, ItemUtils.createFilledResult(itemInHand, player, cleanedBanner, false));
253
			player.awardStat(Stats.CLEAN_BANNER);
254
			LayeredCauldronBlock.lowerFillLevel(state, level, pos);
255
		}
256
257
		return InteractionResult.SUCCESS;
258
	}
259
260
	private static InteractionResult dyedItemIteration(
261
		final BlockState state, final Level level, final BlockPos pos, final Player player, final InteractionHand hand, final ItemStack itemInHand
262
	) {
263
		if (!itemInHand.has(DataComponents.DYED_COLOR)) {
264
			return InteractionResult.TRY_WITH_EMPTY_HAND;
265
		}
266
267
		if (!level.isClientSide()) {
268
			itemInHand.remove(DataComponents.DYED_COLOR);
269
			player.awardStat(Stats.CLEAN_ARMOR);
270
			LayeredCauldronBlock.lowerFillLevel(state, level, pos);
271
		}
272
273
		return InteractionResult.SUCCESS;
274
	}
275
276
	private static boolean isUnderWater(final Level level, final BlockPos pos) {
277
		FluidState fluidState = level.getFluidState(pos.above());
278
		return fluidState.is(FluidTags.WATER);
279
	}
280
}
281

Decompiled Minecraft 26.3 source of CauldronInteractions (net/minecraft/core/cauldron/CauldronInteractions.java) with Mojang official mappings, stored verbatim on the site.