DREAMFIRE Docs ← Back to site
Loading...
Searching...
No Matches
InventoryManager.java
Go to the documentation of this file.
1package com.dreamfirestudios.dreamcore.DreamSmartInvs;
2
3import com.dreamfirestudios.dreamcore.DreamSmartInvs.content.InventoryContents;
4import com.dreamfirestudios.dreamcore.DreamSmartInvs.opener.ChestInventoryOpener;
5import com.dreamfirestudios.dreamcore.DreamSmartInvs.opener.InventoryOpener;
6import com.dreamfirestudios.dreamcore.DreamSmartInvs.opener.SpecialInventoryOpener;
7import net.kyori.adventure.text.format.NamedTextColor;
8import org.bukkit.Bukkit;
9import org.bukkit.ChatColor;
10import org.bukkit.entity.Player;
11import org.bukkit.event.EventHandler;
12import org.bukkit.event.EventPriority;
13import org.bukkit.event.Listener;
14import org.bukkit.event.inventory.*;
15import org.bukkit.event.player.PlayerQuitEvent;
16import org.bukkit.event.server.PluginDisableEvent;
17import org.bukkit.inventory.Inventory;
18import org.bukkit.plugin.PluginManager;
19import org.bukkit.plugin.java.JavaPlugin;
20import org.bukkit.scheduler.BukkitRunnable;
21
22import java.util.*;
23import java.util.logging.Level;
24
25public class InventoryManager {
26 private JavaPlugin plugin;
27 private PluginManager pluginManager;
28
29 private Map<UUID, SmartInventory> inventories;
30 private Map<UUID, InventoryContents> contents;
31
32 private List<InventoryOpener> defaultOpeners;
33 private List<InventoryOpener> openers;
34
35 public InventoryManager(JavaPlugin plugin) {
36 this.plugin = plugin;
37 this.pluginManager = Bukkit.getPluginManager();
38
39 this.inventories = new HashMap<>();
40 this.contents = new HashMap<>();
41
42 this.defaultOpeners = Arrays.asList(
43 new ChestInventoryOpener(),
44 new SpecialInventoryOpener()
45 );
46
47 this.openers = new ArrayList<>();
48 }
49
50 public void init() {
51 pluginManager.registerEvents(new InvListener(), plugin);
52
53 new InvTask().runTaskTimer(plugin, 1, 1);
54 }
55
56 public Optional<InventoryOpener> findOpener(InventoryType type) {
57 Optional<InventoryOpener> opInv = this.openers.stream()
58 .filter(opener -> opener.supports(type))
59 .findAny();
60
61 if (!opInv.isPresent()) {
62 opInv = this.defaultOpeners.stream()
63 .filter(opener -> opener.supports(type))
64 .findAny();
65 }
66
67 return opInv;
68 }
69
70 public void registerOpeners(InventoryOpener... openers) {
71 this.openers.addAll(Arrays.asList(openers));
72 }
73
74 public List<Player> getOpenedPlayers(SmartInventory inv) {
75 List<Player> list = new ArrayList<>();
76
77 this.inventories.forEach((player, playerInv) -> {
78 if (inv.equals(playerInv))
79 list.add(Bukkit.getPlayer(player));
80 });
81
82 return list;
83 }
84
85 public Optional<SmartInventory> getInventory(Player p) {
86 return Optional.ofNullable(this.inventories.get(p.getUniqueId()));
87 }
88
89 protected void setInventory(Player p, SmartInventory inv) {
90 if (inv == null) {
91 this.inventories.remove(p.getUniqueId());
92 }
93 else{
94 this.inventories.put(p.getUniqueId(), inv);
95 }
96
97 }
98
99 public Optional<InventoryContents> getContents(Player p) {
100 return Optional.ofNullable(this.contents.get(p.getUniqueId()));
101 }
102
103 protected void setContents(Player p, InventoryContents contents) {
104 if (contents == null)
105 this.contents.remove(p.getUniqueId());
106 else
107 this.contents.put(p.getUniqueId(), contents);
108 }
109
110 public void handleInventoryOpenError(SmartInventory inventory, Player player, Exception exception) {
111 inventory.close(player);
112
113 Bukkit.getLogger().log(Level.SEVERE, "Error while opening SmartInventory:", exception);
114 }
115
116 public void handleInventoryUpdateError(SmartInventory inventory, Player player, Exception exception) {
117 inventory.close(player);
118
119 Bukkit.getLogger().log(Level.SEVERE, "Error while updating SmartInventory:", exception);
120 }
121
122 @SuppressWarnings("unchecked")
123 class InvListener implements Listener {
124
125 @EventHandler(priority = EventPriority.LOW)
126 public void onInventoryClick(InventoryClickEvent e) {
127 Player p = (Player) e.getWhoClicked();
128
129 Bukkit.getConsoleSender().sendMessage(NamedTextColor.RED + "looking for inventory");
130
131 if (!inventories.containsKey(p.getUniqueId()))
132 return;
133
134 Inventory clickedInventory = e.getClickedInventory();
135 SmartInventory smartInventory = inventories.get(p.getUniqueId());
136
137 // Restrict putting items from the bottom inventory into the top inventory
138
139 Bukkit.getConsoleSender().sendMessage(NamedTextColor.RED + "checking is bottom clickable");
140 if(!smartInventory.getBottomClickable()){
141 Bukkit.getConsoleSender().sendMessage(NamedTextColor.RED + "checking Is bottom ivnentory");
142 if (clickedInventory == p.getOpenInventory().getBottomInventory()) {
143 Bukkit.getConsoleSender().sendMessage(NamedTextColor.RED + "Is bottom ivnentory");
144 if (e.getAction() == InventoryAction.COLLECT_TO_CURSOR || e.getAction() == InventoryAction.MOVE_TO_OTHER_INVENTORY) {
145 int slot = e.getSlot();
146 if(!smartInventory.getClickableTiles().contains(slot)) e.setCancelled(true);
147 return;
148 }
149
150 if (e.getAction() == InventoryAction.NOTHING && e.getClick() != ClickType.MIDDLE) {
151 e.setCancelled(true);
152 return;
153 }
154 }
155 }
156
157 Bukkit.getConsoleSender().sendMessage(NamedTextColor.RED + "checking top ivnentory");
158 if (clickedInventory == p.getOpenInventory().getTopInventory()) {
159 int slot = e.getSlot();
160 Bukkit.getConsoleSender().sendMessage(NamedTextColor.RED + "Is top ivnentory");
161 if(!smartInventory.getClickableTiles().contains(slot)){
162 e.setCancelled(true);
163
164 int row = e.getSlot() / 9;
165 int column = e.getSlot() % 9;
166
167 if (row < 0 || column < 0)
168 return;
169
170 SmartInventory inv = inventories.get(p.getUniqueId());
171
172 if (row >= inv.getRows() || column >= inv.getColumns())
173 return;
174
175 Bukkit.getConsoleSender().sendMessage(NamedTextColor.RED + "FIRE CLICK");
176 inv.getListeners().stream()
177 .filter(listener -> listener.getType() == InventoryClickEvent.class)
178 .forEach(listener -> ((InventoryListener<InventoryClickEvent>) listener).accept(e));
179
180 contents.get(p.getUniqueId()).get(row, column).ifPresent(item -> item.run(e));
181
182 p.updateInventory();
183 }
184 }
185 }
186
187 @EventHandler(priority = EventPriority.LOW)
188 public void onInventoryDrag(InventoryDragEvent e) {
189 Player p = (Player) e.getWhoClicked();
190
191 if (!inventories.containsKey(p.getUniqueId()))
192 return;
193
194 SmartInventory smartInventory = inventories.get(p.getUniqueId());
195
196 for (int slot : e.getRawSlots()) {
197 if (slot >= p.getOpenInventory().getTopInventory().getSize())
198 continue;
199
200 if(!smartInventory.getClickableTiles().contains(slot)) e.setCancelled(true);
201 break;
202 }
203
204 smartInventory.getListeners().stream()
205 .filter(listener -> listener.getType() == InventoryDragEvent.class)
206 .forEach(listener -> ((InventoryListener<InventoryDragEvent>) listener).accept(e));
207 }
208
209 @EventHandler(priority = EventPriority.LOW)
210 public void onInventoryOpen(InventoryOpenEvent e) {
211 Player p = (Player) e.getPlayer();
212
213 if (!inventories.containsKey(p.getUniqueId()))
214 return;
215
216 SmartInventory inv = inventories.get(p.getUniqueId());
217
218 inv.getListeners().stream()
219 .filter(listener -> listener.getType() == InventoryOpenEvent.class)
220 .forEach(listener -> ((InventoryListener<InventoryOpenEvent>) listener).accept(e));
221 }
222
223 @EventHandler(priority = EventPriority.LOW)
224 public void onInventoryClose(InventoryCloseEvent e) {
225 Player p = (Player) e.getPlayer();
226
227 if (!inventories.containsKey(p.getUniqueId()))
228 return;
229
230 SmartInventory inv = inventories.get(p.getUniqueId());
231
232 inv.getListeners().stream()
233 .filter(listener -> listener.getType() == InventoryCloseEvent.class)
234 .forEach(listener -> ((InventoryListener<InventoryCloseEvent>) listener).accept(e));
235
236 inv.getProvider().closeinventory(p, contents.get(p.getUniqueId()), e.getView().getTopInventory());
237
238 if (inv.isCloseable()) {
239 e.getInventory().clear();
240 inventories.remove(p.getUniqueId());
241 contents.remove(p.getUniqueId());
242 } else
243 Bukkit.getScheduler().runTask(plugin, () -> p.openInventory(e.getInventory()));
244 }
245
246 @EventHandler(priority = EventPriority.LOW)
247 public void onPlayerQuit(PlayerQuitEvent e) {
248 Player p = e.getPlayer();
249
250 if (!inventories.containsKey(p.getUniqueId()))
251 return;
252
253 SmartInventory inv = inventories.get(p.getUniqueId());
254
255 inv.getListeners().stream()
256 .filter(listener -> listener.getType() == PlayerQuitEvent.class)
257 .forEach(listener -> ((InventoryListener<PlayerQuitEvent>) listener).accept(e));
258
259 inventories.remove(p.getUniqueId());
260 contents.remove(p.getUniqueId());
261 }
262
263 @EventHandler(priority = EventPriority.LOW)
264 public void onPluginDisable(PluginDisableEvent e) {
265 new HashMap<>(inventories).forEach((player, inv) -> {
266 inv.getListeners().stream()
267 .filter(listener -> listener.getType() == PluginDisableEvent.class)
268 .forEach(listener -> ((InventoryListener<PluginDisableEvent>) listener).accept(e));
269
270 inv.close(Bukkit.getPlayer(player));
271 });
272
273 inventories.clear();
274 contents.clear();
275 }
276
277 }
278
279 class InvTask extends BukkitRunnable {
280
281 @Override
282 public void run() {
283 new HashMap<>(inventories).forEach((uuid, inv) -> {
284 Player player = Bukkit.getPlayer(uuid);
285
286 try {
287 inv.getProvider().update(player, contents.get(uuid));
288 } catch (Exception e) {
289 handleInventoryUpdateError(inv, player, e);
290 }
291 });
292 }
293
294 }
295}
void handleInventoryUpdateError(SmartInventory inventory, Player player, Exception exception)
void handleInventoryOpenError(SmartInventory inventory, Player player, Exception exception)
Optional< InventoryOpener > findOpener(InventoryType type)