SlotElement Supplier as Ingredient

This commit is contained in:
NichtStudioCode 2021-08-08 17:21:40 +02:00
parent 9a485d1921
commit 6cb3e3ed8a
3 changed files with 26 additions and 14 deletions

@ -68,11 +68,16 @@ public class GUIBuilder {
return this;
}
public GUIBuilder addIngredient(char key, @NotNull Supplier<Item> itemSupplier) {
public GUIBuilder addIngredient(char key, @NotNull Supplier<? extends Item> itemSupplier) {
structure.addIngredient(key, itemSupplier);
return this;
}
public GUIBuilder addIngredientElementSupplier(char key, @NotNull Supplier<? extends SlotElement> elementSupplier) {
structure.addIngredientElementSupplier(key, elementSupplier);
return this;
}
public GUIBuilder setItems(@NotNull List<Item> items) {
if (guiType != PAGED_ITEMS && guiType != SCROLL)
throw new UnsupportedOperationException("Items cannot be set in this gui type.");

@ -1,8 +1,6 @@
package de.studiocode.invui.gui.structure;
import de.studiocode.invui.gui.SlotElement;
import de.studiocode.invui.gui.SlotElement.ItemSlotElement;
import de.studiocode.invui.item.Item;
import java.util.function.Supplier;
@ -10,16 +8,16 @@ class Ingredient {
private final SlotElement slotElement;
private final Marker marker;
private final Supplier<Item> itemSupplier;
private final Supplier<? extends SlotElement> elementSupplier;
public Ingredient(SlotElement slotElement) {
this.slotElement = slotElement;
this.itemSupplier = null;
this.elementSupplier = null;
this.marker = null;
}
public Ingredient(Supplier<Item> itemSupplier) {
this.itemSupplier = itemSupplier;
public Ingredient(Supplier<? extends SlotElement> elementSupplier) {
this.elementSupplier = elementSupplier;
this.slotElement = null;
this.marker = null;
}
@ -27,11 +25,11 @@ class Ingredient {
public Ingredient(Marker marker) {
this.marker = marker;
this.slotElement = null;
this.itemSupplier = null;
this.elementSupplier = null;
}
public SlotElement getSlotElement() {
return slotElement == null ? new ItemSlotElement(itemSupplier.get()) : slotElement;
return slotElement == null ? elementSupplier.get() : slotElement;
}
public Marker getMarker() {
@ -39,7 +37,7 @@ class Ingredient {
}
public boolean isSlotElement() {
return slotElement != null || itemSupplier != null;
return slotElement != null || elementSupplier != null;
}
public boolean isMarker() {

@ -38,6 +38,10 @@ public class Structure {
addGlobalIngredient(key, new ItemSlotElement(item));
}
public static void addGlobalIngredient(char key, @NotNull Supplier<? extends Item> itemSupplier) {
addGlobalIngredientElementSupplier(key, () -> new ItemSlotElement(itemSupplier.get()));
}
public static void addGlobalIngredient(char key, @NotNull SlotElement element) {
globalIngredientMap.put(key, new Ingredient(element));
}
@ -46,8 +50,8 @@ public class Structure {
globalIngredientMap.put(key, new Ingredient(marker));
}
public static void addGlobalIngredient(char key, @NotNull Supplier<Item> itemSupplier) {
globalIngredientMap.put(key, new Ingredient(itemSupplier));
public static void addGlobalIngredientElementSupplier(char key, @NotNull Supplier<? extends SlotElement> elementSupplier) {
globalIngredientMap.put(key, new Ingredient(elementSupplier));
}
public Structure addIngredient(char key, @NotNull ItemProvider itemBuilder) {
@ -68,8 +72,13 @@ public class Structure {
return this;
}
public Structure addIngredient(char key, @NotNull Supplier<Item> itemSupplier) {
ingredientMap.put(key, new Ingredient(itemSupplier));
public Structure addIngredient(char key, @NotNull Supplier<? extends Item> itemSupplier) {
ingredientMap.put(key, new Ingredient(() -> new ItemSlotElement(itemSupplier.get())));
return this;
}
public Structure addIngredientElementSupplier(char key, @NotNull Supplier<? extends SlotElement> elementSupplier) {
ingredientMap.put(key, new Ingredient(elementSupplier));
return this;
}