Skip to content

Commit f46191b

Browse files
author
BuildTools
committed
Basic Paginated inventories.
1 parent ec61fd4 commit f46191b

File tree

5 files changed

+117
-24
lines changed

5 files changed

+117
-24
lines changed

src/main/java/com/azortis/azortislib/experimental/inventory/GUIEngine.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ default void initialize(JavaPlugin plugin) {
111111
*
112112
* @param event the event to use
113113
*/
114-
// todo check what happens when clicked outside of the inventory
115114
@EventHandler
116115
default void onInventoryClick(InventoryClickEvent event) {
117116
if (event.getClickedInventory() != null && event.getClickedInventory().getHolder() instanceof Page) {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* An open source utilities library used for Azortis plugins.
3+
* Copyright (C) 2019 Azortis
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
19+
package com.azortis.azortislib.experimental.inventory;
20+
21+
import org.apache.commons.lang3.tuple.Pair;
22+
23+
import java.util.Map;
24+
25+
public class PageableTemplate extends Template {
26+
// todo: fix this, currently this is just a stopgap.
27+
private Map<String, Pair<Integer, Pair<Integer, Item>>> pairMap;
28+
29+
/**
30+
* @param templateConfiguration the gson configuration object if needed
31+
* @param pairMap whether to use the map
32+
* @param isConfigurable whether to ignore or use the template configuration
33+
* @param name
34+
* @param invSize
35+
* @throws NullPointerException throws if pairMap is null or empty.
36+
*/
37+
public PageableTemplate(Map<String, Object> templateConfiguration, Map<String, Pair<Integer, Item>> pairMap, boolean isConfigurable, String name, int invSize) throws NullPointerException {
38+
super(templateConfiguration, pairMap, isConfigurable, name, invSize);
39+
}
40+
41+
}

src/main/java/com/azortis/azortislib/experimental/inventory/impl/v1_15/GUIBuilder.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@
3333
import java.util.function.Consumer;
3434

3535
public class GUIBuilder {
36-
public final Map<String, Pair<Integer, Item>> pairMap;
36+
public Map<String, Pair<Integer, Item>> pairMap;
3737
public boolean isGlobal;
3838
public boolean isConfigurable;
3939
public String uniqueName;
4040
public String inventoryTitle;
4141
public Consumer<Page> update;
4242
public int inventorySize;
43-
private Class<? extends GUI> buildCustom;
43+
protected Class<? extends GUI> buildCustom;
4444

4545
public GUIBuilder() {
4646
pairMap = new HashMap<>();
@@ -87,7 +87,7 @@ public GUIBuilder addPlaceholder(int slot) {
8787
}
8888

8989
public static class ItemBuilder {
90-
private final GUIBuilder builder;
90+
protected final GUIBuilder builder;
9191
public ItemStack itemStack;
9292
public Consumer<InventoryClickEvent> action;
9393
public String itemName;

src/main/java/com/azortis/azortislib/experimental/inventory/impl/v1_15/PageableGUIBuilder.java

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,62 @@
1818

1919
package com.azortis.azortislib.experimental.inventory.impl.v1_15;
2020

21-
import com.azortis.azortislib.experimental.inventory.PageableGUI;
22-
import com.azortis.azortislib.inventory.item.Item;
21+
import com.azortis.azortislib.experimental.inventory.*;
22+
import org.apache.commons.lang3.tuple.MutablePair;
2323
import org.apache.commons.lang3.tuple.Pair;
2424

25+
import java.lang.reflect.InvocationTargetException;
26+
import java.util.HashMap;
2527
import java.util.Map;
28+
import java.util.function.Consumer;
2629

2730
public class PageableGUIBuilder extends GUIBuilder {
31+
private final Map<String, Pair<Integer, Pair<Integer, Item>>> pairMap;
2832
public int pages;
29-
public int pageSize;
30-
private Map<String, Pair<Integer, Pair<Integer, Item>>> pairMap;
33+
34+
public PageableGUIBuilder() {
35+
this.pairMap = new HashMap<>();
36+
}
3137

3238
@Override
33-
public GUIBuilder addPlaceholder(int slot) {
34-
return null; // todo: make this only add to the first page and make paged alternative for adding to multiple pages
39+
public PageableGUIBuilder addPlaceholder(int slot) {
40+
pairMap.values().removeIf((integerItemPair -> integerItemPair.getKey() == slot));
41+
pairMap.put("placeholder-" + slot + "-" + 1,
42+
new MutablePair<>(1,
43+
new MutablePair<>(slot, new com.azortis.azortislib.experimental.inventory.Item(true))));
44+
return this;
3545
}
3646

3747
@Override
3848
public PageableGUI build() {
39-
return null; // todo make this return actual pageable gui.
49+
Map<String, Pair<Integer, Item>> transformedMap = new HashMap<>();
50+
pairMap.forEach((s, integerPairPair) -> transformedMap.put(s,
51+
new MutablePair<>((integerPairPair.getKey() * inventorySize) + integerPairPair.getValue().getKey(),
52+
integerPairPair.getValue().getValue())));
53+
PageableTemplate template = new PageableTemplate(null, transformedMap, isConfigurable, inventoryTitle,
54+
inventorySize);
55+
PageableGUI gui;
56+
if (buildCustom != null) {
57+
try {
58+
gui = (PageableGUI) buildCustom.getConstructor(Template.class, String.class, boolean.class, boolean.class, Consumer.class, int.class, int.class)
59+
.newInstance(template, uniqueName, isGlobal, isConfigurable, update, pages, inventorySize);
60+
} catch (IllegalAccessException | InstantiationException | InvocationTargetException | NoSuchMethodException e) {
61+
gui = new PageableGUIImpl(template, uniqueName, isGlobal, isConfigurable, update, pages, inventorySize);
62+
System.out.println("Warning - Could not find or instantiate constructor for class "
63+
+ buildCustom + ", using default implementation for pageable gui.");
64+
e.printStackTrace();
65+
}
66+
} else {
67+
gui = new PageableGUIImpl(template, uniqueName, isGlobal, isConfigurable, update, pages, inventorySize);
68+
}
69+
gui.setInventoryName(inventoryTitle);
70+
return gui;
4071
}
4172

73+
@Override
74+
public ItemBuilder item() {
75+
return new PageableItemBuilder(this);
76+
}
4277

4378
public static class PageableItemBuilder extends ItemBuilder {
4479
public int page;
@@ -48,8 +83,11 @@ protected PageableItemBuilder(GUIBuilder builder) {
4883
}
4984

5085
@Override
51-
public GUIBuilder add() {
52-
return null; // todo make this add to only actual page and slot
86+
public PageableGUIBuilder add() {
87+
if (itemStack == null) return (PageableGUIBuilder) builder;
88+
builder.with($ -> ((PageableGUIBuilder) $).pairMap.put(itemName,
89+
new MutablePair<>(page, new MutablePair<>(slot, new Item(itemStack, action)))));
90+
return (PageableGUIBuilder) builder;
5391
}
5492
}
5593
}

src/main/java/com/azortis/azortislib/experimental/inventory/impl/v1_15/PageableGUIImpl.java

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@
1818

1919
package com.azortis.azortislib.experimental.inventory.impl.v1_15;
2020

21-
import com.azortis.azortislib.experimental.inventory.Item;
22-
import com.azortis.azortislib.experimental.inventory.Page;
23-
import com.azortis.azortislib.experimental.inventory.PageableGUI;
24-
import com.azortis.azortislib.experimental.inventory.Template;
21+
import com.azortis.azortislib.experimental.inventory.*;
22+
import com.azortis.azortislib.utils.FormatUtil;
23+
import org.bukkit.Bukkit;
2524
import org.bukkit.inventory.Inventory;
2625
import org.jetbrains.annotations.NotNull;
2726

@@ -36,14 +35,18 @@ public class PageableGUIImpl extends GUIImpl implements PageableGUI {
3635
private Item[][] items;
3736
private int pages;
3837

39-
// todo: make pageable template.
40-
public PageableGUIImpl(Template template, String name, boolean isGlobal, boolean isConfigurable, Consumer<Page> onUpdate, int pages) {
38+
public PageableGUIImpl(Template template, String name, boolean isGlobal, boolean isConfigurable, Consumer<Page> onUpdate, int pages, int pageSize) {
4139
super(template, name, isGlobal, isConfigurable, onUpdate);
42-
pageSize = template.getItems().length;
40+
this.pageSize = pageSize;
4341
this.pages = pages;
4442
inventoryNames = new ArrayList<>();
4543
inventoryList = new ArrayList<>();
46-
// todo remember to initialize items 2d array here w/ pageable template.
44+
items = new Item[pages][pageSize];
45+
46+
Item[] fill = template.getItems();
47+
for (int p = 0; p < pages; p++) {
48+
if (pageSize >= 0) System.arraycopy(fill, (p * pageSize), items[p], 0, pageSize);
49+
}
4750
}
4851

4952
/**
@@ -63,8 +66,10 @@ public void addPages(int amount) {
6366
}
6467

6568
protected void expandItemsArray() {
66-
Item[][] temp = new Item[pages][items[0].length]; // todo: finish this
67-
69+
Item[][] newArr = new Item[pages][pageSize];
70+
for (int p = 0; p < pages; p++)
71+
System.arraycopy(items[p], 0, newArr[p], 0, items[p].length);
72+
items = newArr;
6873
}
6974

7075
/**
@@ -123,7 +128,17 @@ public void setInventoryName(String inventoryName) {
123128
*/
124129
@Override
125130
public Inventory getInventory(int page) {
126-
return null; // todo: finish this
131+
if (inventoryList.size() > page) {
132+
return inventoryList.get(page);
133+
} else if (pages > page) {
134+
Page p = GUIManager.getInstance().getEngine().createPage(this);
135+
Inventory i = Bukkit.createInventory(p, pageSize, FormatUtil.color(inventoryNames.get(page)));
136+
p.setInventory(i);
137+
inventoryList.set(page, i);
138+
return i;
139+
140+
}
141+
return null;
127142
}
128143

129144
/**

0 commit comments

Comments
 (0)