Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Bawnorton committed Oct 13, 2024
1 parent b5bc4ea commit 4709e57
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 35 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ org.gradle.jvmargs=-Xmx4G
fabric_versions=1.20.1, 1.21.1
neoforge_versions=1.21.1

mod_version=1.4.20
mod_version=2.0.0
mod_group=com.bawnorton
mod_id=configurable
mod_name=Configurable
Expand Down
22 changes: 5 additions & 17 deletions src/main/java/com/bawnorton/configurable/ConfigurableMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ public final class ConfigurableMain {
.setPrettyPrinting()
.create();

private static final Map<String, Map<Class<?>, Object>> typeAdapters = new HashMap<>();

public static Identifier id(String path) {
return Identifier.of(MOD_ID, path);
}
Expand Down Expand Up @@ -76,15 +74,13 @@ public static void init() {
}
}

registerDefaultTypeAdapters(configName);

try {
ConfigurableWrapper wrapper = new ConfigurableWrapper(ConfigurableApiImplLoader.getImpl(configName));
WRAPPERS.computeIfAbsent(configName, k -> new HashMap<>()).put(sourceSet, wrapper);
addToWrapped(settings::fullyQualifiedLoader, wrapper::setLoader, configName);
if(settings.hasScreenFactory() && !Platform.isServer()) {
addToWrapped(settings::fullyQualifiedScreenFactory, wrapper::setScreenFactory, configName);
}
WRAPPERS.computeIfAbsent(configName, k -> new HashMap<>()).put(sourceSet, wrapper);
} catch (IllegalStateException e) {
LOGGER.error("Could not create configurable wrapper for \"%s\"".formatted(configName), e);
}
Expand All @@ -105,20 +101,12 @@ private static <T> void addToWrapped(Supplier<String> nameGetter, Consumer<Class
}
}

private static void registerDefaultTypeAdapters(String configName) {
registerTypeAdapater(configName, Item.class, new ItemTypeAdapter());
}

public static <T> void registerTypeAdapater(String configName, Class<T> type, Object typeAdapter) {
typeAdapters.computeIfAbsent(configName, k -> new HashMap<>()).put(type, typeAdapter);
}

public static Map<Class<?>, Object> getTypeAdapters(String configName) {
return typeAdapters.getOrDefault(configName, Map.of());
public static Map<Class<?>, Object> getTypeAdapters(String configName, String sourceSet) {
return WRAPPERS.get(configName).get(sourceSet).getTypeAdapters();
}

public static FieldNamingStrategy getFieldNamingStrategy(String configName) {
return WRAPPERS.get(configName)
public static FieldNamingStrategy getFieldNamingStrategy(String configName, String sourceSet) {
return WRAPPERS.get(configName).get(sourceSet).getFieldNamingStrategy();
}

public static Map<String, Map<String, ConfigurableWrapper>> getAllWrappers() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public final class ConfigLoaderGenerator extends ConfigurableGenerator {
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;import java.util.function.UnaryOperator;
import java.util.Set;
import java.util.function.UnaryOperator;
public final class ConfigLoader implements GeneratedConfigLoader<Config> {
private static final Path configPath = Platform.getConfigDir()
Expand All @@ -54,7 +55,8 @@ private static Gson createGson() {
GsonBuilder builder = new GsonBuilder()
.setPrettyPrinting()
.registerTypeAdapter(Reference.class, new ReferenceSerializer());
ConfigurableMain.getTypeAdapters("<name>").forEach(builder::registerTypeHierarchyAdapter);
ConfigurableMain.getTypeAdapters("<name>", "<source_set>").forEach(builder::registerTypeHierarchyAdapter);
builder.setFieldNamingStrategy(ConfigurableMain.getFieldNamingStrategy("<name>", "<source_set>"));
return builder.create();
}
Expand All @@ -81,7 +83,7 @@ public Config loadConfig(UnaryOperator<String> datafixer) {
if(usingLegacyConfig) {
ConfigurableMain.LOGGER.info("Migrating legacy config \\"<file_name>\\"");
Files.deleteIfExists(legacyConfigPath);
saveConfig(config);
saveConfig(parsed);
}
ConfigurableMain.LOGGER.info("Successfully loaded config \\"<file_name>\\"");
Expand Down Expand Up @@ -232,11 +234,11 @@ private void parseReference(String key, JsonPrimitive value, List<String> parent
} else {
reference.setMemento(refValue);
}
} catch (ClassCastException e) {
ConfigurableMain.LOGGER.error("Field: \\"%s\\" of type \\"%s\\" could not be set.".formatted(keyPath, expected), e);
} catch (ClassCastException | IllegalArgumentException e) {
ConfigurableMain.LOGGER.warn("Field: \\"%s\\" of type \\"%s\\" could not be set to \\"%s\\". Falling back to default.".formatted(keyPath, expected, value.toString()));
}
} catch (IllegalAccessException e) {
ConfigurableMain.LOGGER.error("Field: \\"%s\\" could not be set.".formatted(keyPath), e);
ConfigurableMain.LOGGER.error("Field: \\"%s\\" could not be set. Falling back to default".formatted(keyPath), e);
}
}
Expand Down
15 changes: 12 additions & 3 deletions src/main/java/com/bawnorton/configurable/api/ConfigurableApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
import java.util.Map;

public interface ConfigurableApi {
ConfigurableApi DEFAULT = new ConfigurableApi() {};
ConfigurableApi DEFAULT = new ConfigurableApi() {
};

default Map<Class<?>, Object> getTypeAdapters() {
return Map.of();
Expand All @@ -32,6 +33,14 @@ default FieldNamingStrategy defaultFieldNamingStrategy() {
return FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES;
}

//? if neoforge
/*String getConfigName();*/
//? if !fabric {
/*/^*
* The name of the config this API impl belongs to, this is necessary for non-fabric implementations
* <br>
* <b>THIS DOES NOT SET THE NAME OF THE CONFIG</b>
^/
default String getConfigName() {
throw new UnsupportedOperationException();
}
*///?}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,16 @@ public static void load() {
FabricLoader.getInstance().getEntrypointContainers("configurable", ConfigurableApi.class).forEach(container -> {
String id = container.getProvider().getMetadata().getId();
try {
applyImpl(id, container.getEntrypoint());
impls.put(id, container.getEntrypoint());
} catch (Throwable e) {
ConfigurableMain.LOGGER.error("Mod {} provides a broken ConfigurableApi implemenation", id, e);
}
});
//?} elif neoforge {
/*serviceLoader.forEach(apiImpl -> applyImpl(apiImpl.getConfigName(), apiImpl));
/*serviceLoader.forEach(apiImpl -> impls.put(apiImpl.getConfigName(), apiImpl));
*///?}
}

private static void applyImpl(String id, ConfigurableApi apiImpl) {
Map<Class<?>, Object> adapters = apiImpl.getTypeAdapters();
adapters.forEach((type, adapter) -> ConfigurableMain.registerTypeAdapater(id, type, adapter));
impls.put(id, apiImpl);
}

public static ConfigurableApi getImpl(String name) {
return impls.getOrDefault(name, ConfigurableApi.DEFAULT);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@
import com.bawnorton.configurable.generated.GeneratedConfig;
import com.bawnorton.configurable.generated.GeneratedConfigLoader;
import com.bawnorton.configurable.generated.GeneratedConfigScreenFactory;
import com.bawnorton.configurable.ref.gson.ItemTypeAdapter;
import com.google.gson.FieldNamingStrategy;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.item.Item;
import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Map;

public final class ConfigurableWrapper {
private final ConfigurableApi apiImpl;
Expand Down Expand Up @@ -43,6 +48,16 @@ public boolean serverEnforces() {
return apiImpl.serverEnforces();
}

public FieldNamingStrategy getFieldNamingStrategy() {
return apiImpl.defaultFieldNamingStrategy();
}

public Map<Class<?>, Object> getTypeAdapters() {
Map<Class<?>, Object> typeAdapters = new HashMap<>(apiImpl.getTypeAdapters());
typeAdapters.put(Item.class, new ItemTypeAdapter());
return typeAdapters;
}

public GeneratedConfig getConfig() {
if(lastLoadedConfig == null) {
loadConfig();
Expand Down

0 comments on commit 4709e57

Please sign in to comment.