|
| 1 | +package com.gregtechceu.gtceu.common.recipe.condition; |
| 2 | + |
| 3 | +import com.gregtechceu.gtceu.api.machine.trait.RecipeLogic; |
| 4 | +import com.gregtechceu.gtceu.api.recipe.GTRecipe; |
| 5 | +import com.gregtechceu.gtceu.api.recipe.RecipeCondition; |
| 6 | +import com.gregtechceu.gtceu.api.recipe.condition.RecipeConditionType; |
| 7 | +import com.gregtechceu.gtceu.common.data.GTRecipeConditions; |
| 8 | + |
| 9 | +import net.minecraft.core.Holder; |
| 10 | +import net.minecraft.core.registries.Registries; |
| 11 | +import net.minecraft.network.chat.Component; |
| 12 | +import net.minecraft.resources.ResourceLocation; |
| 13 | +import net.minecraft.tags.TagKey; |
| 14 | +import net.minecraft.world.level.Level; |
| 15 | +import net.minecraft.world.level.biome.Biome; |
| 16 | + |
| 17 | +import com.mojang.serialization.Codec; |
| 18 | +import com.mojang.serialization.codecs.RecordCodecBuilder; |
| 19 | +import lombok.Getter; |
| 20 | +import lombok.NoArgsConstructor; |
| 21 | +import org.jetbrains.annotations.NotNull; |
| 22 | + |
| 23 | +@NoArgsConstructor |
| 24 | +public class BiomeTagCondition extends RecipeCondition<BiomeTagCondition> { |
| 25 | + |
| 26 | + public static final Codec<BiomeTagCondition> CODEC = RecordCodecBuilder |
| 27 | + .create(instance -> RecipeCondition.isReverse(instance) |
| 28 | + .and(TagKey.codec(Registries.BIOME).fieldOf("biome_tag").forGetter(val -> val.biome)) |
| 29 | + .apply(instance, BiomeTagCondition::new)); |
| 30 | + |
| 31 | + public final static BiomeTagCondition INSTANCE = new BiomeTagCondition(); |
| 32 | + @Getter |
| 33 | + private TagKey<Biome> biome = TagKey.create(Registries.BIOME, new ResourceLocation("dummy")); |
| 34 | + |
| 35 | + public BiomeTagCondition(boolean isReverse, TagKey<Biome> biome) { |
| 36 | + super(isReverse); |
| 37 | + this.biome = biome; |
| 38 | + } |
| 39 | + |
| 40 | + public BiomeTagCondition(TagKey<Biome> biome) { |
| 41 | + this.biome = biome; |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public RecipeConditionType<BiomeTagCondition> getType() { |
| 46 | + return GTRecipeConditions.BIOME_TAG; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public boolean isOr() { |
| 51 | + return true; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public Component getTooltips() { |
| 56 | + return Component.translatable("recipe.condition.biome.tooltip", |
| 57 | + Component.translatableWithFallback(biome.location().toLanguageKey("biome"), |
| 58 | + biome.location().toString())); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public boolean testCondition(@NotNull GTRecipe recipe, @NotNull RecipeLogic recipeLogic) { |
| 63 | + Level level = recipeLogic.machine.self().getLevel(); |
| 64 | + if (level == null) return false; |
| 65 | + Holder<Biome> biome = level.getBiome(recipeLogic.machine.self().getPos()); |
| 66 | + return biome.is(this.biome); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public BiomeTagCondition createTemplate() { |
| 71 | + return new BiomeTagCondition(); |
| 72 | + } |
| 73 | +} |
0 commit comments