mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-11-28 00:45:03 +00:00
feat: bukkit metapacks and clean up
This commit is contained in:
1 parent
dcf38956c4
commit
02c2176222
36 files changed
+1026
-230
No files matched your search
@@ -40,4 +40,5 @@ dependencies {
|
||||
testImplementation(testFixtures(project(":platforms:minecraft-common")))
|
||||
testImplementation(project(":common:addons:minecraft-attributes"))
|
||||
testImplementation("io.papermc.paper", "paper-api", Versions.Bukkit.paper)
|
||||
testImplementation("com.dfsek.wyck", "Wyck", Versions.Bukkit.wyck)
|
||||
}
|
||||
@@ -6,7 +6,7 @@ import ca.solostudios.strata.version.Version;
|
||||
import com.dfsek.terra.api.addon.BaseAddon;
|
||||
import com.dfsek.terra.api.event.events.config.pack.ConfigPackPreLoadEvent;
|
||||
import com.dfsek.terra.api.event.functional.FunctionalEventHandler;
|
||||
import com.dfsek.terra.bukkit.config.PreLoadCompatibilityOptions;
|
||||
import com.dfsek.terra.minecraft.config.MinecraftCompatibilityOptions;
|
||||
|
||||
|
||||
public class BukkitAddon implements BaseAddon {
|
||||
@@ -23,7 +23,7 @@ public class BukkitAddon implements BaseAddon {
|
||||
terraBukkitPlugin.getEventManager()
|
||||
.getHandler(FunctionalEventHandler.class)
|
||||
.register(this, ConfigPackPreLoadEvent.class)
|
||||
.then(event -> event.getPack().getContext().put(event.loadTemplate(new PreLoadCompatibilityOptions())))
|
||||
.then(event -> event.getPack().getContext().put(event.loadTemplate(new MinecraftCompatibilityOptions())))
|
||||
.global();
|
||||
}
|
||||
|
||||
|
||||
@@ -37,6 +37,7 @@ import java.util.concurrent.CancellationException;
|
||||
|
||||
import com.dfsek.terra.AbstractPlatform;
|
||||
import com.dfsek.terra.api.block.state.BlockState;
|
||||
import com.dfsek.terra.api.config.MetaPack;
|
||||
import com.dfsek.terra.api.handle.ItemHandle;
|
||||
import com.dfsek.terra.api.handle.WorldHandle;
|
||||
import com.dfsek.terra.api.world.attribute.AttributeParents;
|
||||
@@ -121,12 +122,23 @@ public class BukkitPlatform extends AbstractPlatform {
|
||||
BiomeHandler.addToVanillaTags(this);
|
||||
|
||||
Bukkit.getWorlds().forEach(world -> {
|
||||
if(world.getGenerator() instanceof BukkitChunkGeneratorWrapper wrapper) {
|
||||
if(!(world.getGenerator() instanceof BukkitChunkGeneratorWrapper wrapper)) return;
|
||||
MetaPack metaPack = wrapper.getMetaPack();
|
||||
if(metaPack == null) {
|
||||
getConfigRegistry().get(wrapper.getPack().getRegistryKey()).ifPresent(pack -> {
|
||||
wrapper.setPack(pack);
|
||||
LOGGER.info("Replaced pack in chunk generator for world {}", world);
|
||||
});
|
||||
return;
|
||||
}
|
||||
getMetaConfigRegistry().get(metaPack.getRegistryKey()).ifPresent(reloaded -> {
|
||||
try {
|
||||
wrapper.setMetaPack(reloaded, world);
|
||||
LOGGER.info("Replaced pack in chunk generator for world {} from metapack {}", world, reloaded.getID());
|
||||
} catch(IllegalArgumentException e) {
|
||||
LOGGER.warn("World {} keeps its pack: {}", world.getName(), e.getMessage());
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -36,6 +36,7 @@ import java.util.Map;
|
||||
|
||||
import com.dfsek.terra.api.command.CommandSender;
|
||||
import com.dfsek.terra.api.config.ConfigPack;
|
||||
import com.dfsek.terra.api.config.MetaPack;
|
||||
import com.dfsek.terra.api.event.events.platform.CommandRegistrationEvent;
|
||||
import com.dfsek.terra.api.event.events.platform.PlatformInitializationEvent;
|
||||
import com.dfsek.terra.bukkit.attribute.PaperVanilla;
|
||||
@@ -110,11 +111,14 @@ public class TerraBukkitPlugin extends JavaPlugin {
|
||||
public @Nullable
|
||||
ChunkGenerator getDefaultWorldGenerator(@NotNull String worldName, String id) {
|
||||
if(id == null || id.trim().isEmpty()) { return null; }
|
||||
return new BukkitChunkGeneratorWrapper(generatorMap.computeIfAbsent(worldName, name -> {
|
||||
ConfigPack pack = platform.getConfigRegistry().getByID(id).orElseThrow(
|
||||
() -> new IllegalArgumentException("No such config pack \"" + id + "\""));
|
||||
return pack.getGeneratorProvider().newInstance(pack);
|
||||
}), platform.getRawConfigRegistry().getByID(id).orElseThrow(), platform.getWorldHandle().air());
|
||||
ConfigPack pack = platform.getConfigRegistry().getByID(id).orElse(null);
|
||||
if(pack == null) {
|
||||
MetaPack metaPack = platform.getMetaConfigRegistry().getByID(id).orElseThrow(
|
||||
() -> new IllegalArgumentException("No such config pack or metapack \"" + id + "\""));
|
||||
return new BukkitChunkGeneratorWrapper(metaPack, platform.getWorldHandle().air());
|
||||
}
|
||||
var delegate = generatorMap.computeIfAbsent(worldName, name -> pack.getGeneratorProvider().newInstance(pack));
|
||||
return new BukkitChunkGeneratorWrapper(delegate, pack, platform.getWorldHandle().air());
|
||||
}
|
||||
|
||||
public GlobalRegionScheduler getGlobalRegionScheduler() {
|
||||
|
||||
+2
-4
@@ -222,7 +222,7 @@ public final class PaperDimensions {
|
||||
* A new type isn't registered while players are online: their clients already hold the registries.
|
||||
*/
|
||||
public void onWorldInit(World world, BukkitChunkGeneratorWrapper wrapper) {
|
||||
ConfigPack pack = wrapper.getPack();
|
||||
ConfigPack pack = wrapper.bind(world);
|
||||
String keeps = "Pack \"" + pack.getID() + "\": world \"" + world.getName() + "\" keeps its dimension type";
|
||||
Built built;
|
||||
try {
|
||||
@@ -266,9 +266,7 @@ public final class PaperDimensions {
|
||||
}
|
||||
|
||||
private VanillaContext context() {
|
||||
VanillaContext context = parents.context();
|
||||
if(context == null) throw new IllegalStateException("no registry load has begun");
|
||||
return context;
|
||||
return parents.requireContext();
|
||||
}
|
||||
|
||||
private PackParents prepare(ConfigPack pack, Optional<PackAttributes> attributes) {
|
||||
|
||||
+32
@@ -22,9 +22,14 @@ import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.server.level.ServerLevel;
|
||||
import net.minecraft.world.entity.npc.villager.VillagerType;
|
||||
import net.minecraft.world.level.biome.Biome;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.block.data.BlockData;
|
||||
|
||||
import java.lang.invoke.MethodHandle;
|
||||
import java.lang.invoke.MethodHandles;
|
||||
import java.lang.invoke.MethodType;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@@ -82,7 +87,34 @@ public final class PaperVanilla {
|
||||
}
|
||||
}
|
||||
|
||||
public static BlockState blockState(BlockData data) {
|
||||
try {
|
||||
return (BlockState) BlockStates.GET_STATE.invokeExact(data);
|
||||
} catch(Throwable e) {
|
||||
throw new IllegalStateException("block data " + data + " has no vanilla state", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static VanillaContext context(HolderLookup.Provider registries) {
|
||||
return VanillaContext.of(registries, villagerTypes()::get);
|
||||
}
|
||||
|
||||
/**
|
||||
* Needs CraftBukkit; resolved on first use.
|
||||
*/
|
||||
private static final class BlockStates {
|
||||
private static final MethodHandle GET_STATE;
|
||||
|
||||
static {
|
||||
try {
|
||||
Class<?> craft = Class.forName("org.bukkit.craftbukkit.block.data.CraftBlockData", false,
|
||||
Bukkit.getServer().getClass().getClassLoader());
|
||||
GET_STATE = MethodHandles.publicLookup()
|
||||
.findVirtual(craft, "getState", MethodType.methodType(BlockState.class))
|
||||
.asType(MethodType.methodType(BlockState.class, BlockData.class));
|
||||
} catch(ReflectiveOperationException e) {
|
||||
throw new ExceptionInInitializerError(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+27
@@ -77,6 +77,10 @@ public final class WyckBridge {
|
||||
LevelStemEditor.create().dimension(Dimension.reference(key(key))).apply(world);
|
||||
}
|
||||
|
||||
public static void installGenerator(World world, net.minecraft.world.level.chunk.ChunkGenerator generator) {
|
||||
LevelStemEditor.create().chunkGenerator(new MinecraftGenerator(generator)).apply(world);
|
||||
}
|
||||
|
||||
public static void registerBiomes(List<dev.wyck.biome.Biome> biomes) {
|
||||
BiomeRegistry.registry().register(biomes);
|
||||
}
|
||||
@@ -90,4 +94,27 @@ public final class WyckBridge {
|
||||
private static ResourceKey key(Identifier id) {
|
||||
return ResourceKey.of(id.getNamespace(), id.getPath());
|
||||
}
|
||||
|
||||
/**
|
||||
* Wyck installs whatever generator {@link #toMinecraft()} returns.
|
||||
*/
|
||||
private record MinecraftGenerator(net.minecraft.world.level.chunk.ChunkGenerator generator)
|
||||
implements dev.wyck.worldgen.chunk.ChunkGenerator {
|
||||
@Override
|
||||
public dev.wyck.worldgen.biome.BiomeSource biomeSource() {
|
||||
return new MinecraftBiomeSource(generator.getBiomeSource());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object toMinecraft() {
|
||||
return generator;
|
||||
}
|
||||
}
|
||||
|
||||
private record MinecraftBiomeSource(net.minecraft.world.level.biome.BiomeSource source) implements dev.wyck.worldgen.biome.BiomeSource {
|
||||
@Override
|
||||
public Object toMinecraft() {
|
||||
return source;
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -35,7 +35,7 @@ public class BukkitBiomeProvider extends BiomeProvider implements Handle {
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private static org.bukkit.block.Biome toBukkit(Biome biome) {
|
||||
static org.bukkit.block.Biome toBukkit(Biome biome) {
|
||||
BukkitPlatformBiome platformBiome = (BukkitPlatformBiome) biome.getPlatformBiome();
|
||||
return platformBiome.getContext().has(BukkitBiomeInfo.class)
|
||||
? platformBiome.getContext().get(BukkitBiomeInfo.class).bukkitBiome()
|
||||
|
||||
+40
-6
@@ -36,6 +36,7 @@ import java.util.Random;
|
||||
|
||||
import com.dfsek.terra.api.block.state.BlockState;
|
||||
import com.dfsek.terra.api.config.ConfigPack;
|
||||
import com.dfsek.terra.api.config.MetaPack;
|
||||
import com.dfsek.terra.api.world.chunk.generation.ChunkGenerator;
|
||||
import com.dfsek.terra.api.world.chunk.generation.util.GeneratorWrapper;
|
||||
import com.dfsek.terra.bukkit.util.BukkitUtils;
|
||||
@@ -48,8 +49,9 @@ public class BukkitChunkGeneratorWrapper extends org.bukkit.generator.ChunkGener
|
||||
private final BlockState air;
|
||||
private final BukkitBlockPopulator blockPopulator;
|
||||
private volatile @Nullable BukkitBiomeProvider biomeProvider;
|
||||
private volatile ChunkGenerator delegate;
|
||||
private volatile ConfigPack pack;
|
||||
private volatile @Nullable ChunkGenerator delegate;
|
||||
private volatile @Nullable ConfigPack pack;
|
||||
private volatile @Nullable MetaPack metaPack;
|
||||
private volatile boolean mobGeneration = true;
|
||||
|
||||
|
||||
@@ -60,19 +62,38 @@ public class BukkitChunkGeneratorWrapper extends org.bukkit.generator.ChunkGener
|
||||
this.blockPopulator = new BukkitBlockPopulator(pack, air);
|
||||
}
|
||||
|
||||
public BukkitChunkGeneratorWrapper(MetaPack metaPack, BlockState air) {
|
||||
this.metaPack = metaPack;
|
||||
this.air = air;
|
||||
this.blockPopulator = new BukkitBlockPopulator(null, air);
|
||||
}
|
||||
|
||||
/**
|
||||
* Picks the world's pack from the metapack on the first call.
|
||||
*/
|
||||
public ConfigPack bind(WorldInfo world) {
|
||||
ConfigPack bound = pack;
|
||||
if(bound != null) return bound;
|
||||
synchronized(this) {
|
||||
if(pack == null) setPack(MetaPackWorlds.packFor(metaPack, world.getEnvironment(), world.getName()));
|
||||
return pack;
|
||||
}
|
||||
}
|
||||
|
||||
public void setDelegate(ChunkGenerator delegate) {
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable BiomeProvider getDefaultBiomeProvider(@NotNull WorldInfo worldInfo) {
|
||||
BukkitBiomeProvider provider = new BukkitBiomeProvider(pack.getBiomeProvider());
|
||||
BukkitBiomeProvider provider = new BukkitBiomeProvider(bind(worldInfo).getBiomeProvider());
|
||||
biomeProvider = provider;
|
||||
return provider;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generateNoise(@NotNull WorldInfo worldInfo, @NotNull Random random, int x, int z, @NotNull ChunkData chunkData) {
|
||||
ConfigPack pack = bind(worldInfo);
|
||||
BukkitWorldProperties properties = new BukkitWorldProperties(worldInfo);
|
||||
delegate.generateChunkData(new BukkitProtoChunk(chunkData), properties, pack.getBiomeProvider(), x, z);
|
||||
}
|
||||
@@ -80,7 +101,7 @@ public class BukkitChunkGeneratorWrapper extends org.bukkit.generator.ChunkGener
|
||||
@Override
|
||||
public int getBaseHeight(@NotNull WorldInfo worldInfo, @NotNull Random random, int x, int z, @NotNull HeightMap heightMap) {
|
||||
BukkitWorldProperties properties = new BukkitWorldProperties(worldInfo);
|
||||
com.dfsek.terra.api.world.biome.generation.BiomeProvider biomeProvider = pack.getBiomeProvider();
|
||||
com.dfsek.terra.api.world.biome.generation.BiomeProvider biomeProvider = bind(worldInfo).getBiomeProvider();
|
||||
int y = properties.getMaxHeight();
|
||||
while(y > properties.getMinHeight() &&
|
||||
!isHeightmapBlock(heightMap, ((BukkitBlockState) delegate.getBlock(properties, x, y - 1, z, biomeProvider)).getHandle())) {
|
||||
@@ -105,6 +126,7 @@ public class BukkitChunkGeneratorWrapper extends org.bukkit.generator.ChunkGener
|
||||
|
||||
@Override
|
||||
public @NotNull List<BlockPopulator> getDefaultPopulators(@NotNull World world) {
|
||||
bind(world);
|
||||
return List.of(blockPopulator);
|
||||
}
|
||||
|
||||
@@ -134,15 +156,27 @@ public class BukkitChunkGeneratorWrapper extends org.bukkit.generator.ChunkGener
|
||||
}
|
||||
|
||||
public ConfigPack getPack() {
|
||||
return pack;
|
||||
ConfigPack bound = pack;
|
||||
if(bound == null) throw new IllegalStateException("Metapack \"" + metaPack.getID() + "\" has no world to pick a pack for yet");
|
||||
return bound;
|
||||
}
|
||||
|
||||
public @Nullable MetaPack getMetaPack() {
|
||||
return metaPack;
|
||||
}
|
||||
|
||||
public synchronized void setMetaPack(MetaPack metaPack, World world) {
|
||||
ConfigPack pack = MetaPackWorlds.packFor(metaPack, world.getEnvironment(), world.getName());
|
||||
this.metaPack = metaPack;
|
||||
setPack(pack);
|
||||
}
|
||||
|
||||
public void setPack(ConfigPack pack) {
|
||||
this.pack = pack;
|
||||
setDelegate(pack.getGeneratorProvider().newInstance(pack));
|
||||
BukkitBiomeProvider provider = biomeProvider;
|
||||
if(provider != null) provider.setDelegate(pack.getBiomeProvider());
|
||||
blockPopulator.setPack(pack);
|
||||
this.pack = pack; // last: bind() reads it without locking
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
package com.dfsek.terra.bukkit.generator;
|
||||
|
||||
import org.bukkit.World;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import com.dfsek.terra.api.config.ConfigPack;
|
||||
import com.dfsek.terra.api.config.MetaPack;
|
||||
|
||||
|
||||
/**
|
||||
* Finds a Bukkit world's pack in a metapack by the level stem for the world's environment.
|
||||
*/
|
||||
public final class MetaPackWorlds {
|
||||
private static final String MINECRAFT = "minecraft:";
|
||||
|
||||
private MetaPackWorlds() {
|
||||
}
|
||||
|
||||
public static @Nullable String stem(World.Environment environment) {
|
||||
return switch(environment) {
|
||||
case NORMAL -> "minecraft:overworld";
|
||||
case NETHER -> "minecraft:the_nether";
|
||||
case THE_END -> "minecraft:the_end";
|
||||
case CUSTOM -> null;
|
||||
};
|
||||
}
|
||||
|
||||
public static ConfigPack packFor(MetaPack metaPack, World.Environment environment, String worldName) {
|
||||
String stem = stem(environment);
|
||||
if(stem == null) {
|
||||
throw new IllegalArgumentException(
|
||||
"Metapack \"" + metaPack.getID() + "\" has no pack for world \"" + worldName + "\": environment " + environment +
|
||||
" has no level stem");
|
||||
}
|
||||
ConfigPack pack = lookup(metaPack.packs(), stem);
|
||||
if(pack == null) {
|
||||
throw new IllegalArgumentException(
|
||||
"Metapack \"" + metaPack.getID() + "\" has no pack for " + stem + " (world \"" + worldName + "\")");
|
||||
}
|
||||
return pack;
|
||||
}
|
||||
|
||||
private static @Nullable ConfigPack lookup(Map<String, ConfigPack> packs, String stem) {
|
||||
ConfigPack pack = packs.get(stem);
|
||||
return pack != null ? pack : packs.get(stem.substring(MINECRAFT.length()));
|
||||
}
|
||||
}
|
||||
+100
@@ -0,0 +1,100 @@
|
||||
package com.dfsek.terra.bukkit.generator;
|
||||
|
||||
import com.mojang.serialization.MapCodec;
|
||||
import net.minecraft.core.Holder;
|
||||
import net.minecraft.core.HolderGetter;
|
||||
import net.minecraft.core.registries.Registries;
|
||||
import net.minecraft.resources.Identifier;
|
||||
import net.minecraft.resources.ResourceKey;
|
||||
import net.minecraft.world.level.biome.Biome;
|
||||
import net.minecraft.world.level.biome.BiomeResolver;
|
||||
import net.minecraft.world.level.biome.BiomeSource;
|
||||
import net.minecraft.world.level.biome.Climate.Sampler;
|
||||
import org.bukkit.NamespacedKey;
|
||||
|
||||
import java.util.IdentityHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import com.dfsek.terra.api.config.ConfigPack;
|
||||
import com.dfsek.terra.api.world.biome.generation.BiomeProvider;
|
||||
import com.dfsek.terra.bukkit.world.BukkitPlatformBiome;
|
||||
|
||||
|
||||
/**
|
||||
* Biomes come straight from the pack's biome provider, with no climate sampling.
|
||||
*/
|
||||
public final class PaperBiomeSource extends BiomeSource {
|
||||
private final BukkitChunkGeneratorWrapper generator;
|
||||
private final Function<com.dfsek.terra.api.world.biome.Biome, Holder<Biome>> vanilla;
|
||||
private final long seed;
|
||||
private volatile Biomes biomes;
|
||||
|
||||
public PaperBiomeSource(BukkitChunkGeneratorWrapper generator, Function<com.dfsek.terra.api.world.biome.Biome, Holder<Biome>> vanilla,
|
||||
long seed) {
|
||||
this.generator = generator;
|
||||
this.vanilla = vanilla;
|
||||
this.seed = seed;
|
||||
this.biomes = biomes(generator.getPack());
|
||||
}
|
||||
|
||||
public Holder<Biome> biome(int quartX, int quartY, int quartZ) {
|
||||
Biomes current = current();
|
||||
com.dfsek.terra.api.world.biome.Biome biome = current.provider.getBiome(quartX << 2, quartY << 2, quartZ << 2, seed);
|
||||
Holder<Biome> holder = current.holders.get(biome);
|
||||
return holder != null ? holder : vanilla.apply(biome);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BiomeResolver createResolver(Sampler sampler) {
|
||||
return this::biome;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Stream<Holder<Biome>> collectPossibleBiomes() {
|
||||
return current().holders.values().stream().distinct();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected MapCodec<? extends BiomeSource> codec() {
|
||||
return MapCodec.assumeMapUnsafe(BiomeSource.CODEC);
|
||||
}
|
||||
|
||||
private Biomes current() {
|
||||
Biomes current = biomes;
|
||||
ConfigPack pack = generator.getPack();
|
||||
if(current.pack == pack) return current;
|
||||
Biomes reloaded = biomes(pack);
|
||||
biomes = reloaded;
|
||||
return reloaded;
|
||||
}
|
||||
|
||||
private Biomes biomes(ConfigPack pack) {
|
||||
BiomeProvider provider = pack.getBiomeProvider();
|
||||
Map<com.dfsek.terra.api.world.biome.Biome, Holder<Biome>> holders = new IdentityHashMap<>();
|
||||
for(com.dfsek.terra.api.world.biome.Biome biome : provider.getBiomes()) holders.put(biome, vanilla.apply(biome));
|
||||
return new Biomes(pack, provider, holders);
|
||||
}
|
||||
|
||||
public static Function<com.dfsek.terra.api.world.biome.Biome, Holder<Biome>> registered(HolderGetter<Biome> registry) {
|
||||
return biome -> holder(registry, key(BukkitBiomeProvider.toBukkit(biome).getKey()),
|
||||
key(((BukkitPlatformBiome) biome.getPlatformBiome()).getHandle().getKey()), biome.getID());
|
||||
}
|
||||
|
||||
/**
|
||||
* Falls back to the vanilla parent for a biome added by a reload while players were online.
|
||||
*/
|
||||
static Holder<Biome> holder(HolderGetter<Biome> registry, ResourceKey<Biome> registered, ResourceKey<Biome> parent, String id) {
|
||||
return registry.get(registered)
|
||||
.or(() -> registry.get(parent))
|
||||
.orElseThrow(() -> new IllegalStateException("Biome " + id + " has no registered vanilla biome"));
|
||||
}
|
||||
|
||||
private static ResourceKey<Biome> key(NamespacedKey key) {
|
||||
return ResourceKey.create(Registries.BIOME, Identifier.fromNamespaceAndPath(key.getNamespace(), key.getKey()));
|
||||
}
|
||||
|
||||
private record Biomes(ConfigPack pack, BiomeProvider provider, Map<com.dfsek.terra.api.world.biome.Biome, Holder<Biome>> holders) {
|
||||
}
|
||||
}
|
||||
+118
@@ -0,0 +1,118 @@
|
||||
package com.dfsek.terra.bukkit.generator;
|
||||
|
||||
import com.mojang.serialization.MapCodec;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.core.Holder;
|
||||
import net.minecraft.server.level.WorldGenRegion;
|
||||
import net.minecraft.world.level.LevelHeightAccessor;
|
||||
import net.minecraft.world.level.NoiseColumn;
|
||||
import net.minecraft.world.level.StructureManager;
|
||||
import net.minecraft.world.level.WorldGenLevel;
|
||||
import net.minecraft.world.level.biome.Biome;
|
||||
import net.minecraft.world.level.biome.BiomeManager;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import net.minecraft.world.level.chunk.ChunkAccess;
|
||||
import net.minecraft.world.level.chunk.ChunkGenerator;
|
||||
import net.minecraft.world.level.levelgen.Heightmap;
|
||||
import net.minecraft.world.level.levelgen.RandomState;
|
||||
import net.minecraft.world.level.levelgen.blending.Blender;
|
||||
import net.minecraft.world.level.levelgen.densityfunction.SamplerContext;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
import com.dfsek.terra.api.config.ConfigPack;
|
||||
import com.dfsek.terra.api.world.info.WorldProperties;
|
||||
import com.dfsek.terra.bukkit.attribute.PaperVanilla;
|
||||
import com.dfsek.terra.bukkit.world.block.data.BukkitBlockState;
|
||||
import com.dfsek.terra.minecraft.config.MinecraftCompatibilityOptions;
|
||||
import com.dfsek.terra.minecraft.generation.TerraTerrain;
|
||||
|
||||
|
||||
public final class PaperChunkGenerator extends ChunkGenerator {
|
||||
private final ChunkGenerator paper;
|
||||
private final BukkitChunkGeneratorWrapper generator;
|
||||
private final long seed;
|
||||
|
||||
public PaperChunkGenerator(ChunkGenerator paper, BukkitChunkGeneratorWrapper generator, PaperBiomeSource biomes, long seed) {
|
||||
super(biomes);
|
||||
this.paper = paper;
|
||||
this.generator = generator;
|
||||
this.seed = seed;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected MapCodec<? extends ChunkGenerator> codec() {
|
||||
return MapCodec.assumeMapUnsafe(ChunkGenerator.CODEC);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<ChunkAccess> buildTerrain(ChunkAccess chunk, Blender blender, RandomState randomState,
|
||||
StructureManager structures, BiomeManager biomeManager, WorldGenRegion region,
|
||||
Set<Holder<Biome>> biomes) {
|
||||
return paper.buildTerrain(chunk, blender, randomState, structures, biomeManager, region, biomes)
|
||||
.thenApply(built -> {
|
||||
ConfigPack pack = generator.getPack();
|
||||
TerraTerrain.beard(built, structures, generator.getHandle(), pack.getBiomeProvider(), properties(built),
|
||||
pack.getContext().get(MinecraftCompatibilityOptions.class), PaperChunkGenerator::vanilla);
|
||||
return built;
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getBaseHeight(int x, int z, Heightmap.Types heightmap, LevelHeightAccessor height, RandomState randomState) {
|
||||
return TerraTerrain.baseHeight(generator.getHandle(), generator.getPack().getBiomeProvider(), properties(height), height, x, z,
|
||||
heightmap.isOpaque(), PaperChunkGenerator::vanilla);
|
||||
}
|
||||
|
||||
@Override
|
||||
public NoiseColumn getBaseColumn(int x, int z, LevelHeightAccessor height, RandomState randomState) {
|
||||
return TerraTerrain.baseColumn(generator.getHandle(), generator.getPack().getBiomeProvider(), properties(height), height, x, z,
|
||||
PaperChunkGenerator::vanilla);
|
||||
}
|
||||
|
||||
private static BlockState vanilla(com.dfsek.terra.api.block.state.BlockState state) {
|
||||
return PaperVanilla.blockState(((BukkitBlockState) state).getHandle());
|
||||
}
|
||||
|
||||
private WorldProperties properties(LevelHeightAccessor height) {
|
||||
return new VanillaWorldProperties(seed, height.getMinY(), height.getMinY() + height.getHeight());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void applyBiomeDecoration(WorldGenLevel level, ChunkAccess chunk, StructureManager structures) {
|
||||
paper.applyBiomeDecoration(level, chunk, structures);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void spawnOriginalMobs(WorldGenRegion region) {
|
||||
paper.spawnOriginalMobs(region);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getGenDepth() {
|
||||
return paper.getGenDepth();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSeaLevel() {
|
||||
return paper.getSeaLevel();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMinY() {
|
||||
return paper.getMinY();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addDebugScreenInfo(List<String> text, RandomState randomState, BlockPos pos, SamplerContext context) {
|
||||
}
|
||||
|
||||
private record VanillaWorldProperties(long getSeed, int getMinHeight, int getMaxHeight) implements WorldProperties {
|
||||
@Override
|
||||
public Object getHandle() {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
package com.dfsek.terra.bukkit.generator;
|
||||
|
||||
import net.minecraft.core.registries.Registries;
|
||||
import net.minecraft.server.level.ServerLevel;
|
||||
import net.minecraft.world.level.chunk.ChunkGenerator;
|
||||
import org.bukkit.World;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.dfsek.terra.bukkit.attribute.PaperVanilla;
|
||||
import com.dfsek.terra.bukkit.attribute.WyckBridge;
|
||||
|
||||
|
||||
public final class PaperGenerators {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(PaperGenerators.class);
|
||||
|
||||
private PaperGenerators() {
|
||||
}
|
||||
|
||||
public static void install(World world, BukkitChunkGeneratorWrapper wrapper) {
|
||||
try {
|
||||
wrapper.bind(world);
|
||||
ServerLevel level = PaperVanilla.level(world);
|
||||
ChunkGenerator paper = level.getChunkSource().getGenerator();
|
||||
if(paper instanceof PaperChunkGenerator) return;
|
||||
long seed = level.getSeed();
|
||||
PaperBiomeSource biomes = new PaperBiomeSource(wrapper,
|
||||
PaperBiomeSource.registered(level.registryAccess().lookupOrThrow(Registries.BIOME)), seed);
|
||||
WyckBridge.installGenerator(world, new PaperChunkGenerator(paper, wrapper, biomes, seed));
|
||||
} catch(RuntimeException e) {
|
||||
LOGGER.warn("Could not replace Paper's generator in world \"{}\". Falling back to it: no structure beards, slower biome lookups",
|
||||
world.getName(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
+9
-7
@@ -6,6 +6,7 @@ import org.mvplugins.multiverse.external.jetbrains.annotations.NotNull;
|
||||
import org.mvplugins.multiverse.external.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import com.dfsek.terra.api.Platform;
|
||||
import com.dfsek.terra.api.registry.key.Keyed;
|
||||
@@ -26,22 +27,23 @@ public final class MultiverseGeneratorPluginHook implements GeneratorPlugin {
|
||||
|
||||
@Override
|
||||
public @NotNull Collection<String> suggestIds(@Nullable String s) {
|
||||
return platform.getConfigRegistry().entries().stream()
|
||||
.map(Keyed::getID)
|
||||
.toList();
|
||||
return ids().toList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable Collection<String> getExampleUsages() {
|
||||
return platform.getConfigRegistry()
|
||||
.entries()
|
||||
.stream()
|
||||
.map(Keyed::getID)
|
||||
return ids()
|
||||
.map("/mv create example_world NORMAL -g Terra:%s"::formatted)
|
||||
.limit(5) // reasonable amount
|
||||
.toList();
|
||||
}
|
||||
|
||||
private Stream<String> ids() {
|
||||
return Stream.concat(platform.getConfigRegistry().entries().stream().map(Keyed::getID),
|
||||
platform.getMetaConfigRegistry().entries().stream().map(Keyed::getID))
|
||||
.distinct();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable String getInfoLink() {
|
||||
return "https://terra.polydev.org/";
|
||||
|
||||
+4
-1
@@ -24,6 +24,7 @@ import org.bukkit.event.world.WorldInitEvent;
|
||||
|
||||
import com.dfsek.terra.bukkit.attribute.PaperDimensions;
|
||||
import com.dfsek.terra.bukkit.generator.BukkitChunkGeneratorWrapper;
|
||||
import com.dfsek.terra.bukkit.generator.PaperGenerators;
|
||||
|
||||
|
||||
public class DimensionListener implements Listener {
|
||||
@@ -36,6 +37,8 @@ public class DimensionListener implements Listener {
|
||||
@EventHandler(priority = EventPriority.LOWEST)
|
||||
public void onWorldInit(WorldInitEvent event) {
|
||||
World world = event.getWorld();
|
||||
if(world.getGenerator() instanceof BukkitChunkGeneratorWrapper wrapper) dimensions.onWorldInit(world, wrapper);
|
||||
if(!(world.getGenerator() instanceof BukkitChunkGeneratorWrapper wrapper)) return;
|
||||
dimensions.onWorldInit(world, wrapper);
|
||||
PaperGenerators.install(world, wrapper);
|
||||
}
|
||||
}
|
||||
+20
-37
@@ -21,12 +21,10 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
@@ -46,6 +44,7 @@ import com.dfsek.terra.minecraft.attribute.BiomeTags.PlatformTagSource;
|
||||
import com.dfsek.terra.minecraft.attribute.MinecraftAttributeParents;
|
||||
import com.dfsek.terra.minecraft.attribute.MinecraftBiomeFactory;
|
||||
import com.dfsek.terra.minecraft.attribute.PackParents;
|
||||
import com.dfsek.terra.minecraft.attribute.TerraBiomeIndex;
|
||||
import com.dfsek.terra.minecraft.attribute.VanillaContext;
|
||||
import com.dfsek.terra.world.attribute.platform.AttributeReport;
|
||||
import com.dfsek.terra.world.attribute.platform.AttributeSlots;
|
||||
@@ -53,8 +52,7 @@ import com.dfsek.terra.world.attribute.platform.AttributeSlots;
|
||||
|
||||
public final class BiomeHandler {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(BiomeHandler.class);
|
||||
private static final Map<Identifier, List<Identifier>> BY_PARENT = new HashMap<>();
|
||||
private static final Map<Identifier, PlatformTagSource> PLATFORM_TAGS = new HashMap<>();
|
||||
private static TerraBiomeIndex biomes = new TerraBiomeIndex();
|
||||
|
||||
private BiomeHandler() { }
|
||||
|
||||
@@ -63,8 +61,7 @@ public final class BiomeHandler {
|
||||
*/
|
||||
public static synchronized void registerBiomes(BukkitPlatform platform) {
|
||||
MinecraftAttributeParents parents = platform.attributeParents();
|
||||
VanillaContext context = parents.context();
|
||||
if(context == null) throw new IllegalStateException("no registry load has begun");
|
||||
VanillaContext context = parents.requireContext();
|
||||
MinecraftBiomeFactory factory = new MinecraftBiomeFactory(platform.getAttributeHandle(), context);
|
||||
Registry<Biome> registry = PaperVanilla.server().registryAccess().lookupOrThrow(Registries.BIOME);
|
||||
org.bukkit.Registry<org.bukkit.block.Biome> bukkitRegistry = RegistryAccess.registryAccess().getRegistry(RegistryKey.BIOME);
|
||||
@@ -86,7 +83,7 @@ public final class BiomeHandler {
|
||||
platformBiome.setParentEntries(parentEntries(() -> parents.biomeEntries(packParents, parentKey, parent), pack.getID(),
|
||||
biome.getID(), LOGGER::warn));
|
||||
|
||||
Identifier terraId = Identifier.fromNamespaceAndPath("terra", createBiomeID(pack, id));
|
||||
Identifier terraId = MinecraftBiomeFactory.terraId(pack, id);
|
||||
packRun.add(terraId, parentKey.identifier(), registry.containsKey(terraId), () -> {
|
||||
if(playersOnline) {
|
||||
deferred.incrementAndGet();
|
||||
@@ -109,16 +106,13 @@ public final class BiomeHandler {
|
||||
}), LOGGER::warn);
|
||||
|
||||
if(deferred.get() > 0) {
|
||||
LOGGER.info("Players are online, so {} new custom biomes generate as their vanilla biomes until the server restarts.",
|
||||
LOGGER.info("{} new custom biomes generate as their vanilla parents until the server restarts (players were online during the reload).",
|
||||
deferred.get());
|
||||
}
|
||||
registerNew(run.toRegister, WyckBridge::registerBiomes, LOGGER::info);
|
||||
Map<ResourceKey<Biome>, ResourceKey<VillagerType>> villagerTypes = PaperVanilla.villagerTypes();
|
||||
run.villagerTypes.forEach((terraId, type) -> villagerTypes.put(ResourceKey.create(Registries.BIOME, terraId), type));
|
||||
BY_PARENT.clear();
|
||||
BY_PARENT.putAll(run.byParent);
|
||||
PLATFORM_TAGS.clear();
|
||||
PLATFORM_TAGS.putAll(run.platformTags);
|
||||
biomes = run.biomes;
|
||||
run.afterRegistration.forEach(Runnable::run);
|
||||
}
|
||||
|
||||
@@ -166,8 +160,7 @@ public final class BiomeHandler {
|
||||
private final @Nullable Run<B> before;
|
||||
final List<B> toRegister = new ArrayList<>();
|
||||
final Map<Identifier, ResourceKey<VillagerType>> villagerTypes = new LinkedHashMap<>();
|
||||
final Map<Identifier, List<Identifier>> byParent = new LinkedHashMap<>();
|
||||
final Map<Identifier, PlatformTagSource> platformTags = new LinkedHashMap<>();
|
||||
final TerraBiomeIndex biomes = new TerraBiomeIndex();
|
||||
final List<Runnable> afterRegistration = new ArrayList<>();
|
||||
|
||||
Run(@Nullable Run<B> before) {
|
||||
@@ -177,8 +170,7 @@ public final class BiomeHandler {
|
||||
void add(Identifier terraId, Identifier parent, boolean registered, Supplier<@Nullable Registration<B>> registration,
|
||||
PlatformTagSource tags, Runnable after, String pack, String config, Consumer<String> warn) {
|
||||
if(recorded(terraId)) {
|
||||
warn.accept("Pack \"" + pack + "\", config \"" + config + "\": biome \"" + terraId
|
||||
+ "\" shared with another config: their IDs differ only in case");
|
||||
warn.accept(TerraBiomeIndex.sharedBiome(pack, config, terraId));
|
||||
afterRegistration.add(after);
|
||||
return;
|
||||
}
|
||||
@@ -188,20 +180,18 @@ public final class BiomeHandler {
|
||||
toRegister.add(queued.biome());
|
||||
villagerTypes.put(terraId, queued.villagerType());
|
||||
}
|
||||
byParent.computeIfAbsent(parent, i -> new ArrayList<>()).add(terraId);
|
||||
platformTags.put(terraId, tags);
|
||||
biomes.record(terraId, parent, tags);
|
||||
afterRegistration.add(after);
|
||||
}
|
||||
|
||||
private boolean recorded(Identifier terraId) {
|
||||
return platformTags.containsKey(terraId) || (before != null && before.recorded(terraId));
|
||||
return biomes.contains(terraId) || (before != null && before.recorded(terraId));
|
||||
}
|
||||
|
||||
private void addAll(Run<B> run) {
|
||||
toRegister.addAll(run.toRegister);
|
||||
villagerTypes.putAll(run.villagerTypes);
|
||||
run.byParent.forEach((parent, biomes) -> byParent.computeIfAbsent(parent, i -> new ArrayList<>()).addAll(biomes));
|
||||
platformTags.putAll(run.platformTags);
|
||||
biomes.addAll(run.biomes);
|
||||
afterRegistration.addAll(run.afterRegistration);
|
||||
}
|
||||
}
|
||||
@@ -230,7 +220,7 @@ public final class BiomeHandler {
|
||||
Map<TagKey<Biome>, List<Holder<Biome>>> after = new LinkedHashMap<>();
|
||||
before.forEach((tag, members) -> after.put(tag, new ArrayList<>(members)));
|
||||
|
||||
BiomeTags.addTerraBiomes(after, id -> registry.get(id).map(h -> h), BY_PARENT, PLATFORM_TAGS);
|
||||
BiomeTags.addTerraBiomes(after, id -> registry.get(id).map(h -> h), biomes);
|
||||
|
||||
Map<TagKey<Biome>, List<Identifier>> additions = additions(before, after);
|
||||
int added = appendAll(additions, WyckBridge::appendBiomeTag, "wyck could not append to the tag", LOGGER::warn);
|
||||
@@ -247,8 +237,7 @@ public final class BiomeHandler {
|
||||
registrar.getAllTags().forEach((tag, members) -> tags.put(TagKey.create(Registries.BIOME, identifier(tag.key())),
|
||||
members.stream().map(member -> identifier(member.key())).toList()));
|
||||
|
||||
Map<TagKey<Biome>, List<Identifier>> additions = reloadAdditions(tags, id -> registry.get(id).map(h -> h), BY_PARENT,
|
||||
PLATFORM_TAGS);
|
||||
Map<TagKey<Biome>, List<Identifier>> additions = reloadAdditions(tags, id -> registry.get(id).map(h -> h), biomes);
|
||||
int added = appendAll(additions, (tag, biomes) -> registrar.addToTag(
|
||||
io.papermc.paper.registry.tag.TagKey.create(RegistryKey.BIOME, key(tag.location())),
|
||||
biomes.stream().map(biome -> TypedKey.create(RegistryKey.BIOME, key(biome))).toList()),
|
||||
@@ -265,24 +254,23 @@ public final class BiomeHandler {
|
||||
*/
|
||||
static Map<TagKey<Biome>, List<Identifier>> reloadAdditions(Map<TagKey<Biome>, List<Identifier>> tags,
|
||||
Function<Identifier, Optional<Holder<Biome>>> lookup,
|
||||
Map<Identifier, List<Identifier>> byParent,
|
||||
Map<Identifier, PlatformTagSource> platformTags) {
|
||||
TerraBiomeIndex biomes) {
|
||||
Map<TagKey<Biome>, List<Holder<Biome>>> before = new LinkedHashMap<>();
|
||||
Map<TagKey<Biome>, List<Holder<Biome>>> after = new LinkedHashMap<>();
|
||||
tags.forEach((tag, members) -> {
|
||||
List<Holder<Biome>> old = members.stream().map(lookup).flatMap(Optional::stream).toList();
|
||||
List<Holder<Biome>> biomes = new ArrayList<>(old);
|
||||
List<Holder<Biome>> withTerra = new ArrayList<>(old);
|
||||
for(Identifier member : members) {
|
||||
for(Identifier terra : byParent.getOrDefault(member, List.of())) {
|
||||
lookup.apply(terra).ifPresentOrElse(biomes::add,
|
||||
() -> LOGGER.debug("Terra biome {} is not registered, so it is not added to the reloaded tag {}", terra,
|
||||
for(Identifier terra : biomes.children(member)) {
|
||||
lookup.apply(terra).ifPresentOrElse(withTerra::add,
|
||||
() -> LOGGER.debug("Terra biome {} is not registered; left out of reloaded tag {}", terra,
|
||||
tag.location()));
|
||||
}
|
||||
}
|
||||
before.put(tag, old);
|
||||
after.put(tag, biomes);
|
||||
after.put(tag, withTerra);
|
||||
});
|
||||
BiomeTags.addTerraBiomes(after, lookup, Map.of(), platformTags);
|
||||
BiomeTags.addPlatformTags(after, lookup, biomes.tags());
|
||||
return additions(before, after);
|
||||
}
|
||||
|
||||
@@ -359,9 +347,4 @@ public final class BiomeHandler {
|
||||
return appended;
|
||||
}
|
||||
|
||||
public static String createBiomeID(ConfigPack pack, com.dfsek.terra.api.registry.key.RegistryKey biomeID) {
|
||||
return pack.getID().toLowerCase(Locale.ROOT)
|
||||
+ "/" + biomeID.getNamespace().toLowerCase(Locale.ROOT)
|
||||
+ "/" + biomeID.getID().toLowerCase(Locale.ROOT);
|
||||
}
|
||||
}
|
||||
+83
@@ -0,0 +1,83 @@
|
||||
package com.dfsek.terra.bukkit.generator;
|
||||
|
||||
import org.bukkit.World.Environment;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.util.Map;
|
||||
|
||||
import com.dfsek.terra.api.config.ConfigPack;
|
||||
import com.dfsek.terra.api.config.MetaPack;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
|
||||
class MetaPackWorldsTest {
|
||||
private static final ConfigPack OVERWORLD = pack("OVERWORLD");
|
||||
private static final ConfigPack NETHER = pack("NETHER");
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"minecraft:overworld, NORMAL",
|
||||
"overworld, NORMAL",
|
||||
"minecraft:the_nether, NETHER",
|
||||
"the_nether, NETHER"
|
||||
})
|
||||
void picksThePackForTheWorldsStem(String key, Environment environment) {
|
||||
assertSame(OVERWORLD, MetaPackWorlds.packFor(metaPack(Map.of(key, OVERWORLD)), environment, "world"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void namespacedKeyWins() {
|
||||
MetaPack metaPack = metaPack(Map.of("overworld", NETHER, "minecraft:overworld", OVERWORLD));
|
||||
|
||||
assertSame(OVERWORLD, MetaPackWorlds.packFor(metaPack, Environment.NORMAL, "world"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void missingStemNamesTheMetapackStemAndWorld() {
|
||||
MetaPack metaPack = metaPack(Map.of("minecraft:overworld", OVERWORLD));
|
||||
|
||||
IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
|
||||
() -> MetaPackWorlds.packFor(metaPack, Environment.THE_END, "world_the_end"));
|
||||
|
||||
assertEquals("Metapack \"DEFAULT\" has no pack for minecraft:the_end (world \"world_the_end\")", e.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void customEnvironmentHasNoStem() {
|
||||
MetaPack metaPack = metaPack(Map.of("minecraft:overworld", OVERWORLD));
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> MetaPackWorlds.packFor(metaPack, Environment.CUSTOM, "custom"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void unboundWrapperRefusesGetPack() {
|
||||
BukkitChunkGeneratorWrapper wrapper = new BukkitChunkGeneratorWrapper(metaPack(Map.of()), null);
|
||||
|
||||
assertThrows(IllegalStateException.class, wrapper::getPack);
|
||||
}
|
||||
|
||||
private static MetaPack metaPack(Map<String, ConfigPack> packs) {
|
||||
return (MetaPack) Proxy.newProxyInstance(MetaPack.class.getClassLoader(), new Class<?>[]{ MetaPack.class },
|
||||
(proxy, method, args) -> switch(method.getName()) {
|
||||
case "packs" -> packs;
|
||||
case "getID" -> "DEFAULT";
|
||||
default -> throw new UnsupportedOperationException(method.getName());
|
||||
});
|
||||
}
|
||||
|
||||
private static ConfigPack pack(String id) {
|
||||
return (ConfigPack) Proxy.newProxyInstance(ConfigPack.class.getClassLoader(), new Class<?>[]{ ConfigPack.class },
|
||||
(proxy, method, args) -> switch(method.getName()) {
|
||||
case "getID", "toString" -> id;
|
||||
case "hashCode" -> System.identityHashCode(proxy);
|
||||
case "equals" -> proxy == args[0];
|
||||
default -> throw new UnsupportedOperationException(method.getName());
|
||||
});
|
||||
}
|
||||
}
|
||||
+118
@@ -0,0 +1,118 @@
|
||||
package com.dfsek.terra.bukkit.generator;
|
||||
|
||||
import net.minecraft.core.Holder;
|
||||
import net.minecraft.core.HolderGetter;
|
||||
import net.minecraft.core.registries.Registries;
|
||||
import net.minecraft.resources.Identifier;
|
||||
import net.minecraft.resources.ResourceKey;
|
||||
import net.minecraft.world.level.biome.Biome;
|
||||
import net.minecraft.world.level.biome.Biomes;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import com.dfsek.terra.api.config.ConfigPack;
|
||||
import com.dfsek.terra.api.world.biome.generation.BiomeProvider;
|
||||
import com.dfsek.terra.api.world.chunk.generation.util.provider.ChunkGeneratorProvider;
|
||||
import com.dfsek.terra.minecraft.attribute.MinecraftTestSupport;
|
||||
import com.dfsek.terra.minecraft.attribute.TestBiome;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
|
||||
class PaperBiomeSourceTest {
|
||||
private static final HolderGetter<Biome> BIOMES = MinecraftTestSupport.REGISTRIES.lookupOrThrow(Registries.BIOME);
|
||||
private static final ResourceKey<Biome> UNREGISTERED = ResourceKey.create(Registries.BIOME, Identifier.parse("terra:late"));
|
||||
private static final TestBiome DESERT = new TestBiome("desert", 0, Biomes.DESERT.identifier());
|
||||
private static final TestBiome PLAINS = new TestBiome("plains", 1, Biomes.PLAINS.identifier());
|
||||
private static final Map<com.dfsek.terra.api.world.biome.Biome, ResourceKey<Biome>> KEYS = Map.of(DESERT, Biomes.DESERT, PLAINS,
|
||||
Biomes.PLAINS);
|
||||
|
||||
private final List<List<Integer>> queries = new ArrayList<>();
|
||||
|
||||
@Test
|
||||
void resolvesAtBlockCoordinatesWithoutTheSampler() {
|
||||
PaperBiomeSource source = source(pack(provider(DESERT)));
|
||||
|
||||
Holder<Biome> holder = source.createResolver(null).getNoiseBiome(3, -2, 5);
|
||||
|
||||
assertEquals(Biomes.DESERT, key(holder));
|
||||
assertEquals(List.of(List.of(12, -8, 20)), queries);
|
||||
}
|
||||
|
||||
@Test
|
||||
void possibleBiomesAreThePacksBiomes() {
|
||||
PaperBiomeSource source = source(pack(provider(DESERT, DESERT, PLAINS)));
|
||||
|
||||
assertEquals(Set.of(Biomes.DESERT, Biomes.PLAINS),
|
||||
source.possibleBiomes().stream().map(PaperBiomeSourceTest::key).collect(Collectors.toSet()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void followsAReloadedPack() {
|
||||
BukkitChunkGeneratorWrapper wrapper = new BukkitChunkGeneratorWrapper(null, pack(provider(DESERT)), null);
|
||||
PaperBiomeSource source = new PaperBiomeSource(wrapper, biome -> BIOMES.getOrThrow(KEYS.get(biome)), 0);
|
||||
|
||||
wrapper.setPack(pack(provider(PLAINS)));
|
||||
|
||||
assertEquals(Biomes.PLAINS, key(source.biome(0, 0, 0)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void registeredBiomeWins() {
|
||||
assertEquals(Biomes.DESERT, key(PaperBiomeSource.holder(BIOMES, Biomes.DESERT, Biomes.JUNGLE, "desert")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void unregisteredBiomeFallsBackToItsParent() {
|
||||
assertEquals(Biomes.JUNGLE, key(PaperBiomeSource.holder(BIOMES, UNREGISTERED, Biomes.JUNGLE, "late")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void neitherRegisteredFails() {
|
||||
assertThrows(IllegalStateException.class, () -> PaperBiomeSource.holder(BIOMES, UNREGISTERED, UNREGISTERED, "late"));
|
||||
}
|
||||
|
||||
private PaperBiomeSource source(ConfigPack pack) {
|
||||
return new PaperBiomeSource(new BukkitChunkGeneratorWrapper(null, pack, null), biome -> BIOMES.getOrThrow(KEYS.get(biome)), 0);
|
||||
}
|
||||
|
||||
private BiomeProvider provider(com.dfsek.terra.api.world.biome.Biome first, com.dfsek.terra.api.world.biome.Biome... rest) {
|
||||
List<com.dfsek.terra.api.world.biome.Biome> all = new ArrayList<>(List.of(rest));
|
||||
if(!all.contains(first)) all.addFirst(first);
|
||||
return new BiomeProvider() {
|
||||
@Override
|
||||
public com.dfsek.terra.api.world.biome.Biome getBiome(int x, int y, int z, long seed) {
|
||||
queries.add(List.of(x, y, z));
|
||||
return first;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<com.dfsek.terra.api.world.biome.Biome> getBiomes() {
|
||||
return all;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private static ConfigPack pack(BiomeProvider provider) {
|
||||
ChunkGeneratorProvider generators = pack -> null;
|
||||
return (ConfigPack) Proxy.newProxyInstance(ConfigPack.class.getClassLoader(), new Class<?>[]{ ConfigPack.class },
|
||||
(proxy, method, args) -> switch(method.getName()) {
|
||||
case "getBiomeProvider" -> provider;
|
||||
case "getGeneratorProvider" -> generators;
|
||||
case "hashCode" -> System.identityHashCode(proxy);
|
||||
case "equals" -> proxy == args[0];
|
||||
default -> throw new UnsupportedOperationException(method.getName());
|
||||
});
|
||||
}
|
||||
|
||||
private static ResourceKey<Biome> key(Holder<Biome> holder) {
|
||||
return holder.unwrapKey().orElseThrow();
|
||||
}
|
||||
}
|
||||
+13
-11
@@ -45,6 +45,7 @@ import com.dfsek.terra.api.config.ConfigPack;
|
||||
import com.dfsek.terra.api.registry.key.RegistryKey;
|
||||
import com.dfsek.terra.minecraft.attribute.BiomeTags.PlatformTagSource;
|
||||
import com.dfsek.terra.minecraft.attribute.MinecraftBiomeFactory;
|
||||
import com.dfsek.terra.minecraft.attribute.TerraBiomeIndex;
|
||||
import com.dfsek.terra.world.attribute.platform.AttributeReport;
|
||||
import com.dfsek.terra.world.attribute.platform.AttributeSlots;
|
||||
|
||||
@@ -188,8 +189,8 @@ class BiomeHandlerTest {
|
||||
assertEquals(List.of("first/a", "last/a"), run.toRegister);
|
||||
assertEquals(List.of(Identifier.parse("terra:first/a"), Identifier.parse("terra:last/a")),
|
||||
List.copyOf(run.villagerTypes.keySet()));
|
||||
assertEquals(Map.of(PLAINS, List.of(Identifier.parse("terra:first/a"), Identifier.parse("terra:last/a"))), run.byParent);
|
||||
assertEquals(Set.of(Identifier.parse("terra:first/a"), Identifier.parse("terra:last/a")), run.platformTags.keySet());
|
||||
assertEquals(Map.of(PLAINS, List.of(Identifier.parse("terra:first/a"), Identifier.parse("terra:last/a"))), run.biomes.byParent());
|
||||
assertEquals(Set.of(Identifier.parse("terra:first/a"), Identifier.parse("terra:last/a")), run.biomes.tags().keySet());
|
||||
assertEquals(List.of("first/a", "last/a"), after);
|
||||
}
|
||||
|
||||
@@ -202,7 +203,7 @@ class BiomeHandlerTest {
|
||||
|
||||
assertEquals(List.of(), run.toRegister);
|
||||
assertEquals(Map.of(), run.villagerTypes);
|
||||
assertEquals(Map.of(PLAINS, List.of(Identifier.parse("terra:test/a"))), run.byParent);
|
||||
assertEquals(Map.of(PLAINS, List.of(Identifier.parse("terra:test/a"))), run.biomes.byParent());
|
||||
assertEquals(1, run.afterRegistration.size());
|
||||
}
|
||||
|
||||
@@ -224,12 +225,12 @@ class BiomeHandlerTest {
|
||||
Identifier snowy = Identifier.parse("terra:test/snowy");
|
||||
assertEquals(List.of("test/Snowy"), run.toRegister);
|
||||
assertEquals(Map.of(snowy, VillagerType.SNOW), run.villagerTypes);
|
||||
assertEquals(Map.of(PLAINS, List.of(snowy)), run.byParent);
|
||||
assertEquals("Snowy", run.platformTags.get(snowy).config());
|
||||
assertEquals(Map.of(PLAINS, List.of(snowy)), run.biomes.byParent());
|
||||
assertEquals("Snowy", run.biomes.tags().get(snowy).config());
|
||||
assertEquals(List.of("test/Snowy", "test/SNOWY", "TEST/snowy"), after);
|
||||
assertEquals(List.of(
|
||||
"Pack \"test\", config \"SNOWY\": biome \"terra:test/snowy\" shared with another config: their IDs differ only in case",
|
||||
"Pack \"TEST\", config \"snowy\": biome \"terra:test/snowy\" shared with another config: their IDs differ only in case"),
|
||||
"Pack \"test\", config \"SNOWY\": reusing biome \"terra:test/snowy\"; another config's ID matches apart from case",
|
||||
"Pack \"TEST\", config \"snowy\": reusing biome \"terra:test/snowy\"; another config's ID matches apart from case"),
|
||||
warnings);
|
||||
}
|
||||
|
||||
@@ -269,8 +270,10 @@ class BiomeHandlerTest {
|
||||
RegistryKey.parse("minecraft:is_forest")));
|
||||
PlatformTagSource source = new PlatformTagSource(new AttributeReport("test", warnings::add, m -> { }), "BIOME", platformTags);
|
||||
|
||||
Map<TagKey<Biome>, List<Identifier>> additions = BiomeHandler.reloadAdditions(tags, BiomeHandlerTest::lookup,
|
||||
Map.of(plains, List.of(terra)), Map.of(terra, source));
|
||||
TerraBiomeIndex biomes = new TerraBiomeIndex();
|
||||
biomes.record(terra, plains, source);
|
||||
|
||||
Map<TagKey<Biome>, List<Identifier>> additions = BiomeHandler.reloadAdditions(tags, BiomeHandlerTest::lookup, biomes);
|
||||
|
||||
assertEquals(List.of(villagePlains, isForest, hasSnow), List.copyOf(additions.keySet()));
|
||||
assertEquals(List.of(terra), additions.get(villagePlains));
|
||||
@@ -280,7 +283,6 @@ class BiomeHandlerTest {
|
||||
|
||||
Map<TagKey<Biome>, List<Identifier>> reloaded = new LinkedHashMap<>(tags);
|
||||
additions.forEach((tag, ids) -> reloaded.merge(tag, ids, (old, added) -> Stream.concat(old.stream(), added.stream()).toList()));
|
||||
assertEquals(Map.of(), BiomeHandler.reloadAdditions(reloaded, BiomeHandlerTest::lookup, Map.of(plains, List.of(terra)),
|
||||
Map.of(terra, source)));
|
||||
assertEquals(Map.of(), BiomeHandler.reloadAdditions(reloaded, BiomeHandlerTest::lookup, biomes));
|
||||
}
|
||||
}
|
||||
+7
-3
@@ -30,9 +30,8 @@ public final class BiomeTags {
|
||||
public record PlatformTagSource(AttributeReport report, String config, Set<RegistryKey> tags) { }
|
||||
|
||||
public static void addTerraBiomes(Map<TagKey<Biome>, List<Holder<Biome>>> collect,
|
||||
Function<Identifier, Optional<Holder<Biome>>> lookup,
|
||||
Map<Identifier, List<Identifier>> byParent, Map<Identifier, PlatformTagSource> platformTags) {
|
||||
byParent.forEach((vb, terraBiomes) ->
|
||||
Function<Identifier, Optional<Holder<Biome>>> lookup, TerraBiomeIndex biomes) {
|
||||
biomes.byParent().forEach((vb, terraBiomes) ->
|
||||
lookup.apply(vb).ifPresentOrElse(
|
||||
vanilla -> terraBiomes.forEach(tb -> lookup.apply(tb).ifPresentOrElse(
|
||||
terra -> {
|
||||
@@ -42,7 +41,12 @@ public final class BiomeTags {
|
||||
},
|
||||
() -> logger.error("No such biome: {}", tb))),
|
||||
() -> logger.error("No vanilla biome: {}", vb)));
|
||||
addPlatformTags(collect, lookup, biomes.tags());
|
||||
}
|
||||
|
||||
public static void addPlatformTags(Map<TagKey<Biome>, List<Holder<Biome>>> collect,
|
||||
Function<Identifier, Optional<Holder<Biome>>> lookup,
|
||||
Map<Identifier, PlatformTagSource> platformTags) {
|
||||
Set<TagKey<Biome>> existing = new HashSet<>(collect.keySet());
|
||||
platformTags.forEach((tb, source) -> {
|
||||
if(source.tags().isEmpty()) return;
|
||||
|
||||
+6
@@ -70,6 +70,12 @@ public final class MinecraftAttributeParents implements AttributeParents {
|
||||
return state.context();
|
||||
}
|
||||
|
||||
public VanillaContext requireContext() {
|
||||
VanillaContext context = state.context();
|
||||
if(context == null) throw new IllegalStateException("no registry load has begun");
|
||||
return context;
|
||||
}
|
||||
|
||||
public PackParents prepare(ConfigPack pack, Optional<PackAttributes> attributes, AttributeReport report) {
|
||||
return prepare(pack, attributes, report, BuiltinDimensionTypes.OVERWORLD);
|
||||
}
|
||||
|
||||
+8
@@ -14,9 +14,11 @@ import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
import com.dfsek.terra.api.config.ConfigPack;
|
||||
import com.dfsek.terra.api.registry.key.RegistryKey;
|
||||
import com.dfsek.terra.api.world.attribute.Attribute;
|
||||
import com.dfsek.terra.api.world.attribute.AttributeEntry;
|
||||
@@ -33,6 +35,12 @@ public final class MinecraftBiomeFactory {
|
||||
private final VanillaContext context;
|
||||
private final VanillaAttributeMaps maps;
|
||||
|
||||
public static Identifier terraId(ConfigPack pack, RegistryKey biome) {
|
||||
return Identifier.fromNamespaceAndPath("terra", pack.getID().toLowerCase(Locale.ROOT) + "/"
|
||||
+ biome.getNamespace().toLowerCase(Locale.ROOT) + "/"
|
||||
+ biome.getID().toLowerCase(Locale.ROOT));
|
||||
}
|
||||
|
||||
public MinecraftBiomeFactory(AttributeHandle handle, VanillaContext context) {
|
||||
this.handle = Objects.requireNonNull(handle, "handle");
|
||||
this.context = Objects.requireNonNull(context, "context");
|
||||
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
package com.dfsek.terra.minecraft.attribute;
|
||||
|
||||
import net.minecraft.resources.Identifier;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.dfsek.terra.minecraft.attribute.BiomeTags.PlatformTagSource;
|
||||
|
||||
|
||||
/**
|
||||
* Registered Terra biomes grouped by vanilla parent, and the platform tags of each.
|
||||
*/
|
||||
public final class TerraBiomeIndex {
|
||||
private final Map<Identifier, List<Identifier>> byParent = new LinkedHashMap<>();
|
||||
private final Map<Identifier, PlatformTagSource> tags = new LinkedHashMap<>();
|
||||
|
||||
/**
|
||||
* Warning for a config whose lowercased biome ID is already taken. The config reuses that biome.
|
||||
*/
|
||||
public static String sharedBiome(String pack, String config, Identifier terraId) {
|
||||
return "Pack \"" + pack + "\", config \"" + config + "\": reusing biome \"" + terraId
|
||||
+ "\"; another config's ID matches apart from case";
|
||||
}
|
||||
|
||||
public boolean contains(Identifier terraId) {
|
||||
return tags.containsKey(terraId);
|
||||
}
|
||||
|
||||
public void record(Identifier terraId, Identifier parent, PlatformTagSource source) {
|
||||
byParent.computeIfAbsent(parent, i -> new ArrayList<>()).add(terraId);
|
||||
tags.put(terraId, source);
|
||||
}
|
||||
|
||||
public void addAll(TerraBiomeIndex other) {
|
||||
other.byParent.forEach((parent, biomes) -> byParent.computeIfAbsent(parent, i -> new ArrayList<>()).addAll(biomes));
|
||||
tags.putAll(other.tags);
|
||||
}
|
||||
|
||||
public List<Identifier> children(Identifier parent) {
|
||||
return byParent.getOrDefault(parent, List.of());
|
||||
}
|
||||
|
||||
public Map<Identifier, List<Identifier>> byParent() {
|
||||
return Collections.unmodifiableMap(byParent);
|
||||
}
|
||||
|
||||
public Map<Identifier, PlatformTagSource> tags() {
|
||||
return Collections.unmodifiableMap(tags);
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -15,7 +15,7 @@
|
||||
* along with Terra. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.dfsek.terra.bukkit.config;
|
||||
package com.dfsek.terra.minecraft.config;
|
||||
|
||||
import com.dfsek.tectonic.api.config.template.ConfigTemplate;
|
||||
import com.dfsek.tectonic.api.config.template.annotations.Default;
|
||||
@@ -25,7 +25,7 @@ import com.dfsek.terra.api.properties.Properties;
|
||||
|
||||
|
||||
@SuppressWarnings("FieldMayBeFinal")
|
||||
public class PreLoadCompatibilityOptions implements ConfigTemplate, Properties {
|
||||
public class MinecraftCompatibilityOptions implements ConfigTemplate, Properties {
|
||||
@Value("minecraft.use-vanilla-biomes")
|
||||
@Default
|
||||
private boolean vanillaBiomes = false;
|
||||
+100
@@ -0,0 +1,100 @@
|
||||
package com.dfsek.terra.minecraft.generation;
|
||||
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.world.level.LevelHeightAccessor;
|
||||
import net.minecraft.world.level.NoiseColumn;
|
||||
import net.minecraft.world.level.StructureManager;
|
||||
import net.minecraft.world.level.block.Blocks;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import net.minecraft.world.level.chunk.ChunkAccess;
|
||||
import net.minecraft.world.level.levelgen.Beardifier;
|
||||
import net.minecraft.world.level.levelgen.densityfunction.SamplerContext;
|
||||
import net.minecraft.world.level.levelgen.structure.Structure;
|
||||
import net.minecraft.world.level.levelgen.structure.TerrainAdjustment;
|
||||
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import com.dfsek.terra.api.world.biome.generation.BiomeProvider;
|
||||
import com.dfsek.terra.api.world.chunk.generation.ChunkGenerator;
|
||||
import com.dfsek.terra.api.world.info.WorldProperties;
|
||||
import com.dfsek.terra.minecraft.config.MinecraftCompatibilityOptions;
|
||||
|
||||
|
||||
/**
|
||||
* Structure beards, base height and base column, shared by the Fabric and Paper generators. {@code vanilla} converts a Terra
|
||||
* block state to Minecraft's.
|
||||
*/
|
||||
public final class TerraTerrain {
|
||||
private TerraTerrain() {
|
||||
}
|
||||
|
||||
public static void beard(ChunkAccess chunk, StructureManager structures, ChunkGenerator terra, BiomeProvider biomes,
|
||||
WorldProperties world, MinecraftCompatibilityOptions options,
|
||||
Function<com.dfsek.terra.api.block.state.BlockState, BlockState> vanilla) {
|
||||
if(!options.isBeard() || !adaptsTerrain(chunk)) return;
|
||||
Beardifier beardifier = Beardifier.forStructuresInChunk(structures, chunk.getPos());
|
||||
BlockPos.MutableBlockPos pos = new BlockPos.MutableBlockPos();
|
||||
beard(chunk.getPos().getMinBlockX(), chunk.getPos().getMinBlockZ(), chunk.getMinY(), chunk.getMaxY(),
|
||||
(x, y, z) -> beardifier.sampleValue(SamplerContext.EMPTY_UNCACHED, x, y, z), terra, biomes, world, options.getBeardThreshold(),
|
||||
options.getAirThreshold(), vanilla, (x, y, z, state) -> chunk.setBlockState(pos.set(x, y, z), state, 0));
|
||||
}
|
||||
|
||||
static void beard(int chunkX, int chunkZ, int minY, int maxY, Density density, ChunkGenerator terra, BiomeProvider biomes,
|
||||
WorldProperties world, double threshold, double airThreshold,
|
||||
Function<com.dfsek.terra.api.block.state.BlockState, BlockState> vanilla, BlockSink blocks) {
|
||||
BlockState air = Blocks.AIR.defaultBlockState();
|
||||
for(int x = 0; x < 16; x++) {
|
||||
for(int z = 0; z < 16; z++) {
|
||||
int worldX = chunkX + x;
|
||||
int worldZ = chunkZ + z;
|
||||
int depth = 0;
|
||||
for(int y = maxY; y >= minY; y--) {
|
||||
double value = density.sample(worldX, y, worldZ);
|
||||
if(value > threshold) {
|
||||
blocks.set(x, y, z, vanilla.apply(terra.getPalette(worldX, y, worldZ, world, biomes)
|
||||
.get(depth++, worldX, y, worldZ, world.getSeed())));
|
||||
} else if(value < airThreshold) {
|
||||
blocks.set(x, y, z, air);
|
||||
} else {
|
||||
depth = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static boolean adaptsTerrain(ChunkAccess chunk) {
|
||||
for(Structure structure : chunk.getAllReferences().keySet()) {
|
||||
if(structure.terrainAdaptation() != TerrainAdjustment.NONE) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static int baseHeight(ChunkGenerator terra, BiomeProvider biomes, WorldProperties world, LevelHeightAccessor height, int x,
|
||||
int z, Predicate<BlockState> opaque, Function<com.dfsek.terra.api.block.state.BlockState, BlockState> vanilla) {
|
||||
for(int y = height.getMaxY(); y >= height.getMinY(); y--) {
|
||||
if(opaque.test(vanilla.apply(terra.getBlock(world, x, y, z, biomes)))) return y + 1;
|
||||
}
|
||||
return height.getMinY();
|
||||
}
|
||||
|
||||
public static NoiseColumn baseColumn(ChunkGenerator terra, BiomeProvider biomes, WorldProperties world, LevelHeightAccessor height,
|
||||
int x, int z, Function<com.dfsek.terra.api.block.state.BlockState, BlockState> vanilla) {
|
||||
BlockState[] column = new BlockState[height.getHeight()];
|
||||
for(int i = 0; i < column.length; i++) {
|
||||
column[i] = vanilla.apply(terra.getBlock(world, x, height.getMinY() + i, z, biomes));
|
||||
}
|
||||
return new NoiseColumn(height.getMinY(), column);
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
interface Density {
|
||||
double sample(int x, int y, int z);
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
interface BlockSink {
|
||||
void set(int x, int y, int z, BlockState state);
|
||||
}
|
||||
}
|
||||
+12
-6
@@ -49,10 +49,14 @@ class BiomeTagsTest {
|
||||
.map(h -> h);
|
||||
}
|
||||
|
||||
private void add(Map<Identifier, List<Identifier>> byParent, String... tags) {
|
||||
private PlatformTagSource source(String... tags) {
|
||||
Set<RegistryKey> keys = new LinkedHashSet<>();
|
||||
for(String t : tags) keys.add(RegistryKey.parse(t));
|
||||
BiomeTags.addTerraBiomes(collect, this::lookup, byParent, Map.of(TERRA_ID, new PlatformTagSource(report, "BIOME", keys)));
|
||||
return new PlatformTagSource(report, "BIOME", keys);
|
||||
}
|
||||
|
||||
private void add(String... tags) {
|
||||
BiomeTags.addPlatformTags(collect, this::lookup, Map.of(TERRA_ID, source(tags)));
|
||||
}
|
||||
|
||||
private static void bindTags(Holder<Biome> holder, TagKey<Biome> tag) throws ReflectiveOperationException {
|
||||
@@ -66,7 +70,7 @@ class BiomeTagsTest {
|
||||
Holder<Biome> forest = registry.get(Biomes.FOREST).orElseThrow();
|
||||
collect.put(IS_FOREST, new ArrayList<>(List.of(forest)));
|
||||
|
||||
add(Map.of(), "mypack:has_snow", "minecraft:is_forest");
|
||||
add("mypack:has_snow", "minecraft:is_forest");
|
||||
|
||||
assertEquals(List.of(terra), collect.get(HAS_SNOW));
|
||||
assertEquals(List.of(forest, terra), collect.get(IS_FOREST));
|
||||
@@ -79,7 +83,9 @@ class BiomeTagsTest {
|
||||
bindTags(forest, IS_FOREST);
|
||||
Identifier forestId = Biomes.FOREST.identifier();
|
||||
|
||||
add(Map.of(forestId, List.of(TERRA_ID)));
|
||||
TerraBiomeIndex biomes = new TerraBiomeIndex();
|
||||
biomes.record(TERRA_ID, forestId, source());
|
||||
BiomeTags.addTerraBiomes(collect, this::lookup, biomes);
|
||||
|
||||
assertEquals(List.of(terra), collect.get(IS_FOREST));
|
||||
assertEquals(List.of(), warnings);
|
||||
@@ -87,7 +93,7 @@ class BiomeTagsTest {
|
||||
|
||||
@Test
|
||||
void upperCaseTagIsIgnored() {
|
||||
add(Map.of(), "mypack:Has_Snow");
|
||||
add("mypack:Has_Snow");
|
||||
|
||||
assertTrue(collect.isEmpty(), collect.toString());
|
||||
assertEquals(1, warnings.size(), warnings.toString());
|
||||
@@ -97,7 +103,7 @@ class BiomeTagsTest {
|
||||
|
||||
@Test
|
||||
void missingMinecraftTagWarnsAndIsCreated() {
|
||||
add(Map.of(), "minecraft:is_forrest");
|
||||
add("minecraft:is_forrest");
|
||||
|
||||
assertEquals(List.of("Pack \"test\", config \"BIOME\": tag \"minecraft:is_forrest\" created: no such tag in the minecraft namespace"),
|
||||
warnings);
|
||||
|
||||
+120
@@ -0,0 +1,120 @@
|
||||
package com.dfsek.terra.minecraft.generation;
|
||||
|
||||
import net.minecraft.world.level.LevelHeightAccessor;
|
||||
import net.minecraft.world.level.NoiseColumn;
|
||||
import net.minecraft.world.level.block.Blocks;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.IntFunction;
|
||||
|
||||
import com.dfsek.terra.api.world.chunk.generation.ChunkGenerator;
|
||||
import com.dfsek.terra.api.world.chunk.generation.util.Palette;
|
||||
import com.dfsek.terra.api.world.info.WorldProperties;
|
||||
import com.dfsek.terra.minecraft.attribute.MinecraftTestSupport;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
|
||||
|
||||
class TerraTerrainTest {
|
||||
static {
|
||||
MinecraftTestSupport.REGISTRIES.hashCode();
|
||||
}
|
||||
|
||||
private static final com.dfsek.terra.api.block.state.BlockState STONE = terra("stone");
|
||||
private static final com.dfsek.terra.api.block.state.BlockState AIR = terra("air");
|
||||
private static final com.dfsek.terra.api.block.state.BlockState DIRT = terra("dirt");
|
||||
private static final Map<com.dfsek.terra.api.block.state.BlockState, BlockState> VANILLA = Map.of(
|
||||
STONE, Blocks.STONE.defaultBlockState(), AIR, Blocks.AIR.defaultBlockState(), DIRT, Blocks.DIRT.defaultBlockState());
|
||||
private static final Function<com.dfsek.terra.api.block.state.BlockState, BlockState> TO_VANILLA = VANILLA::get;
|
||||
private static final LevelHeightAccessor HEIGHT = LevelHeightAccessor.create(-4, 8);
|
||||
|
||||
@Test
|
||||
void baseColumnCoversTheWholeHeightBottomUp() {
|
||||
NoiseColumn column = TerraTerrain.baseColumn(generator(y -> y < 0 ? STONE : AIR, null), null, null, HEIGHT, 0, 0, TO_VANILLA);
|
||||
|
||||
for(int y = -4; y < 4; y++) {
|
||||
assertSame(y < 0 ? Blocks.STONE.defaultBlockState() : Blocks.AIR.defaultBlockState(), column.getBlock(y), "y " + y);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void baseHeightIsAboveTheTopOpaqueBlock() {
|
||||
int height = TerraTerrain.baseHeight(generator(y -> y <= 1 ? STONE : AIR, null), null, null, HEIGHT, 0, 0,
|
||||
state -> !state.isAir(), TO_VANILLA);
|
||||
|
||||
assertEquals(2, height);
|
||||
}
|
||||
|
||||
@Test
|
||||
void baseHeightCountsTheTopBlock() {
|
||||
int height = TerraTerrain.baseHeight(generator(y -> y == 3 ? STONE : AIR, null), null, null, HEIGHT, 0, 0,
|
||||
state -> !state.isAir(), TO_VANILLA);
|
||||
|
||||
assertEquals(4, height);
|
||||
}
|
||||
|
||||
@Test
|
||||
void baseHeightOfAnEmptyColumnIsTheBottom() {
|
||||
assertEquals(-4, TerraTerrain.baseHeight(generator(y -> AIR, null), null, null, HEIGHT, 0, 0, state -> !state.isAir(), TO_VANILLA));
|
||||
}
|
||||
|
||||
@Test
|
||||
void beardFillsDenseBlocksWithPaletteLayersAndClearsSparseOnes() {
|
||||
List<Integer> layers = new ArrayList<>();
|
||||
Map<Integer, BlockState> placed = new HashMap<>();
|
||||
// Every column gets the same density: dense at 2, 1 and -2, neutral at 0, sparse at -1.
|
||||
TerraTerrain.beard(32, 48, -2, 2, (x, y, z) -> switch(y) {
|
||||
case 2, 1, -2 -> 1;
|
||||
case -1 -> -1;
|
||||
default -> 0;
|
||||
}, generator(y -> AIR, layer -> {
|
||||
layers.add(layer);
|
||||
return layer == 0 ? DIRT : STONE;
|
||||
}), null, properties(), 0.5, -0.5, TO_VANILLA,
|
||||
(x, y, z, state) -> {
|
||||
if(x == 0 && z == 0) placed.put(y, state);
|
||||
});
|
||||
|
||||
assertEquals(Map.of(2, Blocks.DIRT.defaultBlockState(), 1, Blocks.STONE.defaultBlockState(), -1, Blocks.AIR.defaultBlockState(),
|
||||
-2, Blocks.DIRT.defaultBlockState()), placed);
|
||||
assertEquals(List.of(0, 1, 0), layers.subList(0, 3));
|
||||
}
|
||||
|
||||
private static ChunkGenerator generator(IntFunction<com.dfsek.terra.api.block.state.BlockState> byY,
|
||||
IntFunction<com.dfsek.terra.api.block.state.BlockState> byLayer) {
|
||||
Palette palette = (layer, x, y, z, seed) -> byLayer.apply(layer);
|
||||
return (ChunkGenerator) Proxy.newProxyInstance(ChunkGenerator.class.getClassLoader(), new Class<?>[]{ ChunkGenerator.class },
|
||||
(proxy, method, args) -> switch(method.getName()) {
|
||||
case "getBlock" -> byY.apply((int) args[2]);
|
||||
case "getPalette" -> palette;
|
||||
default -> throw new UnsupportedOperationException(method.getName());
|
||||
});
|
||||
}
|
||||
|
||||
private static WorldProperties properties() {
|
||||
return (WorldProperties) Proxy.newProxyInstance(WorldProperties.class.getClassLoader(), new Class<?>[]{ WorldProperties.class },
|
||||
(proxy, method, args) -> switch(method.getName()) {
|
||||
case "getSeed" -> 0L;
|
||||
default -> throw new UnsupportedOperationException(method.getName());
|
||||
});
|
||||
}
|
||||
|
||||
private static com.dfsek.terra.api.block.state.BlockState terra(String name) {
|
||||
return (com.dfsek.terra.api.block.state.BlockState) Proxy.newProxyInstance(
|
||||
com.dfsek.terra.api.block.state.BlockState.class.getClassLoader(), new Class<?>[]{ com.dfsek.terra.api.block.state.BlockState.class },
|
||||
(proxy, method, args) -> switch(method.getName()) {
|
||||
case "hashCode" -> System.identityHashCode(proxy);
|
||||
case "equals" -> proxy == args[0];
|
||||
case "toString" -> name;
|
||||
default -> throw new UnsupportedOperationException(method.getName());
|
||||
});
|
||||
}
|
||||
}
|
||||
+2
-35
@@ -17,51 +17,18 @@
|
||||
|
||||
package com.dfsek.terra.mod.config;
|
||||
|
||||
import com.dfsek.tectonic.api.config.template.ConfigTemplate;
|
||||
import com.dfsek.tectonic.api.config.template.annotations.Default;
|
||||
import com.dfsek.tectonic.api.config.template.annotations.Value;
|
||||
|
||||
import com.dfsek.terra.api.properties.Properties;
|
||||
import com.dfsek.terra.minecraft.config.MinecraftCompatibilityOptions;
|
||||
|
||||
|
||||
@SuppressWarnings("FieldMayBeFinal")
|
||||
public class PreLoadCompatibilityOptions implements ConfigTemplate, Properties {
|
||||
@Value("minecraft.use-vanilla-biomes")
|
||||
@Default
|
||||
private boolean vanillaBiomes = false;
|
||||
|
||||
@Value("minecraft.beard.enable")
|
||||
@Default
|
||||
private boolean beard = true;
|
||||
|
||||
@Value("minecraft.beard.threshold")
|
||||
@Default
|
||||
private double beardThreshold = 0.5;
|
||||
|
||||
@Value("minecraft.beard.air-threshold")
|
||||
@Default
|
||||
private double airThreshold = -0.5;
|
||||
|
||||
public class PreLoadCompatibilityOptions extends MinecraftCompatibilityOptions {
|
||||
@Value("minecraft.inject-flora")
|
||||
@Default
|
||||
private boolean injectFlora = false;
|
||||
|
||||
public boolean useVanillaBiomes() {
|
||||
return vanillaBiomes;
|
||||
}
|
||||
|
||||
public boolean isBeard() {
|
||||
return beard;
|
||||
}
|
||||
|
||||
public double getBeardThreshold() {
|
||||
return beardThreshold;
|
||||
}
|
||||
|
||||
public double getAirThreshold() {
|
||||
return airThreshold;
|
||||
}
|
||||
|
||||
public boolean isInjectFlora() {
|
||||
return injectFlora;
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ public final class Codecs {
|
||||
TERRA_CONSTANT_RANGE.fieldOf("height").stable().forGetter(GenerationSettings::height),
|
||||
Codec.INT.fieldOf("sea_level").forGetter(GenerationSettings::sealevel),
|
||||
Codec.BOOL.fieldOf("mob_generation").forGetter(GenerationSettings::mobGeneration),
|
||||
Codec.INT.fieldOf("spawn_height").forGetter(GenerationSettings::sealevel))
|
||||
Codec.INT.fieldOf("spawn_height").forGetter(GenerationSettings::spawnHeight))
|
||||
.apply(instance, instance.stable(GenerationSettings::new)));
|
||||
|
||||
|
||||
|
||||
+11
-67
@@ -18,7 +18,6 @@
|
||||
package com.dfsek.terra.mod.generation;
|
||||
|
||||
import com.mojang.serialization.MapCodec;
|
||||
import net.minecraft.commands.arguments.blocks.BlockInput;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.core.Holder;
|
||||
import net.minecraft.server.level.WorldGenRegion;
|
||||
@@ -31,10 +30,8 @@ import net.minecraft.world.level.StructureManager;
|
||||
import net.minecraft.world.level.WorldGenLevel;
|
||||
import net.minecraft.world.level.biome.Biome;
|
||||
import net.minecraft.world.level.biome.BiomeManager;
|
||||
import net.minecraft.world.level.block.Blocks;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import net.minecraft.world.level.chunk.ChunkAccess;
|
||||
import net.minecraft.world.level.levelgen.Beardifier;
|
||||
import net.minecraft.world.level.levelgen.Heightmap.Types;
|
||||
import net.minecraft.world.level.levelgen.LegacyRandomSource;
|
||||
import net.minecraft.world.level.levelgen.RandomState;
|
||||
@@ -57,7 +54,7 @@ import com.dfsek.terra.api.world.chunk.generation.ProtoChunk;
|
||||
import com.dfsek.terra.api.world.chunk.generation.ProtoWorld;
|
||||
import com.dfsek.terra.api.world.chunk.generation.stage.Chunkified;
|
||||
import com.dfsek.terra.api.world.chunk.generation.util.GeneratorWrapper;
|
||||
import com.dfsek.terra.api.world.info.WorldProperties;
|
||||
import com.dfsek.terra.minecraft.generation.TerraTerrain;
|
||||
import com.dfsek.terra.mod.config.PreLoadCompatibilityOptions;
|
||||
import com.dfsek.terra.mod.data.Codecs;
|
||||
import com.dfsek.terra.mod.mixin.access.StructureManagerAccessor;
|
||||
@@ -109,51 +106,12 @@ public class MinecraftChunkGeneratorWrapper extends net.minecraft.world.level.ch
|
||||
BiomeProvider biomeProvider = pack.getBiomeProvider();
|
||||
delegate.generateChunkData((ProtoChunk) chunk, world, biomeProvider, chunk.getPos().x(), chunk.getPos().z());
|
||||
|
||||
PreLoadCompatibilityOptions compatibilityOptions = pack.getContext().get(PreLoadCompatibilityOptions.class);
|
||||
if(compatibilityOptions.isBeard()) {
|
||||
beard(structureAccessor, chunk, world, biomeProvider, compatibilityOptions);
|
||||
}
|
||||
TerraTerrain.beard(chunk, structureAccessor, delegate, biomeProvider, world,
|
||||
pack.getContext().get(PreLoadCompatibilityOptions.class), MinecraftChunkGeneratorWrapper::vanilla);
|
||||
return chunk;
|
||||
}, Util.backgroundExecutor());
|
||||
}
|
||||
|
||||
private void beard(StructureManager structureAccessor, ChunkAccess chunk, WorldProperties world, BiomeProvider biomeProvider,
|
||||
PreLoadCompatibilityOptions compatibilityOptions) {
|
||||
Beardifier structureWeightSampler = Beardifier.forStructuresInChunk(structureAccessor,
|
||||
chunk.getPos());
|
||||
double threshold = compatibilityOptions.getBeardThreshold();
|
||||
double airThreshold = compatibilityOptions.getAirThreshold();
|
||||
int xi = chunk.getPos().x() << 4;
|
||||
int zi = chunk.getPos().z() << 4;
|
||||
for(int x = 0; x < 16; x++) {
|
||||
for(int z = 0; z < 16; z++) {
|
||||
int depth = 0;
|
||||
for(int y = world.getMaxHeight(); y >= world.getMinHeight(); y--) {
|
||||
double noise = structureWeightSampler.sampleValue(SamplerContext.EMPTY_UNCACHED, x + xi, y, z + zi);
|
||||
if(noise > threshold) {
|
||||
com.dfsek.terra.api.block.state.BlockState data = delegate.getPalette(x + xi, y, z + zi, world, biomeProvider).get(
|
||||
depth, x + xi, y, z + zi, world.getSeed());
|
||||
BlockPos blockPos = new BlockPos(x, y, z);
|
||||
boolean isExtended = data.isExtended() && data.getClass().equals(BlockInput.class);
|
||||
if(isExtended) {
|
||||
BlockStateExtended blockStateExtended = (BlockStateExtended) data;
|
||||
|
||||
net.minecraft.world.level.block.state.BlockState blockState = (net.minecraft.world.level.block.state.BlockState) blockStateExtended.getState();
|
||||
chunk.setBlockState(blockPos, blockState, 0);
|
||||
} else {
|
||||
chunk.setBlockState(blockPos, (net.minecraft.world.level.block.state.BlockState) data, 0);
|
||||
}
|
||||
depth++;
|
||||
} else if(noise < airThreshold) {
|
||||
chunk.setBlockState(new BlockPos(x, y, z), Blocks.AIR.defaultBlockState(), 0);
|
||||
} else {
|
||||
depth = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void applyBiomeDecoration(WorldGenLevel world, ChunkAccess chunk, StructureManager structureAccessor) {
|
||||
super.applyBiomeDecoration(world, chunk, structureAccessor);
|
||||
@@ -186,32 +144,18 @@ public class MinecraftChunkGeneratorWrapper extends net.minecraft.world.level.ch
|
||||
|
||||
@Override
|
||||
public int getBaseHeight(int x, int z, Types heightmap, LevelHeightAccessor height, RandomState noiseConfig) {
|
||||
WorldProperties properties = MinecraftAdapter.adapt(height, noiseConfig.seed());
|
||||
BiomeProvider biomeProvider = pack.getBiomeProvider();
|
||||
int min = height.getMinY();
|
||||
for(int y = height.getMaxY() - 1; y >= min; y--) {
|
||||
com.dfsek.terra.api.block.state.BlockState terraBlockState = delegate.getBlock(properties, x, y, z, biomeProvider);
|
||||
BlockState blockState =
|
||||
(BlockState) (terraBlockState.isExtended() ? ((BlockStateExtended) terraBlockState).getState() : terraBlockState);
|
||||
if(heightmap
|
||||
.isOpaque()
|
||||
.test(blockState)) return y + 1;
|
||||
}
|
||||
return min;
|
||||
return TerraTerrain.baseHeight(delegate, pack.getBiomeProvider(), MinecraftAdapter.adapt(height, noiseConfig.seed()), height, x, z,
|
||||
heightmap.isOpaque(), MinecraftChunkGeneratorWrapper::vanilla);
|
||||
}
|
||||
|
||||
@Override
|
||||
public NoiseColumn getBaseColumn(int x, int z, LevelHeightAccessor height, RandomState noiseConfig) {
|
||||
BlockState[] array = new BlockState[height.getHeight()];
|
||||
WorldProperties properties = MinecraftAdapter.adapt(height, noiseConfig.seed());
|
||||
BiomeProvider biomeProvider = pack.getBiomeProvider();
|
||||
for(int y = height.getMaxY() - 1; y >= height.getMinY(); y--) {
|
||||
com.dfsek.terra.api.block.state.BlockState terraBlockState = delegate.getBlock(properties, x, y, z, biomeProvider);
|
||||
BlockState blockState =
|
||||
(BlockState) (terraBlockState.isExtended() ? ((BlockStateExtended) terraBlockState).getState() : terraBlockState);
|
||||
array[y - height.getMinY()] = blockState;
|
||||
}
|
||||
return new NoiseColumn(height.getMinY(), array);
|
||||
return TerraTerrain.baseColumn(delegate, pack.getBiomeProvider(), MinecraftAdapter.adapt(height, noiseConfig.seed()), height, x, z,
|
||||
MinecraftChunkGeneratorWrapper::vanilla);
|
||||
}
|
||||
|
||||
private static BlockState vanilla(com.dfsek.terra.api.block.state.BlockState state) {
|
||||
return (BlockState) (state.isExtended() ? ((BlockStateExtended) state).getState() : state);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,33 +1,19 @@
|
||||
package com.dfsek.terra.mod.util;
|
||||
|
||||
import net.minecraft.resources.Identifier;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
import com.dfsek.terra.api.config.ConfigPack;
|
||||
import com.dfsek.terra.minecraft.attribute.BiomeTags.PlatformTagSource;
|
||||
import com.dfsek.terra.minecraft.attribute.TerraBiomeIndex;
|
||||
|
||||
|
||||
public class BiomeUtil {
|
||||
public static final Map<Identifier, List<Identifier>>
|
||||
TERRA_BIOME_MAP = new HashMap<>();
|
||||
public final class BiomeUtil {
|
||||
private static volatile TerraBiomeIndex terraBiomes = new TerraBiomeIndex();
|
||||
|
||||
public static final Map<Identifier, PlatformTagSource>
|
||||
TERRA_BIOME_PLATFORM_TAGS = new HashMap<>();
|
||||
|
||||
public static String createBiomeID(ConfigPack pack, com.dfsek.terra.api.registry.key.RegistryKey biomeID) {
|
||||
return pack.getID()
|
||||
.toLowerCase() + "/" + biomeID.getNamespace().toLowerCase(Locale.ROOT) + "/" + biomeID.getID().toLowerCase(Locale.ROOT);
|
||||
private BiomeUtil() {
|
||||
}
|
||||
|
||||
public static Map<Identifier, List<Identifier>> getTerraBiomeMap() {
|
||||
return Map.copyOf(TERRA_BIOME_MAP);
|
||||
public static TerraBiomeIndex terraBiomes() {
|
||||
return terraBiomes;
|
||||
}
|
||||
|
||||
public static Map<Identifier, PlatformTagSource> getTerraBiomePlatformTags() {
|
||||
return Map.copyOf(TERRA_BIOME_PLATFORM_TAGS);
|
||||
public static void setTerraBiomes(TerraBiomeIndex biomes) {
|
||||
terraBiomes = biomes;
|
||||
}
|
||||
}
|
||||
@@ -38,6 +38,7 @@ import com.dfsek.terra.api.block.entity.Sign;
|
||||
import com.dfsek.terra.api.config.ConfigPack;
|
||||
import com.dfsek.terra.api.entity.EntityType;
|
||||
import com.dfsek.terra.api.util.range.ConstantRange;
|
||||
import com.dfsek.terra.minecraft.attribute.MinecraftBiomeFactory;
|
||||
import com.dfsek.terra.mod.CommonPlatform;
|
||||
import com.dfsek.terra.mod.config.PreLoadCompatibilityOptions;
|
||||
import com.dfsek.terra.mod.config.ProtoPlatformBiome;
|
||||
@@ -133,7 +134,7 @@ public final class MinecraftUtil {
|
||||
ResourceKey<net.minecraft.world.level.biome.Biome> vanillaKey = ((ProtoPlatformBiome) biome.getPlatformBiome()).get(biomeRegistry);
|
||||
biomeRegistry.getOptional(vanillaKey)
|
||||
.ifPresentOrElse(vanillaBiome -> {
|
||||
Identifier terraBiomeIdentifier = Identifier.fromNamespaceAndPath("terra", BiomeUtil.createBiomeID(pack, id));
|
||||
Identifier terraBiomeIdentifier = MinecraftBiomeFactory.terraId(pack, id);
|
||||
biomeRegistry.getOptional(terraBiomeIdentifier).ifPresentOrElse(
|
||||
terraBiome -> {
|
||||
List<Feature> flowerFeatures = List.copyOf(
|
||||
|
||||
@@ -105,8 +105,7 @@ public class PresetUtil {
|
||||
Identifier demensionIdentifier = Identifier.parse(key);
|
||||
|
||||
MinecraftAttributeParents parents = platform.attributeParents();
|
||||
VanillaContext context = parents.context();
|
||||
if(context == null) throw new IllegalStateException("no registry load has begun");
|
||||
VanillaContext context = parents.requireContext();
|
||||
ResourceKey<DimensionType> filled = MinecraftUtil.registerDimensionTypeKey(demensionIdentifier);
|
||||
if(context.dimensionTypes().get(filled).isEmpty()) filled = BuiltinDimensionTypes.OVERWORLD;
|
||||
PackParents packParents = parents.prepare(pack, platform.getAttributeHandle().pack(pack), AttributeReport.logging(pack), filled);
|
||||
|
||||
@@ -73,8 +73,7 @@ public final class TagUtil {
|
||||
logger.info("Doing biome tag garbage....");
|
||||
Map<TagKey<Biome>, List<Holder<Biome>>> collect = tagsToMutableMap(registry);
|
||||
|
||||
BiomeTags.addTerraBiomes(collect, id -> MinecraftUtil.getEntry(registry, id), BiomeUtil.getTerraBiomeMap(),
|
||||
BiomeUtil.getTerraBiomePlatformTags());
|
||||
BiomeTags.addTerraBiomes(collect, id -> MinecraftUtil.getEntry(registry, id), BiomeUtil.terraBiomes());
|
||||
|
||||
registry.prepareTagReload(new LoadResult<>(registry.key(), collect)).apply();
|
||||
|
||||
|
||||
+13
-9
@@ -7,7 +7,6 @@ import net.minecraft.resources.ResourceKey;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import com.dfsek.terra.api.config.ConfigPack;
|
||||
import com.dfsek.terra.api.world.biome.Biome;
|
||||
@@ -15,6 +14,7 @@ import com.dfsek.terra.minecraft.attribute.BiomeTags.PlatformTagSource;
|
||||
import com.dfsek.terra.minecraft.attribute.MinecraftAttributeParents;
|
||||
import com.dfsek.terra.minecraft.attribute.MinecraftBiomeFactory;
|
||||
import com.dfsek.terra.minecraft.attribute.PackParents;
|
||||
import com.dfsek.terra.minecraft.attribute.TerraBiomeIndex;
|
||||
import com.dfsek.terra.mod.CommonPlatform;
|
||||
import com.dfsek.terra.mod.ModPlatform;
|
||||
import com.dfsek.terra.mod.config.PreLoadCompatibilityOptions;
|
||||
@@ -34,16 +34,16 @@ public final class LifecycleBiomeUtil {
|
||||
|
||||
public static void registerBiomes(Registry<net.minecraft.world.level.biome.Biome> biomeRegistry, MinecraftBiomeFactory factory) {
|
||||
logger.info("Registering biomes...");
|
||||
BiomeUtil.TERRA_BIOME_MAP.clear();
|
||||
BiomeUtil.TERRA_BIOME_PLATFORM_TAGS.clear();
|
||||
TerraBiomeIndex biomes = new TerraBiomeIndex();
|
||||
ModPlatform platform = CommonPlatform.get();
|
||||
MinecraftAttributeParents parents = platform.attributeParents();
|
||||
platform.getConfigRegistry().forEach(pack -> { // Register all Terra biomes.
|
||||
PackParents packParents = parents.prepare(pack, platform.getAttributeHandle().pack(pack),
|
||||
AttributeReport.logging(pack));
|
||||
pack.getCheckedRegistry(Biome.class)
|
||||
.forEach((id, biome) -> registerBiome(biome, pack, packParents, id, biomeRegistry, parents, factory));
|
||||
.forEach((id, biome) -> registerBiome(biome, pack, packParents, id, biomeRegistry, parents, factory, biomes));
|
||||
});
|
||||
BiomeUtil.setTerraBiomes(biomes);
|
||||
logger.info("Terra biomes registered.");
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ public final class LifecycleBiomeUtil {
|
||||
private static void registerBiome(Biome biome, ConfigPack pack, PackParents packParents,
|
||||
com.dfsek.terra.api.registry.key.RegistryKey id,
|
||||
Registry<net.minecraft.world.level.biome.Biome> registry, MinecraftAttributeParents parents,
|
||||
MinecraftBiomeFactory factory) {
|
||||
MinecraftBiomeFactory factory, TerraBiomeIndex biomes) {
|
||||
ProtoPlatformBiome proto = (ProtoPlatformBiome) biome.getPlatformBiome();
|
||||
ResourceKey<net.minecraft.world.level.biome.Biome> vanilla = proto.get(registry);
|
||||
net.minecraft.world.level.biome.Biome vanillaBiome = vanilla == null ? null : registry.getValue(vanilla);
|
||||
@@ -75,7 +75,13 @@ public final class LifecycleBiomeUtil {
|
||||
return;
|
||||
}
|
||||
|
||||
Identifier identifier = Identifier.fromNamespaceAndPath("terra", BiomeUtil.createBiomeID(pack, id));
|
||||
Identifier identifier = MinecraftBiomeFactory.terraId(pack, id);
|
||||
|
||||
if(biomes.contains(identifier)) {
|
||||
logger.warn(TerraBiomeIndex.sharedBiome(pack.getID(), biome.getID(), identifier));
|
||||
proto.setDelegate(MinecraftUtil.getEntry(registry, identifier).orElseThrow());
|
||||
return;
|
||||
}
|
||||
|
||||
if(registry.containsKey(identifier)) {
|
||||
// Registered by an earlier load; edits wait for the next registry load.
|
||||
@@ -88,8 +94,6 @@ public final class LifecycleBiomeUtil {
|
||||
VillagerTypeAccessor.getBiomeTypeToIdMap().put(ResourceKey.create(Registries.BIOME, identifier), built.villagerType());
|
||||
}
|
||||
|
||||
BiomeUtil.TERRA_BIOME_PLATFORM_TAGS.put(identifier,
|
||||
new PlatformTagSource(packParents.report(), biome.getID(), biome.getPlatformTags()));
|
||||
BiomeUtil.TERRA_BIOME_MAP.computeIfAbsent(vanilla.identifier(), i -> new ArrayList<>()).add(identifier);
|
||||
biomes.record(identifier, vanilla.identifier(), new PlatformTagSource(packParents.report(), biome.getID(), biome.getPlatformTags()));
|
||||
}
|
||||
}
|
||||
+2
-5
@@ -40,8 +40,7 @@ public final class LifecycleUtil {
|
||||
|
||||
private static void registerTimelines(ModPlatform platform, WritableRegistry<Timeline> timelines) {
|
||||
MinecraftAttributeParents parents = platform.attributeParents();
|
||||
VanillaContext context = parents.context();
|
||||
if(context == null) throw new IllegalStateException("no registry load has begun");
|
||||
VanillaContext context = parents.requireContext();
|
||||
platform.getConfigRegistry().forEach(pack -> {
|
||||
PackParents packParents = parents.prepare(pack, platform.getAttributeHandle().pack(pack),
|
||||
AttributeReport.logging(pack));
|
||||
@@ -50,8 +49,6 @@ public final class LifecycleUtil {
|
||||
}
|
||||
|
||||
public static MinecraftBiomeFactory biomeFactory(ModPlatform platform) {
|
||||
VanillaContext context = platform.attributeParents().context();
|
||||
if(context == null) throw new IllegalStateException("no registry load has begun");
|
||||
return new MinecraftBiomeFactory(platform.getAttributeHandle(), context);
|
||||
return new MinecraftBiomeFactory(platform.getAttributeHandle(), platform.attributeParents().requireContext());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user