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 extends FieldBuilder> { protected SelectionTopCellElement topCellElement; protected SelectionCellCreator cellCreator; protected Function> tooltipSupplier = str -> Optional.empty(); protected Consumer saveConsumer = null; protected Iterable selections = Collections.emptyList(); public DropdownMenuBuilder(String resetButtonKey, String fieldNameKey, SelectionTopCellElement topCellElement, SelectionCellCreator cellCreator) { super(resetButtonKey, fieldNameKey); this.topCellElement = Objects.requireNonNull(topCellElement); this.cellCreator = Objects.requireNonNull(cellCreator); } public DropdownMenuBuilder setSelections(Iterable selections) { this.selections = selections; return this; } public DropdownMenuBuilder setDefaultValue(Supplier defaultValue) { this.defaultValue = defaultValue; return this; } public DropdownMenuBuilder setDefaultValue(T defaultValue) { this.defaultValue = () -> Objects.requireNonNull(defaultValue); return this; } public DropdownMenuBuilder setSaveConsumer(Consumer saveConsumer) { this.saveConsumer = saveConsumer; return this; } public DropdownMenuBuilder setTooltipSupplier(Supplier> tooltipSupplier) { this.tooltipSupplier = str -> tooltipSupplier.get(); return this; } public DropdownMenuBuilder setTooltipSupplier(Function> tooltipSupplier) { this.tooltipSupplier = tooltipSupplier; return this; } public DropdownMenuBuilder setTooltip(Optional 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> errorSupplier) { this.errorSupplier = errorSupplier; return this; } @Nonnull @Override public DropdownBoxEntry build() { DropdownBoxEntry entry = new DropdownBoxEntry(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 IDENTIFIER_FUNCTION = str -> { try { return new Identifier(str); } catch (NumberFormatException e) { return null; } }; public static final Function 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 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 ITEM_FUNCTION = str -> { try { return Registry.ITEM.getOrEmpty(new Identifier(str)).get(); } catch (Exception ignored) { } return null; }; public static final Function 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 SelectionTopCellElement of(T value, Function toObjectFunction) { return of(value, toObjectFunction, Object::toString); } public static SelectionTopCellElement of(T value, Function toObjectFunction, Function toStringFunction) { return new DefaultSelectionTopCellElement<>(value, toObjectFunction, toStringFunction); } public static SelectionTopCellElement ofItemIdentifier(Item item) { return new DefaultSelectionTopCellElement(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 ofBlockIdentifier(Block block) { return new DefaultSelectionTopCellElement(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 ofItemObject(Item item) { return new DefaultSelectionTopCellElement(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 ofBlockObject(Block block) { return new DefaultSelectionTopCellElement(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 SelectionCellCreator of() { return new DefaultSelectionCellCreator<>(); } public static SelectionCellCreator of(Function toStringFunction) { return new DefaultSelectionCellCreator<>(toStringFunction); } public static SelectionCellCreator ofWidth(int cellWidth) { return new DefaultSelectionCellCreator() { @Override public int getCellWidth() { return cellWidth; } }; } public static SelectionCellCreator ofWidth(int cellWidth, Function toStringFunction) { return new DefaultSelectionCellCreator(toStringFunction) { @Override public int getCellWidth() { return cellWidth; } }; } public static SelectionCellCreator ofCellCount(int maxItems) { return new DefaultSelectionCellCreator() { @Override public int getDropBoxMaxHeight() { return getCellHeight() * maxItems; } }; } public static SelectionCellCreator ofCellCount(int maxItems, Function toStringFunction) { return new DefaultSelectionCellCreator(toStringFunction) { @Override public int getDropBoxMaxHeight() { return getCellHeight() * maxItems; } }; } public static SelectionCellCreator of(int cellWidth, int maxItems) { return new DefaultSelectionCellCreator() { @Override public int getCellWidth() { return cellWidth; } @Override public int getDropBoxMaxHeight() { return getCellHeight() * maxItems; } }; } public static SelectionCellCreator of(int cellWidth, int maxItems, Function toStringFunction) { return new DefaultSelectionCellCreator(toStringFunction) { @Override public int getCellWidth() { return cellWidth; } @Override public int getDropBoxMaxHeight() { return getCellHeight() * maxItems; } }; } public static SelectionCellCreator of(int cellHeight, int cellWidth, int maxItems) { return new DefaultSelectionCellCreator() { @Override public int getCellHeight() { return cellHeight; } @Override public int getCellWidth() { return cellWidth; } @Override public int getDropBoxMaxHeight() { return getCellHeight() * maxItems; } }; } public static SelectionCellCreator of(int cellHeight, int cellWidth, int maxItems, Function toStringFunction) { return new DefaultSelectionCellCreator(toStringFunction) { @Override public int getCellHeight() { return cellHeight; } @Override public int getCellWidth() { return cellWidth; } @Override public int getDropBoxMaxHeight() { return getCellHeight() * maxItems; } }; } public static SelectionCellCreator ofItemIdentifier() { return ofItemIdentifier(20, 146, 7); } public static SelectionCellCreator ofItemIdentifier(int maxItems) { return ofItemIdentifier(20, 146, maxItems); } public static SelectionCellCreator ofItemIdentifier(int cellHeight, int cellWidth, int maxItems) { return new DefaultSelectionCellCreator() { @Override public DropdownBoxEntry.SelectionCellElement create(Identifier selection) { ItemStack s = new ItemStack(Registry.ITEM.get(selection)); return new DropdownBoxEntry.DefaultSelectionCellElement(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 ofBlockIdentifier() { return ofBlockIdentifier(20, 146, 7); } public static SelectionCellCreator ofBlockIdentifier(int maxItems) { return ofBlockIdentifier(20, 146, maxItems); } public static SelectionCellCreator ofBlockIdentifier(int cellHeight, int cellWidth, int maxItems) { return new DefaultSelectionCellCreator() { @Override public DropdownBoxEntry.SelectionCellElement create(Identifier selection) { ItemStack s = new ItemStack(Registry.BLOCK.get(selection)); return new DropdownBoxEntry.DefaultSelectionCellElement(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 ofItemObject() { return ofItemObject(20, 146, 7); } public static SelectionCellCreator ofItemObject(int maxItems) { return ofItemObject(20, 146, maxItems); } public static SelectionCellCreator ofItemObject(int cellHeight, int cellWidth, int maxItems) { return new DefaultSelectionCellCreator(i -> Registry.ITEM.getId(i).toString()) { @Override public DropdownBoxEntry.SelectionCellElement create(Item selection) { ItemStack s = new ItemStack(selection); return new DropdownBoxEntry.DefaultSelectionCellElement(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 ofBlockObject() { return ofBlockObject(20, 146, 7); } public static SelectionCellCreator ofBlockObject(int maxItems) { return ofBlockObject(20, 146, maxItems); } public static SelectionCellCreator ofBlockObject(int cellHeight, int cellWidth, int maxItems) { return new DefaultSelectionCellCreator(i -> Registry.BLOCK.getId(i).toString()) { @Override public DropdownBoxEntry.SelectionCellElement create(Block selection) { ItemStack s = new ItemStack(selection); return new DropdownBoxEntry.DefaultSelectionCellElement(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; } }; } } }