From c1d1255a91606b01c785d4824c6f83e885284505 Mon Sep 17 00:00:00 2001 From: NichtStudioCode <51272202+NichtStudioCode@users.noreply.github.com> Date: Wed, 27 Jan 2021 17:15:16 +0100 Subject: [PATCH] Nested GUIs and VirtualInventories --- .../java/de/studiocode/invgui/gui/GUI.java | 51 +++++++++++++++---- .../studiocode/invgui/gui/impl/BaseGUI.java | 32 ++++++++++++ .../invgui/gui/impl/IndexedGUI.java | 10 ++-- .../de/studiocode/invgui/util/SlotUtils.java | 4 +- 4 files changed, 79 insertions(+), 18 deletions(-) diff --git a/src/main/java/de/studiocode/invgui/gui/GUI.java b/src/main/java/de/studiocode/invgui/gui/GUI.java index 4ee6332..07b66da 100644 --- a/src/main/java/de/studiocode/invgui/gui/GUI.java +++ b/src/main/java/de/studiocode/invgui/gui/GUI.java @@ -71,7 +71,7 @@ public interface GUI { void setSlotElement(int index, @NotNull SlotElement slotElement); /** - * Gets the {@link SlotElement} on these coordinates + * Gets the {@link SlotElement} on these coordinates. * * @param x The x coordinate * @param y The y coordinate @@ -80,13 +80,30 @@ public interface GUI { SlotElement getSlotElement(int x, int y); /** - * Gets the {@link SlotElement} placed on that slot + * Gets the {@link SlotElement} placed on that slot. * * @param index The slot index * @return The {@link SlotElement} placed on that slot */ SlotElement getSlotElement(int index); + /** + * Gets if there is a {@link SlotElement} on these coordinates. + * + * @param x The x coordinate + * @param y The y coordinate + * @return If there is a {@link SlotElement} placed there + */ + boolean hasSlotElement(int x, int y); + + /** + * Gets if there is a {@link SlotElement} placed on that slot. + * + * @param index The slot index + * @return If there is a {@link SlotElement} placed there + */ + boolean hasSlotElement(int index); + /** * Gets a all {@link SlotElement}s of this {@link GUI} in an Array. * @@ -169,15 +186,6 @@ public interface GUI { */ void remove(int index); - /** - * Fills the slots of this {@link GUI} with the content of another one, - * allowing for nested {@link GUI}s - * - * @param offset Defines the index where the nested {@link GUI} should start (inclusive) - * @param gui The {@link GUI} which should be put inside this {@link GUI} - */ - void nest(int offset, @NotNull GUI gui); - /** * A method called if a slot in the {@link Inventory} has been clicked. * @@ -254,4 +262,25 @@ public interface GUI { */ void fillRectangle(int x, int y, int width, int height, Item item, boolean replaceExisting); + /** + * Fills a rectangle of another {@link GUI} in this {@link GUI}. + * + * @param x The x coordinate where the rectangle should start + * @param y The y coordinate where the rectangle should start + * @param gui The {@link GUI} to be put into this {@link GUI} + * @param replaceExisting If existing {@link SlotElement}s should be replaced. + */ + void fillRectangle(int x, int y, GUI gui, boolean replaceExisting); + + /** + * Fills a rectangle of a {@link VirtualInventory} in this {@link GUI}. + * + * @param x The x coordinate where the rectangle should start + * @param y The y coordinate where the rectangle should start + * @param width The line length of the rectangle. (VirtualInventory does not define a width) + * @param virtualInventory The {@link VirtualInventory} to be put into this {@link GUI}. + * @param replaceExisting If existing {@link SlotElement}s should be replaced. + */ + void fillRectangle(int x, int y, int width, VirtualInventory virtualInventory, boolean replaceExisting); + } diff --git a/src/main/java/de/studiocode/invgui/gui/impl/BaseGUI.java b/src/main/java/de/studiocode/invgui/gui/impl/BaseGUI.java index 43a31fa..9404004 100644 --- a/src/main/java/de/studiocode/invgui/gui/impl/BaseGUI.java +++ b/src/main/java/de/studiocode/invgui/gui/impl/BaseGUI.java @@ -1,9 +1,13 @@ package de.studiocode.invgui.gui.impl; +import de.studiocode.invgui.gui.GUI; import de.studiocode.invgui.gui.SlotElement; import de.studiocode.invgui.gui.SlotElement.ItemStackHolder; +import de.studiocode.invgui.gui.SlotElement.LinkedSlotElement; +import de.studiocode.invgui.gui.SlotElement.VISlotElement; import de.studiocode.invgui.item.Item; import de.studiocode.invgui.util.SlotUtils; +import de.studiocode.invgui.virtualinventory.VirtualInventory; import org.jetbrains.annotations.NotNull; import java.util.SortedSet; @@ -33,6 +37,11 @@ public abstract class BaseGUI extends IndexedGUI { return getSlotElement(convToIndex(x, y)); } + @Override + public boolean hasSlotElement(int x, int y) { + return hasSlotElement(convToIndex(x, y)); + } + @Override public ItemStackHolder getItemStackHolder(int x, int y) { return getItemStackHolder(convToIndex(x, y)); @@ -112,4 +121,27 @@ public abstract class BaseGUI extends IndexedGUI { fill(SlotUtils.getSlotsRect(x, y, width, height, this.width), item, replaceExisting); } + @Override + public void fillRectangle(int x, int y, GUI gui, boolean replaceExisting) { + int slotIndex = 0; + for (int slot : SlotUtils.getSlotsRect(x, y, gui.getWidth(), gui.getHeight(), this.width)) { + if (hasSlotElement(slot) && !replaceExisting) continue; + setSlotElement(slot, new LinkedSlotElement(gui, slotIndex)); + slotIndex++; + } + } + + @Override + public void fillRectangle(int x, int y, int width, VirtualInventory virtualInventory, boolean replaceExisting) { + int height = (int) Math.ceil((double) virtualInventory.getSize() / (double) width); + + int slotIndex = 0; + for (int slot : SlotUtils.getSlotsRect(x, y, width, height, this.width)) { + if (slotIndex >= virtualInventory.getSize()) return; + if (hasSlotElement(slot) && !replaceExisting) continue; + setSlotElement(slot, new VISlotElement(virtualInventory, slotIndex)); + slotIndex++; + } + } + } diff --git a/src/main/java/de/studiocode/invgui/gui/impl/IndexedGUI.java b/src/main/java/de/studiocode/invgui/gui/impl/IndexedGUI.java index b7b1f08..ca0a5f1 100644 --- a/src/main/java/de/studiocode/invgui/gui/impl/IndexedGUI.java +++ b/src/main/java/de/studiocode/invgui/gui/impl/IndexedGUI.java @@ -166,6 +166,11 @@ abstract class IndexedGUI implements GUI { return slotElements[index]; } + @Override + public boolean hasSlotElement(int index) { + return slotElements[index] != null; + } + @Override public SlotElement[] getSlotElements() { return slotElements.clone(); @@ -209,11 +214,6 @@ abstract class IndexedGUI implements GUI { slotElements[index] = null; } - @Override - public void nest(int offset, @NotNull GUI gui) { - for (int i = 0; i < gui.getSize(); i++) slotElements[i + offset] = new LinkedSlotElement(gui, i); - } - @Override public int getSize() { return size; diff --git a/src/main/java/de/studiocode/invgui/util/SlotUtils.java b/src/main/java/de/studiocode/invgui/util/SlotUtils.java index ee5d077..0b229b0 100644 --- a/src/main/java/de/studiocode/invgui/util/SlotUtils.java +++ b/src/main/java/de/studiocode/invgui/util/SlotUtils.java @@ -28,8 +28,8 @@ public class SlotUtils { public static SortedSet getSlotsRect(int x, int y, int width, int height, int frameWidth) { SortedSet slots = new TreeSet<>(); - for (int y1 = y; y1 <= height; y1++) { - for (int x1 = x; x1 <= width; x1++) { + for (int y1 = y; y1 < height + y; y1++) { + for (int x1 = x; x1 < width + x; x1++) { slots.add(convertToIndex(x1, y1, frameWidth)); } }