-
Notifications
You must be signed in to change notification settings - Fork 1
/
SoulsInventory.java
110 lines (98 loc) · 3.58 KB
/
SoulsInventory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package com.playmonumenta.libraryofsouls;
import com.goncalomb.bukkit.mylib.utils.CustomInventory;
import com.goncalomb.bukkit.mylib.utils.UtilsMc;
import com.playmonumenta.libraryofsouls.utils.Utils;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
public class SoulsInventory extends CustomInventory {
private List<? extends Soul> mCurrentSlots;
private final List<? extends Soul> mSouls;
private int mOffset;
private boolean mHasPrevPage;
private boolean mHasNextPage;
private final String mTitleModifier;
public SoulsInventory(Player owner, List<? extends Soul> souls, String titleModifier) {
super(owner, 54, "Souls Library" + (titleModifier.isEmpty() ? "" : " " + Utils.hashColor(titleModifier)));
mTitleModifier = titleModifier;
mSouls = souls;
mOffset = 0;
loadWindow();
}
public SoulsInventory(SoulsInventory other, Player owner) {
super(owner, 54, "Souls Library" + (other.mTitleModifier.isEmpty() ? "" : " " + Utils.hashColor(other.mTitleModifier)));
mTitleModifier = other.mTitleModifier;
mSouls = other.mSouls;
mOffset = 0;
loadWindow();
}
private void loadWindow() {
mCurrentSlots = mSouls.subList(mOffset, Math.min(mSouls.size(), mOffset + 36));
for (int i = 0; i < 36; i++) {
if (i < mCurrentSlots.size()) {
_inventory.setItem(i, mCurrentSlots.get(i).getPlaceholder());
} else {
_inventory.setItem(i, null);
}
}
if (mOffset > 0) {
_inventory.setItem(45, UtilsMc.newSingleItemStack(Material.ARROW, "[" + Integer.toString(mOffset / 36) + "] Previous Page"));
mHasPrevPage = true;
} else {
mHasPrevPage = false;
_inventory.setItem(45, null);
}
if (mCurrentSlots.size() >= 36) {
_inventory.setItem(53, UtilsMc.newSingleItemStack(Material.ARROW, "[" + Integer.toString(mOffset / 36) + "] Next Page"));
mHasNextPage = true;
} else {
mHasNextPage = false;
_inventory.setItem(53, null);
}
}
@Override
protected void inventoryClick(final InventoryClickEvent event) {
if (event.getClickedInventory() == null) {
// Player clicked off the screen
return;
} else if ((event.getCursor().getType() != Material.AIR && event.getClickedInventory().equals(getInventory()))
|| (event.isShiftClick() && !event.getClickedInventory().equals(getInventory()))) {
// Attempted to place something in the souls inventory
event.setCancelled(true);
} else if (event.getClickedInventory().equals(getInventory())) {
// Clicked in the SoulsInventory
final int slot = event.getRawSlot();
if (slot >= 0 && slot < 36 && mCurrentSlots.size() > slot) {
if (event.isShiftClick()) {
event.setCurrentItem(mCurrentSlots.get(slot).getBoS());
Bukkit.getScheduler().runTask(getPlugin(), new Runnable() {
@Override
public void run() {
event.setCurrentItem(mCurrentSlots.get(slot).getPlaceholder());
}
});
} else if (event.isRightClick()) {
event.setCancelled(true);
SpawnerInventory.openSpawnerInventory(mCurrentSlots.get(slot), (Player)event.getWhoClicked(), this);
} else {
if (event.getCursor().getType() == Material.AIR) {
event.getView().setCursor(mCurrentSlots.get(slot).getBoS());
}
event.setCancelled(true);
}
} else if (slot == 45 && mHasPrevPage) {
mOffset -= 36;
loadWindow();
event.setCancelled(true);
} else if (slot == 53 && mHasNextPage) {
mOffset += 36;
loadWindow();
event.setCancelled(true);
} else if (event.getCursor().getType() == Material.AIR) {
event.setCancelled(true);
}
}
}
}