-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathSignGUIAction.java
More file actions
230 lines (193 loc) · 8.48 KB
/
SignGUIAction.java
File metadata and controls
230 lines (193 loc) · 8.48 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package de.rapha149.signgui;
import de.rapha149.signgui.scheduler.SchedulerAdapterFactory;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.Arrays;
/**
* An interface used for handling the action after the player finished editing the sign.
*/
public interface SignGUIAction {
/**
* @return The {@link de.rapha149.signgui.SignGUIAction.SignGUIActionInfo} instance containing information about this action
*/
SignGUIActionInfo getInfo();
/**
* Called to execute the actions after the player finished editing the sign.
*
* @param gui The {@link de.rapha149.signgui.SignGUI} instance
* @param signEditor The {@link de.rapha149.signgui.SignEditor} instance containing information relevant to the {@link de.rapha149.signgui.version.VersionWrapper}
* @param player The player who edited the sign
*/
void execute(SignGUI gui, SignEditor signEditor, Player player);
/**
* Creates a new SignGUIAction that opens the sign gui again with the new lines.
*
* @param lines The new lines, may be less then 4
* @return The new {@link de.rapha149.signgui.SignGUIAction} instance
* @throws IllegalArgumentException If lines is null.
*/
static SignGUIAction displayNewLines(String... lines) {
Validate.notNull(lines, "The lines cannot be null");
return new SignGUIAction() {
private SignGUIActionInfo info = new SignGUIActionInfo("displayNewLines", true, 1);
@Override
public SignGUIActionInfo getInfo() {
return info;
}
@Override
public void execute(SignGUI gui, SignEditor signEditor, Player player) {
gui.displayNewLines(player, signEditor, Arrays.copyOf(lines, 4), null);
}
};
}
/**
* Creates a new SignGUIAction that opens the sign gui again with the new lines using the Adventure component (1.20.5+).
* Lines set using this method are only shown when using a mojang-mapped Paper plugin.
* If you want to set fallback lines to use when Adventure components cannot be used, use {@link #displayNewAdventureLines(Object[], String[])}.
* Please note that if you use this method and the Adventure components cannot be used, the sign will be empty.
*
* @param adventureLines The new adventure lines, may be less then 4
* @return The new {@link de.rapha149.signgui.SignGUIAction} instance
* @throws IllegalArgumentException If adventure lines is null.
* @see #displayNewAdventureLines(Object[], String[])
*/
static SignGUIAction displayNewAdventureLines(Object... adventureLines) {
return displayNewAdventureLines(adventureLines, null);
}
/**
* Creates a new SignGUIAction that opens the sign gui again with the new lines using the Adventure component (1.20.5+).
* Lines set using this method are only shown when using a mojang-mapped Paper plugin.
* Please note that if you use don't submit fallback lines'and the Adventure components cannot be used, the sign will be empty.
*
* @param adventureLines The new lines, may be less then 4
* @param fallbackLines The fallback lines, may be less then 4. These are used when the Adventure components cannot be used. May be null.
* @return The new {@link de.rapha149.signgui.SignGUIAction} instance
* @throws IllegalArgumentException If adventure lines is null.
* @see #displayNewLines(String...)
*/
static SignGUIAction displayNewAdventureLines(Object[] adventureLines, String[] fallbackLines) {
Validate.notNull(adventureLines, "The lines cannot be null");
return new SignGUIAction() {
private SignGUIActionInfo info = new SignGUIActionInfo("displayNewLines", true, 1);
@Override
public SignGUIActionInfo getInfo() {
return info;
}
@Override
public void execute(SignGUI gui, SignEditor signEditor, Player player) {
gui.displayNewLines(player, signEditor, fallbackLines != null ? Arrays.copyOf(fallbackLines, 4) : new String[4],
Arrays.copyOf(adventureLines, 4));
}
};
}
/**
* Creates a new SignGUIAction that opens an inventory.
* The inventory is opened synchronously by calling the method {@link org.bukkit.scheduler.BukkitScheduler#runTask(org.bukkit.plugin.Plugin, Runnable)}
*
* @param plugin Your {@link org.bukkit.plugin.java.JavaPlugin} instance
* @param inventory The inventory to open
* @return The new {@link de.rapha149.signgui.SignGUIAction} instance
*/
static SignGUIAction openInventory(JavaPlugin plugin, Inventory inventory) {
Validate.notNull(plugin, "The plugin cannot be null");
Validate.notNull(inventory, "The inventory cannot be null");
return new SignGUIAction() {
private SignGUIActionInfo info = new SignGUIActionInfo("openInventory", false, 1);
@Override
public SignGUIActionInfo getInfo() {
return info;
}
@Override
public void execute(SignGUI gui, SignEditor signEditor, Player player) {
SchedulerAdapterFactory.getScheduler().runAtEntity(plugin, player, () -> player.openInventory(inventory), null);
}
};
}
/**
* Creates a new SignGUIAction that runs a runnable.
* The runnable will be run asynchronously.
*
* @param runnable The runnable to run
* @return The new {@link de.rapha149.signgui.SignGUIAction} instance
*/
static SignGUIAction run(Runnable runnable) {
Validate.notNull(runnable, "The runnable cannot be null");
return new SignGUIAction() {
private SignGUIActionInfo info = new SignGUIActionInfo("run", false, 0);
@Override
public SignGUIActionInfo getInfo() {
return info;
}
@Override
public void execute(SignGUI gui, SignEditor signEditor, Player player) {
runnable.run();
}
};
}
/**
* Creates a new SignGUIAction that runs a runnable synchronously.
*
* @param plugin Your {@link org.bukkit.plugin.java.JavaPlugin} instance
* @param runnable The runnable to run
* @return The new {@link de.rapha149.signgui.SignGUIAction} instance
*/
static SignGUIAction runSync(JavaPlugin plugin, Runnable runnable) {
Validate.notNull(plugin, "The plugin cannot be null");
Validate.notNull(runnable, "The runnable cannot be null");
return new SignGUIAction() {
private SignGUIActionInfo info = new SignGUIActionInfo("runSync", false, 0);
@Override
public SignGUIActionInfo getInfo() {
return info;
}
@Override
public void execute(SignGUI gui, SignEditor signEditor, Player player) {
SchedulerAdapterFactory.getScheduler().runAtEntity(plugin, player, runnable, null);
}
};
}
/**
* Describes a {@link de.rapha149.signgui.SignGUIAction}
*/
class SignGUIActionInfo {
private final String name;
private final boolean keepOpen;
private final int conflicting;
/**
* Creates a new SignGUIActionInfo.
*
* @param name The name of the action
* @param keepOpen Whether the sign gui should be kept open
* @param conflicting The conflicting int
*/
public SignGUIActionInfo(String name, boolean keepOpen, int conflicting) {
this.name = name;
this.keepOpen = keepOpen;
this.conflicting = conflicting;
}
/**
* @return The name of the action
*/
public String getName() {
return name;
}
/**
* @return Whether the sign gui should be kept open.
*/
public boolean isKeepOpen() {
return keepOpen;
}
/**
* Checks whether the result is conflicting with another result.
*
* @param other The conflicting int of the other result
* @return Whether the result is conflicting with the other result
*/
public boolean isConflicting(SignGUIActionInfo other) {
return (conflicting & other.conflicting) != 0;
}
}
}