Skip to content

Commit

Permalink
add api methods to add button that redirects to an external page
Browse files Browse the repository at this point in the history
  • Loading branch information
douira committed Nov 2, 2024
1 parent 384a818 commit 9d65347
Show file tree
Hide file tree
Showing 39 changed files with 766 additions and 319 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import java.util.function.Function;
import java.util.function.Supplier;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ public interface ConfigBuilder {
IntegerOptionBuilder createIntegerOption(ResourceLocation id);

<E extends Enum<E>> EnumOptionBuilder<E> createEnumOption(ResourceLocation id, Class<E> enumClass);

ExternalButtonOptionBuilder createExternalButtonOption(ResourceLocation id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import java.util.function.Function;
import java.util.function.Supplier;

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

import net.caffeinemc.mods.sodium.api.config.ConfigState;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;

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

public interface ExternalButtonOptionBuilder extends OptionBuilder {
ExternalButtonOptionBuilder setScreenProvider(Consumer<Screen> currentScreenConsumer);

@Override
ExternalButtonOptionBuilder setName(Component name);

@Override
ExternalButtonOptionBuilder setTooltip(Component tooltip);

@Override
ExternalButtonOptionBuilder setEnabled(boolean available);

@Override
ExternalButtonOptionBuilder setEnabledProvider(Function<ConfigState, Boolean> provider, ResourceLocation... dependencies);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import java.util.function.Function;
import java.util.function.Supplier;

public interface IntegerOptionBuilder extends OptionBuilder<Integer> {
public interface IntegerOptionBuilder extends StatefulOptionBuilder<Integer> {
IntegerOptionBuilder setRange(int min, int max, int step);

IntegerOptionBuilder setRange(Range range);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,17 @@
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.caffeinemc.mods.sodium.api.config.ConfigState;
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);
public interface OptionBuilder {
OptionBuilder setName(Component name);

OptionBuilder<V> setStorageHandler(StorageEventHandler storage);
OptionBuilder setTooltip(Component tooltip);

OptionBuilder<V> setTooltip(Component tooltip);
OptionBuilder setEnabled(boolean available);

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);
OptionBuilder setEnabledProvider(Function<ConfigState, Boolean> provider, ResourceLocation... dependencies);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
public interface OptionGroupBuilder {
OptionGroupBuilder setName(Component name);

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

import net.caffeinemc.mods.sodium.api.config.ConfigState;
import net.caffeinemc.mods.sodium.api.config.StorageEventHandler;
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 StatefulOptionBuilder<V> extends OptionBuilder {
StatefulOptionBuilder<V> setStorageHandler(StorageEventHandler storage);

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

StatefulOptionBuilder<V> setImpact(OptionImpact impact);

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

StatefulOptionBuilder<V> setDefaultValue(V value);

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

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

StatefulOptionBuilder<V> setBinding(OptionBinding<V> binding);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
import java.util.EnumSet;
import java.util.function.Function;

class BooleanOption extends Option<Boolean> {
BooleanOption(ResourceLocation id, Collection<ResourceLocation> dependencies, Component name, StorageEventHandler storage, Function<Boolean, Component> tooltipProvider, OptionImpact impact, EnumSet<OptionFlag> flags, DependentValue<Boolean> defaultValue, DependentValue<Boolean> enabled, OptionBinding<Boolean> binding) {
super(id, dependencies, name, storage, tooltipProvider, impact, flags, defaultValue, enabled, binding);
class BooleanOption extends StatefulOption<Boolean> {
BooleanOption(ResourceLocation id, Collection<ResourceLocation> dependencies, Component name, DependentValue<Boolean> enabled, StorageEventHandler storage, Function<Boolean, Component> tooltipProvider, OptionImpact impact, EnumSet<OptionFlag> flags, DependentValue<Boolean> defaultValue, OptionBinding<Boolean> binding) {
super(id, dependencies, name, enabled, storage, tooltipProvider, impact, flags, defaultValue, binding);
}

@Override
Control<Boolean> createControl() {
Control createControl() {
return new TickBoxControl(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
import java.util.function.Function;
import java.util.function.Supplier;

class BooleanOptionBuilderImpl extends OptionBuilderImpl<Boolean> implements BooleanOptionBuilder {
class BooleanOptionBuilderImpl extends StatefulOptionBuilderImpl<Boolean> implements BooleanOptionBuilder {
BooleanOptionBuilderImpl(ResourceLocation id) {
super(id);
}

@Override
BooleanOption build() {
this.prepareBuild();
return new BooleanOption(this.id, this.getDependencies(), this.name, this.storage, this.tooltipProvider, this.impact, this.flags, this.defaultValue, this.enabled, this.binding);
return new BooleanOption(this.id, this.getDependencies(), this.name, this.enabled, this.storage, this.tooltipProvider, this.impact, this.flags, this.defaultValue, this.binding);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
import java.util.EnumSet;
import java.util.Map;

// TODO: notify storage handlers after update is completed
public class Config implements ConfigState {
private final Map<ResourceLocation, Option<?>> options = new Object2ReferenceLinkedOpenHashMap<>();
private final Map<ResourceLocation, Option> options = new Object2ReferenceLinkedOpenHashMap<>();
private final ObjectOpenHashSet<StorageEventHandler> pendingStorageHandlers = new ObjectOpenHashSet<>();
private final ImmutableList<ModOptions> modOptions;

Expand Down Expand Up @@ -55,7 +54,7 @@ private void validateDependencyGraph() {
}
}

private void checkDependencyCycles(Option<?> option, ObjectOpenHashSet<ResourceLocation> stack, ObjectOpenHashSet<ResourceLocation> finished) {
private void checkDependencyCycles(Option option, ObjectOpenHashSet<ResourceLocation> stack, ObjectOpenHashSet<ResourceLocation> finished) {
if (!stack.add(option.id)) {
throw new IllegalArgumentException("Cycle detected in dependency graph starting from option " + option.id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ public ConfigBuilderImpl(String defaultNamespace, String defaultName, String def
this.defaultVersion = defaultVersion;
}

public Collection<ModOptions> build() {
var configs = new ArrayList<ModOptions>(this.pendingModConfigBuilders.size());
for (var builder : this.pendingModConfigBuilders) {
configs.add(builder.build());
}
return configs;
}

@Override
public ModOptionsBuilder registerModOptions(String namespace, String name, String version) {
var builder = new ModOptionsBuilderImpl(namespace, name, version);
Expand Down Expand Up @@ -67,11 +75,8 @@ public <E extends Enum<E>> EnumOptionBuilder<E> createEnumOption(ResourceLocatio
return new EnumOptionBuilderImpl<>(id, enumClass);
}

public Collection<ModOptions> build() {
var configs = new ArrayList<ModOptions>(this.pendingModConfigBuilders.size());
for (var builder : this.pendingModConfigBuilders) {
configs.add(builder.build());
}
return configs;
@Override
public ExternalButtonOptionBuilder createExternalButtonOption(ResourceLocation id) {
return new ExternalButtonOptionBuilderImpl(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
import java.util.Set;
import java.util.function.Function;

class EnumOption<E extends Enum<E>> extends Option<E> {
public class EnumOption<E extends Enum<E>> extends StatefulOption<E> {
final Class<E> enumClass;

private final DependentValue<Set<E>> allowedValues;
private final Function<E, Component> elementNameProvider;

EnumOption(ResourceLocation id, Collection<ResourceLocation> dependencies, Class<E> enumClass, Component name, StorageEventHandler storage, Function<E, Component> tooltipProvider, OptionImpact impact, EnumSet<OptionFlag> flags, DependentValue<E> defaultValue, DependentValue<Boolean> enabled, OptionBinding<E> binding, DependentValue<Set<E>> allowedValues, Function<E, Component> elementNameProvider) {
super(id, dependencies, name, storage, tooltipProvider, impact, flags, defaultValue, enabled, binding);
EnumOption(ResourceLocation id, Collection<ResourceLocation> dependencies, Component name, DependentValue<Boolean> enabled, StorageEventHandler storage, Function<E, Component> tooltipProvider, OptionImpact impact, EnumSet<OptionFlag> flags, DependentValue<E> defaultValue, OptionBinding<E> binding, Class<E> enumClass, DependentValue<Set<E>> allowedValues, Function<E, Component> elementNameProvider) {
super(id, dependencies, name, enabled, storage, tooltipProvider, impact, flags, defaultValue, binding);
this.enumClass = enumClass;
this.allowedValues = allowedValues;
this.elementNameProvider = elementNameProvider;
Expand All @@ -34,7 +34,7 @@ boolean isValueValid(E value) {
}

@Override
Control<E> createControl() {
Control createControl() {
// TODO: doesn't update allowed values when dependencies change
return new CyclingControl<>(this, this.enumClass, this.elementNameProvider, this.allowedValues.get(this.state).toArray(this.enumClass.getEnumConstants()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import java.util.function.Function;
import java.util.function.Supplier;

class EnumOptionBuilderImpl<E extends Enum<E>> extends OptionBuilderImpl<E> implements EnumOptionBuilder<E> {
class EnumOptionBuilderImpl<E extends Enum<E>> extends StatefulOptionBuilderImpl<E> implements EnumOptionBuilder<E> {
final Class<E> enumClass;

DependentValue<Set<E>> allowedValues;
Expand All @@ -31,7 +31,7 @@ class EnumOptionBuilderImpl<E extends Enum<E>> extends OptionBuilderImpl<E> impl
}

@Override
Option<E> build() {
Option build() {
this.prepareBuild();

if (this.allowedValues == null) {
Expand All @@ -44,7 +44,7 @@ Option<E> build() {

Validate.notNull(this.elementNameProvider, "Element name provider must be set or enum class must implement TextProvider");

return new EnumOption<>(this.id, this.getDependencies(), this.enumClass, this.name, this.storage, this.tooltipProvider, this.impact, this.flags, this.defaultValue, this.enabled, this.binding, this.allowedValues, this.elementNameProvider);
return new EnumOption<>(this.id, this.getDependencies(), this.name, this.enabled, this.storage, this.tooltipProvider, this.impact, this.flags, this.defaultValue, this.binding, this.enumClass, this.allowedValues, this.elementNameProvider);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package net.caffeinemc.mods.sodium.client.config.structure;

import net.caffeinemc.mods.sodium.client.config.value.DependentValue;
import net.caffeinemc.mods.sodium.client.gui.options.control.Control;
import net.caffeinemc.mods.sodium.client.gui.options.control.ExternalButtonControl;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;

import java.util.Collection;
import java.util.function.Consumer;

public class ExternalButtonOption extends StaticOption {
final Consumer<Screen> currentScreenConsumer;

ExternalButtonOption(ResourceLocation id, Collection<ResourceLocation> dependencies, Component name, DependentValue<Boolean> enabled, Component tooltip, Consumer<Screen> currentScreenConsumer) {
super(id, dependencies, name, enabled, tooltip);
this.currentScreenConsumer = currentScreenConsumer;
}

@Override
Control createControl() {
return new ExternalButtonControl(this, this.currentScreenConsumer);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package net.caffeinemc.mods.sodium.client.config.structure;

import net.caffeinemc.mods.sodium.api.config.ConfigState;
import net.caffeinemc.mods.sodium.api.config.structure.ExternalButtonOptionBuilder;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import org.apache.commons.lang3.Validate;

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

class ExternalButtonOptionBuilderImpl extends StaticOptionBuilderImpl implements ExternalButtonOptionBuilder {
private Consumer<Screen> currentScreenConsumer;

ExternalButtonOptionBuilderImpl(ResourceLocation id) {
super(id);
}

@Override
void prepareBuild() {
super.prepareBuild();

Validate.notNull(this.currentScreenConsumer, "Screen provider must be set");
}

@Override
Option build() {
this.prepareBuild();

return new ExternalButtonOption(this.id, this.getDependencies(), this.name, this.enabled, this.tooltip, this.currentScreenConsumer);
}

@Override
public ExternalButtonOptionBuilder setScreenProvider(Consumer<Screen> currentScreenConsumer) {
this.currentScreenConsumer = currentScreenConsumer;
return this;
}

@Override
public ExternalButtonOptionBuilder setName(Component name) {
super.setName(name);
return this;
}

@Override
public ExternalButtonOptionBuilder setEnabled(boolean available) {
super.setEnabled(available);
return this;
}

@Override
public ExternalButtonOptionBuilder setEnabledProvider(Function<ConfigState, Boolean> provider, ResourceLocation... dependencies) {
super.setEnabledProvider(provider, dependencies);
return this;
}

@Override
public ExternalButtonOptionBuilder setTooltip(Component tooltip) {
super.setTooltip(tooltip);
return this;
}
}
Loading

0 comments on commit 9d65347

Please sign in to comment.