11package world .bentobox .aoneblock ;
22
3+ import java .util .List ;
34import java .util .Objects ;
5+ import java .util .Optional ;
46import java .util .Set ;
57import java .util .TreeMap ;
68import java .util .stream .Collectors ;
@@ -20,23 +22,23 @@ public class AOneBlockPlaceholders {
2022 private static final TreeMap <Double , String > SCALE ;
2123 private static final String INFINITE = "aoneblock.placeholders.infinite" ;
2224 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╍╍╍╍╍╍╍╍" );
3335 }
3436
3537 private final AOneBlock addon ;
3638
3739 public AOneBlockPlaceholders (AOneBlock addon ,
3840 world .bentobox .bentobox .managers .PlaceholdersManager placeholdersManager ) {
39- this .addon = addon ;
41+ this .addon = addon ;
4042 placeholdersManager .registerPlaceholder (addon , "visited_island_phase" , this ::getPhaseByLocation );
4143 placeholdersManager .registerPlaceholder (addon , "visited_island_count" , this ::getCountByLocation );
4244 placeholdersManager .registerPlaceholder (addon , "my_island_phase" , this ::getPhase );
@@ -61,14 +63,36 @@ public AOneBlockPlaceholders(AOneBlock addon,
6163
6264 }
6365
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+
6492 public String getPhaseBlocksNames (User user ) {
6593 if (user == null || user .getUniqueId () == null )
6694 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 ("" );
7296 }
7397
7498 private String getPhaseBlocksForIsland (User user , Island i ) {
@@ -111,10 +135,10 @@ public String getPhaseBlocksNamesByLocation(User user) {
111135 * @return Phase name
112136 */
113137 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 ("" );
118142 }
119143
120144 /**
@@ -125,9 +149,9 @@ public String getPhaseByLocation(User user) {
125149 */
126150 public String getCountByLocation (User user ) {
127151 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 ("" );
131155 }
132156
133157 /**
@@ -137,10 +161,9 @@ public String getCountByLocation(User user) {
137161 * @return phase name
138162 */
139163 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 ("" );
144167 }
145168
146169 /**
@@ -150,10 +173,9 @@ public String getPhase(User user) {
150173 * @return string of block count
151174 */
152175 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 ("" );
157179 }
158180
159181 /**
@@ -164,9 +186,9 @@ public String getCount(User user) {
164186 */
165187 public String getNextPhaseByLocation (User user ) {
166188 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 ("" );
170192 }
171193
172194 /**
@@ -176,10 +198,10 @@ public String getNextPhaseByLocation(User user) {
176198 * @return next island phase
177199 */
178200 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 ( "" );
183205 }
184206
185207 /**
@@ -190,10 +212,10 @@ public String getNextPhase(User user) {
190212 */
191213 public String getNextPhaseBlocksByLocation (User user ) {
192214 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 ("" );
197219 }
198220
199221 /**
@@ -203,14 +225,12 @@ public String getNextPhaseBlocksByLocation(User user) {
203225 * @return string number of blocks
204226 */
205227 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 ("" );
214234 }
215235
216236 /**
@@ -220,14 +240,12 @@ public String getNextPhaseBlocks(User user) {
220240 * @return string number of blocks
221241 */
222242 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 ("" );
231249 }
232250
233251 /**
@@ -238,10 +256,10 @@ public String getPhaseBlocks(User user) {
238256 */
239257 public String getPercentDoneByLocation (User user ) {
240258 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 ("" );
245263 }
246264
247265 /**
@@ -251,14 +269,12 @@ public String getPercentDoneByLocation(User user) {
251269 * @return string percentage
252270 */
253271 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 ("" );
262278 }
263279
264280 /**
@@ -269,11 +285,11 @@ public String getPercentDone(User user) {
269285 */
270286 public String getDoneScaleByLocation (User user ) {
271287 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 ("" );
277293 }
278294
279295 /**
@@ -283,14 +299,12 @@ public String getDoneScaleByLocation(User user) {
283299 * @return colored scale
284300 */
285301 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 ("" );
294308 }
295309
296310 /**
@@ -300,12 +314,11 @@ public String getDoneScale(User user) {
300314 * @return string of Lifetime count
301315 */
302316 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 "" ;
307319
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 ("" );
309322 }
310323
311324 /**
@@ -316,9 +329,9 @@ public String getLifetime(User user) {
316329 */
317330 public String getLifetimeByLocation (User user ) {
318331 if (user == null || user .getUniqueId () == null || !addon .inWorld (user .getWorld ()))
319- return "" ;
332+ return "" ;
320333
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 ("" );
323336 }
324337}
0 commit comments