Skip to content

Commit

Permalink
feat: Add a config system using YACL
Browse files Browse the repository at this point in the history
Also added Mod Menu compatibility.
  • Loading branch information
Steveplays28 committed Jul 16, 2024
1 parent b28f4b9 commit 5f27dfc
Show file tree
Hide file tree
Showing 16 changed files with 140 additions and 40 deletions.
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ allprojects {
name = "NeoForged"
url = "https://maven.neoforged.net/releases/"
}
maven {
name "Xander Maven"
url "https://maven.isxander.dev/releases"
}
}

tasks.withType(JavaCompile).configureEach {
Expand Down
8 changes: 7 additions & 1 deletion common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,14 @@ dependencies {
// Architectury API
modApi("dev.architectury:architectury:${rootProject.architectury_api_version}") { exclude group: 'net.fabricmc', module: 'fabric-loader' }

// YetAnotherConfigLib
modImplementation("dev.isxander:yet-another-config-lib:${rootProject.yet_another_config_lib_version}-fabric")

// Mod Menu
modImplementation("maven.modrinth:modmenu:${rootProject.mod_menu_version}")

// Distant Horizons
modImplementation "maven.modrinth:distanthorizons:${rootProject.distant_horizons_version}-${rootProject.minecraft_version}"
modImplementation("maven.modrinth:distanthorizons:${rootProject.distant_horizons_version}-${rootProject.minecraft_version}")
}

publishing {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.github.steveplays28.noisium.compat.lithium;

import io.github.steveplays28.noisium.util.ModUtil;
import io.github.steveplays28.noisium.util.ModLoaderUtil;

public class NoisiumLithiumCompat {
public static final String LITHIUM_MOD_ID = "lithium";
Expand All @@ -11,6 +11,6 @@ public class NoisiumLithiumCompat {
* @return If Lithium, or a (Neo)Forge fork, is loaded.
*/
public static boolean isLithiumLoaded() {
return ModUtil.isModPresent(LITHIUM_MOD_ID) || ModUtil.isModPresent(CANARY_MOD_ID) || ModUtil.isModPresent(RADIUM_MOD_ID);
return ModLoaderUtil.isModPresent(LITHIUM_MOD_ID) || ModLoaderUtil.isModPresent(CANARY_MOD_ID) || ModLoaderUtil.isModPresent(RADIUM_MOD_ID);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.github.steveplays28.noisium.experimental.client.compat.modmenu;

import com.terraformersmc.modmenu.api.ConfigScreenFactory;
import com.terraformersmc.modmenu.api.ModMenuApi;
import io.github.steveplays28.noisium.experimental.config.NoisiumConfig;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;

@Environment(EnvType.CLIENT)
public class NoisiumModMenuCompat implements ModMenuApi {
@Override
public ConfigScreenFactory<?> getModConfigScreenFactory() {
return parent -> NoisiumConfig.HANDLER.generateGui().generateScreen(parent);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.github.steveplays28.noisium.experimental.config;

import dev.isxander.yacl3.config.v2.api.ConfigClassHandler;
import dev.isxander.yacl3.config.v2.api.SerialEntry;
import dev.isxander.yacl3.config.v2.api.autogen.AutoGen;
import dev.isxander.yacl3.config.v2.api.autogen.IntField;
import dev.isxander.yacl3.config.v2.api.serializer.GsonConfigSerializerBuilder;
import io.github.steveplays28.noisium.util.ModLoaderUtil;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.NotNull;

import static io.github.steveplays28.noisium.Noisium.MOD_ID;

public class NoisiumConfig {
public static final @NotNull String JSON_5_FILE_SUFFIX = ".json5";
public static final @NotNull ConfigClassHandler<NoisiumConfig> HANDLER = ConfigClassHandler.createBuilder(NoisiumConfig.class).id(
new Identifier(MOD_ID, "config")).serializer(config -> GsonConfigSerializerBuilder.create(config).setPath(
ModLoaderUtil.getConfigDir().resolve(String.format("%s/config%s", MOD_ID, JSON_5_FILE_SUFFIX))).setJson5(true).build()).build();

private static final @NotNull String SERVER_CATEGORY = "Server";

@AutoGen(category = SERVER_CATEGORY)
@SerialEntry(comment = "The amount of threads used by a server world's chunk manager. Every world has its own chunk manager, and thus its own threads. After changing this option you MUST restart the server.")
@IntField(min = 1, format = "%i threads")
public int serverWorldChunkManagerThreads = 2;
@AutoGen(category = SERVER_CATEGORY)
@SerialEntry(comment = "The amount of threads used by a server world's chunk manager lighting populator. Every world has its own chunk manager, and thus its own threads. After changing this option you MUST restart the server")
@IntField(min = 1, format = "%i threads")
public int serverWorldChunkManagerLightingThreads = 2;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import dev.architectury.event.events.common.LifecycleEvent;
import dev.architectury.event.events.common.TickEvent;
import io.github.steveplays28.noisium.Noisium;
import io.github.steveplays28.noisium.experimental.config.NoisiumConfig;
import io.github.steveplays28.noisium.experimental.extension.world.chunk.NoisiumWorldChunkExtension;
import io.github.steveplays28.noisium.experimental.server.world.chunk.event.NoisiumServerChunkEvent;
import io.github.steveplays28.noisium.experimental.util.world.chunk.ChunkUtil;
Expand Down Expand Up @@ -82,13 +83,13 @@ public NoisiumServerWorldChunkManager(@NotNull ServerWorld serverWorld, @NotNull
worldDirectoryPath.resolve("poi"), dataFixer, false, serverWorld.getRegistryManager(), serverWorld);
this.versionedChunkStorage = new VersionedChunkStorage(worldDirectoryPath.resolve("region"), dataFixer, false);
this.threadPoolExecutor = Executors.newFixedThreadPool(
4, new ThreadFactoryBuilder().setNameFormat(
NoisiumConfig.HANDLER.instance().serverWorldChunkManagerThreads, new ThreadFactoryBuilder().setNameFormat(
"Noisium Server World Chunk Manager " + serverWorld.getDimension().effects() + " %d").build());
this.noisePopulationThreadPoolExecutor = Executors.newFixedThreadPool(
2, new ThreadFactoryBuilder().setNameFormat(
NoisiumConfig.HANDLER.instance().serverWorldChunkManagerThreads, new ThreadFactoryBuilder().setNameFormat(
"Noisium Server World Chunk Manager Noise Population " + serverWorld.getDimension().effects() + " %d").build());
this.lightingThreadPoolExecutor = Executors.newFixedThreadPool(
2, new ThreadFactoryBuilder().setNameFormat(
NoisiumConfig.HANDLER.instance().serverWorldChunkManagerLightingThreads, new ThreadFactoryBuilder().setNameFormat(
"Noisium Server World Chunk Manager Lighting " + serverWorld.getDimension().effects() + " %d").build());
this.loadingWorldChunks = new ConcurrentHashMap<>();
this.ioWorldChunks = new ConcurrentHashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import java.util.Set;
import java.util.function.Supplier;

import static io.github.steveplays28.noisium.util.ModUtil.isModPresent;
import static io.github.steveplays28.noisium.util.ModLoaderUtil.isModPresent;

public class NoisiumMixinPlugin implements IMixinConfigPlugin {
private static final Supplier<Boolean> TRUE = () -> true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
package io.github.steveplays28.noisium.util;

import dev.architectury.injectables.annotations.ExpectPlatform;
import org.jetbrains.annotations.NotNull;

import java.nio.file.Path;

@SuppressWarnings("unused")
public abstract class ModUtil {
public abstract class ModLoaderUtil {
/**
* Checks if a mod is present during loading.
*/
@ExpectPlatform
public static boolean isModPresent(String id) {
throw new AssertionError("Platform implementation expected.");
}

/**
* @return The config directory of the mod loader.
*/
@ExpectPlatform
public static @NotNull Path getConfigDir() {
throw new AssertionError("Platform implementation expected.");
}
}
3 changes: 3 additions & 0 deletions fabric/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ dependencies {
// Fabric API
modImplementation "net.fabricmc.fabric-api:fabric-api:${rootProject.fabric_api_version}"

// YetAnotherConfigLib
modRuntimeOnly("dev.isxander:yet-another-config-lib:${rootProject.yet_another_config_lib_version}-fabric")

// Mod Menu
modRuntimeOnly "maven.modrinth:modmenu:${rootProject.mod_menu_version}"

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.github.steveplays28.noisium.util.fabric;

import io.github.steveplays28.noisium.util.ModLoaderUtil;
import net.fabricmc.loader.api.FabricLoader;
import org.jetbrains.annotations.NotNull;

import java.nio.file.Path;

/**
* Implements {@link ModLoaderUtil}.
*/
@SuppressWarnings("unused")
public class ModLoaderUtilImpl {
/**
* Checks if a mod is present during loading.
*/
public static boolean isModPresent(String id) {
return FabricLoader.getInstance().isModLoaded(id);
}

/**
* @return The config directory of the mod loader.
*/
public static @NotNull Path getConfigDir() {
return FabricLoader.getInstance().getConfigDir();
}
}

This file was deleted.

3 changes: 3 additions & 0 deletions fabric/src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
"entrypoints": {
"main": [
"io.github.steveplays28.noisium.fabric.NoisiumFabric"
],
"modmenu": [
"io.github.steveplays28.noisium.experimental.client.compat.modmenu.NoisiumModMenuCompat"
]
},
"mixins": [
Expand Down
3 changes: 3 additions & 0 deletions forge/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ dependencies {
// Architectury API
modRuntimeOnly("dev.architectury:architectury-forge:${rootProject.architectury_api_version}")

// YetAnotherConfigLib
modRuntimeOnly("dev.isxander:yet-another-config-lib:${rootProject.yet_another_config_lib_version}-forge")

common(project(path: ":common", configuration: "namedElements")) { transitive false }
shadowCommon(project(path: ":common", configuration: "transformProductionForge")) { transitive = false }
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.github.steveplays28.noisium.util.forge;

import io.github.steveplays28.noisium.util.ModLoaderUtil;
import net.minecraftforge.fml.loading.FMLPaths;
import net.minecraftforge.fml.loading.LoadingModList;
import org.jetbrains.annotations.NotNull;

import java.nio.file.Path;

/**
* Implements {@link ModLoaderUtil}.
*/
@SuppressWarnings("unused")
public class ModLoaderUtilImpl {
/**
* Checks if a mod is present during loading.
*/
public static boolean isModPresent(String id) {
return LoadingModList.get().getModFileById(id) != null;
}

/**
* @return The config directory of the mod loader.
*/
public static @NotNull Path getConfigDir() {
return FMLPaths.CONFIGDIR.get();
}
}

This file was deleted.

1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@ neoforge_version=47.1.100
mixin_extras_version=0.3.5
architectury_api_version=9.2.14
fabric_api_version=0.90.7+1.20.1
yet_another_config_lib_version=3.4.4+1.20.1
mod_menu_version=7.2.2
distant_horizons_version=2.1.2-a

0 comments on commit 5f27dfc

Please sign in to comment.