Skip to content

Commit

Permalink
Merge pull request #386 from BentoBoxWorld/phase_block_names_placeholder
Browse files Browse the repository at this point in the history
Adds placeholders for phase block listing #385
  • Loading branch information
tastybento committed Apr 17, 2024
2 parents d1d0f2c + b5b8c2f commit f4c344a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 6 deletions.
66 changes: 60 additions & 6 deletions src/main/java/world/bentobox/aoneblock/AOneBlockPlaceholders.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package world.bentobox.aoneblock;

import java.util.Objects;
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.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 {

Expand Down Expand Up @@ -46,6 +53,53 @@ public AOneBlockPlaceholders(AOneBlock addon,
// 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);

}

public String getPhaseBlocksNames(User user) {
if (user == null || user.getUniqueId() == null)
return "";
Island i = addon.getIslands().getIsland(addon.getOverWorld(), user);
if (i == null) {
return "";
}
return getPhaseBlocksForIsland(user, i);
}

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("aoneblock.placeholders.block-list-format", TextVariables.NAME,
string))
.collect(Collectors.joining());
// Removing the last newline character or comma if it exists
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("");
}

/**
Expand All @@ -68,7 +122,7 @@ public String getPhaseByLocation(User user) {
* @return String of count
*/
public String getCountByLocation(User user) {
if (user == null || user.getUniqueId() == null)
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("");
Expand Down Expand Up @@ -107,7 +161,7 @@ public String getCount(User user) {
* @return next phase
*/
public String getNextPhaseByLocation(User user) {
if (user == null || user.getUniqueId() == null)
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("");
Expand All @@ -133,7 +187,7 @@ public String getNextPhase(User user) {
* @return string number of blocks
*/
public String getNextPhaseBlocksByLocation(User user) {
if (user == null || user.getUniqueId() == null)
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)
Expand Down Expand Up @@ -181,7 +235,7 @@ public String getPhaseBlocks(User user) {
* @return string percentage
*/
public String getPercentDoneByLocation(User user) {
if (user == null || user.getUniqueId() == null)
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)
Expand Down Expand Up @@ -212,7 +266,7 @@ public String getPercentDone(User user) {
* @return colored scale
*/
public String getDoneScaleByLocation(User user) {
if (user == null || user.getUniqueId() == null)
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)
Expand Down Expand Up @@ -259,7 +313,7 @@ public String getLifetime(User user) {
* @return String of Lifetime
*/
public String getLifetimeByLocation(User user) {
if (user == null || user.getUniqueId() == null)
if (user == null || user.getUniqueId() == null || !addon.inWorld(user.getWorld()))
return "";

return this.addon.getIslands().getProtectedIslandAt(Objects.requireNonNull(user.getLocation()))
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/locales/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ aoneblock:
cooldown: "&c Next phase will be available in [number] seconds!"
placeholders:
infinite: Infinite
block-list-format: |
&7- &e [name]
gui:
titles:
phases: '&0&l OneBlock Phases'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,12 @@ public void setUp() throws Exception {
// User
when(user.getLocation()).thenReturn(location);
when(user.getTranslation("aoneblock.placeholders.infinite")).thenReturn("Infinite");
when(user.getWorld()).thenReturn(world);
// Addon
when(addon.getIslands()).thenReturn(im);
when(addon.getOverWorld()).thenReturn(world);
when(addon.getOneBlockManager()).thenReturn(obm);
when(addon.inWorld(world)).thenReturn(true);
when(im.getProtectedIslandAt(any())).thenReturn(Optional.of(island));
when(im.getIsland(world, user)).thenReturn(island);
obi = new OneBlockIslands("uniqueId");
Expand Down

0 comments on commit f4c344a

Please sign in to comment.