diff --git a/src/main/java/de/hysky/skyblocker/skyblock/itemlist/ItemRepository.java b/src/main/java/de/hysky/skyblocker/skyblock/itemlist/ItemRepository.java index 8c071f5979..b5b0cbc476 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/itemlist/ItemRepository.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/itemlist/ItemRepository.java @@ -117,11 +117,12 @@ public static List getItems() { } public static Stream getItemsStream() { + if (!filesImported) return Stream.empty(); return items.stream(); } /** - * @param neuId the NEU item id gotten through {@link NEUItem#getSkyblockItemId()}, {@link ItemStack#getNeuName()}, or {@link ItemUtils#getNeuId(String, String) ItemTooltip#getNeuName(String, String)} + * @param neuId the NEU item id gotten through {@link NEUItem#getSkyblockItemId()}, {@link ItemStack#getNeuName()}, or {@link ItemUtils#getNeuId(ItemStack) ItemTooltip#getNeuName(String, String)} */ @Nullable public static ItemStack getItemStack(String neuId) { diff --git a/src/main/java/de/hysky/skyblocker/utils/render/gui/DropdownWidget.java b/src/main/java/de/hysky/skyblocker/utils/render/gui/DropdownWidget.java index a4bfb7c353..f92ec0a5e9 100644 --- a/src/main/java/de/hysky/skyblocker/utils/render/gui/DropdownWidget.java +++ b/src/main/java/de/hysky/skyblocker/utils/render/gui/DropdownWidget.java @@ -15,29 +15,32 @@ import java.util.function.Consumer; public class DropdownWidget extends ContainerWidget { - private static final MinecraftClient client = MinecraftClient.getInstance(); - public static final int ENTRY_HEIGHT = 15; - protected final List entries; - protected final Consumer selectCallback; + private static final MinecraftClient client = MinecraftClient.getInstance(); + public static final int ENTRY_HEIGHT = 15; + private static final int HEADER_HEIGHT = ENTRY_HEIGHT + 4; + protected final List entries; + protected final Consumer selectCallback; private final DropdownList dropdownList; protected T prevSelected; - protected T selected; - protected boolean open; - - public DropdownWidget(MinecraftClient minecraftClient, int x, int y, int width, int maxHeight, List entries, Consumer selectCallback, T selected) { - super(x, y, width, Math.min((entries.size() + 1) * ENTRY_HEIGHT + 8, maxHeight), Text.empty()); - this.entries = entries; - this.selectCallback = selectCallback; - this.selected = selected; - dropdownList = new DropdownList(minecraftClient, x + 1, y + ENTRY_HEIGHT + 4, width - 2, maxHeight - ENTRY_HEIGHT - 4); + protected T selected; + protected boolean open; + private int maxHeight; + + public DropdownWidget(MinecraftClient minecraftClient, int x, int y, int width, int maxHeight, List entries, Consumer selectCallback, T selected) { + super(x, y, width, HEADER_HEIGHT, Text.empty()); + this.maxHeight = maxHeight; + this.entries = entries; + this.selectCallback = selectCallback; + this.selected = selected; + dropdownList = new DropdownList(minecraftClient, x + 1, y + HEADER_HEIGHT, width - 2, maxHeight - HEADER_HEIGHT); for (T element : entries) { dropdownList.addEntry(new Entry(element)); } } public void setMaxHeight(int maxHeight) { - setHeight(maxHeight); - dropdownList.setHeight(maxHeight - ENTRY_HEIGHT - 4); + this.maxHeight = maxHeight; + setOpen(open); } @Override @@ -52,14 +55,14 @@ protected void renderWidget(DrawContext context, int mouseX, int mouseY, float d matrices.translate(0, 0, 100); dropdownList.visible = open; dropdownList.render(context, mouseX, mouseY, delta); - context.fill(getX(), getY(), getRight(), getY() + ENTRY_HEIGHT + 5, 0xFF << 24); - context.drawBorder(getX(), getY(), getWidth(), ENTRY_HEIGHT + 5, -1); + context.fill(getX(), getY(), getRight(), getY() + HEADER_HEIGHT + 1, 0xFF << 24); + context.drawBorder(getX(), getY(), getWidth(), HEADER_HEIGHT + 1, -1); drawScrollableText(context, client.textRenderer, Text.literal( - selected.toString()), + selected.toString()), getX() + 2, getY() + 2, getRight() - 2, - getY() + ENTRY_HEIGHT + 2, + getY() + HEADER_HEIGHT - 2, -1); matrices.pop(); } @@ -67,9 +70,19 @@ protected void renderWidget(DrawContext context, int mouseX, int mouseY, float d @Override protected void appendClickableNarrations(NarrationMessageBuilder builder) {} + private void setOpen(boolean open) { + this.open = open; + if (this.open) { + setHeight(maxHeight); + dropdownList.setHeight(Math.min(entries.size() * ENTRY_HEIGHT + 4, maxHeight - HEADER_HEIGHT)); + } else { + setHeight(HEADER_HEIGHT); + } + } + protected void select(T entry) { selected = entry; - open = false; + setOpen(false); if (selected != prevSelected) { selectCallback.accept(entry); prevSelected = selected; @@ -85,7 +98,7 @@ public void setX(int x) { @Override public void setY(int y) { super.setY(y); - dropdownList.setY(getY() + ENTRY_HEIGHT + 4); + dropdownList.setY(getY() + HEADER_HEIGHT); } @Override @@ -97,18 +110,18 @@ public void setWidth(int width) { @Override public void setHeight(int height) { super.setHeight(height); - dropdownList.setHeight(height - ENTRY_HEIGHT - 4); + dropdownList.setHeight(height - HEADER_HEIGHT); } @Override public boolean mouseClicked(double mouseX, double mouseY, int button) { - if (!visible) return false; - if (getX() <= mouseX && mouseX < getX() + getWidth() && getY() <= mouseY && mouseY < getY() + ENTRY_HEIGHT + 4) { - open = !open; - playDownSound(client.getSoundManager()); - return true; - } - return super.mouseClicked(mouseX, mouseY, button); + if (!visible) return false; + if (getX() <= mouseX && mouseX < getX() + getWidth() && getY() <= mouseY && mouseY < getY() + HEADER_HEIGHT) { + setOpen(!open); + playDownSound(client.getSoundManager()); + return true; + } + return super.mouseClicked(mouseX, mouseY, button); } @Override @@ -213,7 +226,7 @@ protected void enableScissor(DrawContext context) { context.enableScissor(this.getX(), this.getY() + 1, this.getRight(), this.getBottom() - 1); } } - + private class Entry extends ElementListWidget.Entry { private final T entry;