1
1
package world .bentobox .aoneblock ;
2
2
3
+ import java .util .List ;
3
4
import java .util .Objects ;
5
+ import java .util .Optional ;
4
6
import java .util .Set ;
5
7
import java .util .TreeMap ;
6
8
import java .util .stream .Collectors ;
@@ -20,23 +22,23 @@ public class AOneBlockPlaceholders {
20
22
private static final TreeMap <Double , String > SCALE ;
21
23
private static final String INFINITE = "aoneblock.placeholders.infinite" ;
22
24
static {
23
- SCALE = new TreeMap <>();
24
- SCALE .put (0D , "&c╍╍╍╍╍╍╍╍" );
25
- SCALE .put (12.5 , "&a╍&c╍╍╍╍╍╍╍" );
26
- SCALE .put (25.0 , "&a╍╍&c╍╍╍╍╍╍" );
27
- SCALE .put (37.5 , "&a╍╍╍&c╍╍╍╍╍" );
28
- SCALE .put (50D , "&a╍╍╍╍&c╍╍╍╍" );
29
- SCALE .put (62.5 , "&a╍╍╍╍╍&c╍╍╍" );
30
- SCALE .put (75.0 , "&a╍╍╍╍╍╍&c╍╍" );
31
- SCALE .put (87.5 , "&a╍╍╍╍╍╍╍&c╍" );
32
- SCALE .put (100D , "&a╍╍╍╍╍╍╍╍" );
25
+ SCALE = new TreeMap <>();
26
+ SCALE .put (0D , "&c╍╍╍╍╍╍╍╍" );
27
+ SCALE .put (12.5 , "&a╍&c╍╍╍╍╍╍╍" );
28
+ SCALE .put (25.0 , "&a╍╍&c╍╍╍╍╍╍" );
29
+ SCALE .put (37.5 , "&a╍╍╍&c╍╍╍╍╍" );
30
+ SCALE .put (50D , "&a╍╍╍╍&c╍╍╍╍" );
31
+ SCALE .put (62.5 , "&a╍╍╍╍╍&c╍╍╍" );
32
+ SCALE .put (75.0 , "&a╍╍╍╍╍╍&c╍╍" );
33
+ SCALE .put (87.5 , "&a╍╍╍╍╍╍╍&c╍" );
34
+ SCALE .put (100D , "&a╍╍╍╍╍╍╍╍" );
33
35
}
34
36
35
37
private final AOneBlock addon ;
36
38
37
39
public AOneBlockPlaceholders (AOneBlock addon ,
38
40
world .bentobox .bentobox .managers .PlaceholdersManager placeholdersManager ) {
39
- this .addon = addon ;
41
+ this .addon = addon ;
40
42
placeholdersManager .registerPlaceholder (addon , "visited_island_phase" , this ::getPhaseByLocation );
41
43
placeholdersManager .registerPlaceholder (addon , "visited_island_count" , this ::getCountByLocation );
42
44
placeholdersManager .registerPlaceholder (addon , "my_island_phase" , this ::getPhase );
@@ -61,14 +63,36 @@ public AOneBlockPlaceholders(AOneBlock addon,
61
63
62
64
}
63
65
66
+ /**
67
+ * Get the user's island. Will get either the user's active island, or the island they own.
68
+ * If they own more than one, then one owned island is picked.
69
+ * @param user user
70
+ * @return island
71
+ */
72
+ private Optional <Island > getUsersIsland (User user ) {
73
+ // Get the active island for the user
74
+ Island i = addon .getIslands ().getIsland (addon .getOverWorld (), user );
75
+ if (i != null && i .getOwner () != null && user .getUniqueId ().equals (i .getOwner ())) {
76
+ // Owner of this island and currently on this island
77
+ return Optional .ofNullable (i );
78
+ }
79
+
80
+ // Check for other owned islands
81
+ List <Island > ownedIslands = addon .getIslands ().getIslands (addon .getOverWorld (), user ).stream ()
82
+ .filter (is -> user .getUniqueId ().equals (is .getOwner ())).toList ();
83
+ if (ownedIslands .size () == 1 ) {
84
+ // Replace with the owned island
85
+ i = ownedIslands .get (0 ); // pick one
86
+ }
87
+ // Return what we have found
88
+ return Optional .ofNullable (i );
89
+
90
+ }
91
+
64
92
public String getPhaseBlocksNames (User user ) {
65
93
if (user == null || user .getUniqueId () == null )
66
94
return "" ;
67
- Island i = addon .getIslands ().getIsland (addon .getOverWorld (), user );
68
- if (i == null ) {
69
- return "" ;
70
- }
71
- return getPhaseBlocksForIsland (user , i );
95
+ return getUsersIsland (user ).map (i -> getPhaseBlocksForIsland (user , i )).orElse ("" );
72
96
}
73
97
74
98
private String getPhaseBlocksForIsland (User user , Island i ) {
@@ -111,10 +135,10 @@ public String getPhaseBlocksNamesByLocation(User user) {
111
135
* @return Phase name
112
136
*/
113
137
public String getPhaseByLocation (User user ) {
114
- if (user == null || user .getUniqueId () == null )
115
- return "" ;
116
- return addon .getIslands ().getProtectedIslandAt (Objects .requireNonNull (user .getLocation ()))
117
- .map (addon ::getOneBlocksIsland ).map (OneBlockIslands ::getPhaseName ).orElse ("" );
138
+ if (user == null || user .getUniqueId () == null )
139
+ return "" ;
140
+ return addon .getIslands ().getProtectedIslandAt (Objects .requireNonNull (user .getLocation ()))
141
+ .map (addon ::getOneBlocksIsland ).map (OneBlockIslands ::getPhaseName ).orElse ("" );
118
142
}
119
143
120
144
/**
@@ -125,9 +149,9 @@ public String getPhaseByLocation(User user) {
125
149
*/
126
150
public String getCountByLocation (User user ) {
127
151
if (user == null || user .getUniqueId () == null || !addon .inWorld (user .getWorld ()))
128
- return "" ;
129
- return addon .getIslands ().getProtectedIslandAt (Objects .requireNonNull (user .getLocation ()))
130
- .map (addon ::getOneBlocksIsland ).map (OneBlockIslands ::getBlockNumber ).map (String ::valueOf ).orElse ("" );
152
+ return "" ;
153
+ return addon .getIslands ().getProtectedIslandAt (Objects .requireNonNull (user .getLocation ()))
154
+ .map (addon ::getOneBlocksIsland ).map (OneBlockIslands ::getBlockNumber ).map (String ::valueOf ).orElse ("" );
131
155
}
132
156
133
157
/**
@@ -137,10 +161,9 @@ public String getCountByLocation(User user) {
137
161
* @return phase name
138
162
*/
139
163
public String getPhase (User user ) {
140
- if (user == null || user .getUniqueId () == null )
141
- return "" ;
142
- Island i = addon .getIslands ().getIsland (addon .getOverWorld (), user );
143
- return i == null ? "" : addon .getOneBlocksIsland (i ).getPhaseName ();
164
+ if (user == null || user .getUniqueId () == null )
165
+ return "" ;
166
+ return getUsersIsland (user ).map (i -> addon .getOneBlocksIsland (i ).getPhaseName ()).orElse ("" );
144
167
}
145
168
146
169
/**
@@ -150,10 +173,9 @@ public String getPhase(User user) {
150
173
* @return string of block count
151
174
*/
152
175
public String getCount (User user ) {
153
- if (user == null || user .getUniqueId () == null )
154
- return "" ;
155
- Island i = addon .getIslands ().getIsland (addon .getOverWorld (), user );
156
- return i == null ? "" : String .valueOf (addon .getOneBlocksIsland (i ).getBlockNumber ());
176
+ if (user == null || user .getUniqueId () == null )
177
+ return "" ;
178
+ return getUsersIsland (user ).map (i -> String .valueOf (addon .getOneBlocksIsland (i ).getBlockNumber ())).orElse ("" );
157
179
}
158
180
159
181
/**
@@ -164,9 +186,9 @@ public String getCount(User user) {
164
186
*/
165
187
public String getNextPhaseByLocation (User user ) {
166
188
if (user == null || user .getUniqueId () == null || !addon .inWorld (user .getWorld ()))
167
- return "" ;
168
- return addon .getIslands ().getProtectedIslandAt (Objects .requireNonNull (user .getLocation ()))
169
- .map (addon ::getOneBlocksIsland ).map (addon .getOneBlockManager ()::getNextPhase ).orElse ("" );
189
+ return "" ;
190
+ return addon .getIslands ().getProtectedIslandAt (Objects .requireNonNull (user .getLocation ()))
191
+ .map (addon ::getOneBlocksIsland ).map (addon .getOneBlockManager ()::getNextPhase ).orElse ("" );
170
192
}
171
193
172
194
/**
@@ -176,10 +198,10 @@ public String getNextPhaseByLocation(User user) {
176
198
* @return next island phase
177
199
*/
178
200
public String getNextPhase (User user ) {
179
- if (user == null || user .getUniqueId () == null )
180
- return "" ;
181
- Island i = addon .getIslands ().getIsland (addon .getOverWorld (), user );
182
- return i == null ? "" : addon . getOneBlockManager (). getNextPhase ( addon . getOneBlocksIsland ( i ) );
201
+ if (user == null || user .getUniqueId () == null )
202
+ return "" ;
203
+ return getUsersIsland ( user ). map ( i -> addon .getOneBlockManager ().getNextPhase (addon .getOneBlocksIsland ( i )))
204
+ . orElse ( "" );
183
205
}
184
206
185
207
/**
@@ -190,10 +212,10 @@ public String getNextPhase(User user) {
190
212
*/
191
213
public String getNextPhaseBlocksByLocation (User user ) {
192
214
if (user == null || user .getUniqueId () == null || !addon .inWorld (user .getWorld ()))
193
- return "" ;
194
- return addon .getIslands ().getProtectedIslandAt (Objects .requireNonNull (user .getLocation ()))
195
- .map (addon ::getOneBlocksIsland ).map (addon .getOneBlockManager ()::getNextPhaseBlocks )
196
- .map (num -> num < 0 ? user .getTranslation (INFINITE ) : String .valueOf (num )).orElse ("" );
215
+ return "" ;
216
+ return addon .getIslands ().getProtectedIslandAt (Objects .requireNonNull (user .getLocation ()))
217
+ .map (addon ::getOneBlocksIsland ).map (addon .getOneBlockManager ()::getNextPhaseBlocks )
218
+ .map (num -> num < 0 ? user .getTranslation (INFINITE ) : String .valueOf (num )).orElse ("" );
197
219
}
198
220
199
221
/**
@@ -203,14 +225,12 @@ public String getNextPhaseBlocksByLocation(User user) {
203
225
* @return string number of blocks
204
226
*/
205
227
public String getNextPhaseBlocks (User user ) {
206
- if (user == null || user .getUniqueId () == null )
207
- return "" ;
208
- Island i = addon .getIslands ().getIsland (addon .getOverWorld (), user );
209
- if (i == null ) {
210
- return "" ;
211
- }
212
- int num = addon .getOneBlockManager ().getNextPhaseBlocks (addon .getOneBlocksIsland (i ));
213
- return num < 0 ? user .getTranslation (INFINITE ) : String .valueOf (num );
228
+ if (user == null || user .getUniqueId () == null )
229
+ return "" ;
230
+ return getUsersIsland (user ).map (i -> {
231
+ int num = addon .getOneBlockManager ().getNextPhaseBlocks (addon .getOneBlocksIsland (i ));
232
+ return num < 0 ? user .getTranslation (INFINITE ) : String .valueOf (num );
233
+ }).orElse ("" );
214
234
}
215
235
216
236
/**
@@ -220,14 +240,12 @@ public String getNextPhaseBlocks(User user) {
220
240
* @return string number of blocks
221
241
*/
222
242
public String getPhaseBlocks (User user ) {
223
- if (user == null || user .getUniqueId () == null )
224
- return "" ;
225
- Island i = addon .getIslands ().getIsland (addon .getOverWorld (), user );
226
- if (i == null ) {
227
- return "" ;
228
- }
229
- int num = addon .getOneBlockManager ().getPhaseBlocks (addon .getOneBlocksIsland (i ));
230
- return num < 0 ? user .getTranslation (INFINITE ) : String .valueOf (num );
243
+ if (user == null || user .getUniqueId () == null )
244
+ return "" ;
245
+ return getUsersIsland (user ).map (i -> {
246
+ int num = addon .getOneBlockManager ().getPhaseBlocks (addon .getOneBlocksIsland (i ));
247
+ return num < 0 ? user .getTranslation (INFINITE ) : String .valueOf (num );
248
+ }).orElse ("" );
231
249
}
232
250
233
251
/**
@@ -238,10 +256,10 @@ public String getPhaseBlocks(User user) {
238
256
*/
239
257
public String getPercentDoneByLocation (User user ) {
240
258
if (user == null || user .getUniqueId () == null || !addon .inWorld (user .getWorld ()))
241
- return "" ;
242
- return addon .getIslands ().getProtectedIslandAt (Objects .requireNonNull (user .getLocation ()))
243
- .map (addon ::getOneBlocksIsland ).map (addon .getOneBlockManager ()::getPercentageDone )
244
- .map (num -> Math .round (num ) + "%" ).orElse ("" );
259
+ return "" ;
260
+ return addon .getIslands ().getProtectedIslandAt (Objects .requireNonNull (user .getLocation ()))
261
+ .map (addon ::getOneBlocksIsland ).map (addon .getOneBlockManager ()::getPercentageDone )
262
+ .map (num -> Math .round (num ) + "%" ).orElse ("" );
245
263
}
246
264
247
265
/**
@@ -251,14 +269,12 @@ public String getPercentDoneByLocation(User user) {
251
269
* @return string percentage
252
270
*/
253
271
public String getPercentDone (User user ) {
254
- if (user == null || user .getUniqueId () == null )
255
- return "" ;
256
- Island i = addon .getIslands ().getIsland (addon .getOverWorld (), user );
257
- if (i == null ) {
258
- return "" ;
259
- }
260
- double num = addon .getOneBlockManager ().getPercentageDone (addon .getOneBlocksIsland (i ));
261
- return Math .round (num ) + "%" ;
272
+ if (user == null || user .getUniqueId () == null )
273
+ return "" ;
274
+ return getUsersIsland (user ).map (i -> {
275
+ double num = addon .getOneBlockManager ().getPercentageDone (addon .getOneBlocksIsland (i ));
276
+ return Math .round (num ) + "%" ;
277
+ }).orElse ("" );
262
278
}
263
279
264
280
/**
@@ -269,11 +285,11 @@ public String getPercentDone(User user) {
269
285
*/
270
286
public String getDoneScaleByLocation (User user ) {
271
287
if (user == null || user .getUniqueId () == null || !addon .inWorld (user .getWorld ()))
272
- return "" ;
273
- return addon .getIslands ().getProtectedIslandAt (Objects .requireNonNull (user .getLocation ()))
274
- .map (addon ::getOneBlocksIsland ).map (addon .getOneBlockManager ()::getPercentageDone )
275
- .map (num -> SCALE .floorEntry (num ).getValue ())
276
- .map (s -> s .replace ("╍" , addon .getSettings ().getPercentCompleteSymbol ())).orElse ("" );
288
+ return "" ;
289
+ return addon .getIslands ().getProtectedIslandAt (Objects .requireNonNull (user .getLocation ()))
290
+ .map (addon ::getOneBlocksIsland ).map (addon .getOneBlockManager ()::getPercentageDone )
291
+ .map (num -> SCALE .floorEntry (num ).getValue ())
292
+ .map (s -> s .replace ("╍" , addon .getSettings ().getPercentCompleteSymbol ())).orElse ("" );
277
293
}
278
294
279
295
/**
@@ -283,14 +299,12 @@ public String getDoneScaleByLocation(User user) {
283
299
* @return colored scale
284
300
*/
285
301
public String getDoneScale (User user ) {
286
- if (user == null || user .getUniqueId () == null )
287
- return "" ;
288
- Island i = addon .getIslands ().getIsland (addon .getOverWorld (), user );
289
- if (i == null ) {
290
- return "" ;
291
- }
292
- double num = addon .getOneBlockManager ().getPercentageDone (addon .getOneBlocksIsland (i ));
293
- return SCALE .floorEntry (num ).getValue ().replace ("╍" , addon .getSettings ().getPercentCompleteSymbol ());
302
+ if (user == null || user .getUniqueId () == null )
303
+ return "" ;
304
+ return getUsersIsland (user ).map (i -> {
305
+ double num = addon .getOneBlockManager ().getPercentageDone (addon .getOneBlocksIsland (i ));
306
+ return SCALE .floorEntry (num ).getValue ().replace ("╍" , addon .getSettings ().getPercentCompleteSymbol ());
307
+ }).orElse ("" );
294
308
}
295
309
296
310
/**
@@ -300,12 +314,11 @@ public String getDoneScale(User user) {
300
314
* @return string of Lifetime count
301
315
*/
302
316
public String getLifetime (User user ) {
303
- if (user == null || user .getUniqueId () == null )
304
- return "" ;
305
-
306
- Island island = this .addon .getIslands ().getIsland (this .addon .getOverWorld (), user );
317
+ if (user == null || user .getUniqueId () == null )
318
+ return "" ;
307
319
308
- return island == null ? "" : String .valueOf (this .addon .getOneBlocksIsland (island ).getLifetime ());
320
+ return getUsersIsland (user ).map (i -> String .valueOf (this .addon .getOneBlocksIsland (i ).getLifetime ()))
321
+ .orElse ("" );
309
322
}
310
323
311
324
/**
@@ -316,9 +329,9 @@ public String getLifetime(User user) {
316
329
*/
317
330
public String getLifetimeByLocation (User user ) {
318
331
if (user == null || user .getUniqueId () == null || !addon .inWorld (user .getWorld ()))
319
- return "" ;
332
+ return "" ;
320
333
321
- return this .addon .getIslands ().getProtectedIslandAt (Objects .requireNonNull (user .getLocation ()))
322
- .map (this .addon ::getOneBlocksIsland ).map (OneBlockIslands ::getLifetime ).map (String ::valueOf ).orElse ("" );
334
+ return this .addon .getIslands ().getProtectedIslandAt (Objects .requireNonNull (user .getLocation ()))
335
+ .map (this .addon ::getOneBlocksIsland ).map (OneBlockIslands ::getLifetime ).map (String ::valueOf ).orElse ("" );
323
336
}
324
337
}
0 commit comments