package org.betterx.bclib.util; import com.mojang.datafixers.util.Either; import net.minecraft.core.Holder; import net.minecraft.core.HolderOwner; import net.minecraft.core.Registry; import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceLocation; import net.minecraft.tags.TagKey; import java.util.Optional; import java.util.Set; import java.util.function.Predicate; import java.util.stream.Stream; import org.jetbrains.annotations.Nullable; public class FullReferenceHolder implements Holder { private Set> tags = Set.of(); @Nullable private ResourceKey key; @Nullable private T value; private ResourceKey> owner; private FullReferenceHolder( ResourceKey> owner, @Nullable ResourceKey resourceKey, @Nullable T object ) { this.owner = owner; this.key = resourceKey; this.value = object; } public static FullReferenceHolder create( ResourceKey> owner, ResourceKey resourceKey, @Nullable T object ) { return new FullReferenceHolder(owner, resourceKey, object); } public static FullReferenceHolder create( ResourceKey> owner, ResourceLocation id, @Nullable T object ) { return new FullReferenceHolder(owner, ResourceKey.create(owner, id), object); } public ResourceKey key() { if (this.key == null) { throw new IllegalStateException("Trying to access unbound value '" + this.value + "' from registry " + this.owner); } else { return this.key; } } @Override public T value() { if (this.value == null) { throw new IllegalStateException("Trying to access unbound value '" + this.key + "' from registry " + this.owner); } else { return this.value; } } @Override public boolean is(ResourceLocation resourceLocation) { return this.key().location().equals(resourceLocation); } @Override public boolean is(ResourceKey resourceKey) { return this.key() == resourceKey; } @Override public boolean is(TagKey tagKey) { return this.tags.contains(tagKey); } @Override public Stream> tags() { return this.tags.stream(); } @Override public boolean is(Predicate> predicate) { return predicate.test(this.key()); } @Override public boolean canSerializeIn(HolderOwner holderOwner) { return true; } @Override public Either, T> unwrap() { return Either.left(this.key()); } @Override public Optional> unwrapKey() { return Optional.of(this.key()); } @Override public Kind kind() { return Holder.Kind.REFERENCE; } @Override public boolean isBound() { return this.key != null && this.value != null; } @Override public String toString() { return "FullReference{" + this.key + "=" + this.value + "}"; } }