Skip to content

Commit

Permalink
Add WIP config api with IMS' redesign of the options page and contari…
Browse files Browse the repository at this point in the history
…a's forge mod entrypoint loader code
  • Loading branch information
douira committed Oct 24, 2024
1 parent bb8e33c commit b1b5ae7
Show file tree
Hide file tree
Showing 66 changed files with 2,455 additions and 1,006 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.caffeinemc.mods.sodium.api.config;

import net.caffeinemc.mods.sodium.api.config.structure.ConfigBuilder;

public interface ConfigEntryPoint {
void registerConfigEarly(ConfigBuilder builder);

void registerConfigLate(ConfigBuilder builder);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package net.caffeinemc.mods.sodium.api.config;

import net.minecraft.resources.ResourceLocation;

public interface ConfigState {
boolean readBooleanOption(ResourceLocation id);

int readIntOption(ResourceLocation id);

<E extends Enum<E>> E readEnumOption(ResourceLocation id, Class<E> enumClass);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package net.caffeinemc.mods.sodium.api.config;

@FunctionalInterface
public interface StorageEventHandler {
void afterSave();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package net.caffeinemc.mods.sodium.api.config.option;

import net.minecraft.network.chat.Component;

public interface ControlValueFormatter {
Component format(int value);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package net.caffeinemc.mods.sodium.api.config.option;

import net.minecraft.network.chat.Component;

public interface NameProvider {
Component getName();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.caffeinemc.mods.sodium.api.config.option;

public interface OptionBinding<V> {
void save(V value);

V load();

// TODO: add shortcuts to generate for vanilla option bindings
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.caffeinemc.mods.sodium.client.gui.options;
package net.caffeinemc.mods.sodium.api.config.option;

public enum OptionFlag {
REQUIRES_RENDERER_RELOAD,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package net.caffeinemc.mods.sodium.client.gui.options;
package net.caffeinemc.mods.sodium.api.config.option;

import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.Component;

public enum OptionImpact implements TextProvider {
public enum OptionImpact implements NameProvider {
LOW(ChatFormatting.GREEN, "sodium.option_impact.low"),
MEDIUM(ChatFormatting.YELLOW, "sodium.option_impact.medium"),
HIGH(ChatFormatting.GOLD, "sodium.option_impact.high"),
Expand All @@ -17,7 +17,7 @@ public enum OptionImpact implements TextProvider {
}

@Override
public Component getLocalizedName() {
public Component getName() {
return this.text;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package net.caffeinemc.mods.sodium.api.config.option;

public record Range(int min, int max, int step) {
public boolean isValueValid(int value) {
return value >= this.min && value <= this.max && (value - this.min) % this.step == 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package net.caffeinemc.mods.sodium.api.config.structure;

import net.caffeinemc.mods.sodium.api.config.*;
import net.caffeinemc.mods.sodium.api.config.option.OptionBinding;
import net.caffeinemc.mods.sodium.api.config.option.OptionFlag;
import net.caffeinemc.mods.sodium.api.config.option.OptionImpact;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;

import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

public interface BooleanOptionBuilder extends OptionBuilder<Boolean> {
@Override
BooleanOptionBuilder setName(Component name);

@Override
BooleanOptionBuilder setStorageHandler(StorageEventHandler storage);

@Override
BooleanOptionBuilder setTooltip(Component tooltip);

@Override
BooleanOptionBuilder setTooltip(Function<Boolean, Component> tooltip);

@Override
BooleanOptionBuilder setImpact(OptionImpact impact);

@Override
BooleanOptionBuilder setFlags(OptionFlag... flags);

@Override
BooleanOptionBuilder setDefaultValue(Boolean value);

@Override
BooleanOptionBuilder setDefaultProvider(Function<ConfigState, Boolean> provider, ResourceLocation... dependencies);

@Override
BooleanOptionBuilder setEnabled(boolean available);

@Override
BooleanOptionBuilder setEnabledProvider(Function<ConfigState, Boolean> provider, ResourceLocation... dependencies);

@Override
BooleanOptionBuilder setBinding(Consumer<Boolean> save, Supplier<Boolean> load);

@Override
BooleanOptionBuilder setBinding(OptionBinding<Boolean> binding);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package net.caffeinemc.mods.sodium.api.config.structure;

import net.minecraft.resources.ResourceLocation;

public interface ConfigBuilder {
ModOptionsBuilder registerModConfig(String namespace, String name, String version);

ModOptionsBuilder registerOwnModConfig();

OptionPageBuilder createOptionPage();

OptionGroupBuilder createOptionGroup();

BooleanOptionBuilder createBooleanOption(ResourceLocation id);

IntegerOptionBuilder createIntegerOption(ResourceLocation id);

<E extends Enum<E>> EnumOptionBuilder<E> createEnumOption(ResourceLocation id, Class<E> enumClass);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package net.caffeinemc.mods.sodium.api.config.structure;

import net.caffeinemc.mods.sodium.api.config.*;
import net.caffeinemc.mods.sodium.api.config.option.OptionBinding;
import net.caffeinemc.mods.sodium.api.config.option.OptionFlag;
import net.caffeinemc.mods.sodium.api.config.option.OptionImpact;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;

import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

public interface EnumOptionBuilder<E extends Enum<E>> extends OptionBuilder<E> {
@Override
EnumOptionBuilder<E> setName(Component name);

@Override
EnumOptionBuilder<E> setStorageHandler(StorageEventHandler storage);

@Override
EnumOptionBuilder<E> setTooltip(Component tooltip);

@Override
EnumOptionBuilder<E> setTooltip(Function<E, Component> tooltip);

@Override
EnumOptionBuilder<E> setImpact(OptionImpact impact);

@Override
EnumOptionBuilder<E> setFlags(OptionFlag... flags);

@Override
EnumOptionBuilder<E> setDefaultValue(E value);

@Override
EnumOptionBuilder<E> setDefaultProvider(Function<ConfigState, E> provider, ResourceLocation... dependencies);

@Override
EnumOptionBuilder<E> setEnabled(boolean available);

@Override
EnumOptionBuilder<E> setEnabledProvider(Function<ConfigState, Boolean> provider, ResourceLocation... dependencies);

@Override
EnumOptionBuilder<E> setBinding(Consumer<E> save, Supplier<E> load);

@Override
EnumOptionBuilder<E> setBinding(OptionBinding<E> binding);

EnumOptionBuilder<E> setAllowedValues(Set<E> allowedValues);

EnumOptionBuilder<E> setAllowedValuesProvider(Function<ConfigState, Set<E>> provider, ResourceLocation... dependencies);

EnumOptionBuilder<E> setElementNameProvider(Function<E, Component> provider);

static <E extends Enum<E>> Function<E, Component> nameProviderFrom(Component... names) {
return e -> names[e.ordinal()];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package net.caffeinemc.mods.sodium.api.config.structure;

import net.caffeinemc.mods.sodium.api.config.*;
import net.caffeinemc.mods.sodium.api.config.option.*;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;

import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

public interface IntegerOptionBuilder extends OptionBuilder<Integer> {
@Override
IntegerOptionBuilder setName(Component name);

@Override
IntegerOptionBuilder setStorageHandler(StorageEventHandler storage);

@Override
IntegerOptionBuilder setTooltip(Component tooltip);

@Override
IntegerOptionBuilder setTooltip(Function<Integer, Component> tooltip);

@Override
IntegerOptionBuilder setImpact(OptionImpact impact);

@Override
IntegerOptionBuilder setFlags(OptionFlag... flags);

@Override
IntegerOptionBuilder setDefaultValue(Integer value);

@Override
IntegerOptionBuilder setDefaultProvider(Function<ConfigState, Integer> provider, ResourceLocation... dependencies);

@Override
IntegerOptionBuilder setEnabled(boolean available);

@Override
IntegerOptionBuilder setEnabledProvider(Function<ConfigState, Boolean> provider, ResourceLocation... dependencies);

@Override
IntegerOptionBuilder setBinding(Consumer<Integer> save, Supplier<Integer> load);

@Override
IntegerOptionBuilder setBinding(OptionBinding<Integer> binding);

IntegerOptionBuilder setRange(int min, int max, int step);

IntegerOptionBuilder setRange(Range range);

IntegerOptionBuilder setRangeProvider(Function<ConfigState, Range> provider, ResourceLocation... dependencies);

IntegerOptionBuilder setValueFormatter(ControlValueFormatter formatter);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.caffeinemc.mods.sodium.api.config.structure;

public interface ModOptionsBuilder {
ModOptionsBuilder setName(String name);

ModOptionsBuilder setVersion(String version);

ModOptionsBuilder addPage(OptionPageBuilder page);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package net.caffeinemc.mods.sodium.api.config.structure;

import net.caffeinemc.mods.sodium.api.config.*;
import net.caffeinemc.mods.sodium.api.config.option.OptionBinding;
import net.caffeinemc.mods.sodium.api.config.option.OptionFlag;
import net.caffeinemc.mods.sodium.api.config.option.OptionImpact;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;

import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

public interface OptionBuilder<V> {
OptionBuilder<V> setName(Component name);

OptionBuilder<V> setStorageHandler(StorageEventHandler storage);

OptionBuilder<V> setTooltip(Component tooltip);

OptionBuilder<V> setTooltip(Function<V, Component> tooltip);

OptionBuilder<V> setImpact(OptionImpact impact);

OptionBuilder<V> setFlags(OptionFlag... flags);

OptionBuilder<V> setDefaultValue(V value);

OptionBuilder<V> setDefaultProvider(Function<ConfigState, V> provider, ResourceLocation... dependencies);

OptionBuilder<V> setEnabled(boolean available);

OptionBuilder<V> setEnabledProvider(Function<ConfigState, Boolean> provider, ResourceLocation... dependencies);

OptionBuilder<V> setBinding(Consumer<V> save, Supplier<V> load);

OptionBuilder<V> setBinding(OptionBinding<V> binding);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.caffeinemc.mods.sodium.api.config.structure;

import net.minecraft.network.chat.Component;

public interface OptionGroupBuilder {
OptionGroupBuilder setName(Component name);

OptionGroupBuilder addOption(OptionBuilder<?> option);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.caffeinemc.mods.sodium.api.config.structure;

import net.minecraft.network.chat.Component;

public interface OptionPageBuilder {
OptionPageBuilder setName(Component name);

OptionPageBuilder addOptionGroup(OptionGroupBuilder group);
}
Loading

0 comments on commit b1b5ae7

Please sign in to comment.