UseRemainder .java

net.minecraft.world.item.component.UseRemainder (record, 43 lines): members: convertInto, OnExtraCreatedRemainder, stackCountBeforeUsing, usedStack, CODEC.

net.minecraft.world.item.componentrecord43 linescommon1.3 KB

API Surface (2)

record UseRemainder(ItemStackTemplate convertInto)
ItemStack convertIntoRemainder(final ItemStack usedStack, final int stackCountBeforeUsing, final boolean hasInfiniteMaterials, final UseRemainder.OnExtraCreatedRemainder onExtraCreatedRemainder)

Fields & Constants (5)

CODEC
STREAM_CODEC
usedStack
stackCountBeforeUsing
hasInfiniteMaterials

Code Graph: Dependencies (4)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

UseRemainder.java43 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.item.component;
2
3
import com.mojang.serialization.Codec;
4
import net.minecraft.network.RegistryFriendlyByteBuf;
5
import net.minecraft.network.codec.StreamCodec;
6
import net.minecraft.world.item.ItemStack;
7
import net.minecraft.world.item.ItemStackTemplate;
8
9
public record UseRemainder(ItemStackTemplate convertInto) {
10
	public static final Codec<UseRemainder> CODEC = ItemStackTemplate.CODEC.xmap(UseRemainder::new, UseRemainder::convertInto);
11
	public static final StreamCodec<RegistryFriendlyByteBuf, UseRemainder> STREAM_CODEC = StreamCodec.composite(
12
		ItemStackTemplate.STREAM_CODEC, UseRemainder::convertInto, UseRemainder::new
13
	);
14
15
	public ItemStack convertIntoRemainder(
16
		final ItemStack usedStack,
17
		final int stackCountBeforeUsing,
18
		final boolean hasInfiniteMaterials,
19
		final UseRemainder.OnExtraCreatedRemainder onExtraCreatedRemainder
20
	) {
21
		if (hasInfiniteMaterials) {
22
			return usedStack;
23
		}
24
25
		if (usedStack.getCount() >= stackCountBeforeUsing) {
26
			return usedStack;
27
		}
28
29
		ItemStack remainderStack = this.convertInto.create();
30
		if (usedStack.isEmpty()) {
31
			return remainderStack;
32
		}
33
34
		onExtraCreatedRemainder.apply(remainderStack);
35
		return usedStack;
36
	}
37
38
	@FunctionalInterface
39
	public interface OnExtraCreatedRemainder {
40
		void apply(final ItemStack extraCreatedRemainder);
41
	}
42
}
43

Decompiled Minecraft 26.3 source of UseRemainder (net/minecraft/world/item/component/UseRemainder.java) with Mojang official mappings, stored verbatim on the site.