diff --git a/patches/minecraft/net/minecraft/resources/ResourceKey.java.patch b/patches/minecraft/net/minecraft/resources/ResourceKey.java.patch index 7a62bac753..2258b4a090 100644 --- a/patches/minecraft/net/minecraft/resources/ResourceKey.java.patch +++ b/patches/minecraft/net/minecraft/resources/ResourceKey.java.patch @@ -5,7 +5,7 @@ import net.minecraft.network.codec.StreamCodec; -public class ResourceKey { -+public class ResourceKey implements Comparable> { ++public class ResourceKey implements Comparable>, net.minecraftforge.common.extensions.IForgeResourceKey { private static final ConcurrentMap> VALUES = new MapMaker().weakValues().makeMap(); private final ResourceLocation registryName; private final ResourceLocation location; diff --git a/src/main/java/net/minecraftforge/common/extensions/IForgeResourceKey.java b/src/main/java/net/minecraftforge/common/extensions/IForgeResourceKey.java new file mode 100644 index 0000000000..f815cb1022 --- /dev/null +++ b/src/main/java/net/minecraftforge/common/extensions/IForgeResourceKey.java @@ -0,0 +1,42 @@ +/* + * Copyright (c) Forge Development LLC and contributors + * SPDX-License-Identifier: LGPL-2.1-only + */ + +package net.minecraftforge.common.extensions; + +import net.minecraft.core.Holder; +import net.minecraft.core.RegistryAccess; +import net.minecraft.resources.ResourceKey; +import net.minecraft.world.entity.Entity; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.block.entity.BlockEntity; + +import java.util.Objects; + +public interface IForgeResourceKey { + private ResourceKey self() { + return (ResourceKey) this; + } + + default Holder getOrThrow(BlockEntity blockEntity) { + return getOrThrow(blockEntity.getLevel()); + } + + default Holder getOrThrow(Entity entity) { + return getOrThrow(entity.registryAccess()); + } + + default Holder getOrThrow(Level level) { + return getOrThrow(level.registryAccess()); + } + + /** + * @param registryAccess of {@link RegistryAccess} + * @return {@link Holder} of the Object that this {@link ResourceKey} is registered to + */ + default Holder getOrThrow(RegistryAccess registryAccess) { + Objects.requireNonNull(registryAccess, "registryAccess was null"); + return registryAccess.registryOrThrow(self().registryKey()).getHolderOrThrow(self()); + } +}