minecraft-src/net/minecraft/world/item/component/UseRemainder.java
2025-07-04 02:49:36 +03:00

49 lines
1.5 KiB
Java

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;
public record UseRemainder(ItemStack convertInto) {
public static final Codec<UseRemainder> CODEC = ItemStack.CODEC.xmap(UseRemainder::new, UseRemainder::convertInto);
public static final StreamCodec<RegistryFriendlyByteBuf, UseRemainder> STREAM_CODEC = StreamCodec.composite(
ItemStack.STREAM_CODEC, UseRemainder::convertInto, UseRemainder::new
);
public ItemStack convertIntoRemainder(ItemStack stack, int count, boolean hasInfiniteMaterials, UseRemainder.OnExtraCreatedRemainder onExtraCreated) {
if (hasInfiniteMaterials) {
return stack;
} else if (stack.getCount() >= count) {
return stack;
} else {
ItemStack itemStack = this.convertInto.copy();
if (stack.isEmpty()) {
return itemStack;
} else {
onExtraCreated.apply(itemStack);
return stack;
}
}
}
public boolean equals(Object object) {
if (this == object) {
return true;
} else if (object != null && this.getClass() == object.getClass()) {
UseRemainder useRemainder = (UseRemainder)object;
return ItemStack.matches(this.convertInto, useRemainder.convertInto);
} else {
return false;
}
}
public int hashCode() {
return ItemStack.hashItemAndComponents(this.convertInto);
}
@FunctionalInterface
public interface OnExtraCreatedRemainder {
void apply(ItemStack itemStack);
}
}