2025-11-24 22:52:51 +03:00

54 lines
2.1 KiB
Java

/*
* 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<T>(Either<Holder<T>, ResourceKey<T>> contents) {
public EitherHolder(Holder<T> holder) {
this(Either.left(holder));
}
public EitherHolder(ResourceKey<T> key) {
this(Either.right(key));
}
public static <T> Codec<EitherHolder<T>> codec(ResourceKey<Registry<T>> registry, Codec<Holder<T>> 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 <T> StreamCodec<RegistryFriendlyByteBuf, EitherHolder<T>> streamCodec(ResourceKey<Registry<T>> registry, StreamCodec<RegistryFriendlyByteBuf, Holder<T>> streamHolderCodec) {
return StreamCodec.composite(ByteBufCodecs.either(streamHolderCodec, ResourceKey.streamCodec(registry)), EitherHolder::contents, EitherHolder::new);
}
public Optional<T> unwrap(Registry<T> registry) {
return (Optional)this.contents.map(holder -> Optional.of(holder.value()), registry::getOptional);
}
public Optional<Holder<T>> unwrap(HolderLookup.Provider provider) {
return (Optional)this.contents.map(Optional::of, key -> provider.get(key).map(e -> e));
}
public Optional<ResourceKey<T>> key() {
return (Optional)this.contents.map(Holder::unwrapKey, Optional::of);
}
}