Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 13 additions & 28 deletions src/main/java/twilightforest/util/Restriction.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -28,7 +26,6 @@
* @param advancements List of advancements that are required to make a biome no longer restricted
*/
public record Restriction(@Nullable ResourceKey<Structure> hintStructureKey, ResourceKey<Enforcement> enforcement, float multiplier, @Nullable ItemStackTemplate lockedBiomeToast, List<Identifier> advancements) {

public static final Codec<Restriction> 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),
Expand All @@ -43,32 +40,20 @@ private static Restriction create(Optional<ResourceKey<Structure>> hintStructure
}

public static Optional<Restriction> 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<Registry<Restriction>> 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<ResourceKey<Structure>> hintStructureKey, ResourceKey<Enforcement> enforcer, float multiplier, Optional<ItemStackTemplate> lockedBiomeToast, List<Identifier> 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();
}
}
}
Loading