Skip to content
This repository was archived by the owner on Jul 24, 2021. It is now read-only.

Commit b1620c1

Browse files
committed
✨ Add templates feature
1 parent 59aea84 commit b1620c1

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

src/main/java/fr/bakaaless/api/inventory/InventoryAPI.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,17 @@ public InventoryAPI setBorder(final Function<Object, ItemStack> function, final
451451
return this;
452452
}
453453

454+
public InventoryAPI applyTemplate(final Template template) {
455+
template.getItems().forEach(item ->
456+
this.addItem(item.clone())
457+
);
458+
return this;
459+
}
460+
461+
public Template generateTemplate() {
462+
return new Template(this.getItems());
463+
}
464+
454465
/**
455466
* Build and open the inventory to a player
456467
* @param player The player to open the inventory
@@ -562,4 +573,22 @@ public void onDrag(final InventoryDragEvent e) {
562573
this.items.stream().filter(item -> e.getInventorySlots().contains(item.getSlot()) || e.getRawSlots().contains(item.getSlot()))
563574
.forEach(item -> e.setCancelled(e.isCancelled() || item.isCancelled()));
564575
}
576+
577+
@Override
578+
public String toString() {
579+
return "InventoryAPI{" +
580+
"inventory=" + this.inventory +
581+
", size=" + this.size +
582+
", title='" + this.title + '\'' +
583+
", type=" + this.type +
584+
", items=" + this.items +
585+
", function=" + this.function +
586+
", closeEvent=" + this.closeEvent +
587+
", clickEvent=" + this.clickEvent +
588+
", interactionCancel=" + this.interactionCancel +
589+
", refreshed=" + this.refreshed +
590+
", build=" + this.build +
591+
", plugin=" + this.plugin +
592+
'}';
593+
}
565594
}

src/main/java/fr/bakaaless/api/inventory/ItemAPI.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,23 @@ public ItemAPI(final int slot, final Function<Object, ItemStack> function, final
4747
this.consumer = consumer;
4848
}
4949

50+
/**
51+
* @param slot The slot where will be located the item.
52+
* @param item The wished ItemStack
53+
* @param function A function that return an ItemStack, for the refresh task
54+
* @param cancelled The boolean to enable/disable the interaction protection for this slot
55+
* @param consumer A lambda expression that correspond to the executed code when item is clicked
56+
*/
57+
private ItemAPI(final int slot, final ItemStack item, final Function<Object, ItemStack> function, final boolean cancelled, final Consumer<InventoryClickEvent> consumer) {
58+
this.slot = slot;
59+
this.item = item;
60+
this.function = function;
61+
this.item = new ItemStack(Material.AIR);
62+
this.refresh(this);
63+
this.cancelled = cancelled;
64+
this.consumer = consumer;
65+
}
66+
5067
/**
5168
* Refresh this item at its slot.
5269
* @param o An {@link InventoryAPI} instance.
@@ -124,4 +141,19 @@ public void setCancelled(final boolean cancelled) {
124141
public void setConsumer(final Consumer<InventoryClickEvent> consumer) {
125142
this.consumer = consumer;
126143
}
144+
145+
public ItemAPI clone() {
146+
return new ItemAPI(this.slot, this.item, this.function, this.cancelled, this.consumer);
147+
}
148+
149+
@Override
150+
public String toString() {
151+
return "ItemAPI{" +
152+
"slot=" + this.slot +
153+
", function=" + this.function +
154+
", item=" + this.item +
155+
", cancelled=" + this.cancelled +
156+
", consumer=" + this.consumer +
157+
'}';
158+
}
127159
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package fr.bakaaless.api.inventory;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Template {
7+
8+
private final List<ItemAPI> items;
9+
10+
public Template(final List<ItemAPI> items) {
11+
this.items = new ArrayList<>();
12+
for (final ItemAPI item : items)
13+
this.items.add(item.clone());
14+
}
15+
16+
public List<ItemAPI> getItems() {
17+
return this.items;
18+
}
19+
20+
@Override
21+
public String toString() {
22+
return "Template{" +
23+
"items=" + this.items +
24+
'}';
25+
}
26+
}

0 commit comments

Comments
 (0)