-
-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathAOneBlockPlaceholders.java
More file actions
337 lines (301 loc) · 13.4 KB
/
AOneBlockPlaceholders.java
File metadata and controls
337 lines (301 loc) · 13.4 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package world.bentobox.aoneblock;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.TreeMap;
import java.util.stream.Collectors;
import org.bukkit.Material;
import world.bentobox.aoneblock.dataobjects.OneBlockIslands;
import world.bentobox.aoneblock.panels.PhasesPanel;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.hooks.LangUtilsHook;
import world.bentobox.bentobox.util.Util;
public class AOneBlockPlaceholders {
private static final TreeMap<Double, String> SCALE;
private static final String INFINITE = "aoneblock.placeholders.infinite";
static {
SCALE = new TreeMap<>();
SCALE.put(0D, "&c╍╍╍╍╍╍╍╍");
SCALE.put(12.5, "&a╍&c╍╍╍╍╍╍╍");
SCALE.put(25.0, "&a╍╍&c╍╍╍╍╍╍");
SCALE.put(37.5, "&a╍╍╍&c╍╍╍╍╍");
SCALE.put(50D, "&a╍╍╍╍&c╍╍╍╍");
SCALE.put(62.5, "&a╍╍╍╍╍&c╍╍╍");
SCALE.put(75.0, "&a╍╍╍╍╍╍&c╍╍");
SCALE.put(87.5, "&a╍╍╍╍╍╍╍&c╍");
SCALE.put(100D, "&a╍╍╍╍╍╍╍╍");
}
private final AOneBlock addon;
public AOneBlockPlaceholders(AOneBlock addon,
world.bentobox.bentobox.managers.PlaceholdersManager placeholdersManager) {
this.addon = addon;
placeholdersManager.registerPlaceholder(addon, "visited_island_phase", this::getPhaseByLocation);
placeholdersManager.registerPlaceholder(addon, "visited_island_count", this::getCountByLocation);
placeholdersManager.registerPlaceholder(addon, "my_island_phase", this::getPhase);
placeholdersManager.registerPlaceholder(addon, "my_island_count", this::getCount);
placeholdersManager.registerPlaceholder(addon, "visited_island_next_phase", this::getNextPhaseByLocation);
placeholdersManager.registerPlaceholder(addon, "my_island_next_phase", this::getNextPhase);
placeholdersManager.registerPlaceholder(addon, "my_island_blocks_for_phase", this::getPhaseBlocks);
placeholdersManager.registerPlaceholder(addon, "my_island_blocks_to_next_phase", this::getNextPhaseBlocks);
placeholdersManager.registerPlaceholder(addon, "visited_island_blocks_to_next_phase",
this::getNextPhaseBlocksByLocation);
placeholdersManager.registerPlaceholder(addon, "my_island_percent_done", this::getPercentDone);
placeholdersManager.registerPlaceholder(addon, "visited_island_percent_done", this::getPercentDoneByLocation);
placeholdersManager.registerPlaceholder(addon, "my_island_done_scale", this::getDoneScale);
placeholdersManager.registerPlaceholder(addon, "visited_island_done_scale", this::getDoneScaleByLocation);
// Since 1.10
placeholdersManager.registerPlaceholder(addon, "visited_island_lifetime_count", this::getLifetimeByLocation);
placeholdersManager.registerPlaceholder(addon, "my_island_lifetime_count", this::getLifetime);
placeholdersManager.registerPlaceholder(addon, "visited_island_phase_block_list",
this::getPhaseBlocksNamesByLocation);
placeholdersManager.registerPlaceholder(addon, "my_island_phase_block_list", this::getPhaseBlocksNames);
}
/**
* Get the user's island. Will get either the user's active island, or the island they own.
* If they own more than one, then one owned island is picked.
* @param user user
* @return island
*/
private Optional<Island> getUsersIsland(User user) {
// Get the active island for the user
Island i = addon.getIslands().getIsland(addon.getOverWorld(), user);
if (i != null && i.getOwner() != null && user.getUniqueId().equals(i.getOwner())) {
// Owner of this island and currently on this island
return Optional.ofNullable(i);
}
// Check for other owned islands
List<Island> ownedIslands = addon.getIslands().getIslands(addon.getOverWorld(), user).stream()
.filter(is -> user.getUniqueId().equals(is.getOwner())).toList();
if (ownedIslands.size() == 1) {
// Replace with the owned island
i = ownedIslands.getFirst(); // pick one
}
// Return what we have found
return Optional.ofNullable(i);
}
public String getPhaseBlocksNames(User user) {
if (user == null || user.getUniqueId() == null)
return "";
return getUsersIsland(user).map(i -> getPhaseBlocksForIsland(user, i)).orElse("");
}
private String getPhaseBlocksForIsland(User user, Island i) {
String phaseName = addon.getOneBlocksIsland(i).getPhaseName();
Set<Material> set = addon.getOneBlockManager().getPhase(phaseName).map(phase -> phase.getBlocks().keySet())
.orElse(null);
if (set == null) {
return "";
}
String result = set.stream().map(m -> getMaterialName(user, m))
.map(string -> user.getTranslation(PhasesPanel.REFERENCE + "blocks", TextVariables.NAME,
string))
.collect(Collectors.joining());
// Removing the last newline character or comma if it exists
result = result.trim();
if (result.endsWith("\n") || result.endsWith(",")) {
result = result.substring(0, result.length() - 1);
}
return result;
}
private String getMaterialName(User user, Material m) {
return addon.getPlugin().getHooks().getHook("LangUtils").map(hook -> LangUtilsHook.getMaterialName(m, user))
.orElse(Util.prettifyText(m.name()));
}
public String getPhaseBlocksNamesByLocation(User user) {
if (user == null || user.getUniqueId() == null || !addon.inWorld(user.getWorld()))
return "";
return addon.getIslands().getIslandAt(user.getLocation()).map(i -> getPhaseBlocksForIsland(user, i)).orElse("");
}
/**
* Get phase by location of user
*
* @param user - user
* @return Phase name
*/
public String getPhaseByLocation(User user) {
if (user == null || user.getUniqueId() == null)
return "";
return addon.getIslands().getProtectedIslandAt(Objects.requireNonNull(user.getLocation()))
.map(addon::getOneBlocksIsland).map(OneBlockIslands::getPhaseName).orElse("");
}
/**
* Get block count by user location
*
* @param user - user
* @return String of count
*/
public String getCountByLocation(User user) {
if (user == null || user.getUniqueId() == null || !addon.inWorld(user.getWorld()))
return "";
return addon.getIslands().getProtectedIslandAt(Objects.requireNonNull(user.getLocation()))
.map(addon::getOneBlocksIsland).map(OneBlockIslands::getBlockNumber).map(String::valueOf).orElse("");
}
/**
* Get user's island phase
*
* @param user - island owner or team member
* @return phase name
*/
public String getPhase(User user) {
if (user == null || user.getUniqueId() == null)
return "";
return getUsersIsland(user).map(i -> addon.getOneBlocksIsland(i).getPhaseName()).orElse("");
}
/**
* Get island block count
*
* @param user island owner or team member
* @return string of block count
*/
public String getCount(User user) {
if (user == null || user.getUniqueId() == null)
return "";
return getUsersIsland(user).map(i -> String.valueOf(addon.getOneBlocksIsland(i).getBlockNumber())).orElse("");
}
/**
* Get the next phase based on user's location
*
* @param user - user
* @return next phase
*/
public String getNextPhaseByLocation(User user) {
if (user == null || user.getUniqueId() == null || !addon.inWorld(user.getWorld()))
return "";
return addon.getIslands().getProtectedIslandAt(Objects.requireNonNull(user.getLocation()))
.map(addon::getOneBlocksIsland).map(addon.getOneBlockManager()::getNextPhase).orElse("");
}
/**
* Get next island phase
*
* @param user island owner or team member
* @return next island phase
*/
public String getNextPhase(User user) {
if (user == null || user.getUniqueId() == null)
return "";
return getUsersIsland(user).map(i -> addon.getOneBlockManager().getNextPhase(addon.getOneBlocksIsland(i)))
.orElse("");
}
/**
* Get how many blocks until next phase based on user's location
*
* @param user user
* @return string number of blocks
*/
public String getNextPhaseBlocksByLocation(User user) {
if (user == null || user.getUniqueId() == null || !addon.inWorld(user.getWorld()))
return "";
return addon.getIslands().getProtectedIslandAt(Objects.requireNonNull(user.getLocation()))
.map(addon::getOneBlocksIsland).map(addon.getOneBlockManager()::getNextPhaseBlocks)
.map(num -> num < 0 ? user.getTranslation(INFINITE) : String.valueOf(num)).orElse("");
}
/**
* Get how many blocks until the next island phase
*
* @param user owner or team member
* @return string number of blocks
*/
public String getNextPhaseBlocks(User user) {
if (user == null || user.getUniqueId() == null)
return "";
return getUsersIsland(user).map(i -> {
int num = addon.getOneBlockManager().getNextPhaseBlocks(addon.getOneBlocksIsland(i));
return num < 0 ? user.getTranslation(INFINITE) : String.valueOf(num);
}).orElse("");
}
/**
* Get how many blocks for this phase
*
* @param user owner or team member
* @return string number of blocks
*/
public String getPhaseBlocks(User user) {
if (user == null || user.getUniqueId() == null)
return "";
return getUsersIsland(user).map(i -> {
int num = addon.getOneBlockManager().getPhaseBlocks(addon.getOneBlocksIsland(i));
return num < 0 ? user.getTranslation(INFINITE) : String.valueOf(num);
}).orElse("");
}
/**
* Get percentage done of current phase by user's location
*
* @param user - user
* @return string percentage
*/
public String getPercentDoneByLocation(User user) {
if (user == null || user.getUniqueId() == null || !addon.inWorld(user.getWorld()))
return "";
return addon.getIslands().getProtectedIslandAt(Objects.requireNonNull(user.getLocation()))
.map(addon::getOneBlocksIsland).map(addon.getOneBlockManager()::getPercentageDone)
.map(num -> Math.round(num) + "%").orElse("");
}
/**
* Get percentage done of user's island phase
*
* @param user owner or team member
* @return string percentage
*/
public String getPercentDone(User user) {
if (user == null || user.getUniqueId() == null)
return "";
return getUsersIsland(user).map(i -> {
double num = addon.getOneBlockManager().getPercentageDone(addon.getOneBlocksIsland(i));
return Math.round(num) + "%";
}).orElse("");
}
/**
* Get percentage done of phase as colored scale based on user's location
*
* @param user user
* @return colored scale
*/
public String getDoneScaleByLocation(User user) {
if (user == null || user.getUniqueId() == null || !addon.inWorld(user.getWorld()))
return "";
return addon.getIslands().getProtectedIslandAt(Objects.requireNonNull(user.getLocation()))
.map(addon::getOneBlocksIsland).map(addon.getOneBlockManager()::getPercentageDone)
.map(num -> SCALE.floorEntry(num).getValue())
.map(s -> s.replace("╍", addon.getSettings().getPercentCompleteSymbol())).orElse("");
}
/**
* Get percentage done of phase as colored scale
*
* @param user owner or team member
* @return colored scale
*/
public String getDoneScale(User user) {
if (user == null || user.getUniqueId() == null)
return "";
return getUsersIsland(user).map(i -> {
double num = addon.getOneBlockManager().getPercentageDone(addon.getOneBlocksIsland(i));
return SCALE.floorEntry(num).getValue().replace("╍", addon.getSettings().getPercentCompleteSymbol());
}).orElse("");
}
/**
* Get island Lifetime count
*
* @param user island owner or team member
* @return string of Lifetime count
*/
public String getLifetime(User user) {
if (user == null || user.getUniqueId() == null)
return "";
return getUsersIsland(user).map(i -> String.valueOf(this.addon.getOneBlocksIsland(i).getLifetime()))
.orElse("");
}
/**
* Get Lifetime count by user location
*
* @param user - user
* @return String of Lifetime
*/
public String getLifetimeByLocation(User user) {
if (user == null || user.getUniqueId() == null || !addon.inWorld(user.getWorld()))
return "";
return this.addon.getIslands().getProtectedIslandAt(Objects.requireNonNull(user.getLocation()))
.map(this.addon::getOneBlocksIsland).map(OneBlockIslands::getLifetime).map(String::valueOf).orElse("");
}
}