Structure#addIngredient(inventory, background)

This commit is contained in:
NichtStudioCode 2022-01-07 15:32:18 +01:00
parent 98b6f661af
commit 0a823c33c7
3 changed files with 25 additions and 10 deletions

@ -10,6 +10,7 @@ import de.studiocode.invui.virtualinventory.VirtualInventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.ShapedRecipe;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.function.Supplier;
@ -60,6 +61,11 @@ public class GUIBuilder<G extends GUI> {
return this;
}
public GUIBuilder<G> addIngredient(char key, @NotNull VirtualInventory inventory, @Nullable ItemProvider background) {
context.getStructure().addIngredient(key, inventory, background);
return this;
}
public GUIBuilder<G> addIngredient(char key, @NotNull SlotElement element) {
context.getStructure().addIngredient(key, element);
return this;

@ -11,6 +11,7 @@ import de.studiocode.invui.virtualinventory.VirtualInventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.ShapedRecipe;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.HashMap;
import java.util.function.Supplier;
@ -63,45 +64,50 @@ public class Structure implements Cloneable {
}
public Structure addIngredient(char key, @NotNull ItemStack itemStack) {
if (ingredientList != null) throw new UnsupportedOperationException("Structure is locked");
if (ingredientList != null) throw new IllegalStateException("Structure is locked");
return addIngredient(key, new ItemWrapper(itemStack));
}
public Structure addIngredient(char key, @NotNull ItemProvider itemProvider) {
if (ingredientList != null) throw new UnsupportedOperationException("Structure is locked");
if (ingredientList != null) throw new IllegalStateException("Structure is locked");
return addIngredient(key, new SimpleItem(itemProvider));
}
public Structure addIngredient(char key, @NotNull Item item) {
if (ingredientList != null) throw new UnsupportedOperationException("Structure is locked");
if (ingredientList != null) throw new IllegalStateException("Structure is locked");
return addIngredient(key, new ItemSlotElement(item));
}
public Structure addIngredient(char key, @NotNull VirtualInventory inventory) {
if (ingredientList != null) throw new UnsupportedOperationException("Structure is locked");
if (ingredientList != null) throw new IllegalStateException("Structure is locked");
return addIngredientElementSupplier(key, new VISlotElementSupplier(inventory));
}
public Structure addIngredient(char key, @NotNull VirtualInventory inventory, @Nullable ItemProvider background) {
if (ingredientList != null) throw new IllegalStateException("Structure is locked");
return addIngredientElementSupplier(key, new VISlotElementSupplier(inventory, background));
}
public Structure addIngredient(char key, @NotNull SlotElement element) {
if (ingredientList != null) throw new UnsupportedOperationException("Structure is locked");
if (ingredientList != null) throw new IllegalStateException("Structure is locked");
ingredientMap.put(key, new Ingredient(element));
return this;
}
public Structure addIngredient(char key, @NotNull String marker) {
if (ingredientList != null) throw new UnsupportedOperationException("Structure is locked");
if (ingredientList != null) throw new IllegalStateException("Structure is locked");
ingredientMap.put(key, new Ingredient(marker));
return this;
}
public Structure addIngredient(char key, @NotNull Supplier<? extends Item> itemSupplier) {
if (ingredientList != null) throw new UnsupportedOperationException("Structure is locked");
if (ingredientList != null) throw new IllegalStateException("Structure is locked");
ingredientMap.put(key, new Ingredient(() -> new ItemSlotElement(itemSupplier.get())));
return this;
}
public Structure addIngredientElementSupplier(char key, @NotNull Supplier<? extends SlotElement> elementSupplier) {
if (ingredientList != null) throw new UnsupportedOperationException("Structure is locked");
if (ingredientList != null) throw new IllegalStateException("Structure is locked");
ingredientMap.put(key, new Ingredient(elementSupplier));
return this;
}

@ -3,6 +3,8 @@ package de.studiocode.invui.gui.structure;
import de.studiocode.invui.gui.SlotElement.VISlotElement;
import de.studiocode.invui.item.ItemProvider;
import de.studiocode.invui.virtualinventory.VirtualInventory;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.function.Supplier;
@ -12,16 +14,17 @@ public class VISlotElementSupplier implements Supplier<VISlotElement> {
private final ItemProvider background;
private int slot = -1;
public VISlotElementSupplier(VirtualInventory inventory) {
public VISlotElementSupplier(@NotNull VirtualInventory inventory) {
this.inventory = inventory;
this.background = null;
}
public VISlotElementSupplier(VirtualInventory inventory, ItemProvider background) {
public VISlotElementSupplier(@NotNull VirtualInventory inventory, @Nullable ItemProvider background) {
this.inventory = inventory;
this.background = background;
}
@NotNull
@Override
public VISlotElement get() {
if (++slot == inventory.getSize()) slot = 0;