mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2025-02-06 07:21:52 +01:00
Added Names database object to enable name->UUID lookup.
Added test classes for Players, Names and PlayersManager. Debugged a number of issues with these classes.
This commit is contained in:
parent
dd0770cf8c
commit
21aa1691a5
@ -399,6 +399,7 @@ public class Settings implements ISettings<Settings> {
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* Number of minutes to wait
|
||||
* @return the inviteWait
|
||||
*/
|
||||
public int getInviteWait() {
|
||||
|
@ -0,0 +1,51 @@
|
||||
package us.tastybento.bskyblock.database.objects;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import com.google.gson.annotations.Expose;
|
||||
|
||||
/**
|
||||
* Stores player names and uuid's
|
||||
* @author tastybento
|
||||
*
|
||||
*/
|
||||
public class Names implements DataObject {
|
||||
|
||||
@Expose
|
||||
private String uniqueId = ""; // name
|
||||
@Expose
|
||||
private UUID uuid;
|
||||
|
||||
public Names() {}
|
||||
|
||||
public Names(String name, UUID uuid) {
|
||||
this.uniqueId = name;
|
||||
this.uuid = uuid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUniqueId() {
|
||||
return uniqueId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setUniqueId(String uniqueId) {
|
||||
this.uniqueId = uniqueId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the uuid
|
||||
*/
|
||||
public UUID getUuid() {
|
||||
return uuid;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param uuid the uuid to set
|
||||
*/
|
||||
public void setUuid(UUID uuid) {
|
||||
this.uuid = uuid;
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -46,17 +46,13 @@ public class Players implements DataObject {
|
||||
* Constructor - initializes the state variables
|
||||
*
|
||||
*/
|
||||
public Players(BSkyBlock plugin, final UUID uniqueId) {
|
||||
public Players(BSkyBlock plugin, UUID uniqueId) {
|
||||
this.uniqueId = uniqueId.toString();
|
||||
homeLocations = new HashMap<>();
|
||||
playerName = "";
|
||||
resetsLeft = plugin.getSettings().getResetLimit();
|
||||
locale = "";
|
||||
kickedList = new HashMap<>();
|
||||
playerName = Bukkit.getServer().getOfflinePlayer(uniqueId).getName();
|
||||
if (playerName == null) {
|
||||
playerName = uniqueId.toString();
|
||||
}
|
||||
this.playerName = uniqueId.toString(); // To start, until it is set later
|
||||
}
|
||||
|
||||
/**
|
||||
@ -201,19 +197,15 @@ public class Players implements DataObject {
|
||||
* @param deaths the deaths to set
|
||||
*/
|
||||
public void setDeaths(int deaths) {
|
||||
this.deaths = deaths;
|
||||
if (this.deaths > getPlugin().getSettings().getDeathsMax()) {
|
||||
this.deaths = getPlugin().getSettings().getDeathsMax();
|
||||
}
|
||||
this.deaths = deaths > getPlugin().getSettings().getDeathsMax() ? getPlugin().getSettings().getDeathsMax() : deaths;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add death
|
||||
*/
|
||||
public void addDeath() {
|
||||
deaths++;
|
||||
if (deaths > getPlugin().getSettings().getDeathsMax()) {
|
||||
deaths = getPlugin().getSettings().getDeathsMax();
|
||||
if (deaths < getPlugin().getSettings().getDeathsMax()) {
|
||||
deaths++;
|
||||
}
|
||||
}
|
||||
|
||||
@ -241,8 +233,8 @@ public class Players implements DataObject {
|
||||
return 0;
|
||||
} else {
|
||||
// Still not there yet
|
||||
// Temp minutes
|
||||
return (coolDownTime.getTimeInMillis() - timeNow.getTimeInMillis()) / (1000 * 60);
|
||||
// Time in minutes
|
||||
return (long) Math.ceil((coolDownTime.getTimeInMillis() - timeNow.getTimeInMillis()) / (1000 * 60D));
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
|
@ -5,25 +5,23 @@ import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import us.tastybento.bskyblock.BSkyBlock;
|
||||
import us.tastybento.bskyblock.Constants;
|
||||
import us.tastybento.bskyblock.api.user.User;
|
||||
import us.tastybento.bskyblock.database.BSBDatabase;
|
||||
import us.tastybento.bskyblock.database.objects.Island;
|
||||
import us.tastybento.bskyblock.database.objects.Names;
|
||||
import us.tastybento.bskyblock.database.objects.Players;
|
||||
|
||||
public class PlayersManager{
|
||||
public class PlayersManager {
|
||||
|
||||
private BSkyBlock plugin;
|
||||
private BSBDatabase<Players> handler;
|
||||
private BSBDatabase<Names> names;
|
||||
|
||||
private Map<UUID, Players> playerCache;
|
||||
private Set<UUID> inTeleport;
|
||||
@ -39,6 +37,8 @@ public class PlayersManager{
|
||||
this.plugin = plugin;
|
||||
// Set up the database handler to store and retrieve Players classes
|
||||
handler = new BSBDatabase<>(plugin, Players.class);
|
||||
// Set up the names database
|
||||
names = new BSBDatabase<>(plugin, Names.class);
|
||||
playerCache = new HashMap<>();
|
||||
inTeleport = new HashSet<>();
|
||||
}
|
||||
@ -61,7 +61,7 @@ public class PlayersManager{
|
||||
Collection<Players> set = Collections.unmodifiableCollection(playerCache.values());
|
||||
if(async) {
|
||||
Runnable save = () -> set.forEach(handler::saveObject);
|
||||
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, save);
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, save);
|
||||
} else {
|
||||
set.forEach(handler::saveObject);
|
||||
}
|
||||
@ -73,6 +73,11 @@ public class PlayersManager{
|
||||
handler.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get player by UUID. Adds player to cache if not in there already
|
||||
* @param uuid of player
|
||||
* @return player object or null if it does not exist
|
||||
*/
|
||||
public Players getPlayer(UUID uuid){
|
||||
if (!playerCache.containsKey(uuid)) {
|
||||
addPlayer(uuid);
|
||||
@ -85,10 +90,10 @@ public class PlayersManager{
|
||||
*/
|
||||
|
||||
/**
|
||||
* Adds a player to the cache
|
||||
* Adds a player to the cache. If the UUID does not exist, a new player is made
|
||||
* @param playerUUID - the player's UUID
|
||||
*/
|
||||
public void addPlayer(final UUID playerUUID) {
|
||||
public void addPlayer(UUID playerUUID) {
|
||||
if (playerUUID == null) {
|
||||
return;
|
||||
}
|
||||
@ -96,7 +101,7 @@ public class PlayersManager{
|
||||
Players player = null;
|
||||
// If the player is in the database, load it, otherwise create a new player
|
||||
if (handler.objectExists(playerUUID.toString())) {
|
||||
player = handler.loadObject(playerUUID.toString());
|
||||
player = handler.loadObject(playerUUID.toString());
|
||||
} else {
|
||||
player = new Players(plugin, playerUUID);
|
||||
}
|
||||
@ -108,12 +113,12 @@ public class PlayersManager{
|
||||
/**
|
||||
* Stores the player's info and removes the player from the cache
|
||||
*
|
||||
* @param player - the player - UUID of player
|
||||
* @param playerUUID - the player - UUID of player
|
||||
*
|
||||
*/
|
||||
public void removeOnlinePlayer(final UUID player) {
|
||||
save(player);
|
||||
playerCache.remove(player);
|
||||
public void removeOnlinePlayer(UUID playerUUID) {
|
||||
save(playerUUID);
|
||||
playerCache.remove(playerUUID);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -134,7 +139,7 @@ public class PlayersManager{
|
||||
* @param uniqueID - unique ID
|
||||
* @return true if player is know, otherwise false
|
||||
*/
|
||||
public boolean isKnown(final UUID uniqueID) {
|
||||
public boolean isKnown(UUID uniqueID) {
|
||||
if (uniqueID == null) {
|
||||
return false;
|
||||
}
|
||||
@ -147,18 +152,6 @@ public class PlayersManager{
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the player object for the named player
|
||||
*
|
||||
* @param playerUUID - the player's UUID
|
||||
* - String name of player
|
||||
* @return - player object
|
||||
*/
|
||||
public Players get(UUID playerUUID) {
|
||||
addPlayer(playerUUID);
|
||||
return playerCache.get(playerUUID);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the home location for the player
|
||||
* @param playerUUID - the player's UUID
|
||||
@ -229,11 +222,19 @@ public class PlayersManager{
|
||||
@SuppressWarnings("deprecation")
|
||||
public UUID getUUID(String string) {
|
||||
// See if this is a UUID
|
||||
try {
|
||||
return UUID.fromString(string);
|
||||
} catch (Exception e) {}
|
||||
// Look in the name cache
|
||||
return Bukkit.getOfflinePlayer(string).getUniqueId();
|
||||
// example: 5988eecd-1dcd-4080-a843-785b62419abb
|
||||
if (string.length() == 36 && string.contains("-")) {
|
||||
try {
|
||||
return UUID.fromString(string);
|
||||
} catch (Exception e) {}
|
||||
}
|
||||
// Look in the name cache, then the data base and finally Bukkit blocking request
|
||||
return playerCache.values().stream()
|
||||
.filter(p -> p.getPlayerName().equalsIgnoreCase(string)).findFirst()
|
||||
.map(p -> UUID.fromString(p.getUniqueId()))
|
||||
.orElse(names.objectExists(string)
|
||||
? names.loadObject(string).getUuid()
|
||||
: Bukkit.getOfflinePlayer(string).getUniqueId());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -243,6 +244,8 @@ public class PlayersManager{
|
||||
public void setPlayerName(User user) {
|
||||
addPlayer(user.getUniqueId());
|
||||
playerCache.get(user.getUniqueId()).setPlayerName(user.getName());
|
||||
// Add to names database
|
||||
names.saveObject(new Names(user.getName(), user.getUniqueId()));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -260,21 +263,6 @@ public class PlayersManager{
|
||||
return playerCache.get(playerUUID).getPlayerName();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse lookup - returns the owner of an island from the location
|
||||
*
|
||||
* @param loc - location
|
||||
* @return UUID of owner of island
|
||||
*/
|
||||
public UUID getPlayerFromIslandLocation(Location loc) {
|
||||
if (loc == null) {
|
||||
return null;
|
||||
}
|
||||
// Look in the grid
|
||||
Optional<Island> island = plugin.getIslands().getIslandAt(loc);
|
||||
return island.map(Island::getOwner).orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets how many island resets the player has left
|
||||
*
|
||||
@ -346,75 +334,6 @@ public class PlayersManager{
|
||||
playerCache.get(playerUUID).setLocale(localeName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ban target from a player's island. Ban may be blocked by event being cancelled.
|
||||
* @param playerUUID - the player's UUID
|
||||
* @param targetUUID - the target's UUID
|
||||
* @return true if banned, false if not
|
||||
*/
|
||||
public boolean ban(UUID playerUUID, UUID targetUUID) {
|
||||
addPlayer(playerUUID);
|
||||
addPlayer(targetUUID);
|
||||
Island island = plugin.getIslands().getIsland(playerUUID);
|
||||
if (island != null) {
|
||||
// Player has island
|
||||
return island.addToBanList(targetUUID);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unban target from player's island
|
||||
* @param playerUUID - the player's UUID
|
||||
* @param targetUUID - the target's UUID
|
||||
* @return true if target sucessfully unbanned
|
||||
*/
|
||||
public boolean unban(UUID playerUUID, UUID targetUUID) {
|
||||
addPlayer(playerUUID);
|
||||
addPlayer(targetUUID);
|
||||
Island island = plugin.getIslands().getIsland(playerUUID);
|
||||
if (island != null) {
|
||||
// Player has island
|
||||
return island.removeFromBanList(targetUUID);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param playerUUID - the player's UUID
|
||||
* @param targetUUID - the target's UUID
|
||||
* @return true if target is banned from player's island
|
||||
*/
|
||||
public boolean isBanned(UUID playerUUID, UUID targetUUID) {
|
||||
if (playerUUID == null || targetUUID == null) {
|
||||
// If the island is unowned, then playerUUID could be null
|
||||
return false;
|
||||
}
|
||||
addPlayer(playerUUID);
|
||||
addPlayer(targetUUID);
|
||||
// Check if the target player has a permission bypass (admin.noban)
|
||||
Player target = plugin.getServer().getPlayer(targetUUID);
|
||||
if (target != null && target.hasPermission(Constants.PERMPREFIX + "admin.noban")) {
|
||||
return false;
|
||||
}
|
||||
Island island = plugin.getIslands().getIsland(playerUUID);
|
||||
if (island != null) {
|
||||
// Player has island
|
||||
return island.isBanned(targetUUID);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears resets for online players or players in the cache
|
||||
* @param resetLimit
|
||||
*/
|
||||
public void clearResets(int resetLimit) {
|
||||
for (Players player : playerCache.values()) {
|
||||
player.setResetsLeft(resetLimit);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add death to player
|
||||
* @param playerUUID - the player's UUID
|
||||
@ -468,15 +387,6 @@ public class PlayersManager{
|
||||
return inTeleport.contains(uniqueId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets everything to do with a player that needs to be reset
|
||||
* @param player - the player
|
||||
*/
|
||||
public void resetPlayer(Player player) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the player to the database
|
||||
* @param playerUUID - the player's UUID
|
||||
@ -487,7 +397,6 @@ public class PlayersManager{
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Tries to get the user from this name
|
||||
* @param string
|
||||
|
@ -0,0 +1,59 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package us.tastybento.bskyblock.database.objects;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author tastybento
|
||||
*
|
||||
*/
|
||||
public class NamesTest {
|
||||
|
||||
/**
|
||||
* Test method for {@link us.tastybento.bskyblock.database.objects.Names#Names()}.
|
||||
*/
|
||||
@Test
|
||||
public void testNames() {
|
||||
assertNotNull(new Names());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link us.tastybento.bskyblock.database.objects.Names#Names(java.lang.String, java.util.UUID)}.
|
||||
*/
|
||||
@Test
|
||||
public void testNamesStringUUID() {
|
||||
assertNotNull(new Names("name", UUID.randomUUID()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link us.tastybento.bskyblock.database.objects.Names#getUniqueId()}.
|
||||
*/
|
||||
@Test
|
||||
public void testGetUniqueId() {
|
||||
Names name = new Names("name", UUID.randomUUID());
|
||||
assertEquals("name", name.getUniqueId());
|
||||
name.setUniqueId("random");
|
||||
assertEquals("random", name.getUniqueId());
|
||||
}
|
||||
/**
|
||||
* Test method for {@link us.tastybento.bskyblock.database.objects.Names#getUuid()}.
|
||||
*/
|
||||
@Test
|
||||
public void testGetUuid() {
|
||||
|
||||
Names name = new Names();
|
||||
assertNull(name.getUuid());
|
||||
UUID t = UUID.randomUUID();
|
||||
name.setUuid(t);
|
||||
assertEquals(t, name.getUuid());
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,97 @@
|
||||
package us.tastybento.bskyblock.database.objects;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
import org.powermock.reflect.Whitebox;
|
||||
|
||||
import us.tastybento.bskyblock.BSkyBlock;
|
||||
import us.tastybento.bskyblock.Settings;
|
||||
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest({Bukkit.class, BSkyBlock.class})
|
||||
public class PlayersTest {
|
||||
|
||||
private Settings s;
|
||||
|
||||
/**
|
||||
* @throws java.lang.Exception
|
||||
*/
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
// Set up plugin
|
||||
plugin = mock(BSkyBlock.class);
|
||||
Whitebox.setInternalState(BSkyBlock.class, "instance", plugin);
|
||||
s = mock(Settings.class);
|
||||
when(s.getResetLimit()).thenReturn(3);
|
||||
when(s.getDeathsMax()).thenReturn(3);
|
||||
when(plugin.getSettings()).thenReturn(s);
|
||||
}
|
||||
|
||||
private BSkyBlock plugin;
|
||||
|
||||
@Test
|
||||
public void testPlayersBSkyBlockUUID() {
|
||||
assertNotNull(new Players(plugin, UUID.randomUUID()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetHomeLocationLocation() {
|
||||
Players p = new Players(plugin, UUID.randomUUID());
|
||||
Location l = mock(Location.class);
|
||||
p.setHomeLocation(l, 5);
|
||||
assertEquals(l, p.getHomeLocation(5));
|
||||
assertNotEquals(l, p.getHomeLocation(0));
|
||||
p.setHomeLocation(null);
|
||||
assertTrue(p.getHomeLocations().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeaths() {
|
||||
Players p = new Players(plugin, UUID.randomUUID());
|
||||
assertTrue(p.getDeaths() == 0);
|
||||
p.addDeath();
|
||||
assertTrue(p.getDeaths() == 1);
|
||||
p.addDeath();
|
||||
assertTrue(p.getDeaths() == 2);
|
||||
p.addDeath();
|
||||
assertTrue(p.getDeaths() == 3);
|
||||
p.addDeath();
|
||||
assertTrue(p.getDeaths() == 3);
|
||||
p.addDeath();
|
||||
assertTrue(p.getDeaths() == 3);
|
||||
p.setDeaths(10);
|
||||
assertTrue(p.getDeaths() == 3);
|
||||
p.setDeaths(0);
|
||||
assertTrue(p.getDeaths() == 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInviteCoolDownTime() throws InterruptedException {
|
||||
when(s.getInviteWait()).thenReturn(1);
|
||||
Players p = new Players(plugin, UUID.randomUUID());
|
||||
// Check a null location
|
||||
assertTrue(p.getInviteCoolDownTime(null) == 0);
|
||||
// Real location
|
||||
Location l = mock(Location.class);
|
||||
// Should be no cooldown
|
||||
assertTrue(p.getInviteCoolDownTime(l) == 0);
|
||||
// Start the timer
|
||||
p.startInviteCoolDownTimer(l);
|
||||
// More than 0 cooldown
|
||||
assertTrue(p.getInviteCoolDownTime(l) > 0);
|
||||
}
|
||||
}
|
@ -0,0 +1,388 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package us.tastybento.bskyblock.managers;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.scheduler.BukkitScheduler;
|
||||
import org.bukkit.util.Vector;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
import org.powermock.reflect.Whitebox;
|
||||
|
||||
import us.tastybento.bskyblock.BSkyBlock;
|
||||
import us.tastybento.bskyblock.Settings;
|
||||
import us.tastybento.bskyblock.api.user.User;
|
||||
import us.tastybento.bskyblock.database.BSBDatabase;
|
||||
import us.tastybento.bskyblock.database.objects.Names;
|
||||
import us.tastybento.bskyblock.database.objects.Players;
|
||||
import us.tastybento.bskyblock.generators.IslandWorld;
|
||||
import us.tastybento.bskyblock.util.Util;
|
||||
|
||||
/**
|
||||
* @author tastybento
|
||||
*
|
||||
*/
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest({Bukkit.class, BSkyBlock.class, User.class, Util.class, PlayersManager.class })
|
||||
public class PlayersManagerTest {
|
||||
|
||||
private BSkyBlock plugin;
|
||||
private UUID uuid;
|
||||
private User user;
|
||||
private Settings s;
|
||||
private IslandsManager im;
|
||||
private UUID notUUID;
|
||||
private BukkitScheduler sch;
|
||||
private IslandWorld iwm;
|
||||
private World world;
|
||||
private World nether;
|
||||
private World end;
|
||||
private BSBDatabase<?> db;
|
||||
|
||||
/**
|
||||
* @throws java.lang.Exception
|
||||
*/
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
// Set up plugin
|
||||
plugin = mock(BSkyBlock.class);
|
||||
Whitebox.setInternalState(BSkyBlock.class, "instance", plugin);
|
||||
|
||||
// island world mgr
|
||||
iwm = mock(IslandWorld.class);
|
||||
world = mock(World.class);
|
||||
nether = mock(World.class);
|
||||
end = mock(World.class);
|
||||
when(iwm.getEndWorld()).thenReturn(end);
|
||||
when(iwm.getIslandWorld()).thenReturn(world);
|
||||
when(iwm.getNetherWorld()).thenReturn(nether);
|
||||
when(plugin.getIslandWorldManager()).thenReturn(iwm);
|
||||
|
||||
// Settings
|
||||
s = mock(Settings.class);
|
||||
when(plugin.getSettings()).thenReturn(s);
|
||||
|
||||
// Set up spawn
|
||||
Location netherSpawn = mock(Location.class);
|
||||
when(netherSpawn.toVector()).thenReturn(new Vector(0,0,0));
|
||||
when(nether.getSpawnLocation()).thenReturn(netherSpawn);
|
||||
when(s.getNetherSpawnRadius()).thenReturn(100);
|
||||
|
||||
// Player
|
||||
Player p = mock(Player.class);
|
||||
// Sometimes use Mockito.withSettings().verboseLogging()
|
||||
user = mock(User.class);
|
||||
when(user.isOp()).thenReturn(false);
|
||||
uuid = UUID.randomUUID();
|
||||
notUUID = UUID.randomUUID();
|
||||
while(notUUID.equals(uuid)) {
|
||||
notUUID = UUID.randomUUID();
|
||||
}
|
||||
when(user.getUniqueId()).thenReturn(uuid);
|
||||
when(user.getPlayer()).thenReturn(p);
|
||||
when(user.getName()).thenReturn("tastybento");
|
||||
User.setPlugin(plugin);
|
||||
|
||||
// Player has island to begin with
|
||||
im = mock(IslandsManager.class);
|
||||
when(im.hasIsland(Mockito.any())).thenReturn(true);
|
||||
when(im.isOwner(Mockito.any())).thenReturn(true);
|
||||
when(im.getTeamLeader(Mockito.any())).thenReturn(uuid);
|
||||
when(plugin.getIslands()).thenReturn(im);
|
||||
|
||||
|
||||
// Server & Scheduler
|
||||
sch = mock(BukkitScheduler.class);
|
||||
PowerMockito.mockStatic(Bukkit.class);
|
||||
when(Bukkit.getScheduler()).thenReturn(sch);
|
||||
|
||||
// Locales
|
||||
LocalesManager lm = mock(LocalesManager.class);
|
||||
when(lm.get(Mockito.any(), Mockito.any())).thenReturn("mock translation");
|
||||
when(plugin.getLocalesManager()).thenReturn(lm);
|
||||
|
||||
// Normally in world
|
||||
Util.setPlugin(plugin);
|
||||
PowerMockito.mockStatic(Util.class);
|
||||
when(Util.inWorld(Mockito.any(Entity.class))).thenReturn(true);
|
||||
when(Util.inWorld(Mockito.any(Block.class))).thenReturn(true);
|
||||
when(Util.inWorld(Mockito.any(Location.class))).thenReturn(true);
|
||||
|
||||
// Mock database
|
||||
db = mock(BSBDatabase.class);
|
||||
PowerMockito.whenNew(BSBDatabase.class).withAnyArguments().thenReturn(db);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link us.tastybento.bskyblock.managers.PlayersManager#PlayersManager(us.tastybento.bskyblock.BSkyBlock)}.
|
||||
*/
|
||||
@Test
|
||||
public void testPlayersManager() {
|
||||
PlayersManager pm = new PlayersManager(plugin);
|
||||
assertNotNull(pm);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link us.tastybento.bskyblock.managers.PlayersManager#load()}.
|
||||
*/
|
||||
@Test
|
||||
public void testLoad() {
|
||||
PlayersManager pm = new PlayersManager(plugin);
|
||||
pm.load();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link us.tastybento.bskyblock.managers.PlayersManager#save(boolean)}.
|
||||
*/
|
||||
@Test
|
||||
public void testSaveBoolean() {
|
||||
PlayersManager pm = new PlayersManager(plugin);
|
||||
pm.save(false);
|
||||
pm.save(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link us.tastybento.bskyblock.managers.PlayersManager#shutdown()}.
|
||||
*/
|
||||
@Test
|
||||
public void testShutdown() {
|
||||
PlayersManager pm = new PlayersManager(plugin);
|
||||
pm.shutdown();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link us.tastybento.bskyblock.managers.PlayersManager#getPlayer(java.util.UUID)}.
|
||||
*/
|
||||
@Test
|
||||
public void testGetPlayer() {
|
||||
PlayersManager pm = new PlayersManager(plugin);
|
||||
Server server = mock(Server.class);
|
||||
when(Bukkit.getServer()).thenReturn(server);
|
||||
OfflinePlayer olp = mock(OfflinePlayer.class);
|
||||
when(olp.getName()).thenReturn("tasty");
|
||||
when(server.getOfflinePlayer(Mockito.any(UUID.class))).thenReturn(olp);
|
||||
Players player = pm.getPlayer(uuid);
|
||||
assertEquals(uuid.toString(), player.getPlayerName());
|
||||
assertEquals(uuid.toString(), player.getUniqueId());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link us.tastybento.bskyblock.managers.PlayersManager#addPlayer(java.util.UUID)}.
|
||||
*/
|
||||
@Test
|
||||
public void testAddPlayer() {
|
||||
PlayersManager pm = new PlayersManager(plugin);
|
||||
Server server = mock(Server.class);
|
||||
when(Bukkit.getServer()).thenReturn(server);
|
||||
OfflinePlayer olp = mock(OfflinePlayer.class);
|
||||
when(olp.getName()).thenReturn("tasty");
|
||||
when(server.getOfflinePlayer(Mockito.any(UUID.class))).thenReturn(olp);
|
||||
|
||||
pm.addPlayer(null);
|
||||
// Add twice
|
||||
assertFalse(pm.isKnown(uuid));
|
||||
pm.addPlayer(uuid);
|
||||
assertTrue(pm.isKnown(uuid));
|
||||
pm.addPlayer(uuid);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link us.tastybento.bskyblock.managers.PlayersManager#removeOnlinePlayer(java.util.UUID)}.
|
||||
*/
|
||||
@Test
|
||||
public void testRemoveOnlinePlayer() {
|
||||
PlayersManager pm = new PlayersManager(plugin);
|
||||
Server server = mock(Server.class);
|
||||
when(Bukkit.getServer()).thenReturn(server);
|
||||
OfflinePlayer olp = mock(OfflinePlayer.class);
|
||||
when(olp.getName()).thenReturn("tasty");
|
||||
when(server.getOfflinePlayer(Mockito.any(UUID.class))).thenReturn(olp);
|
||||
|
||||
assertFalse(pm.isKnown(uuid));
|
||||
pm.removeOnlinePlayer(uuid);
|
||||
pm.addPlayer(uuid);
|
||||
pm.removeOnlinePlayer(uuid);
|
||||
assertFalse(pm.isKnown(uuid));
|
||||
pm.removeOnlinePlayer(uuid);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link us.tastybento.bskyblock.managers.PlayersManager#removeAllPlayers()}.
|
||||
*/
|
||||
@Test
|
||||
public void testRemoveAllPlayers() {
|
||||
PlayersManager pm = new PlayersManager(plugin);
|
||||
Server server = mock(Server.class);
|
||||
when(Bukkit.getServer()).thenReturn(server);
|
||||
OfflinePlayer olp = mock(OfflinePlayer.class);
|
||||
when(olp.getName()).thenReturn("tasty");
|
||||
when(server.getOfflinePlayer(Mockito.any(UUID.class))).thenReturn(olp);
|
||||
|
||||
pm.addPlayer(uuid);
|
||||
pm.addPlayer(notUUID);
|
||||
|
||||
pm.removeAllPlayers();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link us.tastybento.bskyblock.managers.PlayersManager#isKnown(java.util.UUID)}.
|
||||
*/
|
||||
@Test
|
||||
public void testIsKnown() {
|
||||
PlayersManager pm = new PlayersManager(plugin);
|
||||
Server server = mock(Server.class);
|
||||
when(Bukkit.getServer()).thenReturn(server);
|
||||
OfflinePlayer olp = mock(OfflinePlayer.class);
|
||||
when(olp.getName()).thenReturn("tasty");
|
||||
when(server.getOfflinePlayer(Mockito.any(UUID.class))).thenReturn(olp);
|
||||
|
||||
pm.addPlayer(uuid);
|
||||
pm.addPlayer(notUUID);
|
||||
|
||||
assertFalse(pm.isKnown(null));
|
||||
assertTrue(pm.isKnown(uuid));
|
||||
assertTrue(pm.isKnown(notUUID));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link us.tastybento.bskyblock.managers.PlayersManager#setHomeLocation(java.util.UUID, org.bukkit.Location, int)}.
|
||||
*/
|
||||
@Test
|
||||
public void testSetAndGetHomeLocationUUIDLocationInt() {
|
||||
PlayersManager pm = new PlayersManager(plugin);
|
||||
Server server = mock(Server.class);
|
||||
when(Bukkit.getServer()).thenReturn(server);
|
||||
OfflinePlayer olp = mock(OfflinePlayer.class);
|
||||
when(olp.getName()).thenReturn("tasty");
|
||||
when(server.getOfflinePlayer(Mockito.any(UUID.class))).thenReturn(olp);
|
||||
|
||||
Location l = mock(Location.class);
|
||||
Location l2 = mock(Location.class);
|
||||
Location l3 = mock(Location.class);
|
||||
pm.setHomeLocation(uuid, l, 1);
|
||||
pm.setHomeLocation(uuid, l2, 0);
|
||||
pm.setHomeLocation(uuid, l3, 10);
|
||||
assertEquals(l, pm.getHomeLocation(uuid));
|
||||
assertEquals(l2, pm.getHomeLocation(uuid, 0));
|
||||
assertEquals(l3, pm.getHomeLocation(uuid, 10));
|
||||
assertNotEquals(l, pm.getHomeLocation(uuid, 20));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link us.tastybento.bskyblock.managers.PlayersManager#clearHomeLocations(java.util.UUID)}.
|
||||
*/
|
||||
@Test
|
||||
public void testClearHomeLocations() {
|
||||
PlayersManager pm = new PlayersManager(plugin);
|
||||
Server server = mock(Server.class);
|
||||
when(Bukkit.getServer()).thenReturn(server);
|
||||
OfflinePlayer olp = mock(OfflinePlayer.class);
|
||||
when(olp.getName()).thenReturn("tasty");
|
||||
when(server.getOfflinePlayer(Mockito.any(UUID.class))).thenReturn(olp);
|
||||
|
||||
Location l = mock(Location.class);
|
||||
Location l2 = mock(Location.class);
|
||||
Location l3 = mock(Location.class);
|
||||
pm.setHomeLocation(uuid, l, 1);
|
||||
pm.setHomeLocation(uuid, l2, 0);
|
||||
pm.setHomeLocation(uuid, l3, 10);
|
||||
assertFalse(pm.getHomeLocations(uuid).isEmpty());
|
||||
pm.clearHomeLocations(uuid);
|
||||
assertTrue(pm.getHomeLocations(uuid).isEmpty());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link us.tastybento.bskyblock.managers.PlayersManager#getUUID(java.lang.String)}.
|
||||
*/
|
||||
@Test
|
||||
public void testGetUUID() {
|
||||
PlayersManager pm = new PlayersManager(plugin);
|
||||
assertEquals(uuid,pm.getUUID(uuid.toString()));
|
||||
|
||||
OfflinePlayer olp = mock(OfflinePlayer.class);
|
||||
when(olp.getUniqueId()).thenReturn(uuid);
|
||||
PowerMockito.mockStatic(Bukkit.class);
|
||||
when(Bukkit.getOfflinePlayer(Mockito.any(UUID.class))).thenReturn(olp);
|
||||
|
||||
Names name = mock(Names.class);
|
||||
when(name.getUuid()).thenReturn(uuid);
|
||||
|
||||
// Database
|
||||
when(db.objectExists(Mockito.anyString())).thenReturn(true);
|
||||
when(db.loadObject(Mockito.anyString())).thenAnswer(new Answer<Names>() {
|
||||
|
||||
@Override
|
||||
public Names answer(InvocationOnMock invocation) throws Throwable {
|
||||
|
||||
return name;
|
||||
}
|
||||
|
||||
});
|
||||
assertEquals(uuid, pm.getUUID("tastybento"));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link us.tastybento.bskyblock.managers.PlayersManager#setPlayerName(us.tastybento.bskyblock.api.user.User)}.
|
||||
*/
|
||||
@Test
|
||||
public void testSetandGetPlayerName() {
|
||||
PlayersManager pm = new PlayersManager(plugin);
|
||||
// Add a player
|
||||
pm.addPlayer(uuid);
|
||||
assertEquals(uuid.toString(), pm.getName(user.getUniqueId()));
|
||||
pm.setPlayerName(user);
|
||||
assertEquals(user.getName(), pm.getName(user.getUniqueId()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link us.tastybento.bskyblock.managers.PlayersManager#getResetsLeft(java.util.UUID)}.
|
||||
*/
|
||||
@Test
|
||||
public void testGetSetResetsLeft() {
|
||||
PlayersManager pm = new PlayersManager(plugin);
|
||||
// Add a player
|
||||
pm.addPlayer(uuid);
|
||||
assertEquals(0, pm.getResetsLeft(uuid));
|
||||
pm.setResetsLeft(uuid, 20);
|
||||
assertEquals(20, pm.getResetsLeft(uuid));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link us.tastybento.bskyblock.managers.PlayersManager#save(java.util.UUID)}.
|
||||
*/
|
||||
@Test
|
||||
public void testSaveUUID() {
|
||||
PlayersManager pm = new PlayersManager(plugin);
|
||||
// Add a player
|
||||
pm.addPlayer(uuid);
|
||||
pm.save(uuid);
|
||||
Mockito.verify(db).saveObject(Mockito.any());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user