Skip to content

Commit

Permalink
Add startup configs for early usage (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
TelepathicGrunt committed Jun 2, 2024
1 parent e574c5f commit 6bf2963
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
6 changes: 6 additions & 0 deletions loader/src/main/java/net/neoforged/fml/ModContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
import net.neoforged.bus.api.Event;
import net.neoforged.bus.api.EventPriority;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.fml.config.ConfigTracker;
import net.neoforged.fml.config.IConfigSpec;
import net.neoforged.fml.config.ModConfig;
import net.neoforged.fml.event.IModBusEvent;
import net.neoforged.fml.event.lifecycle.FMLConstructModEvent;
import net.neoforged.fml.loading.FMLPaths;
import net.neoforged.neoforgespi.language.IModInfo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -96,6 +98,10 @@ public <T extends IExtensionPoint> void registerExtensionPoint(Class<T> point, S

public void addConfig(final ModConfig modConfig) {
configs.put(modConfig.getType(), modConfig);

if (modConfig.getType() == ModConfig.Type.STARTUP) {
ConfigTracker.INSTANCE.openConfig(modConfig, FMLPaths.CONFIGDIR.get(), null);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;

@ApiStatus.Internal
public class ConfigTracker {
private static final Logger LOGGER = LogUtils.getLogger();
static final Marker CONFIG = MarkerFactory.getMarker("CONFIG");
Expand All @@ -36,8 +38,8 @@ private ConfigTracker() {
this.configsByMod = new ConcurrentHashMap<>();
this.configSets.put(ModConfig.Type.CLIENT, Collections.synchronizedSet(new LinkedHashSet<>()));
this.configSets.put(ModConfig.Type.COMMON, Collections.synchronizedSet(new LinkedHashSet<>()));
// this.configSets.put(ModConfig.Type.PLAYER, new ConcurrentSkipListSet<>());
this.configSets.put(ModConfig.Type.SERVER, Collections.synchronizedSet(new LinkedHashSet<>()));
this.configSets.put(ModConfig.Type.STARTUP, Collections.synchronizedSet(new LinkedHashSet<>()));
}

void trackConfig(final ModConfig config) {
Expand Down Expand Up @@ -76,7 +78,7 @@ private Path resolveBasePath(ModConfig config, Path configBasePath, @Nullable Pa
return configBasePath;
}

private void openConfig(final ModConfig config, final Path configBasePath, @Nullable Path configOverrideBasePath) {
public void openConfig(final ModConfig config, final Path configBasePath, @Nullable Path configOverrideBasePath) {
LOGGER.trace(CONFIG, "Loading config file type {} at {} for {}", config.getType(), config.getFileName(), config.getModId());
final Path basePath = resolveBasePath(config, configBasePath, configOverrideBasePath);
final CommentedFileConfig configData = ConfigFileTypeHandler.TOML.reader(basePath).apply(config);
Expand Down
17 changes: 11 additions & 6 deletions loader/src/main/java/net/neoforged/fml/config/ModConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,24 @@ public enum Type {
* Suffix is "-client" by default.
*/
CLIENT,
// /**
// * Player type config is configuration that is associated with a player.
// * Preferences around machine states, for example.
// */
// PLAYER,
/**
* Server type config is configuration that is associated with a server instance.
* Only loaded during server startup.
* Stored in a server/save specific "serverconfig" directory.
* Synced to clients during connection.
* Suffix is "-server" by default.
*/
SERVER;
SERVER,
/**
* Startup configs are for configurations that need to run as early as possible.
* Loaded as soon as the config is registered to FML.
* Please be aware when using them, as using these configs to enable/disable registration and anything that must be present on both sides
* can cause clients to have issues connecting to servers with different config values.
* Stored in the global config directory.
* Not synced.
* Suffix is "-startup" by default.
*/
STARTUP;

public String extension() {
return StringUtils.toLowerCase(name());
Expand Down

0 comments on commit 6bf2963

Please sign in to comment.