mirror of
https://github.com/BentoBoxWorld/Level.git
synced 2024-11-22 02:25:56 +01:00
Refactor placeholders (#279)
* Update ReadMe * Fix Jacoco * Remove unused imports * Remove placeholders from main class Created a separate class for cleaner code and added a test class.
This commit is contained in:
parent
0cdb15403b
commit
713a409584
14
README.md
14
README.md
@ -1,11 +1,19 @@
|
||||
# Level
|
||||
[![Build Status](https://ci.codemc.org/buildStatus/icon?job=BentoBoxWorld/Level)](https://ci.codemc.org/job/BentoBoxWorld/job/Level/)
|
||||
[![Build Status](https://ci.codemc.org/buildStatus/icon?job=BentoBoxWorld/Level)](https://ci.codemc.org/job/BentoBoxWorld/job/Level/)[
|
||||
![Bugs](https://sonarcloud.io/api/project_badges/measure?project=BentoBoxWorld_Level&metric=bugs)](https://sonarcloud.io/dashboard?id=BentoBoxWorld_Level)
|
||||
[![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=BentoBoxWorld_Level&metric=reliability_rating)](https://sonarcloud.io/dashboard?id=BentoBoxWorld_Level)
|
||||
[![Lines of Code](https://sonarcloud.io/api/project_badges/measure?project=BentoBoxWorld_Level&metric=ncloc)](https://sonarcloud.io/dashboard?id=BentoBoxWorld_Level)
|
||||
|
||||
## Note: Java 16 and Minecraft 17, or higher are now required
|
||||
## About
|
||||
|
||||
Add-on for BentoBox to calculate island levels for BSkyBlock and AcidIsland. This add-on will work
|
||||
Add-on for BentoBox to calculate island levels for BentoBox game modes like BSkyBlock and AcidIsland. It counts blocks and assigns a value to them.
|
||||
Players gain levels by accumulating points and can lose levels too if their points go down. This add-on will work
|
||||
for game modes listed in the config.yml.
|
||||
|
||||
Full documentation for Level can be found at [docs.bentobox.world](https://docs.bentobox.world/en/latest/addons/Level/).
|
||||
|
||||
Official download releases are at [download.bentobox.world](download.bentobox.world).
|
||||
|
||||
## How to use
|
||||
|
||||
1. Place the level addon jar in the addons folder of the BentoBox plugin
|
||||
|
@ -3,11 +3,8 @@ package world.bentobox.level;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.World;
|
||||
@ -22,7 +19,6 @@ import world.bentobox.bentobox.api.addons.GameModeAddon;
|
||||
import world.bentobox.bentobox.api.configuration.Config;
|
||||
import world.bentobox.bentobox.api.user.User;
|
||||
import world.bentobox.bentobox.database.objects.Island;
|
||||
import world.bentobox.bentobox.managers.RanksManager;
|
||||
import world.bentobox.bentobox.util.Util;
|
||||
import world.bentobox.level.calculators.Pipeliner;
|
||||
import world.bentobox.level.commands.AdminLevelCommand;
|
||||
@ -37,9 +33,7 @@ import world.bentobox.level.config.ConfigSettings;
|
||||
import world.bentobox.level.listeners.IslandActivitiesListeners;
|
||||
import world.bentobox.level.listeners.JoinLeaveListener;
|
||||
import world.bentobox.level.listeners.MigrationListener;
|
||||
import world.bentobox.level.objects.IslandLevels;
|
||||
import world.bentobox.level.objects.LevelsData;
|
||||
import world.bentobox.level.objects.TopTenData;
|
||||
import world.bentobox.level.requests.LevelRequestHandler;
|
||||
import world.bentobox.level.requests.TopTenRequestHandler;
|
||||
import world.bentobox.visit.VisitAddon;
|
||||
@ -121,7 +115,7 @@ public class Level extends Addon {
|
||||
.forEach(gm -> {
|
||||
log("Level hooking into " + gm.getDescription().getName());
|
||||
registerCommands(gm);
|
||||
registerPlaceholders(gm);
|
||||
new PlaceholderManager(this).registerPlaceholders(gm);
|
||||
registeredGameModes.add(gm);
|
||||
});
|
||||
// Register request handlers
|
||||
@ -212,130 +206,6 @@ public class Level extends Addon {
|
||||
return comparisonResult;
|
||||
}
|
||||
|
||||
private void registerPlaceholders(GameModeAddon gm) {
|
||||
if (getPlugin().getPlaceholdersManager() == null) return;
|
||||
// Island Level
|
||||
getPlugin().getPlaceholdersManager().registerPlaceholder(this,
|
||||
gm.getDescription().getName().toLowerCase() + "_island_level",
|
||||
user -> getManager().getIslandLevelString(gm.getOverWorld(), user.getUniqueId()));
|
||||
getPlugin().getPlaceholdersManager().registerPlaceholder(this,
|
||||
gm.getDescription().getName().toLowerCase() + "_island_level_raw",
|
||||
user -> String.valueOf(getManager().getIslandLevel(gm.getOverWorld(), user.getUniqueId())));
|
||||
getPlugin().getPlaceholdersManager().registerPlaceholder(this,
|
||||
gm.getDescription().getName().toLowerCase() + "_island_total_points",
|
||||
user -> {
|
||||
IslandLevels data = getManager().getLevelsData(this.getIslands().getIsland(gm.getOverWorld(), user));
|
||||
return data.getTotalPoints()+"";
|
||||
});
|
||||
|
||||
getPlugin().getPlaceholdersManager().registerPlaceholder(this,
|
||||
gm.getDescription().getName().toLowerCase() + "_points_to_next_level",
|
||||
user -> getManager().getPointsToNextString(gm.getOverWorld(), user.getUniqueId()));
|
||||
getPlugin().getPlaceholdersManager().registerPlaceholder(this,
|
||||
gm.getDescription().getName().toLowerCase() + "_island_level_max",
|
||||
user -> String.valueOf(getManager().getIslandMaxLevel(gm.getOverWorld(), user.getUniqueId())));
|
||||
|
||||
// Visited Island Level
|
||||
getPlugin().getPlaceholdersManager().registerPlaceholder(this,
|
||||
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
|
||||
getPlugin().getPlaceholdersManager().registerPlaceholder(this,
|
||||
gm.getDescription().getName().toLowerCase() + "_top_name_" + i, u -> getRankName(gm.getOverWorld(), rank));
|
||||
// Island Name
|
||||
getPlugin().getPlaceholdersManager().registerPlaceholder(this,
|
||||
gm.getDescription().getName().toLowerCase() + "_top_island_name_" + i, u -> getRankIslandName(gm.getOverWorld(), rank));
|
||||
// Members
|
||||
getPlugin().getPlaceholdersManager().registerPlaceholder(this,
|
||||
gm.getDescription().getName().toLowerCase() + "_top_members_" + i, u -> getRankMembers(gm.getOverWorld(), rank));
|
||||
// Level
|
||||
getPlugin().getPlaceholdersManager().registerPlaceholder(this,
|
||||
gm.getDescription().getName().toLowerCase() + "_top_value_" + i, u -> getRankLevel(gm.getOverWorld(), rank));
|
||||
}
|
||||
|
||||
// Personal rank
|
||||
getPlugin().getPlaceholdersManager().registerPlaceholder(this,
|
||||
gm.getDescription().getName().toLowerCase() + "_rank_value", u -> getRankValue(gm.getOverWorld(), u));
|
||||
}
|
||||
|
||||
String getRankName(World world, int rank) {
|
||||
if (rank < 1) rank = 1;
|
||||
if (rank > TEN) rank = TEN;
|
||||
return getPlayers().getName(getManager().getTopTen(world, TEN).keySet().stream().skip(rank - 1L).limit(1L).findFirst().orElse(null));
|
||||
}
|
||||
|
||||
String getRankIslandName(World world, int rank) {
|
||||
if (rank < 1) rank = 1;
|
||||
if (rank > TEN) rank = TEN;
|
||||
UUID owner = getManager().getTopTen(world, TEN).keySet().stream().skip(rank - 1L).limit(1L).findFirst().orElse(null);
|
||||
if (owner != null) {
|
||||
Island island = getIslands().getIsland(world, owner);
|
||||
if (island != null) {
|
||||
return island.getName() == null ? "" : island.getName();
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
String getRankMembers(World world, int rank) {
|
||||
if (rank < 1) rank = 1;
|
||||
if (rank > TEN) rank = TEN;
|
||||
UUID owner = getManager().getTopTen(world, TEN).keySet().stream().skip(rank - 1L).limit(1L).findFirst().orElse(null);
|
||||
if (owner != null) {
|
||||
Island island = getIslands().getIsland(world, owner);
|
||||
if (island != null) {
|
||||
// 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(getPlayers()::getName)
|
||||
.collect(Collectors.joining(","));
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
String getRankLevel(World world, int rank) {
|
||||
if (rank < 1) rank = 1;
|
||||
if (rank > TEN) rank = TEN;
|
||||
return getManager()
|
||||
.formatLevel(getManager()
|
||||
.getTopTen(world, 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.
|
||||
*/
|
||||
String getRankValue(World world, User user) {
|
||||
if (user == null) {
|
||||
return "";
|
||||
}
|
||||
// Get the island level for this user
|
||||
long level = getManager().getIslandLevel(world, user.getUniqueId());
|
||||
return String.valueOf(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.getLocation())) return "";
|
||||
return getIslands().getIslandAt(user.getLocation())
|
||||
.map(island -> getManager().getIslandLevelString(gm.getOverWorld(), island.getOwner()))
|
||||
.orElse("0");
|
||||
}
|
||||
|
||||
|
||||
private void registerCommands(GameModeAddon gm) {
|
||||
gm.getAdminCommand().ifPresent(adminCommand -> {
|
||||
new AdminLevelCommand(this, adminCommand);
|
||||
|
176
src/main/java/world/bentobox/level/PlaceholderManager.java
Normal file
176
src/main/java/world/bentobox/level/PlaceholderManager.java
Normal file
@ -0,0 +1,176 @@
|
||||
package world.bentobox.level;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.bukkit.World;
|
||||
|
||||
import world.bentobox.bentobox.BentoBox;
|
||||
import world.bentobox.bentobox.api.addons.GameModeAddon;
|
||||
import world.bentobox.bentobox.api.user.User;
|
||||
import world.bentobox.bentobox.database.objects.Island;
|
||||
import world.bentobox.bentobox.managers.PlaceholdersManager;
|
||||
import world.bentobox.bentobox.managers.RanksManager;
|
||||
import world.bentobox.level.objects.IslandLevels;
|
||||
import world.bentobox.level.objects.TopTenData;
|
||||
|
||||
/**
|
||||
* Handles Level placeholders
|
||||
* @author tastybento
|
||||
*
|
||||
*/
|
||||
public class PlaceholderManager {
|
||||
|
||||
private final Level addon;
|
||||
private final BentoBox plugin;
|
||||
|
||||
public PlaceholderManager(Level addon) {
|
||||
this.addon = addon;
|
||||
this.plugin = addon.getPlugin();
|
||||
}
|
||||
|
||||
protected void registerPlaceholders(GameModeAddon gm) {
|
||||
if (plugin.getPlaceholdersManager() == null) return;
|
||||
PlaceholdersManager bpm = plugin.getPlaceholdersManager();
|
||||
// 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",
|
||||
user -> String.valueOf(addon.getManager().getIslandLevel(gm.getOverWorld(), user.getUniqueId())));
|
||||
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",
|
||||
user -> addon.getManager().getPointsToNextString(gm.getOverWorld(), user.getUniqueId()));
|
||||
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));
|
||||
|
||||
// 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));
|
||||
// Island Name
|
||||
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));
|
||||
// Level
|
||||
bpm.registerPlaceholder(addon,
|
||||
gm.getDescription().getName().toLowerCase() + "_top_value_" + i, u -> getRankLevel(gm.getOverWorld(), rank));
|
||||
}
|
||||
|
||||
// Personal rank
|
||||
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
|
||||
* @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));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 "";
|
||||
}
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
// 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 "";
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the rank of the player in a world
|
||||
* @param world world
|
||||
* @param user player
|
||||
* @return rank where 1 is the top rank.
|
||||
*/
|
||||
private String getRankValue(World world, User user) {
|
||||
if (user == null) {
|
||||
return "";
|
||||
}
|
||||
// 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);
|
||||
}
|
||||
|
||||
String getVisitedIslandLevel(GameModeAddon gm, User user) {
|
||||
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");
|
||||
}
|
||||
|
||||
}
|
@ -300,12 +300,4 @@ public class LevelTest {
|
||||
assertEquals(100, s.getLevelCost());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.level.Level#getRankLevel(World, int)}.
|
||||
*/
|
||||
@Test
|
||||
public void testRankLevel() {
|
||||
addon.onEnable();
|
||||
assertEquals("",addon.getRankLevel(world, 1));
|
||||
}
|
||||
}
|
||||
|
290
src/test/java/world/bentobox/level/PlaceholderManagerTest.java
Normal file
290
src/test/java/world/bentobox/level/PlaceholderManagerTest.java
Normal file
@ -0,0 +1,290 @@
|
||||
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.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.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
import org.eclipse.jdt.annotation.NonNull;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
|
||||
import world.bentobox.bentobox.BentoBox;
|
||||
import world.bentobox.bentobox.api.addons.AddonDescription;
|
||||
import world.bentobox.bentobox.api.addons.GameModeAddon;
|
||||
import world.bentobox.bentobox.api.user.User;
|
||||
import world.bentobox.bentobox.database.objects.Island;
|
||||
import world.bentobox.bentobox.managers.IslandsManager;
|
||||
import world.bentobox.bentobox.managers.PlaceholdersManager;
|
||||
import world.bentobox.bentobox.managers.PlayersManager;
|
||||
import world.bentobox.bentobox.managers.RanksManager;
|
||||
import world.bentobox.level.objects.IslandLevels;
|
||||
|
||||
/**
|
||||
* @author tastybento
|
||||
*
|
||||
*/
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest({BentoBox.class})
|
||||
public class PlaceholderManagerTest {
|
||||
|
||||
@Mock
|
||||
private Level addon;
|
||||
@Mock
|
||||
private GameModeAddon gm;
|
||||
@Mock
|
||||
private BentoBox plugin;
|
||||
|
||||
private PlaceholderManager pm;
|
||||
@Mock
|
||||
private PlaceholdersManager bpm;
|
||||
@Mock
|
||||
private LevelsManager lm;
|
||||
@Mock
|
||||
private World world;
|
||||
@Mock
|
||||
private IslandsManager im;
|
||||
@Mock
|
||||
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 @NonNull IslandLevels data;
|
||||
@Mock
|
||||
private PlayersManager players;
|
||||
|
||||
/**
|
||||
* @throws java.lang.Exception
|
||||
*/
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
when(addon.getPlugin()).thenReturn(plugin);
|
||||
|
||||
// Users
|
||||
when(addon.getPlayers()).thenReturn(players);
|
||||
// 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));
|
||||
Island is = new Island();
|
||||
is.setOwner(uuid);
|
||||
is.setName(NAMES.get(i) + "'s island");
|
||||
islands.put(uuid, 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"));
|
||||
Map<UUID, Integer> members = new HashMap<>();
|
||||
map.forEach((uuid, l) -> members.put(uuid, RanksManager.MEMBER_RANK));
|
||||
islands.values().forEach(i -> i.setMembers(members));
|
||||
|
||||
|
||||
// Placeholders manager for plugin
|
||||
when(plugin.getPlaceholdersManager()).thenReturn(bpm);
|
||||
|
||||
// Game mode
|
||||
AddonDescription desc = new AddonDescription.Builder("bentobox", "AOneBlock", "1.3").description("test").authors("tasty").build();
|
||||
when(gm.getDescription()).thenReturn(desc);
|
||||
when(gm.getOverWorld()).thenReturn(world);
|
||||
when(gm.inWorld(world)).thenReturn(true);
|
||||
|
||||
// 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(addon.getIslands()).thenReturn(im);
|
||||
|
||||
// Levels Manager
|
||||
when(lm.getIslandLevel(any(), any())).thenReturn(1234567L);
|
||||
when(lm.getIslandLevelString(any(), any())).thenReturn("1234567");
|
||||
when(lm.getPointsToNextString(any(), any())).thenReturn("1234567");
|
||||
when(lm.getIslandMaxLevel(any(), any())).thenReturn(987654L);
|
||||
when(lm.getTopTen(world, Level.TEN)).thenReturn(map);
|
||||
when(lm.formatLevel(any())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(0, Long.class).toString());
|
||||
|
||||
data = new IslandLevels("uniqueId");
|
||||
data.setTotalPoints(12345678);
|
||||
when(lm.getLevelsData(island)).thenReturn(data);
|
||||
when(addon.getManager()).thenReturn(lm);
|
||||
|
||||
pm = new PlaceholderManager(addon);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.level.PlaceholderManager#PlaceholderManager(world.bentobox.level.Level)}.
|
||||
*/
|
||||
@Test
|
||||
public void testPlaceholderManager() {
|
||||
verify(addon).getPlugin();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.level.PlaceholderManager#registerPlaceholders(world.bentobox.bentobox.api.addons.GameModeAddon)}.
|
||||
*/
|
||||
@Test
|
||||
public void testRegisterPlaceholders() {
|
||||
pm.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());
|
||||
verify(bpm).registerPlaceholder(eq(addon), eq("aoneblock_island_total_points"), any());
|
||||
|
||||
verify(bpm).registerPlaceholder(eq(addon), eq("aoneblock_points_to_next_level"), any());
|
||||
verify(bpm).registerPlaceholder(eq(addon), eq("aoneblock_island_level_max"), any());
|
||||
|
||||
// Visited Island Level
|
||||
verify(bpm).registerPlaceholder(eq(addon), eq("aoneblock_visited_island_level"), any());
|
||||
|
||||
// Register Top Ten Placeholders
|
||||
for (int i = 1; i < 11; i++) {
|
||||
// Name
|
||||
verify(bpm).registerPlaceholder(eq(addon), eq("aoneblock_top_name_" + i), any());
|
||||
// Island Name
|
||||
verify(bpm).registerPlaceholder(eq(addon), eq("aoneblock_top_island_name_" + i), any());
|
||||
// Members
|
||||
verify(bpm).registerPlaceholder(eq(addon), eq("aoneblock_top_members_" + i), any());
|
||||
// Level
|
||||
verify(bpm).registerPlaceholder(eq(addon), eq("aoneblock_top_value_" + i), any());
|
||||
}
|
||||
|
||||
// Personal rank
|
||||
verify(bpm).registerPlaceholder(eq(addon), eq("aoneblock_rank_value"), any());
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 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));
|
||||
// Test the ranks
|
||||
int rank = 1;
|
||||
for (String name : NAMES) {
|
||||
assertEquals(name, pm.getRankName(world, rank++));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 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));
|
||||
// Test the ranks
|
||||
int rank = 1;
|
||||
for (String name : NAMES) {
|
||||
assertEquals(name + "'s island", pm.getRankIslandName(world, rank++));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 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));
|
||||
// Test the ranks
|
||||
for (int rank = 1; rank < 11; rank++) {
|
||||
check(3, pm.getRankMembers(world, rank));
|
||||
}
|
||||
}
|
||||
|
||||
void check(int indicator, String list) {
|
||||
for (String n : NAMES) {
|
||||
assertTrue(n + " is missing for twst " + indicator, list.contains(n));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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));
|
||||
// Test the ranks
|
||||
for (int rank = 1; rank < 11; rank++) {
|
||||
assertEquals(String.valueOf(101 - rank), pm.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
|
||||
public void testGetVisitedIslandLevelNullUser() {
|
||||
assertEquals("", pm.getVisitedIslandLevel(gm, null));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.level.PlaceholderManager#getVisitedIslandLevel(world.bentobox.bentobox.api.addons.GameModeAddon, world.bentobox.bentobox.api.user.User)}.
|
||||
*/
|
||||
@Test
|
||||
public void testGetVisitedIslandLevelUserNotInWorld() {
|
||||
// Another world
|
||||
when(user.getWorld()).thenReturn(mock(World.class));
|
||||
assertEquals("", pm.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
|
||||
public void testGetVisitedIslandLevel() {
|
||||
assertEquals("1234567", pm.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
|
||||
public void testGetVisitedIslandLevelNoIsland() {
|
||||
when(im.getIslandAt(any(Location.class))).thenReturn(Optional.empty());
|
||||
assertEquals("0", pm.getVisitedIslandLevel(gm, user));
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user