From ca0dff21538142027067bf66084aef22434f1de8 Mon Sep 17 00:00:00 2001 From: NichtStudioCode <51272202+NichtStudioCode@users.noreply.github.com> Date: Sun, 22 Aug 2021 14:44:19 +0200 Subject: [PATCH] Reworked GUIBuilder / GUIType --- .../invui/gui/builder/GUIBuilder.java | 121 ++++++------------ .../invui/gui/builder/GUIContext.java | 61 +++++++++ .../studiocode/invui/gui/builder/GUIType.java | 11 -- .../invui/gui/builder/guitype/GUIType.java | 34 +++++ .../gui/builder/guitype/NormalGUIType.java | 23 ++++ .../gui/builder/guitype/PagedGUIsGUIType.java | 23 ++++ .../builder/guitype/PagedItemsGUIType.java | 23 ++++ .../gui/builder/guitype/ScrollGUIType.java | 23 ++++ .../invui/gui/builder/guitype/TabGUIType.java | 23 ++++ 9 files changed, 247 insertions(+), 95 deletions(-) create mode 100644 InvUI/src/main/java/de/studiocode/invui/gui/builder/GUIContext.java delete mode 100644 InvUI/src/main/java/de/studiocode/invui/gui/builder/GUIType.java create mode 100644 InvUI/src/main/java/de/studiocode/invui/gui/builder/guitype/GUIType.java create mode 100644 InvUI/src/main/java/de/studiocode/invui/gui/builder/guitype/NormalGUIType.java create mode 100644 InvUI/src/main/java/de/studiocode/invui/gui/builder/guitype/PagedGUIsGUIType.java create mode 100644 InvUI/src/main/java/de/studiocode/invui/gui/builder/guitype/PagedItemsGUIType.java create mode 100644 InvUI/src/main/java/de/studiocode/invui/gui/builder/guitype/ScrollGUIType.java create mode 100644 InvUI/src/main/java/de/studiocode/invui/gui/builder/guitype/TabGUIType.java 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 308cfcf..0a503f2 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 @@ -2,7 +2,7 @@ package de.studiocode.invui.gui.builder; import de.studiocode.invui.gui.GUI; import de.studiocode.invui.gui.SlotElement; -import de.studiocode.invui.gui.impl.*; +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; @@ -10,143 +10,96 @@ import de.studiocode.invui.item.ItemProvider; import org.bukkit.inventory.ShapedRecipe; import org.jetbrains.annotations.NotNull; -import java.util.ArrayList; import java.util.List; import java.util.function.Supplier; -import static de.studiocode.invui.gui.builder.GUIType.*; /** * A builder class to easily construct {@link GUI}s.
* It provides similar functionality to Bukkit's {@link ShapedRecipe}, as it * allows for a structure String which defines the layout of the {@link GUI}. */ -public class GUIBuilder { +public class GUIBuilder { - private final GUIType guiType; - private final int width; - private final int height; + private final GUIType guiType; + private final GUIContext context; - private Structure structure; - - private List items = new ArrayList<>(); - private List guis = new ArrayList<>(); - - public GUIBuilder(@NotNull GUIType guiType, int width, int height) { + public GUIBuilder(@NotNull GUIType guiType, int width, int height) { this.guiType = guiType; - this.width = width; - this.height = height; + this.context = new GUIContext(width, height); } - public GUIBuilder setStructure(@NotNull String structureData) { - this.structure = new Structure(structureData); + public GUIBuilder setStructure(@NotNull String structureData) { + context.setStructure(new Structure(structureData)); return this; } - public GUIBuilder setStructure(@NotNull Structure structure) { - this.structure = structure; + public GUIBuilder setStructure(@NotNull Structure structure) { + context.setStructure(structure); return this; } - public GUIBuilder addIngredient(char key, @NotNull ItemProvider itemProvider) { - structure.addIngredient(key, itemProvider); + public GUIBuilder addIngredient(char key, @NotNull ItemProvider itemProvider) { + context.getStructure().addIngredient(key, itemProvider); return this; } - public GUIBuilder addIngredient(char key, @NotNull Item item) { - structure.addIngredient(key, item); + public GUIBuilder addIngredient(char key, @NotNull Item item) { + context.getStructure().addIngredient(key, item); return this; } - public GUIBuilder addIngredient(char key, @NotNull SlotElement element) { - structure.addIngredient(key, element); + public GUIBuilder addIngredient(char key, @NotNull SlotElement element) { + context.getStructure().addIngredient(key, element); return this; } - public GUIBuilder addIngredient(char key, @NotNull Marker marker) { - structure.addIngredient(key, marker); + public GUIBuilder addIngredient(char key, @NotNull Marker marker) { + context.getStructure().addIngredient(key, marker); return this; } - public GUIBuilder addIngredient(char key, @NotNull Supplier itemSupplier) { - structure.addIngredient(key, itemSupplier); + public GUIBuilder addIngredient(char key, @NotNull Supplier itemSupplier) { + context.getStructure().addIngredient(key, itemSupplier); return this; } - public GUIBuilder addIngredientElementSupplier(char key, @NotNull Supplier elementSupplier) { - structure.addIngredientElementSupplier(key, elementSupplier); + public GUIBuilder addIngredientElementSupplier(char key, @NotNull Supplier elementSupplier) { + context.getStructure().addIngredientElementSupplier(key, elementSupplier); return this; } - public GUIBuilder setItems(@NotNull List items) { - if (guiType != PAGED_ITEMS && guiType != SCROLL) + public GUIBuilder setItems(@NotNull List items) { + if (!guiType.acceptsItems()) throw new UnsupportedOperationException("Items cannot be set in this gui type."); - this.items = items; + context.setItems(items); return this; } - public GUIBuilder addItem(@NotNull Item item) { - if (guiType != PAGED_ITEMS && guiType != SCROLL) + public GUIBuilder addItem(@NotNull Item item) { + if (!guiType.acceptsItems()) throw new UnsupportedOperationException("Items cannot be set in this gui type."); - items.add(item); + context.getItems().add(item); return this; } - public GUIBuilder setGUIs(@NotNull List guis) { - if (guiType != PAGED_GUIs && guiType != TAB) + public GUIBuilder setGUIs(@NotNull List guis) { + if (!guiType.acceptsGUIs()) throw new UnsupportedOperationException("GUIs cannot be set in this gui type."); - this.guis = guis; + context.setGuis(guis); return this; } - public GUIBuilder addGUI(@NotNull GUI gui) { - if (guiType != PAGED_GUIs && guiType != TAB) + public GUIBuilder addGUI(@NotNull GUI gui) { + if (!guiType.acceptsGUIs()) throw new UnsupportedOperationException("GUIs cannot be set in this gui type."); - guis.add(gui); + context.getGuis().add(gui); return this; } - public GUI build() { - switch (guiType) { - - case NORMAL: - return buildSimpleGUI(); - - case PAGED_ITEMS: - return buildSimplePagedItemsGUI(); - - case PAGED_GUIs: - return buildSimplePagedNestedGUI(); - - case TAB: - return buildSimpleTabGUI(); - - case SCROLL: - return buildSimpleScrollGUI(); - - default: - throw new UnsupportedOperationException("Unknown GUI type"); - } - } - - private SimpleGUI buildSimpleGUI() { - return new SimpleGUI(width, height, structure); - } - - private SimplePagedItemsGUI buildSimplePagedItemsGUI() { - return new SimplePagedItemsGUI(width, height, items, structure); - } - - private SimplePagedNestedGUI buildSimplePagedNestedGUI() { - return new SimplePagedNestedGUI(width, height, guis, structure); - } - - private SimpleTabGUI buildSimpleTabGUI() { - return new SimpleTabGUI(width, height, guis, structure); - } - - private SimpleScrollGUI buildSimpleScrollGUI() { - return new SimpleScrollGUI(width, height, items, structure); + public G build() { + if (context.getStructure() == null) throw new IllegalStateException("GUIContext has not been set yet."); + return guiType.createGUI(context); } } diff --git a/InvUI/src/main/java/de/studiocode/invui/gui/builder/GUIContext.java b/InvUI/src/main/java/de/studiocode/invui/gui/builder/GUIContext.java new file mode 100644 index 0000000..8271c41 --- /dev/null +++ b/InvUI/src/main/java/de/studiocode/invui/gui/builder/GUIContext.java @@ -0,0 +1,61 @@ +package de.studiocode.invui.gui.builder; + +import de.studiocode.invui.gui.GUI; +import de.studiocode.invui.gui.builder.guitype.GUIType; +import de.studiocode.invui.gui.structure.Structure; +import de.studiocode.invui.item.Item; +import org.jetbrains.annotations.NotNull; + +import java.util.ArrayList; +import java.util.List; + +/** + * The {@link GUIContext} contains all information from the {@link GUIBuilder} to be passed to + * an instance of {@link GUIType} to create a new {@link GUI}. + */ +public class GUIContext { + + private final int width; + private final int height; + private Structure structure; + private List guis = new ArrayList<>(); + private List items = new ArrayList<>(); + + public GUIContext(int width, int height) { + this.width = width; + this.height = height; + } + + public int getWidth() { + return width; + } + + public int getHeight() { + return height; + } + + public Structure getStructure() { + return structure; + } + + public void setStructure(@NotNull Structure structure) { + this.structure = structure; + } + + public List getGuis() { + return guis; + } + + public void setGuis(@NotNull List guis) { + this.guis = guis; + } + + public List getItems() { + return items; + } + + public void setItems(@NotNull List items) { + this.items = items; + } + +} diff --git a/InvUI/src/main/java/de/studiocode/invui/gui/builder/GUIType.java b/InvUI/src/main/java/de/studiocode/invui/gui/builder/GUIType.java deleted file mode 100644 index 143b211..0000000 --- a/InvUI/src/main/java/de/studiocode/invui/gui/builder/GUIType.java +++ /dev/null @@ -1,11 +0,0 @@ -package de.studiocode.invui.gui.builder; - -public enum GUIType { - - NORMAL, - PAGED_ITEMS, - PAGED_GUIs, - TAB, - SCROLL - -} diff --git a/InvUI/src/main/java/de/studiocode/invui/gui/builder/guitype/GUIType.java b/InvUI/src/main/java/de/studiocode/invui/gui/builder/guitype/GUIType.java new file mode 100644 index 0000000..88661cd --- /dev/null +++ b/InvUI/src/main/java/de/studiocode/invui/gui/builder/guitype/GUIType.java @@ -0,0 +1,34 @@ +package de.studiocode.invui.gui.builder.guitype; + +import de.studiocode.invui.gui.GUI; +import de.studiocode.invui.gui.builder.GUIContext; +import de.studiocode.invui.gui.impl.*; +import de.studiocode.invui.item.Item; + +public interface GUIType { + + GUIType NORMAL = new NormalGUIType(); + GUIType PAGED_ITEMS = new PagedItemsGUIType(); + GUIType PAGED_GUIs = new PagedGUIsGUIType(); + GUIType TAB = new TabGUIType(); + GUIType SCROLL = new ScrollGUIType(); + + /** + * Creates a {@link GUI} of type {@link G} with the given {@link GUIContext} + * + * @param context The {@link GUIContext} to create the {@link G} from. + * @return The created {@link G} + */ + G createGUI(GUIContext context); + + /** + * @return If this {@link GUIType} accepts {@link GUI GUIs} from the {@link GUIContext}. + */ + boolean acceptsGUIs(); + + /** + * @return If this {@link GUIType} accepts {@link Item Items} from the {@link GUIContext}. + */ + boolean acceptsItems(); + +} diff --git a/InvUI/src/main/java/de/studiocode/invui/gui/builder/guitype/NormalGUIType.java b/InvUI/src/main/java/de/studiocode/invui/gui/builder/guitype/NormalGUIType.java new file mode 100644 index 0000000..9cd2121 --- /dev/null +++ b/InvUI/src/main/java/de/studiocode/invui/gui/builder/guitype/NormalGUIType.java @@ -0,0 +1,23 @@ +package de.studiocode.invui.gui.builder.guitype; + +import de.studiocode.invui.gui.builder.GUIContext; +import de.studiocode.invui.gui.impl.SimpleGUI; + +class NormalGUIType implements GUIType { + + @Override + public SimpleGUI createGUI(GUIContext context) { + return new SimpleGUI(context.getWidth(), context.getHeight(), context.getStructure()); + } + + @Override + public boolean acceptsGUIs() { + return false; + } + + @Override + public boolean acceptsItems() { + return false; + } + +} diff --git a/InvUI/src/main/java/de/studiocode/invui/gui/builder/guitype/PagedGUIsGUIType.java b/InvUI/src/main/java/de/studiocode/invui/gui/builder/guitype/PagedGUIsGUIType.java new file mode 100644 index 0000000..7c79893 --- /dev/null +++ b/InvUI/src/main/java/de/studiocode/invui/gui/builder/guitype/PagedGUIsGUIType.java @@ -0,0 +1,23 @@ +package de.studiocode.invui.gui.builder.guitype; + +import de.studiocode.invui.gui.builder.GUIContext; +import de.studiocode.invui.gui.impl.SimplePagedNestedGUI; + +class PagedGUIsGUIType implements GUIType { + + @Override + public SimplePagedNestedGUI createGUI(GUIContext context) { + return new SimplePagedNestedGUI(context.getWidth(), context.getHeight(), context.getGuis(), context.getStructure()); + } + + @Override + public boolean acceptsGUIs() { + return true; + } + + @Override + public boolean acceptsItems() { + return false; + } + +} diff --git a/InvUI/src/main/java/de/studiocode/invui/gui/builder/guitype/PagedItemsGUIType.java b/InvUI/src/main/java/de/studiocode/invui/gui/builder/guitype/PagedItemsGUIType.java new file mode 100644 index 0000000..b74974a --- /dev/null +++ b/InvUI/src/main/java/de/studiocode/invui/gui/builder/guitype/PagedItemsGUIType.java @@ -0,0 +1,23 @@ +package de.studiocode.invui.gui.builder.guitype; + +import de.studiocode.invui.gui.builder.GUIContext; +import de.studiocode.invui.gui.impl.SimplePagedItemsGUI; + +class PagedItemsGUIType implements GUIType { + + @Override + public SimplePagedItemsGUI createGUI(GUIContext context) { + return new SimplePagedItemsGUI(context.getWidth(), context.getHeight(), context.getItems(), context.getStructure()); + } + + @Override + public boolean acceptsGUIs() { + return false; + } + + @Override + public boolean acceptsItems() { + return true; + } + +} diff --git a/InvUI/src/main/java/de/studiocode/invui/gui/builder/guitype/ScrollGUIType.java b/InvUI/src/main/java/de/studiocode/invui/gui/builder/guitype/ScrollGUIType.java new file mode 100644 index 0000000..f1c643a --- /dev/null +++ b/InvUI/src/main/java/de/studiocode/invui/gui/builder/guitype/ScrollGUIType.java @@ -0,0 +1,23 @@ +package de.studiocode.invui.gui.builder.guitype; + +import de.studiocode.invui.gui.builder.GUIContext; +import de.studiocode.invui.gui.impl.SimpleScrollGUI; + +class ScrollGUIType implements GUIType { + + @Override + public SimpleScrollGUI createGUI(GUIContext context) { + return new SimpleScrollGUI(context.getWidth(), context.getHeight(), context.getItems(), context.getStructure()); + } + + @Override + public boolean acceptsGUIs() { + return false; + } + + @Override + public boolean acceptsItems() { + return true; + } + +} \ No newline at end of file diff --git a/InvUI/src/main/java/de/studiocode/invui/gui/builder/guitype/TabGUIType.java b/InvUI/src/main/java/de/studiocode/invui/gui/builder/guitype/TabGUIType.java new file mode 100644 index 0000000..4c7f862 --- /dev/null +++ b/InvUI/src/main/java/de/studiocode/invui/gui/builder/guitype/TabGUIType.java @@ -0,0 +1,23 @@ +package de.studiocode.invui.gui.builder.guitype; + +import de.studiocode.invui.gui.builder.GUIContext; +import de.studiocode.invui.gui.impl.SimpleTabGUI; + +class TabGUIType implements GUIType { + + @Override + public SimpleTabGUI createGUI(GUIContext context) { + return new SimpleTabGUI(context.getWidth(), context.getHeight(), context.getGuis(), context.getStructure()); + } + + @Override + public boolean acceptsGUIs() { + return true; + } + + @Override + public boolean acceptsItems() { + return false; + } + +}