|
25 | 25 |
|
26 | 26 | import aztech.modern_industrialization.MIText; |
27 | 27 | import aztech.modern_industrialization.machines.recipe.MachineRecipe; |
| 28 | +import aztech.modern_industrialization.proxy.CommonProxy; |
| 29 | +import com.mojang.datafixers.util.Either; |
28 | 30 | import com.mojang.serialization.MapCodec; |
29 | 31 | import java.util.List; |
| 32 | +import net.minecraft.Util; |
30 | 33 | import net.minecraft.core.registries.Registries; |
31 | 34 | import net.minecraft.network.RegistryFriendlyByteBuf; |
32 | 35 | import net.minecraft.network.chat.Component; |
| 36 | +import net.minecraft.network.chat.MutableComponent; |
| 37 | +import net.minecraft.network.codec.ByteBufCodecs; |
33 | 38 | import net.minecraft.network.codec.StreamCodec; |
34 | 39 | import net.minecraft.resources.ResourceKey; |
| 40 | +import net.minecraft.resources.ResourceLocation; |
| 41 | +import net.minecraft.tags.TagKey; |
35 | 42 | import net.minecraft.world.level.biome.Biome; |
| 43 | +import net.neoforged.neoforge.common.util.NeoForgeExtraCodecs; |
36 | 44 |
|
37 | | -public record BiomeProcessCondition(ResourceKey<Biome> biome) implements MachineProcessCondition { |
38 | | - static final MapCodec<BiomeProcessCondition> CODEC = ResourceKey.codec(Registries.BIOME) |
39 | | - .fieldOf("biome") |
| 45 | +public record BiomeProcessCondition(Either<ResourceKey<Biome>, TagKey<Biome>> biome) implements MachineProcessCondition { |
| 46 | + static final MapCodec<BiomeProcessCondition> CODEC = NeoForgeExtraCodecs |
| 47 | + .xor( |
| 48 | + ResourceKey.codec(Registries.BIOME).fieldOf("biome"), |
| 49 | + TagKey.codec(Registries.BIOME).fieldOf("tag")) |
40 | 50 | .xmap(BiomeProcessCondition::new, BiomeProcessCondition::biome); |
41 | 51 | static final StreamCodec<RegistryFriendlyByteBuf, BiomeProcessCondition> STREAM_CODEC = StreamCodec.composite( |
42 | | - ResourceKey.streamCodec(Registries.BIOME), |
| 52 | + ByteBufCodecs.either( |
| 53 | + ResourceKey.streamCodec(Registries.BIOME), |
| 54 | + ResourceLocation.STREAM_CODEC.map(rl -> TagKey.create(Registries.BIOME, rl), TagKey::location)), |
43 | 55 | BiomeProcessCondition::biome, |
44 | 56 | BiomeProcessCondition::new); |
45 | 57 |
|
46 | 58 | @Override |
47 | 59 | public boolean canProcessRecipe(Context context, MachineRecipe recipe) { |
48 | 60 | var entityBiome = context.getLevel().getBiome(context.getBlockEntity().getBlockPos()); |
49 | | - return entityBiome.is(biome); |
| 61 | + return biome.map(entityBiome::is, entityBiome::is); |
| 62 | + } |
| 63 | + |
| 64 | + private static MutableComponent biomeId(ResourceLocation biomeLocation) { |
| 65 | + return Component.translatable(Util.makeDescriptionId("biome", biomeLocation)); |
50 | 66 | } |
51 | 67 |
|
52 | 68 | @Override |
53 | 69 | public void appendDescription(List<Component> list) { |
54 | | - var loc = biome.location(); |
55 | | - var biomeComponent = Component.translatable("biome.%s.%s".formatted(loc.getNamespace(), loc.getPath())); |
56 | | - list.add(MIText.RequiresBiome.text(biomeComponent)); |
| 70 | + biome.ifLeft(rk -> { |
| 71 | + list.add(MIText.RequiresBiome.text(biomeId(rk.location()))); |
| 72 | + }).ifRight(tag -> { |
| 73 | + var holderLookup = CommonProxy.INSTANCE.getClientPlayer().registryAccess(); |
| 74 | + var biomeNames = holderLookup.lookupOrThrow(tag.registry()).get(tag).map(named -> { |
| 75 | + return named.stream() |
| 76 | + .map(holder -> biomeId(holder.unwrapKey().orElseThrow().location())) |
| 77 | + .reduce((a, b) -> a.append(", ").append(b)) |
| 78 | + .orElseThrow(); |
| 79 | + }).orElse(Component.literal("???")); |
| 80 | + |
| 81 | + list.add(MIText.RequiresBiome.text(biomeNames)); |
| 82 | + }); |
57 | 83 | } |
58 | 84 |
|
59 | 85 | @Override |
|
0 commit comments