Skip to content

Commit

Permalink
What was I thinking???
Browse files Browse the repository at this point in the history
  • Loading branch information
xpple committed Aug 5, 2024
1 parent ec5e753 commit 86065ed
Show file tree
Hide file tree
Showing 32 changed files with 271 additions and 475 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ mod's id.
- For Paper users, register the `Configs` class in your plugin's `onEnable` method. Replace `<plugin name>` with your
plugin's name.
```java
new PluginConfigBuilder(<plugin name>, Configs.class).build();
new ModConfigBuilder(<plugin name>, Configs.class).build();
```
That's it! Now you can access `exampleString` through `Configs.exampleString`. You can edit `exampleString` by using the
config command.
Expand Down Expand Up @@ -78,15 +78,15 @@ Replace `${version}` with the artifact version. Append `-fabric` for Fabric and

You may choose between my own maven repository and GitHub's package repository.
### My own
```groovy
```gradle
repositories {
maven {
url 'https://maven.xpple.dev/maven2'
}
}
```
### GitHub packages
```groovy
```gradle
repositories {
maven {
url 'https://maven.pkg.github.com/xpple/BetterConfig'
Expand All @@ -98,7 +98,7 @@ repositories {
}
```
Import it:
```groovy
```gradle
dependencies {
// Fabric
include modImplementation('dev.xpple:betterconfig-fabric:${betterconfig-version}')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@

@ApiStatus.Internal
public class BetterConfigCommon {
public static final String BASE_ID = "betterconfig";
public static final Logger LOGGER = LoggerFactory.getLogger(BASE_ID);
public static final String MOD_ID = "betterconfig";
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import dev.xpple.betterconfig.impl.BetterConfigImpl;

public interface BetterConfigAPI {

static BetterConfigAPI getInstance() {
return BetterConfigImpl.INSTANCE;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@

import java.nio.file.Path;

public interface AbstractConfig {
public interface ModConfig {
/**
* Get the identifier of the mod of this configuration.
* @return the mod's identifier
*/
String getModId();

/**
* Get the class where all the configurations for this mod are defined.
* @return the class with all configurations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,26 @@
import com.google.gson.GsonBuilder;
import com.google.gson.TypeAdapter;
import com.mojang.brigadier.arguments.ArgumentType;
import dev.xpple.betterconfig.impl.BetterConfigImpl;
import dev.xpple.betterconfig.impl.BetterConfigInternals;
import dev.xpple.betterconfig.impl.ModConfigImpl;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;

public abstract class AbstractConfigBuilder<S, C> {
public final class ModConfigBuilder<S, C> {

final Class<?> configsClass;
private final String modId;

final GsonBuilder builder = new GsonBuilder().serializeNulls().enableComplexMapKeySerialization();
final Map<Class<?>, Function<C, ? extends ArgumentType<?>>> arguments = new HashMap<>();
private final Class<?> configsClass;

public AbstractConfigBuilder(Class<?> configsClass) {
private final GsonBuilder builder = new GsonBuilder().serializeNulls().enableComplexMapKeySerialization();
private final Map<Class<?>, Function<C, ? extends ArgumentType<?>>> arguments = new HashMap<>();

public ModConfigBuilder(String modId, Class<?> configsClass) {
this.modId = modId;
this.configsClass = configsClass;
}

Expand All @@ -31,9 +37,9 @@ public AbstractConfigBuilder(Class<?> configsClass) {
* true for all argument types that natively exist in the game. Any custom argument types must be
* converted, however. For this, use {@link dev.xpple.betterconfig.util.WrappedArgumentType} on
* Fabric or {@link io.papermc.paper.command.brigadier.argument.CustomArgumentType} on Paper.
* @see AbstractConfigBuilder#registerTypeHierarchy(Class, TypeAdapter, Supplier)
* @see ModConfigBuilder#registerTypeHierarchy(Class, TypeAdapter, Supplier)
*/
public <T> AbstractConfigBuilder<S, C> registerType(Class<T> type, TypeAdapter<T> adapter, Supplier<ArgumentType<T>> argumentTypeSupplier) {
public <T> ModConfigBuilder<S, C> registerType(Class<T> type, TypeAdapter<T> adapter, Supplier<ArgumentType<T>> argumentTypeSupplier) {
return this.registerType(type, adapter, buildContext -> argumentTypeSupplier.get());
}

Expand All @@ -48,9 +54,9 @@ public <T> AbstractConfigBuilder<S, C> registerType(Class<T> type, TypeAdapter<T
* true for all argument types that natively exist in the game. Any custom argument types must be
* converted, however. For this, use {@link dev.xpple.betterconfig.util.WrappedArgumentType} on
* Fabric or {@link io.papermc.paper.command.brigadier.argument.CustomArgumentType} on Paper.
* @see AbstractConfigBuilder#registerTypeHierarchy(Class, TypeAdapter, Function)
* @see ModConfigBuilder#registerTypeHierarchy(Class, TypeAdapter, Function)
*/
public <T> AbstractConfigBuilder<S, C> registerType(Class<T> type, TypeAdapter<T> adapter, Function<C, ArgumentType<T>> argumentTypeFunction) {
public <T> ModConfigBuilder<S, C> registerType(Class<T> type, TypeAdapter<T> adapter, Function<C, ArgumentType<T>> argumentTypeFunction) {
this.builder.registerTypeAdapter(type, adapter);
this.arguments.put(type, argumentTypeFunction);
return this;
Expand All @@ -67,9 +73,9 @@ public <T> AbstractConfigBuilder<S, C> registerType(Class<T> type, TypeAdapter<T
* true for all argument types that natively exist in the game. Any custom argument types must be
* converted, however. For this, use {@link dev.xpple.betterconfig.util.WrappedArgumentType} on
* Fabric or {@link io.papermc.paper.command.brigadier.argument.CustomArgumentType} on Paper.
* @see AbstractConfigBuilder#registerType(Class, TypeAdapter, Supplier)
* @see ModConfigBuilder#registerType(Class, TypeAdapter, Supplier)
*/
public <T> AbstractConfigBuilder<S, C> registerTypeHierarchy(Class<T> type, TypeAdapter<T> adapter, Supplier<ArgumentType<T>> argumentTypeSupplier) {
public <T> ModConfigBuilder<S, C> registerTypeHierarchy(Class<T> type, TypeAdapter<T> adapter, Supplier<ArgumentType<T>> argumentTypeSupplier) {
return this.registerTypeHierarchy(type, adapter, buildContext -> argumentTypeSupplier.get());
}

Expand All @@ -84,9 +90,9 @@ public <T> AbstractConfigBuilder<S, C> registerTypeHierarchy(Class<T> type, Type
* true for all argument types that natively exist in the game. Any custom argument types must be
* converted, however. For this, use {@link dev.xpple.betterconfig.util.WrappedArgumentType} on
* Fabric or {@link io.papermc.paper.command.brigadier.argument.CustomArgumentType} on Paper.
* @see AbstractConfigBuilder#registerType(Class, TypeAdapter, Function)
* @see ModConfigBuilder#registerType(Class, TypeAdapter, Function)
*/
public <T> AbstractConfigBuilder<S, C> registerTypeHierarchy(Class<T> type, TypeAdapter<T> adapter, Function<C, ArgumentType<T>> argumentTypeFunction) {
public <T> ModConfigBuilder<S, C> registerTypeHierarchy(Class<T> type, TypeAdapter<T> adapter, Function<C, ArgumentType<T>> argumentTypeFunction) {
this.builder.registerTypeHierarchyAdapter(type, adapter);
this.arguments.put(type, argumentTypeFunction);
return this;
Expand All @@ -96,5 +102,12 @@ public <T> AbstractConfigBuilder<S, C> registerTypeHierarchy(Class<T> type, Type
* Finalise the registration process.
* @throws IllegalArgumentException when a configuration already exists for this mod
*/
public abstract void build();
public void build() {
ModConfigImpl<?, ?> modConfig = new ModConfigImpl<>(this.modId, this.configsClass, this.builder.create(), this.arguments);
if (BetterConfigImpl.getModConfigs().putIfAbsent(this.modId, modConfig) == null) {
BetterConfigInternals.init(modConfig);
return;
}
throw new IllegalArgumentException(this.modId);
}
}
Loading

0 comments on commit 86065ed

Please sign in to comment.