diff --git a/src/main/java/twilightforest/util/Restriction.java b/src/main/java/twilightforest/util/Restriction.java index d1784b5216..43e74cb2b4 100644 --- a/src/main/java/twilightforest/util/Restriction.java +++ b/src/main/java/twilightforest/util/Restriction.java @@ -2,21 +2,19 @@ import com.mojang.serialization.Codec; import com.mojang.serialization.codecs.RecordCodecBuilder; -import net.minecraft.core.Holder; -import net.minecraft.core.Registry; import net.minecraft.core.RegistryAccess; import net.minecraft.core.registries.Registries; -import net.minecraft.resources.ResourceKey; import net.minecraft.resources.Identifier; +import net.minecraft.resources.ResourceKey; import net.minecraft.util.ExtraCodecs; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStackTemplate; import net.minecraft.world.level.biome.Biome; import net.minecraft.world.level.levelgen.structure.Structure; +import org.jspecify.annotations.Nullable; import twilightforest.TFRegistries; -import javax.annotation.Nullable; import java.util.List; import java.util.Optional; @@ -28,7 +26,6 @@ * @param advancements List of advancements that are required to make a biome no longer restricted */ public record Restriction(@Nullable ResourceKey hintStructureKey, ResourceKey enforcement, float multiplier, @Nullable ItemStackTemplate lockedBiomeToast, List advancements) { - public static final Codec CODEC = RecordCodecBuilder.create((recordCodecBuilder) -> recordCodecBuilder.group( ResourceKey.codec(Registries.STRUCTURE).optionalFieldOf("structure_key").forGetter((restriction) -> Optional.ofNullable(restriction.hintStructureKey())), ResourceKey.codec(TFRegistries.Keys.ENFORCEMENT).fieldOf("enforcement").forGetter(Restriction::enforcement), @@ -43,32 +40,20 @@ private static Restriction create(Optional> hintStructure } public static Optional getRestrictionForBiome(Biome biome, Entity entity) { - if (!(entity instanceof Player player)) - return Optional.empty(); - - RegistryAccess access = entity.level().registryAccess(); - Identifier biomeLocation = access.lookupOrThrow(Registries.BIOME).getKey(biome); - if (biomeLocation == null) - return Optional.empty(); - - Optional> restrictionsRegistry = access.lookup(TFRegistries.Keys.RESTRICTIONS); - if (restrictionsRegistry.isEmpty()) - return Optional.empty(); - - Restriction restrictions = restrictionsRegistry.get().get(biomeLocation).map(Holder.Reference::value).orElse(null); - if (restrictions == null || PlayerHelper.doesPlayerHaveRequiredAdvancements(player, restrictions.advancements())) { - return Optional.empty(); + if (entity instanceof Player player) { + RegistryAccess access = entity.level().registryAccess(); + Identifier biomeLocation = access.lookupOrThrow(Registries.BIOME).getKey(biome); + if (biomeLocation != null) { + Restriction restrictions = access.lookupOrThrow(TFRegistries.Keys.RESTRICTIONS).getValue(biomeLocation); + if (restrictions != null && !PlayerHelper.doesPlayerHaveRequiredAdvancements(player, restrictions.advancements())) { + return Optional.of(restrictions); + } + } } - - return Optional.of(restrictions); - } - - @SuppressWarnings("OptionalUsedAsFieldOrParameterType") - private static Restriction create(Optional> hintStructureKey, ResourceKey enforcer, float multiplier, Optional lockedBiomeToast, List advancements) { - return new Restriction(hintStructureKey.orElse(null), enforcer, multiplier, lockedBiomeToast.orElse(null), advancements); + return Optional.empty(); } public static boolean isBiomeSafeFor(Biome biome, Entity entity) { return getRestrictionForBiome(biome, entity).isEmpty(); } -} +} \ No newline at end of file