diff --git a/src/main/java/io/github/null2264/cobblegen/data/config/WeightedBlock.java b/src/main/java/io/github/null2264/cobblegen/data/config/WeightedBlock.java index 6ca3257..62971fa 100644 --- a/src/main/java/io/github/null2264/cobblegen/data/config/WeightedBlock.java +++ b/src/main/java/io/github/null2264/cobblegen/data/config/WeightedBlock.java @@ -34,6 +34,10 @@ public class WeightedBlock implements PacketSerializable, Jankson public Integer minY; @Nullable public List neighbours; + @Nullable + public List biomes; + @Nullable + public List excludedBiomes; public WeightedBlock(String id, Double weight) { this(id, weight, null, null); @@ -44,7 +48,7 @@ public WeightedBlock(String id, Double weight, List dimIds) { } public WeightedBlock(String id, Double weight, List dimIds, List excludedDimensions) { - this(id, weight, dimIds, excludedDimensions, null, null, null); + this(id, weight, dimIds, excludedDimensions, null, null, null, null, null); } public WeightedBlock( @@ -54,7 +58,9 @@ public WeightedBlock( @Nullable List excludedDimensions, @Nullable Integer maxY, @Nullable Integer minY, - @Nullable List neighbours + @Nullable List neighbours, + @Nullable List biomes, + @Nullable List excludedBiomes ) { this.id = id; this.weight = weight; @@ -63,6 +69,8 @@ public WeightedBlock( this.maxY = maxY; this.minY = minY; this.neighbours = neighbours; + this.biomes = biomes; + this.excludedBiomes = excludedBiomes; } public static WeightedBlock fromBlock(Block block, Double weight) { @@ -82,7 +90,7 @@ public static WeightedBlock fromBlock( Integer minY ) { final String id = Util.getBlockId(block).toString(); - return new WeightedBlock(id, weight, dimIds, excludedDimensions, maxY, minY, null); + return new WeightedBlock(id, weight, dimIds, excludedDimensions, maxY, minY, null, null, null); } public Block getBlock() { @@ -111,6 +119,9 @@ public void toPacket(FriendlyByteBuf buf) { buf.writeOptional(Util.optional(maxY), FriendlyByteBuf::writeInt); buf.writeOptional(Util.optional(minY), FriendlyByteBuf::writeInt); + + buf.writeOptional(Util.optional(biomes), (o, value) -> o.writeCollection(value, FriendlyByteBuf::writeUtf)); + buf.writeOptional(Util.optional(excludedBiomes), (o, value) -> o.writeCollection(value, FriendlyByteBuf::writeUtf)); } public static WeightedBlock fromPacket(FriendlyByteBuf buf) { @@ -123,6 +134,9 @@ public static WeightedBlock fromPacket(FriendlyByteBuf buf) { Optional maxY = buf.readOptional(FriendlyByteBuf::readInt); Optional minY = buf.readOptional(FriendlyByteBuf::readInt); + Optional> biomes = buf.readOptional((o) -> o.readList(FriendlyByteBuf::readUtf)); + Optional> excludedBiomes = buf.readOptional((o) -> o.readList(FriendlyByteBuf::readUtf)); + return new WeightedBlock( id, weight, @@ -130,7 +144,9 @@ public static WeightedBlock fromPacket(FriendlyByteBuf buf) { excludedDimensions.orElse(null), maxY.orElse(null), minY.orElse(null), - null + null, + biomes.orElse(null), + excludedBiomes.orElse(null) ); } @@ -144,6 +160,8 @@ public JsonObject toJson() { json.put("excludedDimensions", JANKSON.toJson(excludedDimensions)); json.put("maxY", JANKSON.toJson(maxY)); json.put("minY", JANKSON.toJson(minY)); + json.put("biomes", JANKSON.toJson(biomes)); + json.put("excludedBiomes", JANKSON.toJson(excludedBiomes)); return json; } @@ -190,6 +208,26 @@ public static WeightedBlock fromJson(JsonObject json) { minY = ((JsonPrimitive) _minY).asInt(0); } - return new WeightedBlock(id, weight, dimensions, excludedDimensions, maxY, minY, null); + @Nullable + List biomes; + JsonElement _biomes = json.get("biomes"); + if (_biomes instanceof JsonArray) { + biomes = new ArrayList<>(); + ((JsonArray) _biomes).forEach(value -> biomes.add(((JsonPrimitive) value).asString())); + } else { + biomes = null; + } + + @Nullable + List excludedBiomes; + JsonElement _excludedBiomes = json.get("excludedBiomes"); + if (_excludedBiomes instanceof JsonArray) { + excludedBiomes = new ArrayList<>(); + ((JsonArray) _excludedBiomes).forEach(value -> excludedBiomes.add(((JsonPrimitive) value).asString())); + } else { + excludedBiomes = null; + } + + return new WeightedBlock(id, weight, dimensions, excludedDimensions, maxY, minY, null, biomes, excludedBiomes); } } diff --git a/src/main/java/io/github/null2264/cobblegen/data/model/BuiltInGenerator.java b/src/main/java/io/github/null2264/cobblegen/data/model/BuiltInGenerator.java index d5791f8..cfddf7e 100644 --- a/src/main/java/io/github/null2264/cobblegen/data/model/BuiltInGenerator.java +++ b/src/main/java/io/github/null2264/cobblegen/data/model/BuiltInGenerator.java @@ -1,5 +1,6 @@ package io.github.null2264.cobblegen.data.model; +import io.github.null2264.cobblegen.CobbleGen; import io.github.null2264.cobblegen.data.CGIdentifier; import io.github.null2264.cobblegen.data.config.GeneratorMap; import io.github.null2264.cobblegen.data.config.ResultList; @@ -11,6 +12,7 @@ import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.state.BlockState; import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.Nullable; import java.util.List; import java.util.Optional; @@ -28,7 +30,7 @@ public interface BuiltInGenerator extends Generator //#else private String //#endif - randomizeBlockId(Block key, String dim, Integer yLevel, GeneratorMap candidates) { + randomizeBlockId(Block key, String dim, Integer yLevel, GeneratorMap candidates, @Nullable String biome) { ResultList blockIds = candidates.getOrDefault( CGIdentifier.fromMC(Util.getBlockId(key)), candidates.getOrDefault(CGIdentifier.wildcard(), new ResultList()) @@ -42,6 +44,12 @@ public interface BuiltInGenerator extends Generator if (block.excludedDimensions != null && block.excludedDimensions.contains(dim)) continue; + if (biome != null && CobbleGen.META_CONFIG.enableExperimentalFeatures) { + if (block.biomes != null && !block.biomes.contains(biome)) continue; + + if (block.excludedBiomes != null && block.excludedBiomes.contains(biome)) continue; + } + if (block.maxY != null && block.maxY <= yLevel) continue; if (block.minY != null && block.minY >= yLevel) continue; @@ -83,7 +91,8 @@ default Optional getBlockCandidate(LevelAccessor level, BlockPos pos level.getBlockState(pos.below()).getBlock(), Util.getDimension(level), pos.getY(), - candidates + candidates, + Util.getBiome(level, pos) ); if (replacementId == null) { @@ -100,4 +109,4 @@ default Optional getBlockCandidate(LevelAccessor level, BlockPos pos } return Optional.of(Util.getBlock(id).defaultBlockState()); } -} \ No newline at end of file +} diff --git a/src/main/java/io/github/null2264/cobblegen/util/Util.java b/src/main/java/io/github/null2264/cobblegen/util/Util.java index 3762969..440dcf2 100644 --- a/src/main/java/io/github/null2264/cobblegen/util/Util.java +++ b/src/main/java/io/github/null2264/cobblegen/util/Util.java @@ -2,6 +2,7 @@ import io.github.null2264.cobblegen.compat.LoaderCompat; import io.github.null2264.cobblegen.compat.RegistryCompat; +import net.minecraft.core.BlockPos; import net.minecraft.core.Registry; import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceLocation; @@ -125,4 +126,16 @@ public static String getDimension(LevelAccessor level) { ).getKey(level.dimensionType()); return dim != null ? dim.toString() : "minecraft:overworld"; } -} \ No newline at end of file + + @Nullable + public static String getBiome(LevelAccessor level, BlockPos position) { + ResourceLocation biome = level.registryAccess().registryOrThrow( + //#if MC<=11902 + Registry.BIOME_REGISTRY + //#else + //$$ net.minecraft.core.registries.Registries.BIOME + //#endif + ).getKey(level.getBiome(position).value()); + return biome != null ? biome.toString() : null; + } +}