Skip to content

Commit

Permalink
feat: Add experimental biome filter
Browse files Browse the repository at this point in the history
  • Loading branch information
null2264 committed Sep 9, 2024
1 parent 739e972 commit e07238b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public class WeightedBlock implements PacketSerializable<WeightedBlock>, Jankson
public Integer minY;
@Nullable
public List<String> neighbours;
@Nullable
public List<String> biomes;
@Nullable
public List<String> excludedBiomes;

public WeightedBlock(String id, Double weight) {
this(id, weight, null, null);
Expand All @@ -44,7 +48,7 @@ public WeightedBlock(String id, Double weight, List<String> dimIds) {
}

public WeightedBlock(String id, Double weight, List<String> dimIds, List<String> excludedDimensions) {
this(id, weight, dimIds, excludedDimensions, null, null, null);
this(id, weight, dimIds, excludedDimensions, null, null, null, null, null);
}

public WeightedBlock(
Expand All @@ -54,7 +58,9 @@ public WeightedBlock(
@Nullable List<String> excludedDimensions,
@Nullable Integer maxY,
@Nullable Integer minY,
@Nullable List<String> neighbours
@Nullable List<String> neighbours,
@Nullable List<String> biomes,
@Nullable List<String> excludedBiomes
) {
this.id = id;
this.weight = weight;
Expand All @@ -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) {
Expand All @@ -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() {
Expand Down Expand Up @@ -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) {
Expand All @@ -123,14 +134,19 @@ public static WeightedBlock fromPacket(FriendlyByteBuf buf) {
Optional<Integer> maxY = buf.readOptional(FriendlyByteBuf::readInt);
Optional<Integer> minY = buf.readOptional(FriendlyByteBuf::readInt);

Optional<List<String>> biomes = buf.readOptional((o) -> o.readList(FriendlyByteBuf::readUtf));
Optional<List<String>> excludedBiomes = buf.readOptional((o) -> o.readList(FriendlyByteBuf::readUtf));

return new WeightedBlock(
id,
weight,
dimensions.orElse(null),
excludedDimensions.orElse(null),
maxY.orElse(null),
minY.orElse(null),
null
null,
biomes.orElse(null),
excludedBiomes.orElse(null)
);
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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<String> 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<String> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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())
Expand All @@ -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;
Expand Down Expand Up @@ -83,7 +91,8 @@ default Optional<BlockState> getBlockCandidate(LevelAccessor level, BlockPos pos
level.getBlockState(pos.below()).getBlock(),
Util.getDimension(level),
pos.getY(),
candidates
candidates,
Util.getBiome(level, pos)
);

if (replacementId == null) {
Expand All @@ -100,4 +109,4 @@ default Optional<BlockState> getBlockCandidate(LevelAccessor level, BlockPos pos
}
return Optional.of(Util.getBlock(id).defaultBlockState());
}
}
}
15 changes: 14 additions & 1 deletion src/main/java/io/github/null2264/cobblegen/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -125,4 +126,16 @@ public static String getDimension(LevelAccessor level) {
).getKey(level.dimensionType());
return dim != null ? dim.toString() : "minecraft:overworld";
}
}

@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;
}
}

0 comments on commit e07238b

Please sign in to comment.