Skip to content
This repository has been archived by the owner on Jun 22, 2024. It is now read-only.

Commit

Permalink
revent
Browse files Browse the repository at this point in the history
  • Loading branch information
TexBlock committed Oct 9, 2023
1 parent 6026082 commit 1a52993
Show file tree
Hide file tree
Showing 18 changed files with 131 additions and 136 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ forge_config_api_version = v8.0.0-1.20.1-Fabric

port_lib_version = 2.1.1148+1.20-entity-refactor
# adding a module also requires adding a dependency to the FMJ
port_lib_modules = accessors,base,config,entity,extensions,networking,obj_loader,tags,transfer,models,tool_actions,client_events,brewing
port_lib_modules = accessors,base,entity,extensions,networking,obj_loader,tags,transfer,models,tool_actions,client_events,brewing

night_config_version = 3.6.3
jsr305_version = 3.0.2
Expand Down
24 changes: 12 additions & 12 deletions src/main/java/com/simibubi/create/foundation/config/ConfigBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@
import java.util.function.Function;
import java.util.function.Supplier;

import io.github.fabricators_of_create.porting_lib.config.ModConfigSpec;
import io.github.fabricators_of_create.porting_lib.config.ModConfigSpec.BooleanValue;
import io.github.fabricators_of_create.porting_lib.config.ModConfigSpec.Builder;
import io.github.fabricators_of_create.porting_lib.config.ModConfigSpec.ConfigValue;
import io.github.fabricators_of_create.porting_lib.config.ModConfigSpec.DoubleValue;
import io.github.fabricators_of_create.porting_lib.config.ModConfigSpec.EnumValue;
import io.github.fabricators_of_create.porting_lib.config.ModConfigSpec.IntValue;
import net.minecraftforge.common.ForgeConfigSpec;
import net.minecraftforge.common.ForgeConfigSpec.BooleanValue;
import net.minecraftforge.common.ForgeConfigSpec.Builder;
import net.minecraftforge.common.ForgeConfigSpec.ConfigValue;
import net.minecraftforge.common.ForgeConfigSpec.DoubleValue;
import net.minecraftforge.common.ForgeConfigSpec.EnumValue;
import net.minecraftforge.common.ForgeConfigSpec.IntValue;

public abstract class ConfigBase {

public ModConfigSpec specification;
public ForgeConfigSpec specification;

protected int depth;
protected List<CValue<?, ?>> allValues;
protected List<ConfigBase> children;

public void registerAll(final ModConfigSpec.Builder builder) {
public void registerAll(final ForgeConfigSpec.Builder builder) {
for (CValue<?, ?> cValue : allValues)
cValue.register(builder);
}
Expand All @@ -40,7 +40,7 @@ public void onReload() {

@FunctionalInterface
protected static interface IValueProvider<V, T extends ConfigValue<V>>
extends Function<ModConfigSpec.Builder, T> {
extends Function<ForgeConfigSpec.Builder, T> {
}

protected ConfigBool b(boolean current, String name, String... comment) {
Expand Down Expand Up @@ -78,7 +78,7 @@ protected ConfigGroup group(int depth, String name, String... comment) {
protected <T extends ConfigBase> T nested(int depth, Supplier<T> constructor, String... comment) {
T config = constructor.get();
new ConfigGroup(config.getName(), depth, comment);
new CValue<Boolean, ModConfigSpec.BooleanValue>(config.getName(), builder -> {
new CValue<Boolean, ForgeConfigSpec.BooleanValue>(config.getName(), builder -> {
config.depth = depth;
config.registerAll(builder);
if (config.depth > depth)
Expand Down Expand Up @@ -117,7 +117,7 @@ public void addComments(Builder builder, String... comment) {
builder.comment(".");
}

public void register(ModConfigSpec.Builder builder) {
public void register(ForgeConfigSpec.Builder builder) {
value = provider.apply(builder);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import io.github.fabricators_of_create.porting_lib.config.ConfigType;
import io.github.fabricators_of_create.porting_lib.config.ModConfigSpec;

import org.lwjgl.glfw.GLFW;

import com.simibubi.create.Create;
Expand All @@ -28,6 +25,8 @@

import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.Screen;
import net.minecraftforge.common.ForgeConfigSpec;
import net.minecraftforge.fml.config.ModConfig;

public class BaseConfigScreen extends ConfigScreen {

Expand All @@ -45,7 +44,7 @@ public class BaseConfigScreen extends ConfigScreen {
* If you are a Create Addon dev and want to change the config labels,
* add a default action here.
*
* Make sure you call either {@link #withSpecs(ModConfigSpec, ModConfigSpec, ModConfigSpec)}
* Make sure you call either {@link #withSpecs(ForgeConfigSpec, ForgeConfigSpec, ForgeConfigSpec)}
* or {@link #searchForSpecsInModContainer()}
*
* @param modID the modID of your addon/mod
Expand All @@ -68,9 +67,9 @@ public static BaseConfigScreen forCreate(Screen parent) {
protected BoxWidget others;
protected BoxWidget title;

protected ModConfigSpec clientSpec;
protected ModConfigSpec commonSpec;
protected ModConfigSpec serverSpec;
protected ForgeConfigSpec clientSpec;
protected ForgeConfigSpec commonSpec;
protected ForgeConfigSpec serverSpec;
protected String clientTitle = "Client Config";
protected String commonTitle = "Common Config";
protected String serverTitle = "Server Config";
Expand All @@ -94,35 +93,35 @@ private BaseConfigScreen(Screen parent) {

/**
* If you have static references to your Configs or ConfigSpecs (like Create does in {@link AllConfigs}),
* please use {@link #withSpecs(ModConfigSpec, ModConfigSpec, ModConfigSpec)} instead
* please use {@link #withSpecs(ForgeConfigSpec, ForgeConfigSpec, ForgeConfigSpec)} instead
*/
public BaseConfigScreen searchForSpecsInModContainer() {
if (!ConfigHelper.hasAnyForgeConfig(this.modID)){
return this;
}

try {
clientSpec = ConfigHelper.findForgeConfigSpecFor(ConfigType.CLIENT, this.modID);
clientSpec = ConfigHelper.findForgeConfigSpecFor(ModConfig.Type.CLIENT, this.modID);
} catch (Exception e) {
Create.LOGGER.debug("Unable to find ClientConfigSpec for mod: " + this.modID);
}

try {
commonSpec = ConfigHelper.findForgeConfigSpecFor(ConfigType.COMMON, this.modID);
commonSpec = ConfigHelper.findForgeConfigSpecFor(ModConfig.Type.COMMON, this.modID);
} catch (Exception e) {
Create.LOGGER.debug("Unable to find CommonConfigSpec for mod: " + this.modID);
}

try {
serverSpec = ConfigHelper.findForgeConfigSpecFor(ConfigType.SERVER, this.modID);
serverSpec = ConfigHelper.findForgeConfigSpecFor(ModConfig.Type.SERVER, this.modID);
} catch (Exception e) {
Create.LOGGER.debug("Unable to find ServerConfigSpec for mod: " + this.modID);
}

return this;
}

public BaseConfigScreen withSpecs(@Nullable ModConfigSpec client, @Nullable ModConfigSpec common, @Nullable ModConfigSpec server) {
public BaseConfigScreen withSpecs(@Nullable ForgeConfigSpec client, @Nullable ForgeConfigSpec common, @Nullable ForgeConfigSpec server) {
clientSpec = client;
commonSpec = common;
serverSpec = server;
Expand Down Expand Up @@ -151,7 +150,7 @@ protected void init() {
addRenderableWidget(clientConfigWidget = new BoxWidget(width / 2 - 100, height / 2 - 15 - 30, 200, 16).showingElement(clientText));

if (clientSpec != null) {
clientConfigWidget.withCallback(() -> linkTo(new SubMenuConfigScreen(this, ConfigType.CLIENT, clientSpec)));
clientConfigWidget.withCallback(() -> linkTo(new SubMenuConfigScreen(this, ModConfig.Type.CLIENT, clientSpec)));
clientText.withElementRenderer(BoxWidget.gradientFactory.apply(clientConfigWidget));
} else {
clientConfigWidget.active = false;
Expand All @@ -163,7 +162,7 @@ protected void init() {
addRenderableWidget(commonConfigWidget = new BoxWidget(width / 2 - 100, height / 2 - 15, 200, 16).showingElement(commonText));

if (commonSpec != null) {
commonConfigWidget.withCallback(() -> linkTo(new SubMenuConfigScreen(this, ConfigType.COMMON, commonSpec)));
commonConfigWidget.withCallback(() -> linkTo(new SubMenuConfigScreen(this, ModConfig.Type.COMMON, commonSpec)));
commonText.withElementRenderer(BoxWidget.gradientFactory.apply(commonConfigWidget));
} else {
commonConfigWidget.active = false;
Expand All @@ -188,7 +187,7 @@ protected void init() {
"Gameplay settings can only be accessed from the in-game menu after joining a World or Server."),
Palette.ALL_GRAY));
} else {
serverConfigWidget.withCallback(() -> linkTo(new SubMenuConfigScreen(this, ConfigType.SERVER, serverSpec)));
serverConfigWidget.withCallback(() -> linkTo(new SubMenuConfigScreen(this, ModConfig.Type.SERVER, serverSpec)));
serverText.withElementRenderer(BoxWidget.gradientFactory.apply(serverConfigWidget));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import com.simibubi.create.Create;
import com.simibubi.create.foundation.networking.SimplePacketBase;

import io.github.fabricators_of_create.porting_lib.config.ConfigType;
import io.github.fabricators_of_create.porting_lib.config.ModConfigSpec;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.server.level.ServerPlayer;
import net.minecraftforge.common.ForgeConfigSpec;
import net.minecraftforge.fml.config.ModConfig;

public class CConfigureConfigPacket<T> extends SimplePacketBase {

Expand Down Expand Up @@ -43,11 +43,11 @@ public boolean handle(Context context) {
if (sender == null || !sender.hasPermissions(2))
return;

ModConfigSpec spec = ConfigHelper.findForgeConfigSpecFor(ConfigType.SERVER, modID);
ForgeConfigSpec spec = ConfigHelper.findForgeConfigSpecFor(ModConfig.Type.SERVER, modID);
if (spec == null)
return;
ModConfigSpec.ValueSpec valueSpec = spec.getRaw(path);
ModConfigSpec.ConfigValue<T> configValue = spec.getValues().get(path);
ForgeConfigSpec.ValueSpec valueSpec = spec.getRaw(path);
ForgeConfigSpec.ConfigValue<T> configValue = spec.getValues().get(path);

T v = (T) deserialize(configValue.get(), value);
if (!valueSpec.test(v))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,30 @@
import com.simibubi.create.foundation.utility.Pair;
import com.simibubi.create.infrastructure.config.AllConfigs;

import io.github.fabricators_of_create.porting_lib.config.ConfigTracker;
import io.github.fabricators_of_create.porting_lib.config.ConfigType;
import io.github.fabricators_of_create.porting_lib.config.ModConfig;
import io.github.fabricators_of_create.porting_lib.config.ModConfigSpec;
import net.minecraftforge.common.ForgeConfigSpec;
import net.minecraftforge.fml.config.ConfigTracker;
import net.minecraftforge.fml.config.IConfigSpec;
import net.minecraftforge.fml.config.ModConfig;

public class ConfigHelper {

public static final Pattern unitPattern = Pattern.compile("\\[(in .*)]");
public static final Pattern annotationPattern = Pattern.compile("\\[@cui:([^:]*)(?::(.*))?]");

public static final Map<String, ConfigChange> changes = new HashMap<>();
private static final LoadingCache<String, EnumMap<ConfigType, ModConfig>> configCache =
private static final LoadingCache<String, EnumMap<ModConfig.Type, ModConfig>> configCache =
CacheBuilder.newBuilder()
.expireAfterAccess(5, TimeUnit.MINUTES)
.build(new CacheLoader<String, EnumMap<ConfigType, ModConfig>>() {
.build(new CacheLoader<String, EnumMap<ModConfig.Type, ModConfig>>() {
@Override
public EnumMap<ConfigType, ModConfig> load(@Nonnull String key) {
public EnumMap<ModConfig.Type, ModConfig> load(@Nonnull String key) {
return findModConfigsUncached(key);
}
});

// FIXME compat with other config libs?
private static EnumMap<ConfigType, ModConfig> findModConfigsUncached(String modID) {
EnumMap<ConfigType, ModConfig> configs = new EnumMap<>(ConfigType.class);
private static EnumMap<ModConfig.Type, ModConfig> findModConfigsUncached(String modID) {
EnumMap<ModConfig.Type, ModConfig> configs = new EnumMap<>(ModConfig.Type.class);
ConfigTracker.INSTANCE.configSets().forEach((type, modConfigs) -> {
modConfigs.forEach(modConfig -> {
if(modConfig.getModId().equals(modID))
Expand All @@ -55,7 +55,7 @@ private static EnumMap<ConfigType, ModConfig> findModConfigsUncached(String modI
return Objects.requireNonNull(configs);
}

public static ModConfigSpec findConfigSpecFor(ConfigType type, String modID) {
public static IConfigSpec<?> findConfigSpecFor(ModConfig.Type type, String modID) {
if (!modID.equals(Create.ID))
return configCache.getUnchecked(modID)
.get(type)
Expand All @@ -64,10 +64,10 @@ public static ModConfigSpec findConfigSpecFor(ConfigType type, String modID) {
}

@Nullable
public static ModConfigSpec findForgeConfigSpecFor(ConfigType type, String modID) {
ModConfigSpec spec = findConfigSpecFor(type, modID);
if (spec instanceof ModConfigSpec) {
return (ModConfigSpec) spec;
public static ForgeConfigSpec findForgeConfigSpecFor(ModConfig.Type type, String modID) {
IConfigSpec<?> spec = findConfigSpecFor(type, modID);
if (spec instanceof ForgeConfigSpec) {
return (ForgeConfigSpec) spec;
}
return null;
}
Expand All @@ -84,19 +84,20 @@ public static boolean hasAnyForgeConfig(String modID) {
return configCache.getUnchecked(modID)
.values()
.stream()
.anyMatch(config -> config.getSpec() instanceof ModConfigSpec);
.anyMatch(config -> config.getSpec() instanceof ForgeConfigSpec);
return true;
}

// Directly set a value
public static <T> void setConfigValue(ConfigPath path, String value) throws InvalidValueException {
ModConfigSpec spec = findForgeConfigSpecFor(path.getType(), path.getModID());
ForgeConfigSpec spec = findForgeConfigSpecFor(path.getType(), path.getModID());
if (spec == null)
return;

List<String> pathList = Arrays.asList(path.getPath());
ModConfigSpec.ValueSpec valueSpec = spec.getSpec().getRaw(pathList);
ModConfigSpec.ConfigValue<T> configValue = spec.getValues().get(pathList);
ForgeConfigSpec.ValueSpec valueSpec = spec.getRaw(pathList);
ForgeConfigSpec.ConfigValue<T> configValue = spec.getValues()
.get(pathList);
T v = (T) CConfigureConfigPacket.deserialize(configValue.get(), value);
if (!valueSpec.test(v))
throw new InvalidValueException();
Expand All @@ -105,7 +106,7 @@ public static <T> void setConfigValue(ConfigPath path, String value) throws Inva
}

// Add a value to the current UI's changes list
public static <T> void setValue(String path, ModConfigSpec.ConfigValue<T> configValue, T value,
public static <T> void setValue(String path, ForgeConfigSpec.ConfigValue<T> configValue, T value,
@Nullable Map<String, String> annotations) {
if (value.equals(configValue.get())) {
changes.remove(path);
Expand All @@ -116,7 +117,7 @@ public static <T> void setValue(String path, ModConfigSpec.ConfigValue<T> config

// Get a value from the current UI's changes list or the config value, if its
// unchanged
public static <T> T getValue(String path, ModConfigSpec.ConfigValue<T> configValue) {
public static <T> T getValue(String path, ForgeConfigSpec.ConfigValue<T> configValue) {
ConfigChange configChange = changes.get(path);
if (configChange != null)
// noinspection unchecked
Expand Down Expand Up @@ -157,7 +158,7 @@ public static Pair<String, Map<String, String>> readMetadataFromComment(List<Str

public static class ConfigPath {
private String modID = Create.ID;
private ConfigType type = ConfigType.CLIENT;
private ModConfig.Type type = ModConfig.Type.CLIENT;
private String[] path;

public static ConfigPath parse(String string) {
Expand All @@ -172,7 +173,7 @@ public static ConfigPath parse(String string) {
}
String[] split = p.split("\\.");
try {
cp.type = ConfigType.valueOf(split[0].toUpperCase(Locale.ROOT));
cp.type = ModConfig.Type.valueOf(split[0].toUpperCase(Locale.ROOT));
} catch (Exception e) {
throw new IllegalArgumentException("path must start with either 'client.', 'common.' or 'server.'");
}
Expand All @@ -188,7 +189,7 @@ public ConfigPath setID(String modID) {
return this;
}

public ConfigPath setType(ConfigType type) {
public ConfigPath setType(ModConfig.Type type) {
this.type = type;
return this;
}
Expand All @@ -202,7 +203,7 @@ public String getModID() {
return modID;
}

public ConfigType getType() {
public ModConfig.Type getType() {
return type;
}

Expand Down
Loading

0 comments on commit 1a52993

Please sign in to comment.