Nested GUIs and VirtualInventories

This commit is contained in:
NichtStudioCode 2021-01-27 17:15:16 +01:00
parent 8e160a5e87
commit c1d1255a91
4 changed files with 79 additions and 18 deletions

@ -71,7 +71,7 @@ public interface GUI {
void setSlotElement(int index, @NotNull SlotElement slotElement); 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 x The x coordinate
* @param y The y coordinate * @param y The y coordinate
@ -80,13 +80,30 @@ public interface GUI {
SlotElement getSlotElement(int x, int y); 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 * @param index The slot index
* @return The {@link SlotElement} placed on that slot * @return The {@link SlotElement} placed on that slot
*/ */
SlotElement getSlotElement(int index); 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. * Gets a all {@link SlotElement}s of this {@link GUI} in an Array.
* *
@ -169,15 +186,6 @@ public interface GUI {
*/ */
void remove(int index); 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. * 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); 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);
} }

@ -1,9 +1,13 @@
package de.studiocode.invgui.gui.impl; package de.studiocode.invgui.gui.impl;
import de.studiocode.invgui.gui.GUI;
import de.studiocode.invgui.gui.SlotElement; import de.studiocode.invgui.gui.SlotElement;
import de.studiocode.invgui.gui.SlotElement.ItemStackHolder; 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.item.Item;
import de.studiocode.invgui.util.SlotUtils; import de.studiocode.invgui.util.SlotUtils;
import de.studiocode.invgui.virtualinventory.VirtualInventory;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.SortedSet; import java.util.SortedSet;
@ -33,6 +37,11 @@ public abstract class BaseGUI extends IndexedGUI {
return getSlotElement(convToIndex(x, y)); return getSlotElement(convToIndex(x, y));
} }
@Override
public boolean hasSlotElement(int x, int y) {
return hasSlotElement(convToIndex(x, y));
}
@Override @Override
public ItemStackHolder getItemStackHolder(int x, int y) { public ItemStackHolder getItemStackHolder(int x, int y) {
return getItemStackHolder(convToIndex(x, 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); 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++;
}
}
} }

@ -166,6 +166,11 @@ abstract class IndexedGUI implements GUI {
return slotElements[index]; return slotElements[index];
} }
@Override
public boolean hasSlotElement(int index) {
return slotElements[index] != null;
}
@Override @Override
public SlotElement[] getSlotElements() { public SlotElement[] getSlotElements() {
return slotElements.clone(); return slotElements.clone();
@ -209,11 +214,6 @@ abstract class IndexedGUI implements GUI {
slotElements[index] = null; 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 @Override
public int getSize() { public int getSize() {
return size; return size;

@ -28,8 +28,8 @@ public class SlotUtils {
public static SortedSet<Integer> getSlotsRect(int x, int y, int width, int height, int frameWidth) { public static SortedSet<Integer> getSlotsRect(int x, int y, int width, int height, int frameWidth) {
SortedSet<Integer> slots = new TreeSet<>(); SortedSet<Integer> slots = new TreeSet<>();
for (int y1 = y; y1 <= height; y1++) { for (int y1 = y; y1 < height + y; y1++) {
for (int x1 = x; x1 <= width; x1++) { for (int x1 = x; x1 < width + x; x1++) {
slots.add(convertToIndex(x1, y1, frameWidth)); slots.add(convertToIndex(x1, y1, frameWidth));
} }
} }