Skip to content

Commit b5b8c2f

Browse files
committed
Adds placeholders for phse block listing #385
1 parent d1d0f2c commit b5b8c2f

File tree

3 files changed

+64
-6
lines changed

3 files changed

+64
-6
lines changed

src/main/java/world/bentobox/aoneblock/AOneBlockPlaceholders.java

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
package world.bentobox.aoneblock;
22

33
import java.util.Objects;
4+
import java.util.Set;
45
import java.util.TreeMap;
6+
import java.util.stream.Collectors;
7+
8+
import org.bukkit.Material;
59

610
import world.bentobox.aoneblock.dataobjects.OneBlockIslands;
11+
import world.bentobox.bentobox.api.localization.TextVariables;
712
import world.bentobox.bentobox.api.user.User;
813
import world.bentobox.bentobox.database.objects.Island;
14+
import world.bentobox.bentobox.hooks.LangUtilsHook;
15+
import world.bentobox.bentobox.util.Util;
916

1017
public class AOneBlockPlaceholders {
1118

@@ -46,6 +53,53 @@ public AOneBlockPlaceholders(AOneBlock addon,
4653
// Since 1.10
4754
placeholdersManager.registerPlaceholder(addon, "visited_island_lifetime_count", this::getLifetimeByLocation);
4855
placeholdersManager.registerPlaceholder(addon, "my_island_lifetime_count", this::getLifetime);
56+
57+
placeholdersManager.registerPlaceholder(addon, "visited_island_phase_block_list",
58+
this::getPhaseBlocksNamesByLocation);
59+
placeholdersManager.registerPlaceholder(addon, "my_island_phase_block_list", this::getPhaseBlocksNames);
60+
61+
}
62+
63+
public String getPhaseBlocksNames(User user) {
64+
if (user == null || user.getUniqueId() == null)
65+
return "";
66+
Island i = addon.getIslands().getIsland(addon.getOverWorld(), user);
67+
if (i == null) {
68+
return "";
69+
}
70+
return getPhaseBlocksForIsland(user, i);
71+
}
72+
73+
private String getPhaseBlocksForIsland(User user, Island i) {
74+
String phaseName = addon.getOneBlocksIsland(i).getPhaseName();
75+
Set<Material> set = addon.getOneBlockManager().getPhase(phaseName).map(phase -> phase.getBlocks().keySet())
76+
.orElse(null);
77+
if (set == null) {
78+
return "";
79+
}
80+
81+
String result = set.stream().map(m -> getMaterialName(user, m))
82+
.map(string -> user.getTranslation("aoneblock.placeholders.block-list-format", TextVariables.NAME,
83+
string))
84+
.collect(Collectors.joining());
85+
// Removing the last newline character or comma if it exists
86+
if (result.endsWith("\n") || result.endsWith(",")) {
87+
result = result.substring(0, result.length() - 1);
88+
}
89+
90+
return result;
91+
92+
}
93+
94+
private String getMaterialName(User user, Material m) {
95+
return addon.getPlugin().getHooks().getHook("LangUtils").map(hook -> LangUtilsHook.getMaterialName(m, user))
96+
.orElse(Util.prettifyText(m.name()));
97+
}
98+
99+
public String getPhaseBlocksNamesByLocation(User user) {
100+
if (user == null || user.getUniqueId() == null || !addon.inWorld(user.getWorld()))
101+
return "";
102+
return addon.getIslands().getIslandAt(user.getLocation()).map(i -> getPhaseBlocksForIsland(user, i)).orElse("");
49103
}
50104

51105
/**
@@ -68,7 +122,7 @@ public String getPhaseByLocation(User user) {
68122
* @return String of count
69123
*/
70124
public String getCountByLocation(User user) {
71-
if (user == null || user.getUniqueId() == null)
125+
if (user == null || user.getUniqueId() == null || !addon.inWorld(user.getWorld()))
72126
return "";
73127
return addon.getIslands().getProtectedIslandAt(Objects.requireNonNull(user.getLocation()))
74128
.map(addon::getOneBlocksIsland).map(OneBlockIslands::getBlockNumber).map(String::valueOf).orElse("");
@@ -107,7 +161,7 @@ public String getCount(User user) {
107161
* @return next phase
108162
*/
109163
public String getNextPhaseByLocation(User user) {
110-
if (user == null || user.getUniqueId() == null)
164+
if (user == null || user.getUniqueId() == null || !addon.inWorld(user.getWorld()))
111165
return "";
112166
return addon.getIslands().getProtectedIslandAt(Objects.requireNonNull(user.getLocation()))
113167
.map(addon::getOneBlocksIsland).map(addon.getOneBlockManager()::getNextPhase).orElse("");
@@ -133,7 +187,7 @@ public String getNextPhase(User user) {
133187
* @return string number of blocks
134188
*/
135189
public String getNextPhaseBlocksByLocation(User user) {
136-
if (user == null || user.getUniqueId() == null)
190+
if (user == null || user.getUniqueId() == null || !addon.inWorld(user.getWorld()))
137191
return "";
138192
return addon.getIslands().getProtectedIslandAt(Objects.requireNonNull(user.getLocation()))
139193
.map(addon::getOneBlocksIsland).map(addon.getOneBlockManager()::getNextPhaseBlocks)
@@ -181,7 +235,7 @@ public String getPhaseBlocks(User user) {
181235
* @return string percentage
182236
*/
183237
public String getPercentDoneByLocation(User user) {
184-
if (user == null || user.getUniqueId() == null)
238+
if (user == null || user.getUniqueId() == null || !addon.inWorld(user.getWorld()))
185239
return "";
186240
return addon.getIslands().getProtectedIslandAt(Objects.requireNonNull(user.getLocation()))
187241
.map(addon::getOneBlocksIsland).map(addon.getOneBlockManager()::getPercentageDone)
@@ -212,7 +266,7 @@ public String getPercentDone(User user) {
212266
* @return colored scale
213267
*/
214268
public String getDoneScaleByLocation(User user) {
215-
if (user == null || user.getUniqueId() == null)
269+
if (user == null || user.getUniqueId() == null || !addon.inWorld(user.getWorld()))
216270
return "";
217271
return addon.getIslands().getProtectedIslandAt(Objects.requireNonNull(user.getLocation()))
218272
.map(addon::getOneBlocksIsland).map(addon.getOneBlockManager()::getPercentageDone)
@@ -259,7 +313,7 @@ public String getLifetime(User user) {
259313
* @return String of Lifetime
260314
*/
261315
public String getLifetimeByLocation(User user) {
262-
if (user == null || user.getUniqueId() == null)
316+
if (user == null || user.getUniqueId() == null || !addon.inWorld(user.getWorld()))
263317
return "";
264318

265319
return this.addon.getIslands().getProtectedIslandAt(Objects.requireNonNull(user.getLocation()))

src/main/resources/locales/en-US.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ aoneblock:
6464
cooldown: "&c Next phase will be available in [number] seconds!"
6565
placeholders:
6666
infinite: Infinite
67+
block-list-format: |
68+
&7- &e [name]
6769
gui:
6870
titles:
6971
phases: '&0&l OneBlock Phases'

src/test/java/world/bentobox/aoneblock/PlaceholdersManagerTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,12 @@ public void setUp() throws Exception {
6161
// User
6262
when(user.getLocation()).thenReturn(location);
6363
when(user.getTranslation("aoneblock.placeholders.infinite")).thenReturn("Infinite");
64+
when(user.getWorld()).thenReturn(world);
6465
// Addon
6566
when(addon.getIslands()).thenReturn(im);
6667
when(addon.getOverWorld()).thenReturn(world);
6768
when(addon.getOneBlockManager()).thenReturn(obm);
69+
when(addon.inWorld(world)).thenReturn(true);
6870
when(im.getProtectedIslandAt(any())).thenReturn(Optional.of(island));
6971
when(im.getIsland(world, user)).thenReturn(island);
7072
obi = new OneBlockIslands("uniqueId");

0 commit comments

Comments
 (0)