/* * Decompiled with CFR 0.152. * * Could not load the following classes: * com.google.common.collect.ImmutableList */ package net.minecraft.client.gui.components; import com.google.common.collect.ImmutableList; import java.util.Collection; import java.util.List; import java.util.function.BooleanSupplier; import java.util.function.Function; import java.util.function.Supplier; import net.minecraft.client.Minecraft; import net.minecraft.client.OptionInstance; import net.minecraft.client.gui.GuiGraphics; import net.minecraft.client.gui.components.AbstractButton; import net.minecraft.client.gui.components.ResettableOptionWidget; import net.minecraft.client.gui.narration.NarratedElementType; import net.minecraft.client.gui.narration.NarrationElementOutput; import net.minecraft.client.input.InputWithModifiers; import net.minecraft.network.chat.CommonComponents; import net.minecraft.network.chat.Component; import net.minecraft.network.chat.MutableComponent; import net.minecraft.util.Mth; public class CycleButton extends AbstractButton implements ResettableOptionWidget { public static final BooleanSupplier DEFAULT_ALT_LIST_SELECTOR = () -> Minecraft.getInstance().hasAltDown(); private static final List BOOLEAN_OPTIONS = ImmutableList.of((Object)Boolean.TRUE, (Object)Boolean.FALSE); private final Supplier defaultValueSupplier; private final Component name; private int index; private T value; private final ValueListSupplier values; private final Function valueStringifier; private final Function, MutableComponent> narrationProvider; private final OnValueChange onValueChange; private final boolean displayOnlyValue; private final OptionInstance.TooltipSupplier tooltipSupplier; private CycleButton(int x, int y, int width, int height, Component message, Component name, int index, T value, Supplier defaultValueSupplier, ValueListSupplier values, Function valueStringifier, Function, MutableComponent> narrationProvider, OnValueChange onValueChange, OptionInstance.TooltipSupplier tooltipSupplier, boolean displayOnlyValue) { super(x, y, width, height, message); this.name = name; this.index = index; this.defaultValueSupplier = defaultValueSupplier; this.value = value; this.values = values; this.valueStringifier = valueStringifier; this.narrationProvider = narrationProvider; this.onValueChange = onValueChange; this.displayOnlyValue = displayOnlyValue; this.tooltipSupplier = tooltipSupplier; this.updateTooltip(); } @Override protected void renderContents(GuiGraphics graphics, int mouseX, int mouseY, float a) { this.renderDefaultSprite(graphics); this.renderDefaultLabel(graphics.textRendererForWidget(this, GuiGraphics.HoveredTextEffects.NONE)); } private void updateTooltip() { this.setTooltip(this.tooltipSupplier.apply(this.value)); } @Override public void onPress(InputWithModifiers input) { if (input.hasShiftDown()) { this.cycleValue(-1); } else { this.cycleValue(1); } } private void cycleValue(int delta) { List list = this.values.getSelectedList(); this.index = Mth.positiveModulo(this.index + delta, list.size()); T newValue = list.get(this.index); this.updateValue(newValue); this.onValueChange.onValueChange(this, newValue); } private T getCycledValue(int delta) { List list = this.values.getSelectedList(); return list.get(Mth.positiveModulo(this.index + delta, list.size())); } @Override public boolean mouseScrolled(double x, double y, double scrollX, double scrollY) { if (scrollY > 0.0) { this.cycleValue(-1); } else if (scrollY < 0.0) { this.cycleValue(1); } return true; } public void setValue(T newValue) { List list = this.values.getSelectedList(); int newIndex = list.indexOf(newValue); if (newIndex != -1) { this.index = newIndex; } this.updateValue(newValue); } @Override public void resetValue() { this.setValue(this.defaultValueSupplier.get()); } private void updateValue(T newValue) { Component newMessage = this.createLabelForValue(newValue); this.setMessage(newMessage); this.value = newValue; this.updateTooltip(); } private Component createLabelForValue(T newValue) { return this.displayOnlyValue ? this.valueStringifier.apply(newValue) : this.createFullName(newValue); } private MutableComponent createFullName(T newValue) { return CommonComponents.optionNameValue(this.name, this.valueStringifier.apply(newValue)); } public T getValue() { return this.value; } @Override protected MutableComponent createNarrationMessage() { return this.narrationProvider.apply(this); } @Override public void updateWidgetNarration(NarrationElementOutput output) { output.add(NarratedElementType.TITLE, (Component)this.createNarrationMessage()); if (this.active) { T nextValue = this.getCycledValue(1); Component nextValueText = this.createLabelForValue(nextValue); if (this.isFocused()) { output.add(NarratedElementType.USAGE, (Component)Component.translatable("narration.cycle_button.usage.focused", nextValueText)); } else { output.add(NarratedElementType.USAGE, (Component)Component.translatable("narration.cycle_button.usage.hovered", nextValueText)); } } } public MutableComponent createDefaultNarrationMessage() { return CycleButton.wrapDefaultNarrationMessage(this.displayOnlyValue ? this.createFullName(this.value) : this.getMessage()); } public static Builder builder(Function valueStringifier, Supplier defaultValueSupplier) { return new Builder(valueStringifier, defaultValueSupplier); } public static Builder builder(Function valueStringifier, T defaultValue) { return new Builder(valueStringifier, () -> defaultValue); } public static Builder booleanBuilder(Component trueText, Component falseText, boolean defaultValue) { return new Builder(b -> b == Boolean.TRUE ? trueText : falseText, () -> defaultValue).withValues((Collection)BOOLEAN_OPTIONS); } public static Builder onOffBuilder(boolean initialValue) { return new Builder(b -> b == Boolean.TRUE ? CommonComponents.OPTION_ON : CommonComponents.OPTION_OFF, () -> initialValue).withValues((Collection)BOOLEAN_OPTIONS); } public static interface ValueListSupplier { public List getSelectedList(); public List getDefaultList(); public static ValueListSupplier create(Collection values) { ImmutableList copy = ImmutableList.copyOf(values); return new ValueListSupplier((List)copy){ final /* synthetic */ List val$copy; { this.val$copy = list; } @Override public List getSelectedList() { return this.val$copy; } @Override public List getDefaultList() { return this.val$copy; } }; } public static ValueListSupplier create(final BooleanSupplier altSelector, List defaultList, List altList) { ImmutableList defaultCopy = ImmutableList.copyOf(defaultList); ImmutableList altCopy = ImmutableList.copyOf(altList); return new ValueListSupplier((List)altCopy, (List)defaultCopy){ final /* synthetic */ List val$altCopy; final /* synthetic */ List val$defaultCopy; { this.val$altCopy = list; this.val$defaultCopy = list2; } @Override public List getSelectedList() { return altSelector.getAsBoolean() ? this.val$altCopy : this.val$defaultCopy; } @Override public List getDefaultList() { return this.val$defaultCopy; } }; } } @FunctionalInterface public static interface OnValueChange { public void onValueChange(CycleButton var1, T var2); } public static class Builder { private final Supplier defaultValueSupplier; private final Function valueStringifier; private OptionInstance.TooltipSupplier tooltipSupplier = value -> null; private Function, MutableComponent> narrationProvider = CycleButton::createDefaultNarrationMessage; private ValueListSupplier values = ValueListSupplier.create(ImmutableList.of()); private boolean displayOnlyValue; public Builder(Function valueStringifier, Supplier defaultValueSupplier) { this.valueStringifier = valueStringifier; this.defaultValueSupplier = defaultValueSupplier; } public Builder withValues(Collection values) { return this.withValues(ValueListSupplier.create(values)); } @SafeVarargs public final Builder withValues(T ... values) { return this.withValues((Collection)ImmutableList.copyOf((Object[])values)); } public Builder withValues(List values, List altValues) { return this.withValues(ValueListSupplier.create(DEFAULT_ALT_LIST_SELECTOR, values, altValues)); } public Builder withValues(BooleanSupplier altCondition, List values, List altValues) { return this.withValues(ValueListSupplier.create(altCondition, values, altValues)); } public Builder withValues(ValueListSupplier valueListSupplier) { this.values = valueListSupplier; return this; } public Builder withTooltip(OptionInstance.TooltipSupplier tooltipSupplier) { this.tooltipSupplier = tooltipSupplier; return this; } public Builder withCustomNarration(Function, MutableComponent> narrationProvider) { this.narrationProvider = narrationProvider; return this; } public Builder displayOnlyValue(boolean flag) { this.displayOnlyValue = flag; return this; } public Builder displayOnlyValue() { return this.displayOnlyValue(true); } public CycleButton create(Component name, OnValueChange valueChangeListener) { return this.create(0, 0, 150, 20, name, valueChangeListener); } public CycleButton create(int x, int y, int width, int height, Component name) { return this.create(x, y, width, height, name, (button, value) -> {}); } public CycleButton create(int x, int y, int width, int height, Component name, OnValueChange valueChangeListener) { List values = this.values.getDefaultList(); if (values.isEmpty()) { throw new IllegalStateException("No values for cycle button"); } T initialValue = this.defaultValueSupplier.get(); int initialIndex = values.indexOf(initialValue); Component valueText = this.valueStringifier.apply(initialValue); Component initialTitle = this.displayOnlyValue ? valueText : CommonComponents.optionNameValue(name, valueText); return new CycleButton(x, y, width, height, initialTitle, name, initialIndex, initialValue, this.defaultValueSupplier, this.values, this.valueStringifier, this.narrationProvider, valueChangeListener, this.tooltipSupplier, this.displayOnlyValue); } } }