package net.minecraft.world.item.component; import com.mojang.serialization.Codec; import net.minecraft.network.RegistryFriendlyByteBuf; import net.minecraft.network.codec.StreamCodec; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStackTemplate; public record UseRemainder(ItemStackTemplate convertInto) { public static final Codec CODEC = ItemStackTemplate.CODEC.xmap(UseRemainder::new, UseRemainder::convertInto); public static final StreamCodec STREAM_CODEC = StreamCodec.composite( ItemStackTemplate.STREAM_CODEC, UseRemainder::convertInto, UseRemainder::new ); public ItemStack convertIntoRemainder( final ItemStack usedStack, final int stackCountBeforeUsing, final boolean hasInfiniteMaterials, final UseRemainder.OnExtraCreatedRemainder onExtraCreatedRemainder ) { if (hasInfiniteMaterials) { return usedStack; } else if (usedStack.getCount() >= stackCountBeforeUsing) { return usedStack; } else { ItemStack remainderStack = this.convertInto.create(); if (usedStack.isEmpty()) { return remainderStack; } else { onExtraCreatedRemainder.apply(remainderStack); return usedStack; } } } @FunctionalInterface public interface OnExtraCreatedRemainder { void apply(final ItemStack extraCreatedRemainder); } }