-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathRanksGui.java
More file actions
127 lines (107 loc) · 4.46 KB
/
RanksGui.java
File metadata and controls
127 lines (107 loc) · 4.46 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package sh.okx.rankup.ranksgui;
import java.util.function.BiFunction;
import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import sh.okx.rankup.RankupPlugin;
import sh.okx.rankup.gui.Gui;
import sh.okx.rankup.ranks.Rank;
import sh.okx.rankup.ranks.RankElement;
import sh.okx.rankup.util.Colour;
import sh.okx.rankup.util.folia.FoliaScheduler;
public class RanksGui {
private final RankupPlugin plugin;
@Getter
private final Player player;
private int rankupSlot;
@Getter
private Inventory inventory;
public RanksGui(RankupPlugin plugin, Player player) {
this.plugin = plugin;
this.player = player;
}
public void open() {
RankElement<Rank> playerRankElement = plugin.getRankups().getByPlayer(player);
ConfigurationSection playerPath = playerRankElement == null ? null : plugin.getSection(playerRankElement.getRank(), "rankup.ranksgui");
ConfigurationSection basePath = plugin.getMessages().getConfigurationSection("rankup.ranksgui");
String title = get(ConfigurationSection::getString, "title", playerPath, basePath, "Ranks");
int rows = get(Gui::getInt, "rows", playerPath, basePath, 3);
int offset = get(Gui::getInt, "offset", playerPath, basePath, 10);
int width = get(Gui::getInt, "width", playerPath, basePath, 7);
inventory = Bukkit.createInventory(null, rows * 9, Colour.translate(title));
ItemStack fill = get((section, path) -> Gui.getItem(plugin, section.getConfigurationSection(path), player, playerRankElement), "fill", playerPath, basePath, null);
int index = offset;
int rowIndex = offset + width;
RankElement<Rank> rankElement = plugin.getRankups().getTree().getFirst();
boolean complete = playerRankElement != null;
while(rankElement.hasNext()) {
ConfigurationSection rankPath = plugin.getSection(rankElement.getRank(), "rankup.ranksgui");
String path;
if (rankElement == playerRankElement) {
path = "current";
complete = false;
rankupSlot = index;
} else if (complete) {
path = "complete";
} else {
path = "incomplete";
}
RankElement<Rank> rankElement0 = rankElement;
ItemStack item = get((section, path0) -> Gui.getItem(plugin, section.getConfigurationSection(path0), player, rankElement0), path, rankPath, basePath, null);
inventory.setItem(index++, item);
if (index > rows * 9) {
throw new IllegalArgumentException("Ranks GUI is too small for the number of ranks. Increase the number of rows on the ranks GUI.");
}
if (index == rowIndex) {
rowIndex += 9;
index += 9 - width;
}
rankElement = rankElement.getNext();
}
if (fill != null) {
for (int i = 0; i < rows * 9; i++) {
ItemStack item = inventory.getItem(i);
if (item == null) {
inventory.setItem(i, fill);
}
}
}
player.openInventory(inventory);
}
private <T> T get(BiFunction<ConfigurationSection, String, T> fun, String path, ConfigurationSection primary, ConfigurationSection secondary, T def) {
T get = null;
if (primary != null) {
get = fun.apply(primary, path);
}
if (get != null) {
return get;
}
if (secondary != null) {
get = fun.apply(secondary, path);
}
if (get != null) {
return get;
}
return def;
}
public void click(InventoryClickEvent event) {
if (event.getClickedInventory() != event.getInventory()) {
return;
}
int slot = event.getRawSlot();
if (slot == rankupSlot) {
final Runnable runnable = () -> {
player.closeInventory();
Bukkit.dispatchCommand(player, "rankup gui");
};
if (FoliaScheduler.isFolia()) FoliaScheduler.getEntityScheduler().run(player, plugin, $ -> runnable.run(), null);
else Bukkit.getScheduler().runTask(plugin, runnable);
}
}
public void close() {
}
}