Skip to content

Commit 9fa174b

Browse files
authored
Add inventory scheming (#18)
1 parent 5c1467f commit 9fa174b

1 file changed

Lines changed: 107 additions & 0 deletions

File tree

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package fr.mrmicky.fastinv;
2+
3+
import org.bukkit.event.inventory.InventoryClickEvent;
4+
import org.bukkit.inventory.ItemStack;
5+
6+
import java.util.ArrayList;
7+
import java.util.HashMap;
8+
import java.util.List;
9+
import java.util.Map;
10+
import java.util.Objects;
11+
import java.util.function.Consumer;
12+
13+
public class InventoryScheme {
14+
15+
private final List<String> masks = new ArrayList<>();
16+
private final Map<Character, ItemStack> items = new HashMap<>();
17+
private final Map<Character, Consumer<InventoryClickEvent>> handlers = new HashMap<>();
18+
19+
/**
20+
* Add a mask to this scheme including all sort of characters.
21+
* For example: "110101011"
22+
*
23+
* @param mask a 9 characters mask
24+
* @return this scheme instance
25+
*/
26+
public InventoryScheme mask(String mask) {
27+
Objects.requireNonNull(mask);
28+
this.masks.add(mask.length() > 9 ? mask.substring(0, 10) : mask);
29+
30+
return this;
31+
}
32+
33+
/**
34+
* Add multiples masks to this scheme including all sort of characters.
35+
* For example: "111111111", "110101011", "111111111"
36+
*
37+
* @param masks multiple 9-characters masks
38+
* @return this scheme instance
39+
*/
40+
public InventoryScheme masks(String... masks) {
41+
for (String mask : Objects.requireNonNull(masks)) {
42+
mask(mask);
43+
}
44+
return this;
45+
}
46+
47+
/**
48+
* Bind character to the corresponding item in the inventory.
49+
*
50+
* @param character the associated character in the mask
51+
* @param item the item to use for this character
52+
* @param handler consumer for the item
53+
* @return this scheme instance
54+
*/
55+
public InventoryScheme bindItem(char character, ItemStack item, Consumer<InventoryClickEvent> handler) {
56+
this.items.put(character, Objects.requireNonNull(item));
57+
58+
if (handler != null) {
59+
this.handlers.put(character, handler);
60+
}
61+
return this;
62+
}
63+
64+
/**
65+
* Bind character to the corresponding item in the inventory.
66+
*
67+
* @param character the associated character in the mask
68+
* @param item the item to use for this character
69+
* @return this scheme instance
70+
*/
71+
public InventoryScheme bindItem(char character, ItemStack item) {
72+
return this.bindItem(character, item, null);
73+
}
74+
75+
/**
76+
* Unbind any item from this character.
77+
*
78+
* @param character the character to unbind
79+
* @return this scheme instance
80+
*/
81+
public InventoryScheme unbindItem(char character) {
82+
this.items.remove(character);
83+
this.handlers.remove(character);
84+
return this;
85+
}
86+
87+
/**
88+
* Apply the current inventory scheme to the FastInv instance.
89+
*
90+
* @param inv the FastInv instance to apply this scheme to
91+
*/
92+
public void apply(FastInv inv) {
93+
for (int line = 0; line < this.masks.size(); line++) {
94+
String mask = this.masks.get(line);
95+
96+
for (int slot = 0; slot < mask.length(); slot++) {
97+
char c = mask.charAt(slot);
98+
ItemStack item = this.items.get(c);
99+
Consumer<InventoryClickEvent> handler = this.handlers.get(c);
100+
101+
if (item != null) {
102+
inv.setItem(9 * line + slot, item, handler);
103+
}
104+
}
105+
}
106+
}
107+
}

0 commit comments

Comments
 (0)