| 1 | package net.minecraft.network;
|
| 2 |
|
| 3 | import com.mojang.datafixers.DataFixUtils;
|
| 4 | import java.util.Optional;
|
| 5 | import net.minecraft.core.Holder;
|
| 6 | import net.minecraft.core.registries.Registries;
|
| 7 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 8 | import net.minecraft.network.codec.StreamCodec;
|
| 9 | import net.minecraft.world.item.Item;
|
| 10 | import net.minecraft.world.item.ItemStack;
|
| 11 |
|
| 12 | public interface HashedStack {
|
| 13 | HashedStack EMPTY = new HashedStack() {
|
| 14 | @Override
|
| 15 | public String toString() {
|
| 16 | return "<empty>";
|
| 17 | }
|
| 18 |
|
| 19 | @Override
|
| 20 | public boolean matches(final ItemStack stack, final HashedPatchMap.HashGenerator hasher) {
|
| 21 | return stack.isEmpty();
|
| 22 | }
|
| 23 | };
|
| 24 | StreamCodec<RegistryFriendlyByteBuf, HashedStack> STREAM_CODEC = ByteBufCodecs.optional(HashedStack.ActualItem.STREAM_CODEC)
|
| 25 | .map(
|
| 26 | actualItem -> DataFixUtils.orElse((Optional<? extends HashedStack>)actualItem, EMPTY),
|
| 27 | hashedStack -> hashedStack instanceof HashedStack.ActualItem actualItem ? Optional.of(actualItem) : Optional.empty()
|
| 28 | );
|
| 29 |
|
| 30 | boolean matches(ItemStack stack, HashedPatchMap.HashGenerator hasher);
|
| 31 |
|
| 32 | static HashedStack create(final ItemStack itemStack, final HashedPatchMap.HashGenerator hasher) {
|
| 33 | return itemStack.isEmpty()
|
| 34 | ? EMPTY
|
| 35 | : new HashedStack.ActualItem(itemStack.typeHolder(), itemStack.getCount(), HashedPatchMap.create(itemStack.getComponentsPatch(), hasher));
|
| 36 | }
|
| 37 |
|
| 38 | record ActualItem(Holder<Item> item, int count, HashedPatchMap components) implements HashedStack {
|
| 39 | public static final StreamCodec<RegistryFriendlyByteBuf, HashedStack.ActualItem> STREAM_CODEC = StreamCodec.composite(
|
| 40 | ByteBufCodecs.holderRegistry(Registries.ITEM),
|
| 41 | HashedStack.ActualItem::item,
|
| 42 | ByteBufCodecs.VAR_INT,
|
| 43 | HashedStack.ActualItem::count,
|
| 44 | HashedPatchMap.STREAM_CODEC,
|
| 45 | HashedStack.ActualItem::components,
|
| 46 | HashedStack.ActualItem::new
|
| 47 | );
|
| 48 |
|
| 49 | @Override
|
| 50 | public boolean matches(final ItemStack itemStack, final HashedPatchMap.HashGenerator hasher) {
|
| 51 | if (this.count != itemStack.getCount()) {
|
| 52 | return false;
|
| 53 | } else {
|
| 54 | return !this.item.equals(itemStack.typeHolder()) ? false : this.components.matches(itemStack.getComponentsPatch(), hasher);
|
| 55 | }
|
| 56 | }
|
| 57 | }
|
| 58 | }
|
| 59 |
|