Skip to content

Commit

Permalink
feat: add ModuleSettings.Builder#addSetting
Browse files Browse the repository at this point in the history
  • Loading branch information
Luna authored Sep 3, 2023
1 parent d82653f commit 24dc57b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ interface Builder {
*/
Builder addField(final String name, final String friendlyName, final String description, final Object defaultValue);

/**
* Adds a field to the builder
*
* @param setting the setting
* @param <T> the value type
* @return this builder
*/
<T> Builder addSetting(final Setting<T> setting);

/**
* Sets the path of the builder
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ public interface Setting<T> {
*/
Class<T> getValueClass();

/**
* Set the value for this setting
*
* @param newValue The new value to set
*/
void setValue(final T newValue);

/**
* Renders the setting for the ClickGUI
*
Expand Down
23 changes: 15 additions & 8 deletions src/main/java/me/allinkdev/deviousmod/module/DModuleSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
import java.util.Set;

public final class DModuleSettings extends AbstractDataStore implements ModuleSettings {
private final Map<String, DSetting<Object>> settings;
private final Map<String, Setting<Object>> settings;

DModuleSettings(final @NotNull Path path, final Map<String, DSetting<Object>> settings) {
DModuleSettings(final @NotNull Path path, final Map<String, Setting<Object>> settings) {
super(path);

this.settings = settings;
Expand All @@ -31,7 +31,7 @@ public static Builder builder() {
return new Builder();
}

private void addToJsonObject(final JsonObject jsonObject, final String key, final DSetting<?> value) {
private void addToJsonObject(final JsonObject jsonObject, final String key, final Setting<?> value) {
final JsonElement jsonElement = GSON.toJsonTree(value.getValue());
jsonObject.add(key, jsonElement);
}
Expand Down Expand Up @@ -68,7 +68,7 @@ protected void load(final JsonElement jsonElement) {
continue;
}

final DSetting<Object> settingData = this.settings.get(key);
final Setting<Object> settingData = this.settings.get(key);
final Class<?> settingClass = settingData.getValueClass();
final JsonElement element = jsonObject.get(key);
final Object asObject = GSON.fromJson(element, settingClass);
Expand All @@ -89,7 +89,7 @@ public void load() throws IOException {
@Override
@SuppressWarnings("unchecked")
public <T> Setting<T> getSetting(final String name, final Class<T> clazz) {
final DSetting<?> setting = this.settings.get(name);
final Setting<?> setting = this.settings.get(name);

if (setting == null) throw new NullPointerException("Tried to read a field that didn't exist: " + name);
final Class<?> actualClass = setting.getValueClass();
Expand All @@ -101,7 +101,7 @@ public <T> Setting<T> getSetting(final String name, final Class<T> clazz) {

@Override
public <T> void writeValue(final String name, final T object, final Class<T> clazz) throws IOException {
final DSetting<Object> setting = this.settings.get(name);
final Setting<Object> setting = this.settings.get(name);

if (setting == null) throw new NullPointerException("Tried to set a field that doesn't exist: " + name);
final Class<?> actualClass = setting.getValueClass();
Expand All @@ -114,7 +114,7 @@ public <T> void writeValue(final String name, final T object, final Class<T> cla

@Override
public Class<?> getValueClass(final String name) {
final DSetting<?> setting = this.settings.get(name);
final Setting<?> setting = this.settings.get(name);
if (setting == null) throw new NullPointerException("Tried to query the class of a field that didn't exist: " + name);
return setting.getValueClass();
}
Expand All @@ -130,7 +130,7 @@ public boolean hasSettings() {
}

public static final class Builder implements ModuleSettings.Builder {
private final Map<String, DSetting<Object>> objectMap = new Object2ObjectArrayMap<>();
private final Map<String, Setting<Object>> objectMap = new Object2ObjectArrayMap<>();
private @Nullable Path path;

private void throwIfExists(final String name) {
Expand All @@ -149,6 +149,13 @@ public Builder addField(final @NotNull String name, final @NotNull String friend
return this;
}

@Override
public <T> Builder addSetting(final @NotNull Setting<T> setting) {
this.throwIfExists(setting.getName());
this.objectMap.put(setting.getName(), (Setting<Object>) setting);
return this;
}

@Override
public Builder setPath(final @NotNull Path path) {
this.path = path;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public T getValue() {
return this.value;
}

@Override
public void setValue(final T newValue) {
this.value = newValue;
}
Expand Down

0 comments on commit 24dc57b

Please sign in to comment.