Skip to content

Commit

Permalink
Prevent ocean biomes from being generated
Browse files Browse the repository at this point in the history
  • Loading branch information
haykam821 committed Jan 6, 2024
1 parent 23436bb commit f8e0ddd
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ public final class BeaconBreakersChunkGenerator extends GameChunkGenerator {
private final ChunkGenerator chunkGenerator;

public BeaconBreakersChunkGenerator(MinecraftServer server, BeaconBreakersMapConfig mapConfig) {
super(mapConfig.getDimensionOptions().chunkGenerator().getBiomeSource());
super(mapConfig.getChunkGenerator().getBiomeSource());
this.mapConfig = mapConfig;

this.chunkGenerator = mapConfig.getDimensionOptions().chunkGenerator();
this.chunkGenerator = mapConfig.getChunkGenerator();
}

public ChunkGeneratorSettings getSettings() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,53 @@
package io.github.haykam821.beaconbreakers.game.map;

import java.util.List;
import java.util.Optional;

import com.mojang.datafixers.util.Pair;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;

import net.minecraft.registry.RegistryCodecs;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.registry.entry.RegistryEntryList;
import net.minecraft.registry.entry.RegistryFixedCodec;
import net.minecraft.world.biome.Biome;
import net.minecraft.world.biome.source.MultiNoiseBiomeSource;
import net.minecraft.world.biome.source.util.MultiNoiseUtil;
import net.minecraft.world.biome.source.util.MultiNoiseUtil.NoiseHypercube;
import net.minecraft.world.dimension.DimensionOptions;
import net.minecraft.world.dimension.DimensionOptionsRegistryHolder;
import net.minecraft.world.gen.WorldPreset;
import net.minecraft.world.gen.chunk.ChunkGenerator;
import net.minecraft.world.gen.chunk.NoiseChunkGenerator;

public class BeaconBreakersMapConfig {
public static final Codec<BeaconBreakersMapConfig> CODEC = RecordCodecBuilder.create(instance -> {
return instance.group(
RegistryFixedCodec.of(RegistryKeys.WORLD_PRESET).fieldOf("preset").forGetter(mapConfig -> mapConfig.worldPreset),
RegistryKey.createCodec(RegistryKeys.DIMENSION).optionalFieldOf("dimension_options", DimensionOptions.OVERWORLD).forGetter(mapConfig -> mapConfig.dimensionOptions),
RegistryCodecs.entryList(RegistryKeys.BIOME).optionalFieldOf("excluded_biomes").forGetter(mapConfig -> mapConfig.excludedBiomes),
Codec.INT.optionalFieldOf("x", 16).forGetter(BeaconBreakersMapConfig::getX),
Codec.INT.optionalFieldOf("z", 16).forGetter(BeaconBreakersMapConfig::getZ)
).apply(instance, BeaconBreakersMapConfig::new);
});

private final RegistryEntry<WorldPreset> worldPreset;
private final RegistryKey<DimensionOptions> dimensionOptions;
private final Optional<RegistryEntryList<Biome>> excludedBiomes;

private final int x;
private final int z;

public BeaconBreakersMapConfig(RegistryEntry<WorldPreset> worldPreset, RegistryKey<DimensionOptions> dimensionOptions, int x, int z) {
private ChunkGenerator chunkGenerator;

public BeaconBreakersMapConfig(RegistryEntry<WorldPreset> worldPreset, RegistryKey<DimensionOptions> dimensionOptions, Optional<RegistryEntryList<Biome>> excludedBiomes, int x, int z) {
this.worldPreset = worldPreset;
this.dimensionOptions = dimensionOptions;
this.excludedBiomes = excludedBiomes;

this.x = x;
this.z = z;
}
Expand All @@ -45,4 +64,42 @@ public int getX() {
public int getZ() {
return this.z;
}

public ChunkGenerator getChunkGenerator() {
if (this.chunkGenerator == null) {
this.chunkGenerator = this.createChunkGenerator();
}

return this.chunkGenerator;
}

private boolean isIncludedBiome(Pair<NoiseHypercube, RegistryEntry<Biome>> pair) {
return this.excludedBiomes.isEmpty() || !this.excludedBiomes.get().contains(pair.getSecond());
}

private ChunkGenerator createChunkGenerator() {
DimensionOptions dimensionOptions = this.getDimensionOptions();

if (this.excludedBiomes.isPresent()) {
if (dimensionOptions.chunkGenerator() instanceof NoiseChunkGenerator noiseChunkGenerator) {
if (noiseChunkGenerator.getBiomeSource() instanceof MultiNoiseBiomeSource biomeSource) {
List<Pair<NoiseHypercube, RegistryEntry<Biome>>> entries = biomeSource.getBiomeEntries()
.getEntries()
.stream()
.filter(this::isIncludedBiome)
.toList();

MultiNoiseBiomeSource newBiomeSource = MultiNoiseBiomeSource.create(new MultiNoiseUtil.Entries<>(entries));

return new NoiseChunkGenerator(newBiomeSource, noiseChunkGenerator.getSettings());
}

throw new IllegalArgumentException("Cannot exclude biomes from unsupported biome source");
}

throw new IllegalArgumentException("Cannot exclude biomes from unsupported chunk generator");
}

return dimensionOptions.chunkGenerator();
}
}
3 changes: 2 additions & 1 deletion src/main/resources/beaconbreakers.accesswidener
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ accessWidener v1 named
accessible method net/minecraft/entity/LivingEntity dropXp ()V
accessible method net/minecraft/entity/player/PlayerEntity vanishCursedItems ()V
accessible method net/minecraft/entity/player/PlayerEntity dropShoulderEntities ()V
accessible field net/minecraft/world/explosion/Explosion world Lnet/minecraft/world/World;
accessible field net/minecraft/world/explosion/Explosion world Lnet/minecraft/world/World;
accessible method net/minecraft/world/biome/source/MultiNoiseBiomeSource getBiomeEntries ()Lnet/minecraft/world/biome/source/util/MultiNoiseUtil$Entries;
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"type": "beaconbreakers:beacon_breakers",
"map": {
"dimension_options": "minecraft:overworld",
"preset": "minecraft:amplified"
"preset": "minecraft:amplified",
"excluded_biomes": "#minecraft:is_ocean"
},
"players": {
"min": 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"type": "beaconbreakers:beacon_breakers",
"map": {
"dimension_options": "minecraft:overworld",
"preset": "minecraft:amplified"
"preset": "minecraft:amplified",
"excluded_biomes": "#minecraft:is_ocean"
},
"players": {
"min": 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"map": {
"dimension_options": "minecraft:overworld",
"preset": "minecraft:amplified",
"excluded_biomes": "#minecraft:is_ocean",
"x": 5,
"z": 5
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"type": "beaconbreakers:beacon_breakers",
"map": {
"dimension_options": "minecraft:overworld",
"preset": "minecraft:normal"
"preset": "minecraft:normal",
"excluded_biomes": "#minecraft:is_ocean"
},
"players": {
"min": 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"type": "beaconbreakers:beacon_breakers",
"map": {
"dimension_options": "minecraft:overworld",
"preset": "minecraft:normal"
"preset": "minecraft:normal",
"excluded_biomes": "#minecraft:is_ocean"
},
"players": {
"min": 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"map": {
"dimension_options": "minecraft:overworld",
"preset": "minecraft:normal",
"excluded_biomes": "#minecraft:is_ocean",
"x": 5,
"z": 5
},
Expand Down

0 comments on commit f8e0ddd

Please sign in to comment.