Reworked Structure and Markers

This commit is contained in:
NichtStudioCode 2021-08-22 15:19:58 +02:00
parent ca0dff2153
commit 97021078d5
10 changed files with 79 additions and 31 deletions

@ -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<G extends GUI> {
return this;
}
public GUIBuilder<G> addIngredient(char key, @NotNull ItemStack itemStack) {
context.getStructure().addIngredient(key, itemStack);
return this;
}
public GUIBuilder<G> addIngredient(char key, @NotNull ItemProvider itemProvider) {
context.getStructure().addIngredient(key, itemProvider);
return this;
@ -54,7 +59,7 @@ public class GUIBuilder<G extends GUI> {
return this;
}
public GUIBuilder<G> addIngredient(char key, @NotNull Marker marker) {
public GUIBuilder<G> addIngredient(char key, @NotNull String marker) {
context.getStructure().addIngredient(key, marker);
return this;
}

@ -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

@ -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);
}

@ -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);
}

@ -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);
}

@ -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<? extends SlotElement> 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;
}

@ -27,11 +27,11 @@ public class IngredientList extends ArrayList<Ingredient> {
}
}
public int[] findIndicesOfMarker(Marker marker) {
public int[] findIndicesOfMarker(String marker) {
List<Integer> 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);
}

@ -1,7 +0,0 @@
package de.studiocode.invui.gui.structure;
public enum Marker {
ITEM_LIST_SLOT
}

@ -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";
}

@ -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<Character, Ingredient> globalIngredientMap = new HashMap<>();
private final HashMap<Character, Ingredient> ingredientMap = new HashMap<>();
private final String structureData;
private HashMap<Character, Ingredient> 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<? extends Item> 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<? extends SlotElement> 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<Character, Ingredient> 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();
}
}
}