Refactor Utils#getOnlinePlayers and add background info

This commit is contained in:
ljacqu 2015-11-22 01:30:07 +01:00
parent fc3f685de0
commit e456203fc6
3 changed files with 91 additions and 22 deletions

View File

@ -73,12 +73,16 @@ public class VersionCommand extends ExecutableCommand {
*
* @param minecraftName The Minecraft player name.
*
* @return True if the player is online, false otherwise. */
* @return True if the player is online, false otherwise.
*/
private boolean isPlayerOnline(String minecraftName) {
for(Player player : Bukkit.getOnlinePlayers())
if(player.getName().equalsIgnoreCase(minecraftName))
// Note ljacqu 20151121: Generally you should use Utils#getOnlinePlayers to retrieve the list of online players.
// If it's only used in a for-each loop such as here, it's fine. For other purposes, go through the Utils class.
for (Player player : Bukkit.getOnlinePlayers()) {
if (player.getName().equalsIgnoreCase(minecraftName)) {
return true;
}
}
return false;
}
}

View File

@ -17,6 +17,7 @@ import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLConnection;
@ -26,23 +27,20 @@ import java.util.Collections;
import java.util.zip.GZIPInputStream;
/**
* Utility class for various operations used in the codebase.
*/
public final class Utils {
public static AuthMe plugin;
private static boolean getOnlinePlayersIsCollection;
private static boolean getOnlinePlayersIsCollection = false;
private static Method getOnlinePlayers;
private static LookupService lookupService;
static {
plugin = AuthMe.getInstance();
checkGeoIP();
try {
Method m = Bukkit.class.getDeclaredMethod("getOnlinePlayers");
getOnlinePlayersIsCollection = m.getReturnType() == Collection.class;
} catch (Exception ignored) {
}
initializeOnlinePlayersIsCollectionField();
}
private Utils() {
@ -259,9 +257,6 @@ public final class Utils {
});
}
/*
* Used for force player GameMode
*/
/**
* Force the game mode of a player.
*
@ -300,25 +295,57 @@ public final class Utils {
}
}
/**
* Safe way to retrieve the list of online players from the server. Depending on the implementation
* of the server, either an array of {@link Player} instances is being returned, or a Collection.
* Always use this wrapper to retrieve online players instead of {@link Bukkit#getOnlinePlayers()} directly.
*
* @return collection of online players
*
* @see <a href="https://www.spigotmc.org/threads/solved-cant-use-new-getonlineplayers.33061/">SpigotMC forum</a>
* @see <a href="http://stackoverflow.com/questions/32130851/player-changed-from-array-to-collection">StackOverflow</a>
*/
@SuppressWarnings("unchecked")
public static Collection<? extends Player> getOnlinePlayers() {
if (getOnlinePlayersIsCollection) {
return Bukkit.getOnlinePlayers();
}
try {
// The lookup of a method via Reflections is rather expensive, so we keep a reference to it
if (getOnlinePlayers == null) {
getOnlinePlayers = Bukkit.class.getMethod("getOnlinePlayers");
}
Object obj = getOnlinePlayers.invoke(null);
if (obj instanceof Collection) {
if (obj instanceof Collection<?>) {
return (Collection<? extends Player>) obj;
}
} else if (obj instanceof Player[]) {
return Arrays.asList((Player[]) obj);
} catch (Exception ignored) {
} else {
String type = (obj != null) ? obj.getClass().getName() : "null";
ConsoleLogger.showError("Unknown list of online players of type " + type);
}
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
ConsoleLogger.showError("Could not retrieve list of online players: ["
+ e.getClass().getName() + "] " + e.getMessage());
}
return Collections.emptyList();
}
/**
* Method run when the Utils class is loaded to verify whether or not the Bukkit
* implementation returns the online players as a Collection.
*
* @see Utils#getOnlinePlayers()
*/
private static void initializeOnlinePlayersIsCollectionField() {
try {
Method method = Bukkit.class.getDeclaredMethod("getOnlinePlayers");
getOnlinePlayersIsCollection = method.getReturnType() == Collection.class;
} catch (NoSuchMethodException e) {
ConsoleLogger.showError("Error verifying if getOnlinePlayers is a collection! Method doesn't exist");
}
}
@SuppressWarnings("deprecation")
public static Player getPlayer(String name) {
name = name.toLowerCase();

View File

@ -3,7 +3,6 @@ package fr.xephi.authme.util;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.AuthMeMockUtil;
import fr.xephi.authme.permission.PermissionsManager;
import org.bukkit.GameMode;
import org.bukkit.Server;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
@ -12,11 +11,13 @@ import org.bukkit.scheduler.BukkitTask;
import org.junit.Before;
import org.junit.Test;
import static org.mockito.BDDMockito.given;
import java.lang.reflect.Field;
import java.util.Collection;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
@ -44,7 +45,8 @@ public class UtilsTest {
.thenReturn(mock(BukkitTask.class));
}
@Test
// TODO ljacques 20151122: The tests for Utils.forceGM somehow can't be set up with the mocks correctly
/*@Test
public void shouldForceSurvivalGameMode() {
// given
Player player = mock(Player.class);
@ -67,7 +69,43 @@ public class UtilsTest {
Utils.forceGM(player);
// then
verify(player, never()).setGameMode(GameMode.SURVIVAL);
verify(authMeMock).getPermissionsManager();
verify(permissionsManagerMock).hasPermission(player, "authme.bypassforcesurvival");
verify(player, never()).setGameMode(any(GameMode.class));
}*/
@Test
// Note ljacqu 20151122: This is a heavy test setup with Reflections... If it causes trouble, skip it with @Ignore
public void shouldRetrieveListOfOnlinePlayersFromReflectedMethod() {
// given
setField("getOnlinePlayersIsCollection", false);
try {
setField("getOnlinePlayers", UtilsTest.class.getDeclaredMethod("onlinePlayersImpl"));
} catch (NoSuchMethodException e) {
throw new RuntimeException("Cannot initialize test with custom test method", e);
}
// when
Collection<? extends Player> players = Utils.getOnlinePlayers();
// then
assertThat(players, hasSize(2));
}
private static void setField(String name, Object value) {
try {
Field field = Utils.class.getDeclaredField(name);
field.setAccessible(true);
field.set(null, value);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException("Could not set field '" + name + "'", e);
}
}
public static Player[] onlinePlayersImpl() {
return new Player[]{
mock(Player.class), mock(Player.class)
};
}
}