Changed top ten internally to use islands instead of players as keys (#295)

Added %[gamemode]_top_weighted_value_x% placeholder
https://github.com/BentoBoxWorld/Level/issues/294
This commit is contained in:
tastybento 2023-11-19 18:08:47 -08:00 committed by GitHub
parent 26d4839f6a
commit 9d1a5c7476
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 1611 additions and 1674 deletions

View File

@ -96,28 +96,6 @@ public class LevelsManager {
});
}
/**
* Add a score to the top players list
*
* @param world - world
* @param targetPlayer - target player
* @param lv - island level
*/
private void addToTopTen(@NonNull World world, @NonNull UUID targetPlayer, long lv) {
// Get top ten
Map<UUID, Long> topTen = topTenLists.computeIfAbsent(world, k -> new TopTenData(world)).getTopTen();
// Remove this player from the top list no matter what (we'll put them back
// later if required)
topTen.remove(targetPlayer);
// Get the island
Island island = addon.getIslands().getIsland(world, targetPlayer);
if (island != null && island.getOwner() != null && hasTopTenPerm(world, island.getOwner())) {
// Insert the owner into the top ten
topTen.put(island.getOwner(), lv);
}
}
/**
* Add an island to a top ten
*
@ -128,7 +106,7 @@ public class LevelsManager {
private boolean addToTopTen(Island island, long lv) {
if (island != null && island.getOwner() != null && hasTopTenPerm(island.getWorld(), island.getOwner())) {
topTenLists.computeIfAbsent(island.getWorld(), k -> new TopTenData(island.getWorld())).getTopTen()
.put(island.getOwner(), lv);
.put(island.getUniqueId(), lv);
return true;
}
return false;
@ -158,7 +136,7 @@ public class LevelsManager {
result.complete(null);
}
// Save result
setIslandResults(island.getWorld(), island.getOwner(), r);
setIslandResults(island, r);
// Save the island scan details
result.complete(r);
});
@ -321,22 +299,23 @@ public class LevelsManager {
*
* @param world - world requested
* @param size - size of the top ten
* @return sorted top ten map
* @return sorted top ten map. The key is the island unique ID
*/
@NonNull
public Map<UUID, Long> getTopTen(@NonNull World world, int size) {
public Map<String, Long> getTopTen(@NonNull World world, int size) {
createAndCleanRankings(world);
// Return the sorted map
return Collections.unmodifiableMap(topTenLists.get(world).getTopTen().entrySet().stream()
.filter(e -> addon.getIslands().hasIsland(world, e.getKey())).filter(l -> l.getValue() > 0)
.sorted(Collections.reverseOrder(Map.Entry.comparingByValue())).limit(size)
.filter(l -> l.getValue() > 0).sorted(Collections.reverseOrder(Map.Entry.comparingByValue()))
.limit(size)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new)));
}
void createAndCleanRankings(@NonNull World world) {
topTenLists.computeIfAbsent(world, TopTenData::new);
// Remove player from top ten if they are online and do not have the perm
topTenLists.get(world).getTopTen().keySet().removeIf(u -> !hasTopTenPerm(world, u));
topTenLists.get(world).getTopTen().keySet().removeIf(u -> addon.getIslands().getIslandById(u)
.filter(i -> i.getOwner() == null || !hasTopTenPerm(world, i.getOwner())).isPresent());
}
/**
@ -355,10 +334,12 @@ public class LevelsManager {
*/
public int getRank(@NonNull World world, UUID uuid) {
createAndCleanRankings(world);
Stream<Entry<UUID, Long>> stream = topTenLists.get(world).getTopTen().entrySet().stream()
.filter(e -> addon.getIslands().hasIsland(world, e.getKey())).filter(l -> l.getValue() > 0)
.sorted(Collections.reverseOrder(Map.Entry.comparingByValue()));
return (int) (stream.takeWhile(x -> !x.getKey().equals(uuid)).map(Map.Entry::getKey).count() + 1);
Stream<Entry<String, Long>> stream = topTenLists.get(world).getTopTen().entrySet().stream()
.filter(l -> l.getValue() > 0).sorted(Collections.reverseOrder(Map.Entry.comparingByValue()));
// Get player's current island
Island island = addon.getIslands().getIsland(world, uuid);
String id = island == null ? null : island.getUniqueId();
return (int) (stream.takeWhile(x -> !x.getKey().equals(id)).map(Map.Entry::getKey).count() + 1);
}
/**
@ -392,13 +373,12 @@ public class LevelsManager {
}
/**
* Removes a player from a world's top ten and removes world from player's level
* data
* Removes an island from a world's top ten
*
* @param world - world
* @param uuid - the player's uuid
* @param uuid - the island's uuid
*/
public void removeEntry(World world, UUID uuid) {
public void removeEntry(World world, String uuid) {
if (topTenLists.containsKey(world)) {
topTenLists.get(world).getTopTen().remove(uuid);
}
@ -423,7 +403,7 @@ public class LevelsManager {
* member
*
* @param world - world
* @param targetPlayer - player, may be a team member
* @param island - island
* @param lv - level
*/
public void setIslandLevel(@NonNull World world, @NonNull UUID targetPlayer, long lv) {
@ -440,9 +420,8 @@ public class LevelsManager {
}
handler.saveObjectAsync(levelsCache.get(id));
// Update TopTen
addToTopTen(world, targetPlayer, levelsCache.get(id).getLevel());
addToTopTen(island, levelsCache.get(id).getLevel());
}
}
/**
@ -453,9 +432,7 @@ public class LevelsManager {
* @param owner - owner of the island
* @param r - results of the calculation
*/
private void setIslandResults(World world, @NonNull UUID owner, Results r) {
// Get the island
Island island = addon.getIslands().getIsland(world, owner);
private void setIslandResults(Island island, Results r) {
if (island == null)
return;
IslandLevels ld = levelsCache.computeIfAbsent(island.getUniqueId(), IslandLevels::new);
@ -467,7 +444,7 @@ public class LevelsManager {
levelsCache.put(island.getUniqueId(), ld);
handler.saveObjectAsync(ld);
// Update TopTen
addToTopTen(world, owner, ld.getLevel());
addToTopTen(island, ld.getLevel());
}
/**

View File

@ -2,10 +2,13 @@ package world.bentobox.level;
import java.util.Collections;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
import org.bukkit.World;
import org.eclipse.jdt.annotation.Nullable;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.addons.GameModeAddon;
@ -18,6 +21,7 @@ import world.bentobox.level.objects.TopTenData;
/**
* Handles Level placeholders
*
* @author tastybento
*
*/
@ -32,127 +36,155 @@ public class PlaceholderManager {
}
protected void registerPlaceholders(GameModeAddon gm) {
if (plugin.getPlaceholdersManager() == null) return;
if (plugin.getPlaceholdersManager() == null)
return;
PlaceholdersManager bpm = plugin.getPlaceholdersManager();
// Island Level
bpm.registerPlaceholder(addon,
gm.getDescription().getName().toLowerCase() + "_island_level",
bpm.registerPlaceholder(addon, gm.getDescription().getName().toLowerCase() + "_island_level",
user -> addon.getManager().getIslandLevelString(gm.getOverWorld(), user.getUniqueId()));
bpm.registerPlaceholder(addon,
gm.getDescription().getName().toLowerCase() + "_island_level_raw",
// Unformatted island level
bpm.registerPlaceholder(addon, gm.getDescription().getName().toLowerCase() + "_island_level_raw",
user -> String.valueOf(addon.getManager().getIslandLevel(gm.getOverWorld(), user.getUniqueId())));
bpm.registerPlaceholder(addon,
gm.getDescription().getName().toLowerCase() + "_island_total_points",
user -> {
// Total number of points counted before applying level formula
bpm.registerPlaceholder(addon, gm.getDescription().getName().toLowerCase() + "_island_total_points", user -> {
IslandLevels data = addon.getManager().getLevelsData(addon.getIslands().getIsland(gm.getOverWorld(), user));
return data.getTotalPoints() + "";
});
bpm.registerPlaceholder(addon,
gm.getDescription().getName().toLowerCase() + "_points_to_next_level",
// Points to the next level for player
bpm.registerPlaceholder(addon, gm.getDescription().getName().toLowerCase() + "_points_to_next_level",
user -> addon.getManager().getPointsToNextString(gm.getOverWorld(), user.getUniqueId()));
bpm.registerPlaceholder(addon,
gm.getDescription().getName().toLowerCase() + "_island_level_max",
// Maximum level this island has ever been. Current level maybe lower.
bpm.registerPlaceholder(addon, gm.getDescription().getName().toLowerCase() + "_island_level_max",
user -> String.valueOf(addon.getManager().getIslandMaxLevel(gm.getOverWorld(), user.getUniqueId())));
// Visited Island Level
bpm.registerPlaceholder(addon,
gm.getDescription().getName().toLowerCase() + "_visited_island_level", user -> getVisitedIslandLevel(gm, user));
bpm.registerPlaceholder(addon, gm.getDescription().getName().toLowerCase() + "_visited_island_level",
user -> getVisitedIslandLevel(gm, user));
// Register Top Ten Placeholders
for (int i = 1; i < 11; i++) {
final int rank = i;
// Name
bpm.registerPlaceholder(addon,
gm.getDescription().getName().toLowerCase() + "_top_name_" + i, u -> getRankName(gm.getOverWorld(), rank));
bpm.registerPlaceholder(addon, gm.getDescription().getName().toLowerCase() + "_top_name_" + i,
u -> getRankName(gm.getOverWorld(), rank));
// Island Name
bpm.registerPlaceholder(addon,
gm.getDescription().getName().toLowerCase() + "_top_island_name_" + i, u -> getRankIslandName(gm.getOverWorld(), rank));
bpm.registerPlaceholder(addon, gm.getDescription().getName().toLowerCase() + "_top_island_name_" + i,
u -> getRankIslandName(gm.getOverWorld(), rank));
// Members
bpm.registerPlaceholder(addon,
gm.getDescription().getName().toLowerCase() + "_top_members_" + i, u -> getRankMembers(gm.getOverWorld(), rank));
bpm.registerPlaceholder(addon, gm.getDescription().getName().toLowerCase() + "_top_members_" + i,
u -> getRankMembers(gm.getOverWorld(), rank));
// Level
bpm.registerPlaceholder(addon,
gm.getDescription().getName().toLowerCase() + "_top_value_" + i, u -> getRankLevel(gm.getOverWorld(), rank));
bpm.registerPlaceholder(addon, gm.getDescription().getName().toLowerCase() + "_top_value_" + i,
u -> getRankLevel(gm.getOverWorld(), rank));
// Weighted Level (Level / number of members)
bpm.registerPlaceholder(addon, gm.getDescription().getName().toLowerCase() + "_top_weighted_value_" + i,
u -> getWeightedRankLevel(gm.getOverWorld(), rank));
}
// Personal rank
bpm.registerPlaceholder(addon,
gm.getDescription().getName().toLowerCase() + "_rank_value", u -> getRankValue(gm.getOverWorld(), u));
bpm.registerPlaceholder(addon, gm.getDescription().getName().toLowerCase() + "_rank_value",
u -> getRankValue(gm.getOverWorld(), u));
}
/**
* Get the name of the player who holds the rank in this world
* Get the name of the owner of the island who holds the rank in this world.
*
* @param world world
* @param rank rank 1 to 10
* @return rank name
*/
String getRankName(World world, int rank) {
if (rank < 1) rank = 1;
if (rank > Level.TEN) rank = Level.TEN;
return addon.getPlayers().getName(addon.getManager().getTopTen(world, Level.TEN).keySet().stream().skip(rank - 1L).limit(1L).findFirst().orElse(null));
// Ensure rank is within bounds
rank = Math.max(1, Math.min(rank, Level.TEN));
@Nullable
UUID owner = addon.getManager().getTopTen(world, Level.TEN).keySet().stream().skip(rank - 1L).limit(1L)
.findFirst().flatMap(addon.getIslands()::getIslandById).map(Island::getOwner).orElse(null);
return addon.getPlayers().getName(owner);
}
/**
* Get the island name for this rank
*
* @param world world
* @param rank rank 1 to 10
* @return name of island or nothing if there isn't one
*/
String getRankIslandName(World world, int rank) {
if (rank < 1) rank = 1;
if (rank > Level.TEN) rank = Level.TEN;
UUID owner = addon.getManager().getTopTen(world, Level.TEN).keySet().stream().skip(rank - 1L).limit(1L).findFirst().orElse(null);
if (owner != null) {
Island island = addon.getIslands().getIsland(world, owner);
if (island != null) {
return island.getName() == null ? "" : island.getName();
}
}
return "";
// Ensure rank is within bounds
rank = Math.max(1, Math.min(rank, Level.TEN));
return addon.getManager().getTopTen(world, Level.TEN).keySet().stream().skip(rank - 1L).limit(1L).findFirst()
.flatMap(addon.getIslands()::getIslandById).map(Island::getName).orElse("");
}
/**
* Gets a comma separated string of island member names
*
* @param world world
* @param rank rank to request
* @return comma separated string of island member names
*/
String getRankMembers(World world, int rank) {
if (rank < 1) rank = 1;
if (rank > Level.TEN) rank = Level.TEN;
UUID owner = addon.getManager().getTopTen(world, Level.TEN).keySet().stream().skip(rank - 1L).limit(1L).findFirst().orElse(null);
if (owner != null) {
Island island = addon.getIslands().getIsland(world, owner);
if (island != null) {
// Ensure rank is within bounds
rank = Math.max(1, Math.min(rank, Level.TEN));
Optional<Island> island = addon.getManager().getTopTen(world, Level.TEN).keySet().stream().skip(rank - 1L)
.limit(1L).findFirst().flatMap(addon.getIslands()::getIslandById);
if (island.isPresent()) {
// Sort members by rank
return island.getMembers().entrySet().stream()
.filter(e -> e.getValue() >= RanksManager.MEMBER_RANK)
.sorted(Collections.reverseOrder(Map.Entry.comparingByValue()))
.map(Map.Entry::getKey)
.map(addon.getPlayers()::getName)
.collect(Collectors.joining(","));
}
return island.get().getMembers().entrySet().stream().filter(e -> e.getValue() >= RanksManager.MEMBER_RANK)
.sorted(Collections.reverseOrder(Map.Entry.comparingByValue())).map(Map.Entry::getKey)
.map(addon.getPlayers()::getName).collect(Collectors.joining(","));
}
return "";
}
/**
* Gets the weighted level, which is the level / number of players
*
* @param world world
* @param rank level
* @return weighted level
*/
String getWeightedRankLevel(World world, int rank) {
// Ensure rank is within bounds
rank = Math.max(1, Math.min(rank, Level.TEN));
// Retrieve the top ten entries
Map<String, Long> topTen = addon.getManager().getTopTen(world, Level.TEN);
if (topTen.isEmpty()) {
return "";
}
// Find the entry corresponding to the rank
Entry<String, Long> entry = topTen.entrySet().stream().skip(rank - 1).findFirst().orElse(null);
if (entry == null) {
return "";
}
// Calculate the score
Island island = addon.getIslands().getIslandById(entry.getKey()).orElse(null);
if (island == null || island.getMemberSet().isEmpty()) {
return "";
}
double score = (double) entry.getValue() / island.getMemberSet().size();
// Format and return the level
return addon.getManager().formatLevel((long) score);
}
String getRankLevel(World world, int rank) {
if (rank < 1) rank = 1;
if (rank > Level.TEN) rank = Level.TEN;
return addon.getManager()
.formatLevel(addon.getManager()
.getTopTen(world, Level.TEN)
.values()
.stream()
.skip(rank - 1L)
.limit(1L)
.findFirst()
.orElse(null));
// Ensure rank is within bounds
rank = Math.max(1, Math.min(rank, Level.TEN));
return addon.getManager().formatLevel(addon.getManager().getTopTen(world, Level.TEN).values().stream()
.skip(rank - 1L).limit(1L).findFirst().orElse(null));
}
/**
* Return the rank of the player in a world
*
* @param world world
* @param user player
* @return rank where 1 is the top rank.
@ -163,11 +195,13 @@ public class PlaceholderManager {
}
// Get the island level for this user
long level = addon.getManager().getIslandLevel(world, user.getUniqueId());
return String.valueOf(addon.getManager().getTopTenLists().getOrDefault(world, new TopTenData(world)).getTopTen().values().stream().filter(l -> l > level).count() + 1);
return String.valueOf(addon.getManager().getTopTenLists().getOrDefault(world, new TopTenData(world)).getTopTen()
.values().stream().filter(l -> l > level).count() + 1);
}
String getVisitedIslandLevel(GameModeAddon gm, User user) {
if (user == null || !gm.inWorld(user.getWorld())) return "";
if (user == null || !gm.inWorld(user.getWorld()))
return "";
return addon.getIslands().getIslandAt(user.getLocation())
.map(island -> addon.getManager().getIslandLevelString(gm.getOverWorld(), island.getOwner()))
.orElse("0");

View File

@ -6,7 +6,6 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import java.util.UUID;
import java.util.stream.Collectors;
import org.bukkit.World;
@ -45,7 +44,7 @@ public class AdminStatsCommand extends CompositeCommand {
for (Entry<World, TopTenData> en : topTenLists.entrySet()) {
user.sendMessage("admin.stats.world", TextVariables.NAME,
level.getPlugin().getIWM().getWorldName(en.getKey()));
Map<UUID, Long> topTen = en.getValue().getTopTen();
Map<String, Long> topTen = en.getValue().getTopTen();
if (topTen.isEmpty()) {
user.sendMessage("admin.stats.no-data");
return false;

View File

@ -2,7 +2,7 @@ package world.bentobox.level.commands;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.Optional;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User;
@ -30,20 +30,16 @@ public class AdminTopCommand extends CompositeCommand {
public boolean execute(User user, String label, List<String> args) {
user.sendMessage("island.top.gui-title");
int rank = 0;
for (Map.Entry<UUID, Long> topTen : levelPlugin.getManager().getTopTen(getWorld(), Level.TEN).entrySet()) {
Island island = getPlugin().getIslands().getIsland(getWorld(), topTen.getKey());
if (island != null) {
for (Map.Entry<String, Long> topTen : levelPlugin.getManager().getTopTen(getWorld(), Level.TEN).entrySet()) {
Optional<Island> is = getPlugin().getIslands().getIslandById(topTen.getKey());
if (is.isPresent()) {
Island island = is.get();
rank++;
user.sendMessage("admin.top.display",
"[rank]",
String.valueOf(rank),
"[name]",
this.getPlugin().getPlayers().getUser(island.getOwner()).getName(),
"[level]",
user.sendMessage("admin.top.display", "[rank]", String.valueOf(rank), "[name]",
this.getPlugin().getPlayers().getUser(island.getOwner()).getName(), "[level]",
String.valueOf(topTen.getValue()));
}
}
return true;
}
}

View File

@ -6,10 +6,12 @@ import java.util.Optional;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.level.Level;
/**
* Removes a player from the top ten
*
* @author tastybento
*
*/
@ -31,8 +33,11 @@ public class AdminTopRemoveCommand extends CompositeCommand {
this.setDescription("admin.top.remove.description");
}
/* (non-Javadoc)
* @see world.bentobox.bentobox.api.commands.BentoBoxCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)
/*
* (non-Javadoc)
*
* @see world.bentobox.bentobox.api.commands.BentoBoxCommand#canExecute(world.
* bentobox.bentobox.api.user.User, java.lang.String, java.util.List)
*/
@Override
public boolean canExecute(User user, String label, List<String> args) {
@ -51,14 +56,18 @@ public class AdminTopRemoveCommand extends CompositeCommand {
@Override
public boolean execute(User user, String label, List<String> args) {
addon.getManager().removeEntry(getWorld(), target.getUniqueId());
// Removes islands that this target is an owner of
getIslands().getIslands(getWorld(), target.getUniqueId()).stream()
.filter(is -> target.getUniqueId().equals(is.getOwner()))
.forEach(island -> addon.getManager().removeEntry(getWorld(), island.getUniqueId()));
user.sendMessage("general.success");
return true;
}
@Override
public Optional<List<String>> tabComplete(User user, String alias, List<String> args) {
return Optional.of(addon.getManager().getTopTen(getWorld(), Level.TEN).keySet().stream().map(addon.getPlayers()::getName)
.filter(n -> !n.isEmpty()).toList());
return Optional.of(addon.getManager().getTopTen(getWorld(), Level.TEN).keySet().stream()
.map(getIslands()::getIslandById).flatMap(Optional::stream).map(Island::getOwner)
.map(addon.getPlayers()::getName).filter(n -> !n.isEmpty()).toList());
}
}

View File

@ -1,7 +1,5 @@
package world.bentobox.level.listeners;
import java.util.UUID;
import org.bukkit.World;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
@ -21,7 +19,9 @@ import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.level.Level;
/**
* Listens for new islands or ownership changes and sets the level to zero automatically
* Listens for new islands or ownership changes and sets the level to zero
* automatically
*
* @author tastybento
*
*/
@ -54,18 +54,14 @@ public class IslandActivitiesListeners implements Listener {
private void zeroIsland(final Island island) {
// Clear the island setting
if (island.getOwner() != null && island.getWorld() != null) {
addon.getPipeliner().zeroIsland(island).thenAccept(results ->
addon.getManager().setInitialIslandLevel(island, results.getLevel()));
addon.getPipeliner().zeroIsland(island)
.thenAccept(results -> addon.getManager().setInitialIslandLevel(island, results.getLevel()));
}
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onIslandDelete(IslandPreclearEvent e) {
// Remove player from the top ten and level
UUID uuid = e.getIsland().getOwner();
World world = e.getIsland().getWorld();
remove(world, uuid);
remove(e.getIsland().getWorld(), e.getIsland().getUniqueId());
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
@ -74,7 +70,7 @@ public class IslandActivitiesListeners implements Listener {
addon.getManager().deleteIsland(e.getIsland().getUniqueId());
}
private void remove(World world, UUID uuid) {
private void remove(World world, String uuid) {
if (uuid != null && world != null) {
addon.getManager().removeEntry(world, uuid);
}
@ -83,43 +79,43 @@ public class IslandActivitiesListeners implements Listener {
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onNewIslandOwner(TeamSetownerEvent e) {
// Remove player from the top ten and level
remove(e.getIsland().getWorld(), e.getIsland().getOwner());
// Remove island from the top ten and level
remove(e.getIsland().getWorld(), e.getIsland().getUniqueId());
}
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onIsland(TeamJoinedEvent e) {
// TODO: anything to do here?
// Remove player from the top ten and level
remove(e.getIsland().getWorld(), e.getPlayerUUID());
// remove(e.getIsland().getWorld(), e.getPlayerUUID());
}
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onIsland(IslandUnregisteredEvent e) {
// Remove player from the top ten
remove(e.getIsland().getWorld(), e.getPlayerUUID());
// Remove island from the top ten
remove(e.getIsland().getWorld(), e.getIsland().getUniqueId());
}
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onIsland(IslandRegisteredEvent e) {
// TODO: anything to do here?
// Remove player from the top ten
remove(e.getIsland().getWorld(), e.getPlayerUUID());
// remove(e.getIsland().getWorld(), e.getPlayerUUID());
}
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onIsland(TeamLeaveEvent e) {
// TODO: anything to do here?
// Remove player from the top ten and level
remove(e.getIsland().getWorld(), e.getPlayerUUID());
// remove(e.getIsland().getWorld(), e.getPlayerUUID());
}
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onIsland(TeamKickEvent e) {
//// TODO: anything to do here?
// Remove player from the top ten and level
remove(e.getIsland().getWorld(), e.getPlayerUUID());
// remove(e.getIsland().getWorld(), e.getPlayerUUID());
}
}

View File

@ -3,56 +3,41 @@ package world.bentobox.level.objects;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;
import org.bukkit.World;
import com.google.gson.annotations.Expose;
import world.bentobox.bentobox.database.objects.DataObject;
import world.bentobox.bentobox.database.objects.Table;
/**
* This class stores the top ten.
*
* @author tastybento
*
*/
@Table(name = "TopTenData")
public class TopTenData implements DataObject {
public class TopTenData {
// UniqueId is the world name
@Expose
private String uniqueId = "";
@Expose
private Map<UUID, Long> topTen = new LinkedHashMap<>();
private Map<String, Long> topTen = new LinkedHashMap<>();
public TopTenData(World k) {
uniqueId = k.getName().toLowerCase(Locale.ENGLISH);
}
@Override
public String getUniqueId() {
// This is the world name
return uniqueId;
}
@Override
public void setUniqueId(String uniqueId) {
// This is the world name - make it always lowercase
this.uniqueId = uniqueId.toLowerCase(Locale.ENGLISH);
}
/**
* @return the topTen
*/
public Map<UUID, Long> getTopTen() {
public Map<String, Long> getTopTen() {
return topTen;
}
/**
* @param topTen the topTen to set
*/
public void setTopTen(Map<UUID, Long> topTen) {
public void setTopTen(Map<String, Long> topTen) {
this.topTen = topTen;
}
}

View File

@ -5,10 +5,11 @@
package world.bentobox.level.panels;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
@ -29,50 +30,44 @@ import world.bentobox.bentobox.managers.RanksManager;
import world.bentobox.level.Level;
import world.bentobox.level.util.Utils;
/**
* This panel opens top likes panel
*/
public class TopLevelPanel
{
public class TopLevelPanel {
// ---------------------------------------------------------------------
// Section: Internal Constructor
// ---------------------------------------------------------------------
/**
* This is internal constructor. It is used internally in current class to avoid creating objects everywhere.
* This is internal constructor. It is used internally in current class to avoid
* creating objects everywhere.
*
* @param addon Level object.
* @param user User who opens Panel.
* @param world World where gui is opened
* @param permissionPrefix Permission Prefix
*/
private TopLevelPanel(Level addon, User user, World world, String permissionPrefix)
{
private TopLevelPanel(Level addon, User user, World world, String permissionPrefix) {
this.addon = addon;
this.user = user;
this.world = world;
this.iconPermission = permissionPrefix + "level.icon";
this.topIslands = this.addon.getManager().getTopTen(this.world, 10).entrySet().stream().
map(entry -> {
Island island = this.addon.getIslandsManager().getIsland(this.world, entry.getKey());
return new IslandTopRecord(island, entry.getValue());
}).
collect(Collectors.toList());
topIslands = new ArrayList<>();
for (Map.Entry<String, Long> en : addon.getManager().getTopTen(this.world, Level.TEN).entrySet()) {
Optional<Island> is = addon.getIslands().getIslandById(en.getKey());
if (is.isPresent()) {
topIslands.add(new IslandTopRecord(is.get(), en.getValue()));
}
}
}
/**
* Build method manages current panel opening. It uses BentoBox PanelAPI that is easy to use and users can get nice
* panels.
* Build method manages current panel opening. It uses BentoBox PanelAPI that is
* easy to use and users can get nice panels.
*/
public void build()
{
public void build() {
TemplatedPanelBuilder panelBuilder = new TemplatedPanelBuilder();
panelBuilder.user(this.user);
@ -87,47 +82,38 @@ public class TopLevelPanel
panelBuilder.build();
}
// ---------------------------------------------------------------------
// Section: Methods
// ---------------------------------------------------------------------
/**
* Creates fallback based on template.
*
* @param template Template record for fallback button.
* @param index Place of the fallback.
* @return Fallback panel item.
*/
private PanelItem createFallback(ItemTemplateRecord template, long index)
{
if (template == null)
{
private PanelItem createFallback(ItemTemplateRecord template, long index) {
if (template == null) {
return null;
}
PanelItemBuilder builder = new PanelItemBuilder();
if (template.icon() != null)
{
if (template.icon() != null) {
builder.icon(template.icon().clone());
}
if (template.title() != null)
{
builder.name(this.user.getTranslation(this.world, template.title(),
TextVariables.NAME, String.valueOf(index)));
}
else
{
builder.name(this.user.getTranslation(this.world, REFERENCE,
TextVariables.NAME, String.valueOf(index)));
if (template.title() != null) {
builder.name(
this.user.getTranslation(this.world, template.title(), TextVariables.NAME, String.valueOf(index)));
} else {
builder.name(this.user.getTranslation(this.world, REFERENCE, TextVariables.NAME, String.valueOf(index)));
}
if (template.description() != null)
{
builder.description(this.user.getTranslation(this.world, template.description(),
TextVariables.NUMBER, String.valueOf(index)));
if (template.description() != null) {
builder.description(this.user.getTranslation(this.world, template.description(), TextVariables.NUMBER,
String.valueOf(index)));
}
builder.amount(index != 0 ? (int) index : 1);
@ -135,46 +121,40 @@ public class TopLevelPanel
return builder.build();
}
/**
* This method creates player icon with warp functionality.
*
* @return PanelItem for PanelBuilder.
*/
private PanelItem createPlayerButton(ItemTemplateRecord template, TemplatedPanel.ItemSlot itemSlot)
{
private PanelItem createPlayerButton(ItemTemplateRecord template, TemplatedPanel.ItemSlot itemSlot) {
int index = (int) template.dataMap().getOrDefault("index", 0);
if (index < 1)
{
if (index < 1) {
return this.createFallback(template.fallback(), index);
}
IslandTopRecord islandTopRecord = this.topIslands.size() < index ? null : this.topIslands.get(index - 1);
if (islandTopRecord == null)
{
if (islandTopRecord == null) {
return this.createFallback(template.fallback(), index);
}
return this.createIslandIcon(template, islandTopRecord, index);
}
/**
* This method creates button from template for given island top record.
*
* @param template Icon Template.
* @param islandTopRecord Island Top Record.
* @param index Place Index.
* @return PanelItem for PanelBuilder.
*/
private PanelItem createIslandIcon(ItemTemplateRecord template, IslandTopRecord islandTopRecord, int index)
{
private PanelItem createIslandIcon(ItemTemplateRecord template, IslandTopRecord islandTopRecord, int index) {
// Get player island.
Island island = islandTopRecord.island();
if (island == null)
{
if (island == null) {
return this.createFallback(template.fallback(), index);
}
@ -189,23 +169,19 @@ public class TopLevelPanel
// Get only possible actions, by removing all inactive ones.
List<ItemTemplateRecord.ActionRecords> activeActions = new ArrayList<>(template.actions());
activeActions.removeIf(action ->
{
switch (action.actionType().toUpperCase())
{
activeActions.removeIf(action -> {
switch (action.actionType().toUpperCase()) {
case "WARP" -> {
return island.getOwner() == null ||
this.addon.getWarpHook() == null ||
!this.addon.getWarpHook().getWarpSignsManager().hasWarp(this.world, island.getOwner());
return island.getOwner() == null || this.addon.getWarpHook() == null
|| !this.addon.getWarpHook().getWarpSignsManager().hasWarp(this.world, island.getOwner());
}
case "VISIT" -> {
return island.getOwner() == null ||
this.addon.getVisitHook() == null ||
!this.addon.getVisitHook().getAddonManager().preprocessTeleportation(this.user, island);
return island.getOwner() == null || this.addon.getVisitHook() == null
|| !this.addon.getVisitHook().getAddonManager().preprocessTeleportation(this.user, island);
}
case "VIEW" -> {
return island.getOwner() == null ||
!island.getMemberSet(RanksManager.MEMBER_RANK).contains(this.user.getUniqueId());
return island.getOwner() == null
|| !island.getMemberSet(RanksManager.MEMBER_RANK).contains(this.user.getUniqueId());
}
default -> {
return false;
@ -214,33 +190,30 @@ public class TopLevelPanel
});
// Add Click handler
builder.clickHandler((panel, user, clickType, i) ->
{
for (ItemTemplateRecord.ActionRecords action : activeActions)
{
if (clickType == action.clickType() || action.clickType() == ClickType.UNKNOWN)
{
switch (action.actionType().toUpperCase())
{
builder.clickHandler((panel, user, clickType, i) -> {
for (ItemTemplateRecord.ActionRecords action : activeActions) {
if (clickType == action.clickType() || action.clickType() == ClickType.UNKNOWN) {
switch (action.actionType().toUpperCase()) {
case "WARP" -> {
this.user.closeInventory();
this.addon.getWarpHook().getWarpSignsManager().warpPlayer(this.world, this.user, island.getOwner());
this.addon.getWarpHook().getWarpSignsManager().warpPlayer(this.world, this.user,
island.getOwner());
}
case "VISIT" ->
// The command call implementation solves necessity to check for all visits options,
// like cool down, confirmation and preprocess in single go. Would it be better to write
// The command call implementation solves necessity to check for all visits
// options,
// like cool down, confirmation and preprocess in single go. Would it be better
// to write
// all logic here?
this.addon.getPlugin().getIWM().getAddon(this.world).
flatMap(GameModeAddon::getPlayerCommand).ifPresent(command ->
{
String mainCommand =
this.addon.getVisitHook().getSettings().getPlayerMainCommand();
this.addon.getPlugin().getIWM().getAddon(this.world).flatMap(GameModeAddon::getPlayerCommand)
.ifPresent(command -> {
String mainCommand = this.addon.getVisitHook().getSettings().getPlayerMainCommand();
if (!mainCommand.isBlank())
{
if (!mainCommand.isBlank()) {
this.user.closeInventory();
this.user.performCommand(command.getTopLabel() + " " + mainCommand + " " + island.getOwner());
this.user.performCommand(
command.getTopLabel() + " " + mainCommand + " " + island.getOwner());
}
});
@ -262,15 +235,12 @@ public class TopLevelPanel
});
// Collect tooltips.
List<String> tooltips = activeActions.stream().
filter(action -> action.tooltip() != null).
map(action -> this.user.getTranslation(this.world, action.tooltip())).
filter(text -> !text.isBlank()).
collect(Collectors.toCollection(() -> new ArrayList<>(template.actions().size())));
List<String> tooltips = activeActions.stream().filter(action -> action.tooltip() != null)
.map(action -> this.user.getTranslation(this.world, action.tooltip())).filter(text -> !text.isBlank())
.collect(Collectors.toCollection(() -> new ArrayList<>(template.actions().size())));
// Add tooltips.
if (!tooltips.isEmpty())
{
if (!tooltips.isEmpty()) {
// Empty line and tooltips.
builder.description("");
builder.description(tooltips);
@ -279,104 +249,75 @@ public class TopLevelPanel
return builder.build();
}
/**
* Populate given panel item builder name with values from template and island objects.
* Populate given panel item builder name with values from template and island
* objects.
*
* @param builder the builder
* @param template the template
* @param island the island
*/
private void populateIslandTitle(PanelItemBuilder builder,
ItemTemplateRecord template,
Island island)
{
private void populateIslandTitle(PanelItemBuilder builder, ItemTemplateRecord template, Island island) {
// Get Island Name
String nameText;
if (island.getName() == null || island.getName().isEmpty())
{
nameText = this.user.getTranslation(REFERENCE + "owners-island",
PLAYER,
island.getOwner() == null ?
this.user.getTranslation(REFERENCE + "unknown") :
this.addon.getPlayers().getName(island.getOwner()));
}
else
{
if (island.getName() == null || island.getName().isEmpty()) {
nameText = this.user.getTranslation(REFERENCE + "owners-island", PLAYER,
island.getOwner() == null ? this.user.getTranslation(REFERENCE + "unknown")
: this.addon.getPlayers().getName(island.getOwner()));
} else {
nameText = island.getName();
}
// Template specific title is always more important than custom one.
if (template.title() != null && !template.title().isBlank())
{
builder.name(this.user.getTranslation(this.world, template.title(),
TextVariables.NAME, nameText));
}
else
{
if (template.title() != null && !template.title().isBlank()) {
builder.name(this.user.getTranslation(this.world, template.title(), TextVariables.NAME, nameText));
} else {
builder.name(this.user.getTranslation(REFERENCE + "name", TextVariables.NAME, nameText));
}
}
/**
* Populate given panel item builder icon with values from template and island objects.
* Populate given panel item builder icon with values from template and island
* objects.
*
* @param builder the builder
* @param template the template
* @param island the island
*/
private void populateIslandIcon(PanelItemBuilder builder,
ItemTemplateRecord template,
Island island)
{
private void populateIslandIcon(PanelItemBuilder builder, ItemTemplateRecord template, Island island) {
User owner = island.getOwner() == null ? null : User.getInstance(island.getOwner());
// Get permission or island icon
String permissionIcon = owner == null ? null :
Utils.getPermissionValue(owner, this.iconPermission, null);
String permissionIcon = owner == null ? null : Utils.getPermissionValue(owner, this.iconPermission, null);
Material material;
if (permissionIcon != null && !permissionIcon.equals("*"))
{
if (permissionIcon != null && !permissionIcon.equals("*")) {
material = Material.matchMaterial(permissionIcon);
}
else
{
} else {
material = null;
}
if (material != null)
{
if (!material.equals(Material.PLAYER_HEAD))
{
if (material != null) {
if (!material.equals(Material.PLAYER_HEAD)) {
builder.icon(material);
}
else
{
} else {
builder.icon(owner.getName());
}
}
else if (template.icon() != null)
{
} else if (template.icon() != null) {
builder.icon(template.icon().clone());
}
else if (owner != null)
{
} else if (owner != null) {
builder.icon(owner.getName());
}
else
{
} else {
builder.icon(Material.PLAYER_HEAD);
}
}
/**
* Populate given panel item builder description with values from template and island objects.
* Populate given panel item builder description with values from template and
* island objects.
*
* @param builder the builder
* @param template the template
@ -384,94 +325,72 @@ public class TopLevelPanel
* @param islandTopRecord the top record object
* @param index place index.
*/
private void populateIslandDescription(PanelItemBuilder builder,
ItemTemplateRecord template,
Island island,
IslandTopRecord islandTopRecord,
int index)
{
private void populateIslandDescription(PanelItemBuilder builder, ItemTemplateRecord template, Island island,
IslandTopRecord islandTopRecord, int index) {
// Get Owner Name
String ownerText = this.user.getTranslation(REFERENCE + "owner",
PLAYER,
island.getOwner() == null ?
this.user.getTranslation(REFERENCE + "unknown") :
this.addon.getPlayers().getName(island.getOwner()));
String ownerText = this.user.getTranslation(REFERENCE + "owner", PLAYER,
island.getOwner() == null ? this.user.getTranslation(REFERENCE + "unknown")
: this.addon.getPlayers().getName(island.getOwner()));
// Get Members Text
String memberText;
if (island.getMemberSet().size() > 1)
{
if (island.getMemberSet().size() > 1) {
StringBuilder memberBuilder = new StringBuilder(
this.user.getTranslationOrNothing(REFERENCE + "members-title"));
for (UUID uuid : island.getMemberSet())
{
for (UUID uuid : island.getMemberSet()) {
User member = User.getInstance(uuid);
if (memberBuilder.length() > 0)
{
if (memberBuilder.length() > 0) {
memberBuilder.append("\n");
}
memberBuilder.append(
this.user.getTranslationOrNothing(REFERENCE + "member",
PLAYER, member.getName()));
memberBuilder.append(this.user.getTranslationOrNothing(REFERENCE + "member", PLAYER, member.getName()));
}
memberText = memberBuilder.toString();
}
else
{
} else {
memberText = "";
}
String placeText = this.user.getTranslation(REFERENCE + "place",
TextVariables.NUMBER, String.valueOf(index));
String placeText = this.user.getTranslation(REFERENCE + "place", TextVariables.NUMBER, String.valueOf(index));
String levelText = this.user.getTranslation(REFERENCE + "level",
TextVariables.NUMBER, this.addon.getManager().formatLevel(islandTopRecord.level()));
String levelText = this.user.getTranslation(REFERENCE + "level", TextVariables.NUMBER,
this.addon.getManager().formatLevel(islandTopRecord.level()));
// Template specific description is always more important than custom one.
if (template.description() != null && !template.description().isBlank())
{
builder.description(this.user.getTranslation(this.world, template.description(),
"[owner]", ownerText,
"[members]", memberText,
"[level]", levelText,
"[place]", placeText).
replaceAll("(?m)^[ \\t]*\\r?\\n", "").
replaceAll("(?<!\\\\)\\|", "\n").
replace("\\\\\\|", "|")); // Not a regex - replace is more efficient
}
else
{
if (template.description() != null && !template.description().isBlank()) {
builder.description(this.user
.getTranslation(this.world, template.description(), "[owner]", ownerText, "[members]", memberText,
"[level]", levelText, "[place]", placeText)
.replaceAll("(?m)^[ \\t]*\\r?\\n", "").replaceAll("(?<!\\\\)\\|", "\n").replace("\\\\\\|", "|")); // Not
// a
// regex
// -
// replace
// is
// more
// efficient
} else {
// Now combine everything.
String descriptionText = this.user.getTranslation(REFERENCE + "description",
"[owner]", ownerText,
"[members]", memberText,
"[level]", levelText,
"[place]", placeText);
String descriptionText = this.user.getTranslation(REFERENCE + "description", "[owner]", ownerText,
"[members]", memberText, "[level]", levelText, "[place]", placeText);
builder.description(descriptionText.
replaceAll("(?m)^[ \\t]*\\r?\\n", "").
replaceAll("(?<!\\\\)\\|", "\n").
replace("\\\\\\|", "|")); // Not a regex - replace is more efficient
builder.description(descriptionText.replaceAll("(?m)^[ \\t]*\\r?\\n", "").replaceAll("(?<!\\\\)\\|", "\n")
.replace("\\\\\\|", "|")); // Not a regex - replace is more efficient
}
}
/**
* Create viewer button panel item.
*
* @return PanelItem for PanelBuilder.
*/
private PanelItem createViewerButton(ItemTemplateRecord template, TemplatedPanel.ItemSlot itemSlot)
{
private PanelItem createViewerButton(ItemTemplateRecord template, TemplatedPanel.ItemSlot itemSlot) {
Island island = this.addon.getIslands().getIsland(this.world, this.user);
if (island == null || island.getOwner() == null)
{
if (island == null || island.getOwner() == null) {
// Player do not have an island.
return null;
}
@ -484,18 +403,16 @@ public class TopLevelPanel
return this.createIslandIcon(template, topRecord, place);
}
/**
* This method is used to open UserPanel outside this class. It will be much easier to open panel with single method
* call then initializing new object.
* This method is used to open UserPanel outside this class. It will be much
* easier to open panel with single method call then initializing new object.
*
* @param addon Level Addon object
* @param user User who opens panel
* @param world World where gui is opened
* @param permissionPrefix Permission Prefix
*/
public static void openPanel(Level addon, User user, World world, String permissionPrefix)
{
public static void openPanel(Level addon, User user, World world, String permissionPrefix) {
new TopLevelPanel(addon, user, world, permissionPrefix).build();
}
@ -512,9 +429,12 @@ public class TopLevelPanel
/**
* This record is used internally. It converts user -> level to island -> level.
*
* @param island island
* @param level level
*/
private record IslandTopRecord(Island island, Long level) {}
private record IslandTopRecord(Island island, Long level) {
}
// ---------------------------------------------------------------------
// Section: Variables

View File

@ -160,7 +160,7 @@ public class LevelsManagerTest {
when(island.getMemberSet()).thenReturn(iset);
when(island.getOwner()).thenReturn(uuid);
when(island.getWorld()).thenReturn(world);
when(island.getUniqueId()).thenReturn(UUID.randomUUID().toString());
when(island.getUniqueId()).thenReturn(uuid.toString());
// Default to uuid's being island owners
when(im.hasIsland(eq(world), any(UUID.class))).thenReturn(true);
when(im.getIsland(world, uuid)).thenReturn(island);
@ -347,7 +347,7 @@ public class LevelsManagerTest {
*/
@Test
public void testGetTopTenEmpty() {
Map<UUID, Long> tt = lm.getTopTen(world, Level.TEN);
Map<String, Long> tt = lm.getTopTen(world, Level.TEN);
assertTrue(tt.isEmpty());
}
@ -358,23 +358,12 @@ public class LevelsManagerTest {
@Test
public void testGetTopTen() {
testLoadTopTens();
Map<UUID, Long> tt = lm.getTopTen(world, Level.TEN);
Map<String, Long> tt = lm.getTopTen(world, Level.TEN);
assertFalse(tt.isEmpty());
assertEquals(1, tt.size());
assertEquals(1, lm.getTopTen(world, 1).size());
}
/**
* Test method for {@link world.bentobox.level.LevelsManager#getTopTen(org.bukkit.World, int)}.
*/
@Test
public void testGetTopTenNoOwners() {
when(im.hasIsland(eq(world), any(UUID.class))).thenReturn(false);
testLoadTopTens();
Map<UUID, Long> tt = lm.getTopTen(world, Level.TEN);
assertTrue(tt.isEmpty());
}
/**
* Test method for
* {@link world.bentobox.level.LevelsManager#hasTopTenPerm(org.bukkit.World, java.util.UUID)}.
@ -407,11 +396,11 @@ public class LevelsManagerTest {
@Test
public void testRemoveEntry() {
testLoadTopTens();
Map<UUID, Long> tt = lm.getTopTen(world, Level.TEN);
assertTrue(tt.containsKey(uuid));
lm.removeEntry(world, uuid);
Map<String, Long> tt = lm.getTopTen(world, Level.TEN);
assertTrue(tt.containsKey(uuid.toString()));
lm.removeEntry(world, uuid.toString());
tt = lm.getTopTen(world, Level.TEN);
assertFalse(tt.containsKey(uuid));
assertFalse(tt.containsKey(uuid.toString()));
}
/**
@ -443,15 +432,15 @@ public class LevelsManagerTest {
public void testGetRank() {
lm.createAndCleanRankings(world);
Map<World, TopTenData> ttl = lm.getTopTenLists();
Map<UUID, Long> tt = ttl.get(world).getTopTen();
Map<String, Long> tt = ttl.get(world).getTopTen();
for (long i = 100; i < 150; i++) {
tt.put(UUID.randomUUID(), i);
tt.put(UUID.randomUUID().toString(), i);
}
// Put player as lowest rank
tt.put(uuid, 10L);
// Put island as lowest rank
tt.put(uuid.toString(), 10L);
assertEquals(51, lm.getRank(world, uuid));
// Put player as highest rank
tt.put(uuid, 1000L);
// Put island as highest rank
tt.put(uuid.toString(), 1000L);
assertEquals(1, lm.getRank(world, uuid));
// Unknown UUID - lowest rank + 1
assertEquals(52, lm.getRank(world, UUID.randomUUID()));

View File

@ -3,6 +3,7 @@ package world.bentobox.level;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
@ -10,9 +11,10 @@ import static org.mockito.Mockito.when;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
@ -54,7 +56,7 @@ public class PlaceholderManagerTest {
@Mock
private BentoBox plugin;
private PlaceholderManager pm;
private PlaceholderManager phm;
@Mock
private PlaceholdersManager bpm;
@Mock
@ -67,13 +69,24 @@ public class PlaceholderManagerTest {
private Island island;
@Mock
private User user;
private Map<UUID, String> names = new HashMap<>();
private static final List<String> NAMES = List.of("tasty", "bento", "fred", "bonne", "cyprien", "mael", "joe", "horacio", "steph", "vicky");
private Map<UUID, Island> islands = new HashMap<>();
private Map<UUID, Long> map = new HashMap<>();
private static final Map<UUID, String> names = new LinkedHashMap<>();
static {
names.put(UUID.randomUUID(), "tasty");
names.put(UUID.randomUUID(), "bento");
names.put(UUID.randomUUID(), "fred");
names.put(UUID.randomUUID(), "bonne");
names.put(UUID.randomUUID(), "cyprien");
names.put(UUID.randomUUID(), "mael");
names.put(UUID.randomUUID(), "joe");
names.put(UUID.randomUUID(), "horacio");
names.put(UUID.randomUUID(), "steph");
names.put(UUID.randomUUID(), "vicky");
}
private Map<String, Island> islands = new HashMap<>();
private Map<String, Long> map = new HashMap<>();
private @NonNull IslandLevels data;
@Mock
private PlayersManager players;
private PlayersManager pm;
/**
* @throws java.lang.Exception
@ -83,29 +96,31 @@ public class PlaceholderManagerTest {
when(addon.getPlugin()).thenReturn(plugin);
// Users
when(addon.getPlayers()).thenReturn(players);
when(addon.getPlayers()).thenReturn(pm);
// Users
when(user.getWorld()).thenReturn(world);
when(user.getLocation()).thenReturn(mock(Location.class));
for (int i = 0; i < Level.TEN; i++) {
UUID uuid = UUID.randomUUID();
names.put(uuid, NAMES.get(i));
map.put(uuid, (long)(100 - i));
int i = 0;
for (Entry<UUID, String> n : names.entrySet()) {
UUID uuid = UUID.randomUUID(); // Random island ID
map.put(uuid.toString(), (long)(100 - i++)); // level
Island is = new Island();
is.setOwner(uuid);
is.setName(NAMES.get(i) + "'s island");
islands.put(uuid, is);
is.setUniqueId(uuid.toString());
is.setOwner(n.getKey());
is.setName(n.getValue() + "'s island");
islands.put(uuid.toString(), is);
}
// Sort
map = map.entrySet().stream()
.sorted(Collections.reverseOrder(Map.Entry.comparingByValue()))
.collect(Collectors.toMap( Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
when(players.getName(any())).thenAnswer((Answer<String>) invocation -> names.getOrDefault(invocation.getArgument(0, UUID.class), "unknown"));
when(pm.getName(any())).thenAnswer((Answer<String>) invocation -> names.getOrDefault(invocation.getArgument(0, UUID.class), "unknown"));
Map<UUID, Integer> members = new HashMap<>();
map.forEach((uuid, l) -> members.put(uuid, RanksManager.MEMBER_RANK));
islands.values().forEach(i -> i.setMembers(members));
names.forEach((uuid, l) -> members.put(uuid, RanksManager.MEMBER_RANK));
islands.values().forEach(is -> is.setMembers(members));
// Placeholders manager for plugin
@ -120,7 +135,8 @@ public class PlaceholderManagerTest {
// Islands
when(im.getIsland(any(World.class), any(User.class))).thenReturn(island);
when(im.getIslandAt(any(Location.class))).thenReturn(Optional.of(island));
when(im.getIsland(any(World.class), any(UUID.class))).thenAnswer((Answer<Island>) invocation -> islands.get(invocation.getArgument(1, UUID.class)));
when(im.getIslandById(anyString())).thenAnswer((Answer<Optional<Island>>) invocation -> Optional.of(islands.get(invocation.getArgument(0, String.class))));
when(im.getIslands(any(), any(UUID.class))).thenReturn(new HashSet<>(islands.values()));
when(addon.getIslands()).thenReturn(im);
// Levels Manager
@ -136,11 +152,12 @@ public class PlaceholderManagerTest {
when(lm.getLevelsData(island)).thenReturn(data);
when(addon.getManager()).thenReturn(lm);
pm = new PlaceholderManager(addon);
phm = new PlaceholderManager(addon);
}
/**
* Test method for {@link world.bentobox.level.PlaceholderManager#PlaceholderManager(world.bentobox.level.Level)}.
* Test method for
* {@link world.bentobox.level.PlaceholderManager#PlaceholderManager(world.bentobox.level.Level)}.
*/
@Test
public void testPlaceholderManager() {
@ -148,11 +165,12 @@ public class PlaceholderManagerTest {
}
/**
* Test method for {@link world.bentobox.level.PlaceholderManager#registerPlaceholders(world.bentobox.bentobox.api.addons.GameModeAddon)}.
* Test method for
* {@link world.bentobox.level.PlaceholderManager#registerPlaceholders(world.bentobox.bentobox.api.addons.GameModeAddon)}.
*/
@Test
public void testRegisterPlaceholders() {
pm.registerPlaceholders(gm);
phm.registerPlaceholders(gm);
// Island Level
verify(bpm).registerPlaceholder(eq(addon), eq("aoneblock_island_level"), any());
verify(bpm).registerPlaceholder(eq(addon), eq("aoneblock_island_level_raw"), any());
@ -182,78 +200,83 @@ public class PlaceholderManagerTest {
}
/**
* Test method for {@link world.bentobox.level.PlaceholderManager#getRankName(org.bukkit.World, int)}.
* Test method for
* {@link world.bentobox.level.PlaceholderManager#getRankName(org.bukkit.World, int)}.
*/
@Test
public void testGetRankName() {
// Test extremes
assertEquals("tasty", pm.getRankName(world, 0));
assertEquals("vicky", pm.getRankName(world, 100));
assertEquals("tasty", phm.getRankName(world, 0));
assertEquals("vicky", phm.getRankName(world, 100));
// Test the ranks
int rank = 1;
for (String name : NAMES) {
assertEquals(name, pm.getRankName(world, rank++));
for (String name : names.values()) {
assertEquals(name, phm.getRankName(world, rank++));
}
}
/**
* Test method for {@link world.bentobox.level.PlaceholderManager#getRankIslandName(org.bukkit.World, int)}.
* Test method for
* {@link world.bentobox.level.PlaceholderManager#getRankIslandName(org.bukkit.World, int)}.
*/
@Test
public void testGetRankIslandName() {
// Test extremes
assertEquals("tasty's island", pm.getRankIslandName(world, 0));
assertEquals("vicky's island", pm.getRankIslandName(world, 100));
assertEquals("tasty's island", phm.getRankIslandName(world, 0));
assertEquals("vicky's island", phm.getRankIslandName(world, 100));
// Test the ranks
int rank = 1;
for (String name : NAMES) {
assertEquals(name + "'s island", pm.getRankIslandName(world, rank++));
for (String name : names.values()) {
assertEquals(name + "'s island", phm.getRankIslandName(world, rank++));
}
}
/**
* Test method for {@link world.bentobox.level.PlaceholderManager#getRankMembers(org.bukkit.World, int)}.
* Test method for
* {@link world.bentobox.level.PlaceholderManager#getRankMembers(org.bukkit.World, int)}.
*/
@Test
public void testGetRankMembers() {
// Test extremes
check(1, pm.getRankMembers(world, 0));
check(2, pm.getRankMembers(world, 100));
check(1, phm.getRankMembers(world, 0));
check(2, phm.getRankMembers(world, 100));
// Test the ranks
for (int rank = 1; rank < 11; rank++) {
check(3, pm.getRankMembers(world, rank));
check(3, phm.getRankMembers(world, rank));
}
}
void check(int indicator, String list) {
for (String n : NAMES) {
assertTrue(n + " is missing for twst " + indicator, list.contains(n));
for (String n : names.values()) {
assertTrue(n + " is missing for test " + indicator, list.contains(n));
}
}
/**
* Test method for {@link world.bentobox.level.PlaceholderManager#getRankLevel(org.bukkit.World, int)}.
* Test method for
* {@link world.bentobox.level.PlaceholderManager#getRankLevel(org.bukkit.World, int)}.
*/
@Test
public void testGetRankLevel() {
// Test extremes
assertEquals("100", pm.getRankLevel(world, 0));
assertEquals("91", pm.getRankLevel(world, 100));
assertEquals("100", phm.getRankLevel(world, 0));
assertEquals("91", phm.getRankLevel(world, 100));
// Test the ranks
for (int rank = 1; rank < 11; rank++) {
assertEquals(String.valueOf(101 - rank), pm.getRankLevel(world, rank));
assertEquals(String.valueOf(101 - rank), phm.getRankLevel(world, rank));
}
}
/**
* Test method for {@link world.bentobox.level.PlaceholderManager#getVisitedIslandLevel(world.bentobox.bentobox.api.addons.GameModeAddon, world.bentobox.bentobox.api.user.User)}.
* Test method for
* {@link world.bentobox.level.PlaceholderManager#getVisitedIslandLevel(world.bentobox.bentobox.api.addons.GameModeAddon, world.bentobox.bentobox.api.user.User)}.
*/
@Test
public void testGetVisitedIslandLevelNullUser() {
assertEquals("", pm.getVisitedIslandLevel(gm, null));
assertEquals("", phm.getVisitedIslandLevel(gm, null));
}
@ -264,16 +287,17 @@ public class PlaceholderManagerTest {
public void testGetVisitedIslandLevelUserNotInWorld() {
// Another world
when(user.getWorld()).thenReturn(mock(World.class));
assertEquals("", pm.getVisitedIslandLevel(gm, user));
assertEquals("", phm.getVisitedIslandLevel(gm, user));
}
/**
* Test method for {@link world.bentobox.level.PlaceholderManager#getVisitedIslandLevel(world.bentobox.bentobox.api.addons.GameModeAddon, world.bentobox.bentobox.api.user.User)}.
* Test method for
* {@link world.bentobox.level.PlaceholderManager#getVisitedIslandLevel(world.bentobox.bentobox.api.addons.GameModeAddon, world.bentobox.bentobox.api.user.User)}.
*/
@Test
public void testGetVisitedIslandLevel() {
assertEquals("1234567", pm.getVisitedIslandLevel(gm, user));
assertEquals("1234567", phm.getVisitedIslandLevel(gm, user));
}
@ -283,7 +307,7 @@ public class PlaceholderManagerTest {
@Test
public void testGetVisitedIslandLevelNoIsland() {
when(im.getIslandAt(any(Location.class))).thenReturn(Optional.empty());
assertEquals("0", pm.getVisitedIslandLevel(gm, user));
assertEquals("0", phm.getVisitedIslandLevel(gm, user));
}

View File

@ -129,10 +129,10 @@ public class AdminStatsCommandTest {
// Top ten
ttd = new TopTenData(world);
Map<UUID, Long> topten = new HashMap<>();
Map<String, Long> topten = new HashMap<>();
Random r = new Random();
for (int i = 0; i < 1000; i++) {
topten.put(UUID.randomUUID(), r.nextLong(20000));
topten.put(UUID.randomUUID().toString(), r.nextLong(20000));
}
ttd.setTopTen(topten);
asc = new AdminStatsCommand(addon, ic);

View File

@ -5,12 +5,12 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.Collections;
import java.util.Set;
import java.util.UUID;
import org.bukkit.Bukkit;
@ -89,6 +89,7 @@ public class AdminTopRemoveCommandTest {
BentoBox plugin = mock(BentoBox.class);
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
User.setPlugin(plugin);
// Addon
when(ic.getAddon()).thenReturn(addon);
when(ic.getPermissionPrefix()).thenReturn("bskyblock.");
@ -114,6 +115,13 @@ public class AdminTopRemoveCommandTest {
uuid = UUID.randomUUID();
when(user.getUniqueId()).thenReturn(uuid);
when(user.getTranslation(any())).thenAnswer(invocation -> invocation.getArgument(0, String.class));
// Island
when(island.getUniqueId()).thenReturn(uuid.toString());
when(island.getOwner()).thenReturn(uuid);
// Island Manager
when(plugin.getIslands()).thenReturn(im);
when(im.getIslands(any(), any(User.class))).thenReturn(Set.of(island));
when(im.getIslands(any(), any(UUID.class))).thenReturn(Set.of(island));
// Bukkit
PowerMockito.mockStatic(Bukkit.class);
@ -193,7 +201,7 @@ public class AdminTopRemoveCommandTest {
public void testExecuteUserStringListOfString() {
testCanExecuteKnown();
assertTrue(atrc.execute(user, "delete", Collections.singletonList("tastybento")));
verify(manager).removeEntry(any(World.class), eq(uuid));
verify(manager).removeEntry(world, uuid.toString());
verify(user).sendMessage("general.success");
}