Skip to content

Commit 215f088

Browse files
Add PagedInventoriesGuiImpl
1 parent 673e9e5 commit 215f088

4 files changed

Lines changed: 134 additions & 2 deletions

File tree

invui-core/src/main/java/xyz/xenondevs/invui/gui/PagedGui.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.jetbrains.annotations.NotNull;
55
import org.jetbrains.annotations.Nullable;
66
import xyz.xenondevs.invui.gui.structure.Structure;
7+
import xyz.xenondevs.invui.inventory.Inventory;
78
import xyz.xenondevs.invui.item.Item;
89

910
import java.util.List;
@@ -107,6 +108,51 @@ public interface PagedGui<C> extends Gui {
107108
return new PagedNestedGuiImpl(guis, structure);
108109
}
109110

111+
/**
112+
* Creates a new {@link Builder Gui Builder} for a {@link PagedGui} that uses {@link Inventory Inventories} as content.
113+
*
114+
* @return The new {@link Builder Gui Builder}.
115+
*/
116+
static @NotNull Builder<@NotNull Inventory> inventories() {
117+
return new PagedInventoriesGuiImpl.Builder();
118+
}
119+
120+
/**
121+
* Creates a new {@link PagedGui} after configuring a {@link Builder Gui Builder} using the given {@link Consumer}.
122+
*
123+
* @param consumer The {@link Consumer} to configure the {@link Builder Gui Builder}.
124+
* @return The created {@link PagedGui}.
125+
*/
126+
static @NotNull PagedGui<@NotNull Inventory> inventories(@NotNull Consumer<@NotNull Builder<@NotNull Inventory>> consumer) {
127+
Builder<Inventory> builder = inventories();
128+
consumer.accept(builder);
129+
return builder.build();
130+
}
131+
132+
/**
133+
* Creates a new {@link PagedGui}.
134+
*
135+
* @param width The width of the {@link PagedGui}.
136+
* @param height The height of the {@link PagedGui}.
137+
* @param inventories The {@link Inventory Inventories} to use as pages.
138+
* @param contentListSlots The slots where content should be displayed.
139+
* @return The created {@link PagedGui}.
140+
*/
141+
static @NotNull PagedGui<@NotNull Inventory> ofInventories(int width, int height, @NotNull List<@NotNull Inventory> inventories, int... contentListSlots) {
142+
return new PagedInventoriesGuiImpl(width, height, inventories, contentListSlots);
143+
}
144+
145+
/**
146+
* Creates a new {@link PagedGui}.
147+
*
148+
* @param structure The {@link Structure} to use.
149+
* @param inventories The {@link Inventory Inventories} to use as pages.
150+
* @return The created {@link PagedGui}.
151+
*/
152+
static @NotNull PagedGui<@NotNull Inventory> ofInventories(@NotNull Structure structure, @NotNull List<@NotNull Inventory> inventories) {
153+
return new PagedInventoriesGuiImpl(inventories, structure);
154+
}
155+
110156
/**
111157
* Gets the amount of pages this {@link PagedGui} has.
112158
*
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package xyz.xenondevs.invui.gui;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
import org.jetbrains.annotations.Nullable;
5+
import xyz.xenondevs.invui.gui.structure.Structure;
6+
import xyz.xenondevs.invui.inventory.Inventory;
7+
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
import java.util.stream.Collectors;
11+
import java.util.stream.IntStream;
12+
13+
/**
14+
* An {@link AbstractPagedGui} where every page is its own {@link Inventory}.
15+
*
16+
* @see PagedItemsGuiImpl
17+
* @see PagedNestedGuiImpl
18+
*/
19+
final class PagedInventoriesGuiImpl extends AbstractPagedGui<Inventory> {
20+
21+
private List<Inventory> inventories;
22+
23+
/**
24+
* Creates a new {@link PagedInventoriesGuiImpl}.
25+
*
26+
* @param width The width of this Gui.
27+
* @param height The height of this Gui.
28+
* @param inventories The {@link Inventory Inventories} to use as pages.
29+
* @param contentListSlots The slots where content should be displayed.
30+
*/
31+
public PagedInventoriesGuiImpl(int width, int height, @Nullable List<@NotNull Inventory> inventories, int... contentListSlots) {
32+
super(width, height, false, contentListSlots);
33+
setContent(inventories);
34+
}
35+
36+
/**
37+
* Creates a new {@link PagedInventoriesGuiImpl}.
38+
*
39+
* @param inventories The {@link Inventory Inventories} to use as pages.
40+
* @param structure The {@link Structure} to use.
41+
*/
42+
public PagedInventoriesGuiImpl(@Nullable List<@NotNull Inventory> inventories, @NotNull Structure structure) {
43+
super(structure.getWidth(), structure.getHeight(), false, structure);
44+
setContent(inventories);
45+
}
46+
47+
@Override
48+
public int getPageAmount() {
49+
return inventories.size();
50+
}
51+
52+
@Override
53+
public void setContent(@Nullable List<@NotNull Inventory> inventories) {
54+
this.inventories = inventories == null ? new ArrayList<>() : inventories;
55+
update();
56+
}
57+
58+
@Override
59+
protected List<SlotElement> getPageElements(int page) {
60+
if (inventories.size() <= page) return new ArrayList<>();
61+
62+
Inventory inventory = inventories.get(page);
63+
int size = inventory.getSize();
64+
65+
return IntStream.range(0, size)
66+
.mapToObj(i -> new SlotElement.InventorySlotElement(inventory, i))
67+
.collect(Collectors.toList());
68+
}
69+
70+
public static final class Builder extends AbstractBuilder<Inventory> {
71+
72+
@Override
73+
public @NotNull PagedGui<Inventory> build() {
74+
if (structure == null)
75+
throw new IllegalStateException("Structure is not defined.");
76+
77+
var gui = new PagedInventoriesGuiImpl(content, structure);
78+
applyModifiers(gui);
79+
return gui;
80+
}
81+
82+
}
83+
84+
}

invui-core/src/main/java/xyz/xenondevs/invui/gui/PagedItemsGuiImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
import java.util.stream.Collectors;
1212

1313
/**
14-
* A {@link AbstractPagedGui} that is filled with {@link Item Items}.
14+
* An {@link AbstractPagedGui} that is filled with {@link Item Items}.
1515
*
1616
* @see PagedNestedGuiImpl
17+
* @see PagedInventoriesGuiImpl
1718
*/
1819
final class PagedItemsGuiImpl extends AbstractPagedGui<Item> {
1920

invui-core/src/main/java/xyz/xenondevs/invui/gui/PagedNestedGuiImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
import java.util.stream.IntStream;
1111

1212
/**
13-
* A {@link AbstractPagedGui} where every page is its own {@link Gui}.
13+
* An {@link AbstractPagedGui} where every page is its own {@link Gui}.
1414
*
1515
* @see PagedItemsGuiImpl
16+
* @see PagedInventoriesGuiImpl
1617
*/
1718
final class PagedNestedGuiImpl extends AbstractPagedGui<Gui> {
1819

0 commit comments

Comments
 (0)