-
Notifications
You must be signed in to change notification settings - Fork 0
/
GUIFrame.java
146 lines (114 loc) · 4.47 KB
/
GUIFrame.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package fi.natroutter.natlibs.handlers.guibuilder;
import fi.natroutter.natlibs.config.IConfig;
import fi.natroutter.natlibs.objects.BaseItem;
import fi.natroutter.natlibs.utilities.Colors;
import fi.natroutter.natlibs.utilities.Utilities;
import lombok.Getter;
import lombok.Setter;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import java.util.List;
public abstract class GUIFrame {
@Getter
private String rawTitle;
@Getter
private Rows rows;
@Getter
private String strippedTitle;
@Getter @Setter
private SoundSettings openSound;
@Getter @Setter
private SoundSettings closeSound;
@Getter @Setter
private SoundSettings clickSound;
@Setter @Getter
private BaseItem filler;
@Setter @Getter
private int maxPages = 0;
private PlainTextComponentSerializer plain = PlainTextComponentSerializer.plainText();
public GUIFrame(String title, Rows rows) {
this.rawTitle = title;
this.rows = rows;
this.strippedTitle = plain.serialize(Colors.translate(title));
openSound = new SoundSettings(Sound.BLOCK_CHEST_OPEN, 100, 1);
closeSound = new SoundSettings(Sound.BLOCK_CHEST_CLOSE, 100, 1);
clickSound = new SoundSettings(Sound.UI_BUTTON_CLICK, 100, 1);
filler = new BaseItem(Material.BLACK_STAINED_GLASS_PANE).name(" ");
}
public GUIFrame(IConfig title, Rows rows) {
this(title.asString(), rows);
}
protected abstract boolean onShow(Player player, GUI gui, List<Object> args);
public void onClose(Player player, GUI gui){}
public void setCloseSound(Sound sound, float volume, float pitch) {
this.closeSound = new SoundSettings(sound, volume, pitch);
}
public void setOpenSound(Sound sound, float volume, float pitch) {
this.openSound = new SoundSettings(sound, volume, pitch);
}
public void setClickSound(Sound sound, float volume, float pitch) {
this.clickSound = new SoundSettings(sound, volume, pitch);
}
public boolean hasOpen(Player p) {
return GUIListener.guis.containsKey(p.getUniqueId());
}
protected void update(Player p, List<Object> args, boolean sound) {
GUI gui = GUIListener.guis.get(p.getUniqueId());
if (gui != null) {
show(p, gui, args,sound);
}
}
public void switchGUI(Player p, GUIFrame frame) {
switchGUI(p, frame, null);
}
public void switchGUI(Player p, GUIFrame frame, List<Object> args) {
GUI gui = GUIListener.guis.get(p.getUniqueId());
if (gui != null) {
gui.closing = true;
frame.show(p, args, false);
}
}
public void show(Player p) {show(p,null,true);}
public void show(Player p, List<Object> args) {show(p,args,true);}
protected void show(Player p, List<Object> args, boolean sound) {
show(p, new GUI(this), args, sound);
}
protected void show(Player p, GUI gui, List<Object> args, boolean sound) {
GUIListener.guis.put(p.getUniqueId(), gui);
GUIListener.args.put(p.getUniqueId(), args);
Inventory inv = gui.getInv();
inv.clear();
if (!onShow(p, gui, args)) {
gui.closing = true;
GUIListener.guis.remove(p.getUniqueId());
GUIListener.args.remove(p.getUniqueId());
p.closeInventory();
return;
}
if (sound){
if (openSound != null) {
if (openSound.isEnumSound()) {
p.playSound(p.getLocation(), openSound.getSound(), openSound.getVolume(), openSound.getPitch());
} else {
p.playSound(p.getLocation(), openSound.getStrSound(), openSound.getVolume(), openSound.getPitch());
}
}
}
GUIListener.guis.put(p.getUniqueId(), gui);
Inventory privInv = Bukkit.createInventory(p.getInventory().getHolder(), inv.getSize(), gui.getTitle());
if (filler != null) {
for (int i = 0; i < inv.getSize(); i++) {
if (inv.getItem(i) == null || inv.getItem(i).getType() == Material.AIR) {
inv.setItem(i, filler);
}
}
}
privInv.setContents(gui.getInv().getContents().clone());
p.openInventory(privInv);
}
}