/* * Decompiled with CFR 0.152. * * Could not load the following classes: * com.mojang.datafixers.util.Either * com.mojang.serialization.Codec * com.mojang.serialization.DataResult */ package net.minecraft.world.item; import com.mojang.datafixers.util.Either; import com.mojang.serialization.Codec; import com.mojang.serialization.DataResult; import java.util.Optional; import java.util.function.Function; import net.minecraft.core.Holder; import net.minecraft.core.HolderLookup; import net.minecraft.core.Registry; import net.minecraft.network.RegistryFriendlyByteBuf; import net.minecraft.network.codec.ByteBufCodecs; import net.minecraft.network.codec.StreamCodec; import net.minecraft.resources.ResourceKey; public record EitherHolder(Either, ResourceKey> contents) { public EitherHolder(Holder holder) { this(Either.left(holder)); } public EitherHolder(ResourceKey key) { this(Either.right(key)); } public static Codec> codec(ResourceKey> registry, Codec> holderCodec) { return Codec.either(holderCodec, (Codec)ResourceKey.codec(registry).comapFlatMap(key -> DataResult.error(() -> "Cannot parse as key without registry"), Function.identity())).xmap(EitherHolder::new, EitherHolder::contents); } public static StreamCodec> streamCodec(ResourceKey> registry, StreamCodec> streamHolderCodec) { return StreamCodec.composite(ByteBufCodecs.either(streamHolderCodec, ResourceKey.streamCodec(registry)), EitherHolder::contents, EitherHolder::new); } public Optional unwrap(Registry registry) { return (Optional)this.contents.map(holder -> Optional.of(holder.value()), registry::getOptional); } public Optional> unwrap(HolderLookup.Provider provider) { return (Optional)this.contents.map(Optional::of, key -> provider.get(key).map(e -> e)); } public Optional> key() { return (Optional)this.contents.map(Holder::unwrapKey, Optional::of); } }