123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521 |
- package me.shedaniel.clothconfig2.impl.builders;
- import me.shedaniel.clothconfig2.gui.entries.DropdownBoxEntry;
- import me.shedaniel.clothconfig2.gui.entries.DropdownBoxEntry.DefaultSelectionCellCreator;
- import me.shedaniel.clothconfig2.gui.entries.DropdownBoxEntry.DefaultSelectionTopCellElement;
- import me.shedaniel.clothconfig2.gui.entries.DropdownBoxEntry.SelectionCellCreator;
- import me.shedaniel.clothconfig2.gui.entries.DropdownBoxEntry.SelectionTopCellElement;
- import net.minecraft.block.Block;
- import net.minecraft.client.MinecraftClient;
- import net.minecraft.client.render.item.ItemRenderer;
- import net.minecraft.item.Item;
- import net.minecraft.item.ItemStack;
- import net.minecraft.item.Items;
- import net.minecraft.util.Identifier;
- import net.minecraft.util.registry.Registry;
- import javax.annotation.Nonnull;
- import java.util.Collections;
- import java.util.Objects;
- import java.util.Optional;
- import java.util.function.Consumer;
- import java.util.function.Function;
- import java.util.function.Supplier;
- public class DropdownMenuBuilder<T> extends FieldBuilder<T, DropdownBoxEntry<T>> {
- protected SelectionTopCellElement<T> topCellElement;
- protected SelectionCellCreator<T> cellCreator;
- protected Function<T, Optional<String[]>> tooltipSupplier = str -> Optional.empty();
- protected Consumer<T> saveConsumer = null;
- protected Iterable<T> selections = Collections.emptyList();
-
- public DropdownMenuBuilder(String resetButtonKey, String fieldNameKey, SelectionTopCellElement<T> topCellElement, SelectionCellCreator<T> cellCreator) {
- super(resetButtonKey, fieldNameKey);
- this.topCellElement = Objects.requireNonNull(topCellElement);
- this.cellCreator = Objects.requireNonNull(cellCreator);
- }
-
- public DropdownMenuBuilder setSelections(Iterable<T> selections) {
- this.selections = selections;
- return this;
- }
-
- public DropdownMenuBuilder setDefaultValue(Supplier<T> defaultValue) {
- this.defaultValue = defaultValue;
- return this;
- }
-
- public DropdownMenuBuilder setDefaultValue(T defaultValue) {
- this.defaultValue = () -> Objects.requireNonNull(defaultValue);
- return this;
- }
-
- public DropdownMenuBuilder setSaveConsumer(Consumer<T> saveConsumer) {
- this.saveConsumer = saveConsumer;
- return this;
- }
-
- public DropdownMenuBuilder setTooltipSupplier(Supplier<Optional<String[]>> tooltipSupplier) {
- this.tooltipSupplier = str -> tooltipSupplier.get();
- return this;
- }
-
- public DropdownMenuBuilder setTooltipSupplier(Function<T, Optional<String[]>> tooltipSupplier) {
- this.tooltipSupplier = tooltipSupplier;
- return this;
- }
-
- public DropdownMenuBuilder setTooltip(Optional<String[]> tooltip) {
- this.tooltipSupplier = str -> tooltip;
- return this;
- }
-
- public DropdownMenuBuilder setTooltip(String... tooltip) {
- this.tooltipSupplier = str -> Optional.ofNullable(tooltip);
- return this;
- }
-
- public DropdownMenuBuilder requireRestart() {
- requireRestart(true);
- return this;
- }
-
- public DropdownMenuBuilder setErrorSupplier(Function<T, Optional<String>> errorSupplier) {
- this.errorSupplier = errorSupplier;
- return this;
- }
-
- @Nonnull
- @Override
- public DropdownBoxEntry<T> build() {
- DropdownBoxEntry<T> entry = new DropdownBoxEntry<T>(getFieldNameKey(), getResetButtonKey(), null, isRequireRestart(), defaultValue, saveConsumer, selections, topCellElement, cellCreator);
- entry.setTooltipSupplier(() -> tooltipSupplier.apply(entry.getValue()));
- if (errorSupplier != null)
- entry.setErrorSupplier(() -> errorSupplier.apply(entry.getValue()));
- return entry;
- }
-
- public static class TopCellElementBuilder {
- public static final Function<String, Identifier> IDENTIFIER_FUNCTION = str -> {
- try {
- return new Identifier(str);
- } catch (NumberFormatException e) {
- return null;
- }
- };
- public static final Function<String, Identifier> ITEM_IDENTIFIER_FUNCTION = str -> {
- try {
- Identifier identifier = new Identifier(str);
- if (Registry.ITEM.getOrEmpty(identifier).isPresent())
- return identifier;
- } catch (Exception ignored) {
- }
- return null;
- };
- public static final Function<String, Identifier> BLOCK_IDENTIFIER_FUNCTION = str -> {
- try {
- Identifier identifier = new Identifier(str);
- if (Registry.BLOCK.getOrEmpty(identifier).isPresent())
- return identifier;
- } catch (Exception ignored) {
- }
- return null;
- };
- public static final Function<String, Item> ITEM_FUNCTION = str -> {
- try {
- return Registry.ITEM.getOrEmpty(new Identifier(str)).get();
- } catch (Exception ignored) {
- }
- return null;
- };
- public static final Function<String, Block> BLOCK_FUNCTION = str -> {
- try {
- return Registry.BLOCK.getOrEmpty(new Identifier(str)).get();
- } catch (Exception ignored) {
- }
- return null;
- };
- private static final ItemStack BARRIER = new ItemStack(Items.BARRIER);
-
- public static <T> SelectionTopCellElement<T> of(T value, Function<String, T> toObjectFunction) {
- return of(value, toObjectFunction, Object::toString);
- }
-
- public static <T> SelectionTopCellElement<T> of(T value, Function<String, T> toObjectFunction, Function<T, String> toStringFunction) {
- return new DefaultSelectionTopCellElement<>(value, toObjectFunction, toStringFunction);
- }
-
- public static SelectionTopCellElement<Identifier> ofItemIdentifier(Item item) {
- return new DefaultSelectionTopCellElement<Identifier>(Registry.ITEM.getId(item), ITEM_IDENTIFIER_FUNCTION, Identifier::toString) {
- @Override
- public void render(int mouseX, int mouseY, int x, int y, int width, int height, float delta) {
- textFieldWidget.x = x + 4;
- textFieldWidget.y = y + 6;
- textFieldWidget.setWidth(width - 4 - 20);
- textFieldWidget.setEditable(getParent().isEditable());
- textFieldWidget.setEditableColor(getPreferredTextColor());
- textFieldWidget.render(mouseX, mouseY, delta);
- ItemRenderer itemRenderer = MinecraftClient.getInstance().getItemRenderer();
- ItemStack stack = hasConfigError() ? BARRIER : new ItemStack(Registry.ITEM.get(getValue()));
- itemRenderer.renderGuiItemIcon(stack, x + width - 18, y + 2);
- }
- };
- }
-
- public static SelectionTopCellElement<Identifier> ofBlockIdentifier(Block block) {
- return new DefaultSelectionTopCellElement<Identifier>(Registry.BLOCK.getId(block), BLOCK_IDENTIFIER_FUNCTION, Identifier::toString) {
- @Override
- public void render(int mouseX, int mouseY, int x, int y, int width, int height, float delta) {
- textFieldWidget.x = x + 4;
- textFieldWidget.y = y + 6;
- textFieldWidget.setWidth(width - 4 - 20);
- textFieldWidget.setEditable(getParent().isEditable());
- textFieldWidget.setEditableColor(getPreferredTextColor());
- textFieldWidget.render(mouseX, mouseY, delta);
- ItemRenderer itemRenderer = MinecraftClient.getInstance().getItemRenderer();
- ItemStack stack = hasConfigError() ? BARRIER : new ItemStack(Registry.BLOCK.get(getValue()));
- itemRenderer.renderGuiItemIcon(stack, x + width - 18, y + 2);
- }
- };
- }
-
- public static SelectionTopCellElement<Item> ofItemObject(Item item) {
- return new DefaultSelectionTopCellElement<Item>(item, ITEM_FUNCTION, i -> Registry.ITEM.getId(i).toString()) {
- @Override
- public void render(int mouseX, int mouseY, int x, int y, int width, int height, float delta) {
- textFieldWidget.x = x + 4;
- textFieldWidget.y = y + 6;
- textFieldWidget.setWidth(width - 4 - 20);
- textFieldWidget.setEditable(getParent().isEditable());
- textFieldWidget.setEditableColor(getPreferredTextColor());
- textFieldWidget.render(mouseX, mouseY, delta);
- ItemRenderer itemRenderer = MinecraftClient.getInstance().getItemRenderer();
- ItemStack stack = hasConfigError() ? BARRIER : new ItemStack(getValue());
- itemRenderer.renderGuiItemIcon(stack, x + width - 18, y + 2);
- }
- };
- }
-
- public static SelectionTopCellElement<Block> ofBlockObject(Block block) {
- return new DefaultSelectionTopCellElement<Block>(block, BLOCK_FUNCTION, i -> Registry.BLOCK.getId(i).toString()) {
- @Override
- public void render(int mouseX, int mouseY, int x, int y, int width, int height, float delta) {
- textFieldWidget.x = x + 4;
- textFieldWidget.y = y + 6;
- textFieldWidget.setWidth(width - 4 - 20);
- textFieldWidget.setEditable(getParent().isEditable());
- textFieldWidget.setEditableColor(getPreferredTextColor());
- textFieldWidget.render(mouseX, mouseY, delta);
- ItemRenderer itemRenderer = MinecraftClient.getInstance().getItemRenderer();
- ItemStack stack = hasConfigError() ? BARRIER : new ItemStack(getValue());
- itemRenderer.renderGuiItemIcon(stack, x + width - 18, y + 2);
- }
- };
- }
- }
-
- public static class CellCreatorBuilder {
- public static <T> SelectionCellCreator<T> of() {
- return new DefaultSelectionCellCreator<>();
- }
-
- public static <T> SelectionCellCreator<T> of(Function<T, String> toStringFunction) {
- return new DefaultSelectionCellCreator<>(toStringFunction);
- }
-
- public static <T> SelectionCellCreator<T> ofWidth(int cellWidth) {
- return new DefaultSelectionCellCreator<T>() {
- @Override
- public int getCellWidth() {
- return cellWidth;
- }
- };
- }
-
- public static <T> SelectionCellCreator<T> ofWidth(int cellWidth, Function<T, String> toStringFunction) {
- return new DefaultSelectionCellCreator<T>(toStringFunction) {
- @Override
- public int getCellWidth() {
- return cellWidth;
- }
- };
- }
-
- public static <T> SelectionCellCreator<T> ofCellCount(int maxItems) {
- return new DefaultSelectionCellCreator<T>() {
- @Override
- public int getDropBoxMaxHeight() {
- return getCellHeight() * maxItems;
- }
- };
- }
-
- public static <T> SelectionCellCreator<T> ofCellCount(int maxItems, Function<T, String> toStringFunction) {
- return new DefaultSelectionCellCreator<T>(toStringFunction) {
- @Override
- public int getDropBoxMaxHeight() {
- return getCellHeight() * maxItems;
- }
- };
- }
-
- public static <T> SelectionCellCreator<T> of(int cellWidth, int maxItems) {
- return new DefaultSelectionCellCreator<T>() {
- @Override
- public int getCellWidth() {
- return cellWidth;
- }
-
- @Override
- public int getDropBoxMaxHeight() {
- return getCellHeight() * maxItems;
- }
- };
- }
-
- public static <T> SelectionCellCreator<T> of(int cellWidth, int maxItems, Function<T, String> toStringFunction) {
- return new DefaultSelectionCellCreator<T>(toStringFunction) {
- @Override
- public int getCellWidth() {
- return cellWidth;
- }
-
- @Override
- public int getDropBoxMaxHeight() {
- return getCellHeight() * maxItems;
- }
- };
- }
-
- public static <T> SelectionCellCreator<T> of(int cellHeight, int cellWidth, int maxItems) {
- return new DefaultSelectionCellCreator<T>() {
- @Override
- public int getCellHeight() {
- return cellHeight;
- }
-
- @Override
- public int getCellWidth() {
- return cellWidth;
- }
-
- @Override
- public int getDropBoxMaxHeight() {
- return getCellHeight() * maxItems;
- }
- };
- }
-
- public static <T> SelectionCellCreator<T> of(int cellHeight, int cellWidth, int maxItems, Function<T, String> toStringFunction) {
- return new DefaultSelectionCellCreator<T>(toStringFunction) {
- @Override
- public int getCellHeight() {
- return cellHeight;
- }
-
- @Override
- public int getCellWidth() {
- return cellWidth;
- }
-
- @Override
- public int getDropBoxMaxHeight() {
- return getCellHeight() * maxItems;
- }
- };
- }
-
- public static SelectionCellCreator<Identifier> ofItemIdentifier() {
- return ofItemIdentifier(20, 146, 7);
- }
-
- public static SelectionCellCreator<Identifier> ofItemIdentifier(int maxItems) {
- return ofItemIdentifier(20, 146, maxItems);
- }
-
- public static SelectionCellCreator<Identifier> ofItemIdentifier(int cellHeight, int cellWidth, int maxItems) {
- return new DefaultSelectionCellCreator<Identifier>() {
- @Override
- public DropdownBoxEntry.SelectionCellElement<Identifier> create(Identifier selection) {
- ItemStack s = new ItemStack(Registry.ITEM.get(selection));
- return new DropdownBoxEntry.DefaultSelectionCellElement<Identifier>(selection, toStringFunction) {
- @Override
- public void render(int mouseX, int mouseY, int x, int y, int width, int height, float delta) {
- rendering = true;
- this.x = x;
- this.y = y;
- this.width = width;
- this.height = height;
- boolean b = mouseX >= x && mouseX <= x + width && mouseY >= y && mouseY <= y + height;
- if (b)
- fill(x + 1, y + 1, x + width - 1, y + height - 1, -15132391);
- MinecraftClient.getInstance().textRenderer.drawWithShadow(toStringFunction.apply(r), x + 6 + 18, y + 6, b ? 16777215 : 8947848);
- ItemRenderer itemRenderer = MinecraftClient.getInstance().getItemRenderer();
- itemRenderer.renderGuiItemIcon(s, x + 4, y + 2);
- }
- };
- }
-
- @Override
- public int getCellHeight() {
- return cellHeight;
- }
-
- @Override
- public int getCellWidth() {
- return cellWidth;
- }
-
- @Override
- public int getDropBoxMaxHeight() {
- return getCellHeight() * maxItems;
- }
- };
- }
-
-
- public static SelectionCellCreator<Identifier> ofBlockIdentifier() {
- return ofBlockIdentifier(20, 146, 7);
- }
-
- public static SelectionCellCreator<Identifier> ofBlockIdentifier(int maxItems) {
- return ofBlockIdentifier(20, 146, maxItems);
- }
-
- public static SelectionCellCreator<Identifier> ofBlockIdentifier(int cellHeight, int cellWidth, int maxItems) {
- return new DefaultSelectionCellCreator<Identifier>() {
- @Override
- public DropdownBoxEntry.SelectionCellElement<Identifier> create(Identifier selection) {
- ItemStack s = new ItemStack(Registry.BLOCK.get(selection));
- return new DropdownBoxEntry.DefaultSelectionCellElement<Identifier>(selection, toStringFunction) {
- @Override
- public void render(int mouseX, int mouseY, int x, int y, int width, int height, float delta) {
- rendering = true;
- this.x = x;
- this.y = y;
- this.width = width;
- this.height = height;
- boolean b = mouseX >= x && mouseX <= x + width && mouseY >= y && mouseY <= y + height;
- if (b)
- fill(x + 1, y + 1, x + width - 1, y + height - 1, -15132391);
- MinecraftClient.getInstance().textRenderer.drawWithShadow(toStringFunction.apply(r), x + 6 + 18, y + 6, b ? 16777215 : 8947848);
- ItemRenderer itemRenderer = MinecraftClient.getInstance().getItemRenderer();
- itemRenderer.renderGuiItemIcon(s, x + 4, y + 2);
- }
- };
- }
-
- @Override
- public int getCellHeight() {
- return cellHeight;
- }
-
- @Override
- public int getCellWidth() {
- return cellWidth;
- }
-
- @Override
- public int getDropBoxMaxHeight() {
- return getCellHeight() * maxItems;
- }
- };
- }
-
- public static SelectionCellCreator<Item> ofItemObject() {
- return ofItemObject(20, 146, 7);
- }
-
- public static SelectionCellCreator<Item> ofItemObject(int maxItems) {
- return ofItemObject(20, 146, maxItems);
- }
-
- public static SelectionCellCreator<Item> ofItemObject(int cellHeight, int cellWidth, int maxItems) {
- return new DefaultSelectionCellCreator<Item>(i -> Registry.ITEM.getId(i).toString()) {
- @Override
- public DropdownBoxEntry.SelectionCellElement<Item> create(Item selection) {
- ItemStack s = new ItemStack(selection);
- return new DropdownBoxEntry.DefaultSelectionCellElement<Item>(selection, toStringFunction) {
- @Override
- public void render(int mouseX, int mouseY, int x, int y, int width, int height, float delta) {
- rendering = true;
- this.x = x;
- this.y = y;
- this.width = width;
- this.height = height;
- boolean b = mouseX >= x && mouseX <= x + width && mouseY >= y && mouseY <= y + height;
- if (b)
- fill(x + 1, y + 1, x + width - 1, y + height - 1, -15132391);
- MinecraftClient.getInstance().textRenderer.drawWithShadow(toStringFunction.apply(r), x + 6 + 18, y + 6, b ? 16777215 : 8947848);
- ItemRenderer itemRenderer = MinecraftClient.getInstance().getItemRenderer();
- itemRenderer.renderGuiItemIcon(s, x + 4, y + 2);
- }
- };
- }
-
- @Override
- public int getCellHeight() {
- return cellHeight;
- }
-
- @Override
- public int getCellWidth() {
- return cellWidth;
- }
-
- @Override
- public int getDropBoxMaxHeight() {
- return getCellHeight() * maxItems;
- }
- };
- }
-
- public static SelectionCellCreator<Block> ofBlockObject() {
- return ofBlockObject(20, 146, 7);
- }
-
- public static SelectionCellCreator<Block> ofBlockObject(int maxItems) {
- return ofBlockObject(20, 146, maxItems);
- }
-
- public static SelectionCellCreator<Block> ofBlockObject(int cellHeight, int cellWidth, int maxItems) {
- return new DefaultSelectionCellCreator<Block>(i -> Registry.BLOCK.getId(i).toString()) {
- @Override
- public DropdownBoxEntry.SelectionCellElement<Block> create(Block selection) {
- ItemStack s = new ItemStack(selection);
- return new DropdownBoxEntry.DefaultSelectionCellElement<Block>(selection, toStringFunction) {
- @Override
- public void render(int mouseX, int mouseY, int x, int y, int width, int height, float delta) {
- rendering = true;
- this.x = x;
- this.y = y;
- this.width = width;
- this.height = height;
- boolean b = mouseX >= x && mouseX <= x + width && mouseY >= y && mouseY <= y + height;
- if (b)
- fill(x + 1, y + 1, x + width - 1, y + height - 1, -15132391);
- MinecraftClient.getInstance().textRenderer.drawWithShadow(toStringFunction.apply(r), x + 6 + 18, y + 6, b ? 16777215 : 8947848);
- ItemRenderer itemRenderer = MinecraftClient.getInstance().getItemRenderer();
- itemRenderer.renderGuiItemIcon(s, x + 4, y + 2);
- }
- };
- }
-
- @Override
- public int getCellHeight() {
- return cellHeight;
- }
-
- @Override
- public int getCellWidth() {
- return cellWidth;
- }
-
- @Override
- public int getDropBoxMaxHeight() {
- return getCellHeight() * maxItems;
- }
- };
- }
- }
- }
|