Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Placeholder accuracy #410

Merged
merged 1 commit into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
195 changes: 104 additions & 91 deletions src/main/java/world/bentobox/aoneblock/AOneBlockPlaceholders.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
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;
Expand All @@ -20,23 +22,23 @@ 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╍╍╍╍╍╍╍╍");
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;
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);
Expand All @@ -61,14 +63,36 @@ public AOneBlockPlaceholders(AOneBlock addon,

}

/**
* 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.get(0); // pick one
}
// Return what we have found
return Optional.ofNullable(i);

}

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);
return getUsersIsland(user).map(i -> getPhaseBlocksForIsland(user, i)).orElse("");
}

private String getPhaseBlocksForIsland(User user, Island i) {
Expand Down Expand Up @@ -111,10 +135,10 @@ public String getPhaseBlocksNamesByLocation(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("");
if (user == null || user.getUniqueId() == null)
return "";
return addon.getIslands().getProtectedIslandAt(Objects.requireNonNull(user.getLocation()))
.map(addon::getOneBlocksIsland).map(OneBlockIslands::getPhaseName).orElse("");
}

/**
Expand All @@ -125,9 +149,9 @@ public String getPhaseByLocation(User user) {
*/
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("");
return "";
return addon.getIslands().getProtectedIslandAt(Objects.requireNonNull(user.getLocation()))
.map(addon::getOneBlocksIsland).map(OneBlockIslands::getBlockNumber).map(String::valueOf).orElse("");
}

/**
Expand All @@ -137,10 +161,9 @@ public String getCountByLocation(User user) {
* @return phase name
*/
public String getPhase(User user) {
if (user == null || user.getUniqueId() == null)
return "";
Island i = addon.getIslands().getIsland(addon.getOverWorld(), user);
return i == null ? "" : addon.getOneBlocksIsland(i).getPhaseName();
if (user == null || user.getUniqueId() == null)
return "";
return getUsersIsland(user).map(i -> addon.getOneBlocksIsland(i).getPhaseName()).orElse("");
}

/**
Expand All @@ -150,10 +173,9 @@ public String getPhase(User user) {
* @return string of block count
*/
public String getCount(User user) {
if (user == null || user.getUniqueId() == null)
return "";
Island i = addon.getIslands().getIsland(addon.getOverWorld(), user);
return i == null ? "" : String.valueOf(addon.getOneBlocksIsland(i).getBlockNumber());
if (user == null || user.getUniqueId() == null)
return "";
return getUsersIsland(user).map(i -> String.valueOf(addon.getOneBlocksIsland(i).getBlockNumber())).orElse("");
}

/**
Expand All @@ -164,9 +186,9 @@ public String getCount(User user) {
*/
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("");
return "";
return addon.getIslands().getProtectedIslandAt(Objects.requireNonNull(user.getLocation()))
.map(addon::getOneBlocksIsland).map(addon.getOneBlockManager()::getNextPhase).orElse("");
}

/**
Expand All @@ -176,10 +198,10 @@ public String getNextPhaseByLocation(User user) {
* @return next island phase
*/
public String getNextPhase(User user) {
if (user == null || user.getUniqueId() == null)
return "";
Island i = addon.getIslands().getIsland(addon.getOverWorld(), user);
return i == null ? "" : addon.getOneBlockManager().getNextPhase(addon.getOneBlocksIsland(i));
if (user == null || user.getUniqueId() == null)
return "";
return getUsersIsland(user).map(i -> addon.getOneBlockManager().getNextPhase(addon.getOneBlocksIsland(i)))
.orElse("");
}

/**
Expand All @@ -190,10 +212,10 @@ public String getNextPhase(User user) {
*/
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("");
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("");
}

/**
Expand All @@ -203,14 +225,12 @@ public String getNextPhaseBlocksByLocation(User user) {
* @return string number of blocks
*/
public String getNextPhaseBlocks(User user) {
if (user == null || user.getUniqueId() == null)
return "";
Island i = addon.getIslands().getIsland(addon.getOverWorld(), user);
if (i == null) {
return "";
}
int num = addon.getOneBlockManager().getNextPhaseBlocks(addon.getOneBlocksIsland(i));
return num < 0 ? user.getTranslation(INFINITE) : String.valueOf(num);
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("");
}

/**
Expand All @@ -220,14 +240,12 @@ public String getNextPhaseBlocks(User user) {
* @return string number of blocks
*/
public String getPhaseBlocks(User user) {
if (user == null || user.getUniqueId() == null)
return "";
Island i = addon.getIslands().getIsland(addon.getOverWorld(), user);
if (i == null) {
return "";
}
int num = addon.getOneBlockManager().getPhaseBlocks(addon.getOneBlocksIsland(i));
return num < 0 ? user.getTranslation(INFINITE) : String.valueOf(num);
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("");
}

/**
Expand All @@ -238,10 +256,10 @@ public String getPhaseBlocks(User user) {
*/
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("");
return "";
return addon.getIslands().getProtectedIslandAt(Objects.requireNonNull(user.getLocation()))
.map(addon::getOneBlocksIsland).map(addon.getOneBlockManager()::getPercentageDone)
.map(num -> Math.round(num) + "%").orElse("");
}

/**
Expand All @@ -251,14 +269,12 @@ public String getPercentDoneByLocation(User user) {
* @return string percentage
*/
public String getPercentDone(User user) {
if (user == null || user.getUniqueId() == null)
return "";
Island i = addon.getIslands().getIsland(addon.getOverWorld(), user);
if (i == null) {
return "";
}
double num = addon.getOneBlockManager().getPercentageDone(addon.getOneBlocksIsland(i));
return Math.round(num) + "%";
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("");
}

/**
Expand All @@ -269,11 +285,11 @@ public String getPercentDone(User user) {
*/
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("");
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("");
}

/**
Expand All @@ -283,14 +299,12 @@ public String getDoneScaleByLocation(User user) {
* @return colored scale
*/
public String getDoneScale(User user) {
if (user == null || user.getUniqueId() == null)
return "";
Island i = addon.getIslands().getIsland(addon.getOverWorld(), user);
if (i == null) {
return "";
}
double num = addon.getOneBlockManager().getPercentageDone(addon.getOneBlocksIsland(i));
return SCALE.floorEntry(num).getValue().replace("╍", addon.getSettings().getPercentCompleteSymbol());
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("");
}

/**
Expand All @@ -300,12 +314,11 @@ public String getDoneScale(User user) {
* @return string of Lifetime count
*/
public String getLifetime(User user) {
if (user == null || user.getUniqueId() == null)
return "";

Island island = this.addon.getIslands().getIsland(this.addon.getOverWorld(), user);
if (user == null || user.getUniqueId() == null)
return "";

return island == null ? "" : String.valueOf(this.addon.getOneBlocksIsland(island).getLifetime());
return getUsersIsland(user).map(i -> String.valueOf(this.addon.getOneBlocksIsland(i).getLifetime()))
.orElse("");
}

/**
Expand All @@ -316,9 +329,9 @@ public String getLifetime(User user) {
*/
public String getLifetimeByLocation(User user) {
if (user == null || user.getUniqueId() == null || !addon.inWorld(user.getWorld()))
return "";
return "";

return this.addon.getIslands().getProtectedIslandAt(Objects.requireNonNull(user.getLocation()))
.map(this.addon::getOneBlocksIsland).map(OneBlockIslands::getLifetime).map(String::valueOf).orElse("");
return this.addon.getIslands().getProtectedIslandAt(Objects.requireNonNull(user.getLocation()))
.map(this.addon::getOneBlocksIsland).map(OneBlockIslands::getLifetime).map(String::valueOf).orElse("");
}
}
Loading
Loading