package dev.isxander.yacl3.impl; import com.google.common.collect.ImmutableSet; import dev.isxander.yacl3.api.*; import dev.isxander.yacl3.gui.YACLScreen; import dev.isxander.yacl3.gui.controllers.ActionController; import net.minecraft.network.chat.Component; import org.apache.commons.lang3.Validate; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.function.BiConsumer; import java.util.function.Consumer; @ApiStatus.Internal public final class ButtonOptionImpl implements ButtonOption { private final Component name; private final OptionDescription description; private final StateManager> stateManager; private boolean available; private final Controller> controller; public ButtonOptionImpl( @NotNull Component name, @Nullable OptionDescription description, @NotNull BiConsumer action, @Nullable Component text, boolean available ) { this.name = name; this.description = description; this.stateManager = StateManager.createImmutable(action); this.available = available; this.controller = text != null ? new ActionController(this, text) : new ActionController(this); } @Override public @NotNull Component name() { return name; } @Override public @NotNull OptionDescription description() { return description; } @Override public @NotNull Component tooltip() { return description().text(); } @Override public BiConsumer action() { return stateManager().get(); } @Override public boolean available() { return available; } @Override public void setAvailable(boolean available) { this.available = available; } @Override public @NotNull Controller> controller() { return controller; } @Override public @NotNull StateManager> stateManager() { return this.stateManager; } @Override public @NotNull Binding> binding() { return new EmptyBinderImpl(); } @Override public @NotNull ImmutableSet flags() { return ImmutableSet.of(); } @Override public boolean changed() { return false; } @Override public @NotNull BiConsumer pendingValue() { throw new UnsupportedOperationException(); } @Override public void requestSet(@NotNull BiConsumer value) { throw new UnsupportedOperationException(); } @Override public boolean applyValue() { return false; } @Override public void forgetPendingValue() { } @Override public void requestSetDefault() { } @Override public boolean isPendingValueDefault() { throw new UnsupportedOperationException(); } @Override public void addEventListener(OptionEventListener> listener) { } @Override public void addListener(BiConsumer>, BiConsumer> changedListener) { } private static class EmptyBinderImpl implements Binding> { @Override public void setValue(BiConsumer value) { } @Override public BiConsumer getValue() { throw new UnsupportedOperationException(); } @Override public BiConsumer defaultValue() { throw new UnsupportedOperationException(); } } @ApiStatus.Internal public static final class BuilderImpl implements Builder { private Component name; private Component text = null; private OptionDescription description = OptionDescription.EMPTY; private boolean available = true; private BiConsumer action; @Override public Builder name(@NotNull Component name) { Validate.notNull(name, "`name` cannot be null"); this.name = name; return this; } @Override public Builder text(@NotNull Component text) { Validate.notNull(text, "`text` cannot be null"); this.text = text; return this; } @Override public Builder description(@NotNull OptionDescription description) { Validate.notNull(description, "`description` cannot be null"); this.description = description; return this; } @Override public Builder action(@NotNull BiConsumer action) { Validate.notNull(action, "`action` cannot be null"); this.action = action; return this; } @Override @Deprecated public Builder action(@NotNull Consumer action) { Validate.notNull(action, "`action` cannot be null"); this.action = (screen, button) -> action.accept(screen); return this; } @Override public Builder available(boolean available) { this.available = available; return this; } @Override public ButtonOption build() { Validate.notNull(name, "`name` must not be null when building `ButtonOption`"); Validate.notNull(action, "`action` must not be null when building `ButtonOption`"); return new ButtonOptionImpl(name, description, action, text, available); } } }