Skip to content

Commit

Permalink
World generation (#16)
Browse files Browse the repository at this point in the history
* Removed Liminal Library, new parameter-based level generation, new datapack-based registration
  • Loading branch information
encrystudio authored May 25, 2024
1 parent 030fe44 commit 49fb536
Show file tree
Hide file tree
Showing 48 changed files with 1,780 additions and 1,355 deletions.
2 changes: 0 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,10 @@ dependencies {
modApi "com.terraformersmc:modmenu:${project.modmenu_version}"

modImplementation "me.shedaniel.cloth:cloth-config-fabric:${project.cloth_config_version}"
modImplementation "com.github.LudoCrypt:Liminal-Library:${project.limlib_version}"

modImplementation "dev.onyxstudios.cardinal-components-api:cardinal-components-base:${project.cardinal_components_version}"
modImplementation "dev.onyxstudios.cardinal-components-api:cardinal-components-entity:${project.cardinal_components_version}"

include "com.github.LudoCrypt:Liminal-Library:${project.limlib_version}"
// Includes Cardinal Components API as a Jar-in-Jar dependency (optional)
include "dev.onyxstudios.cardinal-components-api:cardinal-components-base:${project.cardinal_components_version}"
include "dev.onyxstudios.cardinal-components-api:cardinal-components-entity:${project.cardinal_components_version}"
Expand Down
1 change: 0 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ loader_version=0.15.7
cloth_config_version=6.5.116
modmenu_version=3.2.5
satin_version=1.7.2
limlib_version=5.2.1
cardinal_components_version=4.2.0
geckolib_version=3.0.80
# Mod Properties
Expand Down
6 changes: 1 addition & 5 deletions src/main/java/com/kpabr/backrooms/BackroomsMod.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
package com.kpabr.backrooms;


import com.kpabr.backrooms.client.render.sky.StrongLiminalShader;
import com.kpabr.backrooms.component.WretchedComponent;
import com.kpabr.backrooms.config.BackroomsConfig;
import com.kpabr.backrooms.init.*;
import name.trimsky.lib_ai.LibAI;
import name.trimsky.lib_ai.example.LibAIMod;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.ludocrypt.limlib.impl.LimlibRegistries;
import net.minecraft.entity.SpawnReason;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import software.bernie.example.GeckoLibMod;
Expand Down Expand Up @@ -42,7 +38,7 @@ public void onInitialize() {
BackroomsItems.init();
BackroomsEntities.init();
BackroomsLevels.init();
Registry.register(LimlibRegistries.LIMINAL_SHADER_APPLIER, id("strong_simple_shader"), StrongLiminalShader.CODEC);

LOGGER.info("Backrooms mod was loaded!");
// registering every tick event
ServerTickEvents.END_SERVER_TICK.register((server) -> {
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/com/kpabr/backrooms/api/LevelsApi.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.kpabr.backrooms.api;

import com.kpabr.backrooms.init.BackroomsLevels;
import com.kpabr.backrooms.world.biome.BaseBiomeSource;
import net.ludocrypt.limlib.api.LiminalWorld;
import net.ludocrypt.limlib.api.world.AbstractNbtChunkGenerator;
import com.mojang.serialization.Codec;

import net.minecraft.util.registry.RegistryKey;
import net.minecraft.world.World;
import net.minecraft.world.biome.source.BiomeSource;
import net.minecraft.world.gen.chunk.ChunkGenerator;

public class LevelsApi {
public static<T extends AbstractNbtChunkGenerator, S extends BaseBiomeSource> LiminalWorld addLevel(String name, Class<T> chunkGenerator, Class<S> biomeSource) {
return BackroomsLevels.addLevel(name, chunkGenerator, biomeSource);
public static RegistryKey<World> addLevel(String namespace, String levelName, String biomeSourceName, Codec<? extends ChunkGenerator> chunkGenerator, Codec<? extends BiomeSource> biomeSource) {
return BackroomsLevels.addLevel(namespace, levelName, biomeSourceName, chunkGenerator, biomeSource);
}
}

This file was deleted.

188 changes: 135 additions & 53 deletions src/main/java/com/kpabr/backrooms/init/BackroomsLevels.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,54 @@
package com.kpabr.backrooms.init;

import com.kpabr.backrooms.BackroomsMod;
import com.kpabr.backrooms.world.biome.*;
import com.kpabr.backrooms.world.biome.biomes.level0.CrimsonHallsBiome;
import com.kpabr.backrooms.world.biome.biomes.level0.DecrepitBiome;
import com.kpabr.backrooms.world.biome.biomes.level0.MegalophobiaBiome;
import com.kpabr.backrooms.world.biome.biomes.level0.YellowHallsBiome;
import com.kpabr.backrooms.world.biome.biomes.level1.CementHallsBiome;
import com.kpabr.backrooms.world.biome.biomes.level1.ParkingGarageBiome;
import com.kpabr.backrooms.world.biome.biomes.level1.WarehouseBiome;
import com.kpabr.backrooms.world.biome.biomes.level2.PipesBiome;
import com.kpabr.backrooms.world.biome.biomes.level3.ElectricalStationBiome;
import com.kpabr.backrooms.world.biome.sources.LevelOneBiomeSource;
import com.kpabr.backrooms.world.biome.sources.LevelThreeBiomeSource;
import com.kpabr.backrooms.world.biome.sources.LevelTwoBiomeSource;
import com.kpabr.backrooms.world.biome.sources.LevelZeroBiomeSource;
import com.kpabr.backrooms.world.chunk.LevelOneChunkGenerator;
import com.kpabr.backrooms.world.chunk.LevelThreeChunkGenerator;
import com.kpabr.backrooms.world.chunk.LevelTwoChunkGenerator;
import com.kpabr.backrooms.world.chunk.LevelZeroChunkGenerator;
import net.ludocrypt.limlib.api.LiminalEffects;
import net.ludocrypt.limlib.api.LiminalWorld;
import net.ludocrypt.limlib.api.render.LiminalBaseEffects;
import net.ludocrypt.limlib.api.sound.ReverbSettings;
import net.ludocrypt.limlib.api.world.AbstractNbtChunkGenerator;
import net.minecraft.tag.TagKey;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.serialization.Codec;

import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback;
import net.fabricmc.fabric.api.dimension.v1.FabricDimensions;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.block.Blocks;
import net.minecraft.command.CommandException;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.LiteralText;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.registry.Registry;
import net.minecraft.util.registry.RegistryKey;
import net.minecraft.world.TeleportTarget;
import net.minecraft.world.World;
import net.minecraft.world.biome.Biome;
import net.minecraft.world.biome.source.BiomeSource;
import net.minecraft.world.dimension.DimensionOptions;
import net.minecraft.world.dimension.DimensionType;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Optional;
import java.util.OptionalLong;
import net.minecraft.world.gen.chunk.ChunkGenerator;

import static com.kpabr.backrooms.util.RegistryHelper.get;
import static net.minecraft.server.command.CommandManager.literal;

public class BackroomsLevels {

// Level 0 biomes
public static final RegistryKey<Biome> DECREPIT_BIOME = get("decrepit", DecrepitBiome.create());
public static final RegistryKey<Biome> MEGALOPHOBIA_BIOME = get("megalophobia", MegalophobiaBiome.create());
Expand All @@ -46,55 +67,116 @@ public class BackroomsLevels {
public static final RegistryKey<Biome> ELECTRICAL_STATION_BIOME = get("electrical_station", ElectricalStationBiome.create());


public static LiminalEffects DEFAULT_LEVEL_EFFECTS = new LiminalEffects(Optional.of(new LiminalBaseEffects.SimpleBaseEffects(Optional.empty(), false, "NONE", true, false, true)), Optional.empty(), Optional.empty(), Optional.empty(), Optional.of(new ReverbSettings().setDecayTime(2.15F).setDensity(0.725F)));
// Levels/Dimensions
// The dimension options refer to the JSON-file in the dimension subfolder of the datapack,
// which will always share it's ID with the world that is created from it
public static final RegistryKey<DimensionOptions> LEVEL_0_DIMENSION_KEY = RegistryKey.of(
Registry.DIMENSION_KEY,
new Identifier(BackroomsMod.ModId, "level_0")
);
public static RegistryKey<World> LEVEL_0_WORLD_KEY = RegistryKey.of(
Registry.WORLD_KEY,
LEVEL_0_DIMENSION_KEY.getValue()
);

public static final RegistryKey<DimensionOptions> LEVEL_1_DIMENSION_KEY = RegistryKey.of(
Registry.DIMENSION_KEY,
new Identifier(BackroomsMod.ModId, "level_1")
);
public static RegistryKey<World> LEVEL_1_WORLD_KEY = RegistryKey.of(
Registry.WORLD_KEY,
LEVEL_1_DIMENSION_KEY.getValue()
);

public static final RegistryKey<DimensionOptions> LEVEL_2_DIMENSION_KEY = RegistryKey.of(
Registry.DIMENSION_KEY,
new Identifier(BackroomsMod.ModId, "level_2")
);
public static RegistryKey<World> LEVEL_2_WORLD_KEY = RegistryKey.of(
Registry.WORLD_KEY,
LEVEL_2_DIMENSION_KEY.getValue()
);

public static final RegistryKey<DimensionOptions> LEVEL_3_DIMENSION_KEY = RegistryKey.of(
Registry.DIMENSION_KEY,
new Identifier(BackroomsMod.ModId, "level_3")
);
public static RegistryKey<World> LEVEL_3_WORLD_KEY = RegistryKey.of(
Registry.WORLD_KEY,
LEVEL_3_DIMENSION_KEY.getValue()
);

// don't forget to change this variable or portal block won't work
public static final int LEVELS_AMOUNT = 4;

public static final LiminalWorld LEVEL_0 = addLevel("level_0", LevelZeroChunkGenerator.class, Level0BiomeSource.class);
public static final LiminalWorld LEVEL_1 = addLevel("level_1", LevelOneChunkGenerator.class, Level1BiomeSource.class);
public static final LiminalWorld LEVEL_2 = addLevel("level_2", LevelTwoChunkGenerator.class, Level2BiomeSource.class);
public static final LiminalWorld LEVEL_3 = addLevel("level_3", LevelThreeChunkGenerator.class, Level3BiomeSource.class);



// ServerWorlds for getting seed and NBT resources
public static ServerWorld LEVEL_0_WORLD;
public static ServerWorld LEVEL_1_WORLD;
public static ServerWorld LEVEL_2_WORLD;
public static ServerWorld LEVEL_3_WORLD;

public static void init() {
Registry.register(Registry.BIOME_SOURCE, "level_0_biome_source", Level0BiomeSource.CODEC);
Registry.register(Registry.BIOME_SOURCE, "level_1_biome_source", Level1BiomeSource.CODEC);
Registry.register(Registry.BIOME_SOURCE, "level_2_biome_source", Level2BiomeSource.CODEC);
Registry.register(Registry.BIOME_SOURCE, "level_3_biome_source", Level3BiomeSource.CODEC);
get("level_0_chunk_generator", LevelZeroChunkGenerator.CODEC);
get("level_1_chunk_generator", LevelOneChunkGenerator.CODEC);
get("level_2_chunk_generator", LevelTwoChunkGenerator.CODEC);
get("level_3_chunk_generator", LevelThreeChunkGenerator.CODEC);
}

public static<T extends AbstractNbtChunkGenerator, S extends BiomeSource> LiminalWorld addLevel(String name, Class<T> chunkGenerator, Class<S> biomeSource) {
return addLevelWithEffects(name, chunkGenerator, biomeSource, DEFAULT_LEVEL_EFFECTS);
addLevel(BackroomsMod.ModId, "level_0", "level_0_biome_source", LevelZeroChunkGenerator.CODEC, LevelZeroBiomeSource.CODEC);
addLevel(BackroomsMod.ModId, "level_1", "level_1_biome_source", LevelOneChunkGenerator.CODEC, LevelOneBiomeSource.CODEC);
addLevel(BackroomsMod.ModId, "level_2", "level_2_biome_source", LevelTwoChunkGenerator.CODEC, LevelTwoBiomeSource.CODEC);
addLevel(BackroomsMod.ModId, "level_3", "level_3_biome_source", LevelThreeChunkGenerator.CODEC, LevelThreeBiomeSource.CODEC);


ServerLifecycleEvents.SERVER_STARTED.register(server -> {
LEVEL_0_WORLD = server.getWorld(LEVEL_0_WORLD_KEY);
LEVEL_1_WORLD = server.getWorld(LEVEL_1_WORLD_KEY);
LEVEL_2_WORLD = server.getWorld(LEVEL_2_WORLD_KEY);
LEVEL_3_WORLD = server.getWorld(LEVEL_3_WORLD_KEY);
});

// only for debug
if (FabricLoader.getInstance().isDevelopmentEnvironment()) {
CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) ->
dispatcher.register(literal("tp0").executes(context -> {
return debugTeleport(context, LEVEL_0_WORLD_KEY);
})));
CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) ->
dispatcher.register(literal("tp1").executes(context -> {
return debugTeleport(context, LEVEL_1_WORLD_KEY);
})));
CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) ->
dispatcher.register(literal("tp2").executes(context -> {
return debugTeleport(context, LEVEL_2_WORLD_KEY);
})));
CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) ->
dispatcher.register(literal("tp3").executes(context -> {
return debugTeleport(context, LEVEL_3_WORLD_KEY);
})));
}
}

public static<T extends AbstractNbtChunkGenerator, S extends BiomeSource> LiminalWorld addLevelWithEffects(String name, Class<T> chunkGenerator, Class<S> biomeSource, LiminalEffects effects) {
final Identifier levelId = BackroomsMod.id(name);

// Messy wrapper
return get(levelId, new LiminalWorld(levelId, DimensionType.create(OptionalLong.of(23500), true, false, false, true, 1.0, false, false, true, false, false, 0, 128, 128, TagKey.of(Registry.BLOCK_KEY, levelId), levelId, /*0.075F*/0.000F),
(world, dimensionTypeRegistry, biomeRegistry, structureRegistry, chunkGeneratorSettingsRegistry, noiseSettingsRegistry, registryManager, seed) ->
new DimensionOptions(
dimensionTypeRegistry.getOrCreateEntry(world.getDimensionTypeKey()),
createChunkGenerator(chunkGenerator, biomeSource, biomeRegistry, seed)
),
effects));
public static RegistryKey<World> addLevel(String namespace, String levelName, String biomeSourceName, Codec<? extends ChunkGenerator> chunkGenerator, Codec<? extends BiomeSource> biomeSource) {
Registry.register(Registry.BIOME_SOURCE, new Identifier(namespace, biomeSourceName), biomeSource);
Registry.register(Registry.CHUNK_GENERATOR, new Identifier(namespace, levelName), chunkGenerator);
return RegistryKey.of(Registry.WORLD_KEY, new Identifier(namespace, levelName));
}

// Creating chunk generator for registerLevel in runtime.
private static<T extends AbstractNbtChunkGenerator, S extends BiomeSource> T createChunkGenerator(Class<T> chunkGeneratorClass, Class<S> biomeSourceClass, Registry<Biome> registry, long seed) {
try {
Constructor<T> chunkGeneratorConstructor = chunkGeneratorClass.getConstructor(BiomeSource.class, long.class);
Constructor<S> biomeSourceConstructor = biomeSourceClass.getConstructor(Registry.class, long.class);
return chunkGeneratorConstructor.newInstance(biomeSourceConstructor.newInstance(registry, seed), seed);
} catch (NoSuchMethodException | InvocationTargetException | InstantiationException | IllegalAccessException e) {
throw new RuntimeException(e);
}
// only for debug
private static int debugTeleport(CommandContext<ServerCommandSource> context, RegistryKey<World> level) throws CommandSyntaxException {
ServerPlayerEntity player = context.getSource().getPlayer();
ServerWorld serverWorld = player.getWorld();
ServerWorld targetWorld = context.getSource().getServer().getWorld(level);

if (serverWorld != targetWorld) {
TeleportTarget target = new TeleportTarget(new Vec3d(0.5, 101, 0.5), Vec3d.ZERO, 0, 0);
FabricDimensions.teleport(player, targetWorld, target);

if (player.world != targetWorld) {
throw new CommandException(new LiteralText("Teleportation failed!"));
}

targetWorld.setBlockState(new BlockPos(0, 100, 0), Blocks.DIAMOND_BLOCK.getDefaultState());
targetWorld.setBlockState(new BlockPos(0, 101, 0), Blocks.TORCH.getDefaultState());
} else {
TeleportTarget target = new TeleportTarget(new Vec3d(0, 100, 0), Vec3d.ZERO,
(float) Math.random() * 360 - 180, (float) Math.random() * 360 - 180);
FabricDimensions.teleport(player, context.getSource().getServer().getWorld(World.OVERWORLD), target);
}
return 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public abstract class BackgroundRendererMixin {
@ModifyVariable(method = "render", at = @At(value = "STORE", ordinal = 2), index = 7)
private static float corners$modifySkyColor(float in) {
MinecraftClient client = MinecraftClient.getInstance();
if (client.world.getRegistryKey().equals(BackroomsLevels.LEVEL_0.getWorldKey())) {
if (client.world.getRegistryKey().equals(BackroomsLevels.LEVEL_0_WORLD_KEY)) {
return 1.0F;
}
return in;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/kpabr/backrooms/mixins/EntityMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ private void backroomsTick(CallbackInfo ci) {
if (isInsideHardBlocks(entity)
&& !((ServerPlayerEntity) entity).isCreative()
&& world.random.nextDouble() < BackroomsConfig.getInstance().suffocationChance) {
World levelZero = getServer().getWorld(BackroomsLevels.LEVEL_0.getWorldKey());
World levelZero = getServer().getWorld(BackroomsLevels.LEVEL_0_WORLD_KEY);

RegistryKey<World> worldKey = world.getRegistryKey();
if(worldKey == World.OVERWORLD) {
teleportToLevel((ServerPlayerEntity) entity, levelZero);
} else if (worldKey == levelZero.getRegistryKey()) {
teleportToLevel((ServerPlayerEntity) entity, getServer().getWorld(BackroomsLevels.LEVEL_1.getWorldKey()));
teleportToLevel((ServerPlayerEntity) entity, getServer().getWorld(BackroomsLevels.LEVEL_1_WORLD_KEY));
}
}
}
Expand Down

This file was deleted.

Loading

0 comments on commit 49fb536

Please sign in to comment.