diff options
Diffstat (limited to 'src/main/java/dev/isxander/yacl/api/Binding.java')
-rw-r--r-- | src/main/java/dev/isxander/yacl/api/Binding.java | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/src/main/java/dev/isxander/yacl/api/Binding.java b/src/main/java/dev/isxander/yacl/api/Binding.java index 74120a0..d870d8c 100644 --- a/src/main/java/dev/isxander/yacl/api/Binding.java +++ b/src/main/java/dev/isxander/yacl/api/Binding.java @@ -1,10 +1,17 @@ package dev.isxander.yacl.api; import dev.isxander.yacl.impl.GenericBindingImpl; +import dev.isxander.yacl.mixin.SimpleOptionAccessor; +import net.minecraft.client.option.SimpleOption; +import org.apache.commons.lang3.Validate; import java.util.function.Consumer; import java.util.function.Supplier; +/** + * Controls modifying the bound option. + * Provides the default value, a setter and a getter. + */ public interface Binding<T> { void setValue(T value); @@ -12,7 +19,31 @@ public interface Binding<T> { T defaultValue(); + /** + * Creates a generic binding. + * + * @param def default value of the option, used to reset + * @param getter should return the current value of the option + * @param setter should set the option to the supplied value + */ static <T> Binding<T> of(T def, Supplier<T> getter, Consumer<T> setter) { + Validate.notNull(def, "`def` must not be null"); + Validate.notNull(getter, "`getter` must not be null"); + Validate.notNull(setter, "`setter` must not be null"); + return new GenericBindingImpl<>(def, getter, setter); } + + /** + * Creates a {@link Binding} for Minecraft's {@link SimpleOption} + */ + static <T> Binding<T> of(SimpleOption<T> minecraftOption) { + Validate.notNull(minecraftOption, "`minecraftOption` must not be null"); + + return new GenericBindingImpl<>( + ((SimpleOptionAccessor<T>) (Object) minecraftOption).getDefaultValue(), + minecraftOption::getValue, + minecraftOption::setValue + ); + } } |