diff --git a/InvUI/src/main/java/de/studiocode/invui/gui/builder/GUIBuilder.java b/InvUI/src/main/java/de/studiocode/invui/gui/builder/GUIBuilder.java index 0a503f2..d6d217f 100644 --- a/InvUI/src/main/java/de/studiocode/invui/gui/builder/GUIBuilder.java +++ b/InvUI/src/main/java/de/studiocode/invui/gui/builder/GUIBuilder.java @@ -3,10 +3,10 @@ package de.studiocode.invui.gui.builder; import de.studiocode.invui.gui.GUI; import de.studiocode.invui.gui.SlotElement; import de.studiocode.invui.gui.builder.guitype.GUIType; -import de.studiocode.invui.gui.structure.Marker; import de.studiocode.invui.gui.structure.Structure; import de.studiocode.invui.item.Item; import de.studiocode.invui.item.ItemProvider; +import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ShapedRecipe; import org.jetbrains.annotations.NotNull; @@ -39,6 +39,11 @@ public class GUIBuilder { return this; } + public GUIBuilder addIngredient(char key, @NotNull ItemStack itemStack) { + context.getStructure().addIngredient(key, itemStack); + return this; + } + public GUIBuilder addIngredient(char key, @NotNull ItemProvider itemProvider) { context.getStructure().addIngredient(key, itemProvider); return this; @@ -54,7 +59,7 @@ public class GUIBuilder { return this; } - public GUIBuilder addIngredient(char key, @NotNull Marker marker) { + public GUIBuilder addIngredient(char key, @NotNull String marker) { context.getStructure().addIngredient(key, marker); return this; } diff --git a/InvUI/src/main/java/de/studiocode/invui/gui/impl/BaseGUI.java b/InvUI/src/main/java/de/studiocode/invui/gui/impl/BaseGUI.java index 4c02697..931c092 100644 --- a/InvUI/src/main/java/de/studiocode/invui/gui/impl/BaseGUI.java +++ b/InvUI/src/main/java/de/studiocode/invui/gui/impl/BaseGUI.java @@ -563,7 +563,7 @@ public abstract class BaseGUI implements GUI, Controllable { @Override public void applyStructure(Structure structure) { - structure.createIngredientList().insertIntoGUI(this); + structure.getIngredientList().insertIntoGUI(this); } @Override diff --git a/InvUI/src/main/java/de/studiocode/invui/gui/impl/PagedGUI.java b/InvUI/src/main/java/de/studiocode/invui/gui/impl/PagedGUI.java index 5a04400..10d4147 100644 --- a/InvUI/src/main/java/de/studiocode/invui/gui/impl/PagedGUI.java +++ b/InvUI/src/main/java/de/studiocode/invui/gui/impl/PagedGUI.java @@ -3,7 +3,7 @@ package de.studiocode.invui.gui.impl; import de.studiocode.invui.gui.GUI; import de.studiocode.invui.gui.SlotElement; import de.studiocode.invui.gui.builder.GUIBuilder; -import de.studiocode.invui.gui.structure.Marker; +import de.studiocode.invui.gui.structure.Markers; import de.studiocode.invui.gui.structure.Structure; import java.util.List; @@ -28,7 +28,7 @@ public abstract class PagedGUI extends BaseGUI { } public PagedGUI(int width, int height, boolean infinitePages, Structure structure) { - this(width, height, infinitePages, structure.createIngredientList().findIndicesOfMarker(Marker.ITEM_LIST_SLOT)); + this(width, height, infinitePages, structure.getIngredientList().findIndicesOfMarker(Markers.ITEM_LIST_SLOT)); applyStructure(structure); } diff --git a/InvUI/src/main/java/de/studiocode/invui/gui/impl/ScrollGUI.java b/InvUI/src/main/java/de/studiocode/invui/gui/impl/ScrollGUI.java index 05b90d3..e53952b 100644 --- a/InvUI/src/main/java/de/studiocode/invui/gui/impl/ScrollGUI.java +++ b/InvUI/src/main/java/de/studiocode/invui/gui/impl/ScrollGUI.java @@ -2,7 +2,7 @@ package de.studiocode.invui.gui.impl; import de.studiocode.invui.gui.GUI; import de.studiocode.invui.gui.SlotElement; -import de.studiocode.invui.gui.structure.Marker; +import de.studiocode.invui.gui.structure.Markers; import de.studiocode.invui.gui.structure.Structure; import de.studiocode.invui.util.SlotUtils; @@ -36,7 +36,7 @@ public abstract class ScrollGUI extends BaseGUI { } public ScrollGUI(int width, int height, boolean infiniteLines, Structure structure) { - this(width, height, infiniteLines, structure.createIngredientList().findIndicesOfMarker(Marker.ITEM_LIST_SLOT)); + this(width, height, infiniteLines, structure.getIngredientList().findIndicesOfMarker(Markers.ITEM_LIST_SLOT)); applyStructure(structure); } diff --git a/InvUI/src/main/java/de/studiocode/invui/gui/impl/TabGUI.java b/InvUI/src/main/java/de/studiocode/invui/gui/impl/TabGUI.java index b39c76e..b6668c9 100644 --- a/InvUI/src/main/java/de/studiocode/invui/gui/impl/TabGUI.java +++ b/InvUI/src/main/java/de/studiocode/invui/gui/impl/TabGUI.java @@ -2,7 +2,7 @@ package de.studiocode.invui.gui.impl; import de.studiocode.invui.gui.Controllable; import de.studiocode.invui.gui.SlotElement; -import de.studiocode.invui.gui.structure.Marker; +import de.studiocode.invui.gui.structure.Markers; import de.studiocode.invui.gui.structure.Structure; import java.util.List; @@ -21,7 +21,7 @@ public abstract class TabGUI extends BaseGUI implements Controllable { } public TabGUI(int width, int height, int tabAmount, Structure structure) { - this(width, height, tabAmount, structure.createIngredientList().findIndicesOfMarker(Marker.ITEM_LIST_SLOT)); + this(width, height, tabAmount, structure.getIngredientList().findIndicesOfMarker(Markers.ITEM_LIST_SLOT)); applyStructure(structure); } diff --git a/InvUI/src/main/java/de/studiocode/invui/gui/structure/Ingredient.java b/InvUI/src/main/java/de/studiocode/invui/gui/structure/Ingredient.java index 60a682b..4c455a7 100644 --- a/InvUI/src/main/java/de/studiocode/invui/gui/structure/Ingredient.java +++ b/InvUI/src/main/java/de/studiocode/invui/gui/structure/Ingredient.java @@ -7,7 +7,7 @@ import java.util.function.Supplier; class Ingredient { private final SlotElement slotElement; - private final Marker marker; + private final String marker; private final Supplier elementSupplier; public Ingredient(SlotElement slotElement) { @@ -22,7 +22,7 @@ class Ingredient { this.marker = null; } - public Ingredient(Marker marker) { + public Ingredient(String marker) { this.marker = marker; this.slotElement = null; this.elementSupplier = null; @@ -32,7 +32,7 @@ class Ingredient { return slotElement == null ? elementSupplier.get() : slotElement; } - public Marker getMarker() { + public String getMarker() { return marker; } diff --git a/InvUI/src/main/java/de/studiocode/invui/gui/structure/IngredientList.java b/InvUI/src/main/java/de/studiocode/invui/gui/structure/IngredientList.java index 6b21b41..acf2782 100644 --- a/InvUI/src/main/java/de/studiocode/invui/gui/structure/IngredientList.java +++ b/InvUI/src/main/java/de/studiocode/invui/gui/structure/IngredientList.java @@ -27,14 +27,14 @@ public class IngredientList extends ArrayList { } } - public int[] findIndicesOfMarker(Marker marker) { + public int[] findIndicesOfMarker(String marker) { List indices = new ArrayList<>(); for (int i = 0; i < size(); i++) { Ingredient ingredient = get(i); - if (ingredient != null && ingredient.isMarker() && ingredient.getMarker() == marker) + if (ingredient != null && ingredient.isMarker() && ingredient.getMarker().equals(marker)) indices.add(i); } - + return indices.stream().mapToInt(Integer::intValue).toArray(); } diff --git a/InvUI/src/main/java/de/studiocode/invui/gui/structure/Marker.java b/InvUI/src/main/java/de/studiocode/invui/gui/structure/Marker.java deleted file mode 100644 index d8d4419..0000000 --- a/InvUI/src/main/java/de/studiocode/invui/gui/structure/Marker.java +++ /dev/null @@ -1,7 +0,0 @@ -package de.studiocode.invui.gui.structure; - -public enum Marker { - - ITEM_LIST_SLOT - -} diff --git a/InvUI/src/main/java/de/studiocode/invui/gui/structure/Markers.java b/InvUI/src/main/java/de/studiocode/invui/gui/structure/Markers.java new file mode 100644 index 0000000..4625673 --- /dev/null +++ b/InvUI/src/main/java/de/studiocode/invui/gui/structure/Markers.java @@ -0,0 +1,18 @@ +package de.studiocode.invui.gui.structure; + +import de.studiocode.invui.gui.impl.PagedGUI; +import de.studiocode.invui.gui.impl.ScrollGUI; +import de.studiocode.invui.gui.impl.TabGUI; + +/** + * Registry class for default markers + */ +public class Markers { + + /** + * The marker for item list slots in {@link PagedGUI PagedGUIs}, + * {@link ScrollGUI ScrollGUIs} and {@link TabGUI TabGUIs} + */ + public static final String ITEM_LIST_SLOT = "ILS"; + +} diff --git a/InvUI/src/main/java/de/studiocode/invui/gui/structure/Structure.java b/InvUI/src/main/java/de/studiocode/invui/gui/structure/Structure.java index bd05bc1..8713049 100644 --- a/InvUI/src/main/java/de/studiocode/invui/gui/structure/Structure.java +++ b/InvUI/src/main/java/de/studiocode/invui/gui/structure/Structure.java @@ -4,8 +4,10 @@ import de.studiocode.invui.gui.GUI; import de.studiocode.invui.gui.SlotElement; import de.studiocode.invui.gui.SlotElement.ItemSlotElement; import de.studiocode.invui.item.Item; -import de.studiocode.invui.item.impl.SimpleItem; import de.studiocode.invui.item.ItemProvider; +import de.studiocode.invui.item.ItemWrapper; +import de.studiocode.invui.item.impl.SimpleItem; +import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ShapedRecipe; import org.jetbrains.annotations.NotNull; @@ -14,15 +16,16 @@ import java.util.function.Supplier; /** * Provides an easy way to design {@link GUI}s. - * Inspired by Bukkit's {@link ShapedRecipe}, this class will let you + * Inspired by Bukkit's {@link ShapedRecipe}, {@link Structure Structures} will let you * design a {@link GUI} in a similar way. */ -public class Structure { +public class Structure implements Cloneable { private static final HashMap globalIngredientMap = new HashMap<>(); - private final HashMap ingredientMap = new HashMap<>(); private final String structureData; + private HashMap ingredientMap = new HashMap<>(); + private IngredientList ingredientList; public Structure(String structureData) { this.structureData = structureData @@ -30,6 +33,10 @@ public class Structure { .replace("\n", ""); } + public static void addGlobalIngredient(char key, @NotNull ItemStack itemStack) { + addGlobalIngredient(key, new ItemWrapper(itemStack)); + } + public static void addGlobalIngredient(char key, @NotNull ItemProvider itemProvider) { addGlobalIngredient(key, new SimpleItem(itemProvider)); } @@ -46,7 +53,7 @@ public class Structure { globalIngredientMap.put(key, new Ingredient(element)); } - public static void addGlobalIngredient(char key, @NotNull Marker marker) { + public static void addGlobalIngredient(char key, @NotNull String marker) { globalIngredientMap.put(key, new Ingredient(marker)); } @@ -54,38 +61,63 @@ public class Structure { globalIngredientMap.put(key, new Ingredient(elementSupplier)); } + public Structure addIngredient(char key, @NotNull ItemStack itemStack) { + if (ingredientList != null) throw new UnsupportedOperationException("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"); return addIngredient(key, new SimpleItem(itemProvider)); } public Structure addIngredient(char key, @NotNull Item item) { + if (ingredientList != null) throw new UnsupportedOperationException("Structure is locked"); return addIngredient(key, new ItemSlotElement(item)); } public Structure addIngredient(char key, @NotNull SlotElement element) { + if (ingredientList != null) throw new UnsupportedOperationException("Structure is locked"); ingredientMap.put(key, new Ingredient(element)); return this; } - public Structure addIngredient(char key, @NotNull Marker marker) { + public Structure addIngredient(char key, @NotNull String marker) { + if (ingredientList != null) throw new UnsupportedOperationException("Structure is locked"); ingredientMap.put(key, new Ingredient(marker)); return this; } public Structure addIngredient(char key, @NotNull Supplier itemSupplier) { + if (ingredientList != null) throw new UnsupportedOperationException("Structure is locked"); ingredientMap.put(key, new Ingredient(() -> new ItemSlotElement(itemSupplier.get()))); return this; } public Structure addIngredientElementSupplier(char key, @NotNull Supplier elementSupplier) { + if (ingredientList != null) throw new UnsupportedOperationException("Structure is locked"); ingredientMap.put(key, new Ingredient(elementSupplier)); return this; } - public IngredientList createIngredientList() { + public IngredientList getIngredientList() { + if (ingredientList != null) return ingredientList; + HashMap ingredients = new HashMap<>(globalIngredientMap); - this.ingredientMap.forEach(ingredients::put); - return new IngredientList(structureData, ingredients); + ingredients.putAll(this.ingredientMap); + return ingredientList = new IngredientList(structureData, ingredients); + } + + @Override + public Structure clone() { + try { + Structure clone = (Structure) super.clone(); + clone.ingredientMap = new HashMap<>(ingredientMap); + clone.ingredientList = null; + return clone; + } catch (CloneNotSupportedException e) { + throw new AssertionError(); + } } }