DREAMFIRE Docs ← Back to site
Loading...
Searching...
No Matches
SmartInventory.java
Go to the documentation of this file.
1package com.dreamfirestudios.dreamcore.DreamSmartInvs;
2
3import com.dreamfirestudios.dreamcore.DreamCore;
4import com.dreamfirestudios.dreamcore.DreamSmartInvs.content.InventoryContents;
5import com.dreamfirestudios.dreamcore.DreamSmartInvs.content.InventoryProvider;
6import com.dreamfirestudios.dreamcore.DreamSmartInvs.opener.InventoryOpener;
7import org.bukkit.Bukkit;
8import org.bukkit.entity.Player;
9import org.bukkit.event.Event;
10import org.bukkit.event.inventory.InventoryCloseEvent;
11import org.bukkit.event.inventory.InventoryType;
12import org.bukkit.inventory.Inventory;
13
14import java.util.ArrayList;
15import java.util.List;
16import java.util.Optional;
17import java.util.concurrent.CompletableFuture;
18
19public class SmartInventory {
20 private String id;
21 private String title;
22 private InventoryType type;
23 private int rows, columns;
24 private boolean closeable;
25 private List<Integer> clickableTiles = new ArrayList<>();
26 private boolean bottomClickable = false;
27
28 private InventoryProvider provider;
29 private SmartInventory parent;
30
31 private List<InventoryListener<? extends Event>> listeners;
32 private InventoryManager manager;
33
34 private SmartInventory(InventoryManager manager) {
35 this.manager = manager;
36 }
37
38 public CompletableFuture<Inventory> open(Player player) {
39 return open(player, 0);
40 }
41 public CompletableFuture<Inventory> open(Player player, int page) {
42 // Ensure the old inventory closure and cleanup runs first
43 Optional<SmartInventory> oldInv = this.manager.getInventory(player);
44
45 if (oldInv.isPresent()) {
46 SmartInventory inv = oldInv.get();
47 Bukkit.getScheduler().runTask(DreamCore.DreamCore, () -> {
48 inv.getListeners().stream()
49 .filter(listener -> listener.getType() == InventoryCloseEvent.class)
50 .forEach(listener -> ((InventoryListener<InventoryCloseEvent>) listener)
51 .accept(new InventoryCloseEvent(player.getOpenInventory())));
52
53 this.manager.setInventory(player, null); // Clear the current inventory
54 });
55 }
56
57 // Setup the new contents
58 InventoryContents contents = new InventoryContents.Impl(this, player.getUniqueId());
59 contents.pagination().page(page);
60
61 this.manager.setContents(player, contents);
62
63 // Continue with provider initialization and opening the inventory
64 return this.provider.init(player, contents).thenApplyAsync(v -> {
65 try {
66 // Check if contents are consistent
67 if (!this.manager.getContents(player).equals(Optional.of(contents))) {
68 return null;
69 }
70
71 // Open the new inventory
72 InventoryOpener opener = this.manager.findOpener(type)
73 .orElseThrow(() -> new IllegalStateException("No opener found for the inventory type " + type.name()));
74 Inventory handle = opener.open(this, player);
75
76 this.manager.setInventory(player, this); // Set the new inventory
77 return handle;
78 } catch (Exception e) {
79 this.manager.handleInventoryOpenError(this, player, e);
80 return null;
81 }
82 });
83 }
84
85 @SuppressWarnings("unchecked")
86 public void close(Player player) {
87 Bukkit.getScheduler().runTask(DreamCore.DreamCore, () -> {
88 listeners.stream()
89 .filter(listener -> listener.getType() == InventoryCloseEvent.class)
90 .forEach(listener -> ((InventoryListener<InventoryCloseEvent>) listener)
91 .accept(new InventoryCloseEvent(player.getOpenInventory())));
92 this.manager.setInventory(player, null);
93 player.closeInventory();
94 this.manager.setContents(player, null);
95 });
96 }
97
98 public String getId() { return id; }
99 public String getTitle() { return title; }
100 public InventoryType getType() { return type; }
101 public int getRows() { return rows; }
102 public int getColumns() { return columns; }
103 public List<Integer> getClickableTiles(){return clickableTiles;}
104 public boolean getBottomClickable(){return bottomClickable;}
105
106 public boolean isCloseable() { return closeable; }
107 public void setCloseable(boolean closeable) { this.closeable = closeable; }
108
109 public InventoryProvider getProvider() { return provider; }
110 public Optional<SmartInventory> getParent() { return Optional.ofNullable(parent); }
111
112 public InventoryManager getManager() { return manager; }
113
114 List<InventoryListener<? extends Event>> getListeners() { return listeners; }
115
116 public static Builder builder() { return new Builder(); }
117
118 public static final class Builder {
119
120 private String id = "unknown";
121 private String title = "";
122 private InventoryType type = InventoryType.CHEST;
123 private int rows = 6, columns = 9;
124 private boolean closeable = true;
125 private List<Integer> clickableTiles = new ArrayList<>();
126 private boolean bottomClickable = false;
127
128 private InventoryManager manager;
129 private InventoryProvider provider;
130 private SmartInventory parent;
131
132 private List<InventoryListener<? extends Event>> listeners = new ArrayList<>();
133
134 private Builder() {}
135
136 public Builder id(String id) {
137 this.id = id;
138 return this;
139 }
140
141 public Builder addClickable(int slot){
142 clickableTiles.add(slot);
143 return this;
144 }
145
146 public Builder addClickable(List<Integer> slot){
147 clickableTiles.addAll(slot);
148 return this;
149 }
150
151 public Builder bottomClickable(boolean clickable){
152 bottomClickable = clickable;
153 return this;
154 }
155
156 public Builder title(String title) {
157 this.title = title;
158 return this;
159 }
160
161 public Builder type(InventoryType type) {
162 this.type = type;
163 return this;
164 }
165
166 public Builder size(int rows, int columns) {
167 this.rows = rows;
168 this.columns = columns;
169 return this;
170 }
171
172 public Builder closeable(boolean closeable) {
173 this.closeable = closeable;
174 return this;
175 }
176
177 public Builder provider(InventoryProvider provider) {
178 this.provider = provider;
179 return this;
180 }
181
183 this.parent = parent;
184 return this;
185 }
186
188 this.listeners.add(listener);
189 return this;
190 }
191
193 this.manager = manager;
194 return this;
195 }
196
198 if(this.provider == null)
199 throw new IllegalStateException("The provider of the SmartInventory.Builder must be set.");
200
201 InventoryManager manager = this.manager != null ? this.manager : DreamCore.SmartInvsPlugin.manager();
202
203 if(manager == null)
204 throw new IllegalStateException("The manager of the SmartInventory.Builder must be set, "
205 + "or the SmartInvs should be loaded as a plugin.");
206
207 SmartInventory inv = new SmartInventory(manager);
208 inv.id = this.id;
209 inv.title = this.title;
210 inv.type = this.type;
211 inv.rows = this.rows;
212 inv.columns = this.columns;
213 inv.closeable = this.closeable;
214 inv.provider = this.provider;
215 inv.parent = this.parent;
216 inv.listeners = this.listeners;
217 inv.clickableTiles = this.clickableTiles;
218 inv.bottomClickable = this.bottomClickable;
219
220 return inv;
221 }
222 }
223}
static SmartInvsPlugin SmartInvsPlugin
void handleInventoryOpenError(SmartInventory inventory, Player player, Exception exception)
Builder listener(InventoryListener<? extends Event > listener)
CompletableFuture< Inventory > open(Player player, int page)
CompletableFuture< Inventory > open(Player player)