Compile against Spigot 1.8 API. Provide inhouse getOnlinePlayers method using reflection as a fallback. Fixes #192.

This commit is contained in:
Olof Larsson 2014-12-03 02:10:01 +01:00
parent 495e839351
commit 84aa54218e
7 changed files with 108 additions and 16 deletions

10
pom.xml
View File

@ -40,9 +40,9 @@
<dependencies>
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
<version>1.7.9-R0.2</version>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.8-R0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>net.milkbowl.vault</groupId>
@ -52,10 +52,6 @@
</dependencies>
<repositories>
<repository>
<id>bukkit-repo</id>
<url>http://repo.bukkit.org/content/groups/public/</url>
</repository>
<repository>
<id>vault-repo</id>
<url>http://nexus.theyeticave.net/content/repositories/pub_releases</url>

View File

@ -35,6 +35,8 @@ import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.scheduler.BukkitTask;
import com.massivecraft.massivecore.util.MUtil;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
@ -284,7 +286,7 @@ public class MetricsLite {
boolean onlineMode = Bukkit.getServer().getOnlineMode(); // TRUE if online mode is enabled
String pluginVersion = description.getVersion();
String serverVersion = Bukkit.getVersion();
int playersOnline = Bukkit.getServer().getOnlinePlayers().length;
int playersOnline = MUtil.getOnlinePlayers().size();
// END server software specific section -- all code below does not use any code outside of this class / Java

View File

@ -9,13 +9,13 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import com.massivecraft.massivecore.particleeffect.ReflectionUtils.PackageType;
import com.massivecraft.massivecore.particleeffect.ReflectionUtils.PacketType;
import com.massivecraft.massivecore.util.MUtil;
/**
* <b>ParticleEffect Library</b>
@ -721,7 +721,7 @@ public enum ParticleEffect {
}
String worldName = center.getWorld().getName();
double squared = range * range;
for (Player player : Bukkit.getOnlinePlayers()) {
for (Player player : MUtil.getOnlinePlayers()) {
if (!player.getWorld().getName().equals(worldName) || player.getLocation().distanceSquared(center) > squared) {
continue;
}

View File

@ -2,7 +2,6 @@ package com.massivecraft.massivecore.util;
import java.io.File;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
@ -181,7 +180,7 @@ public class IdUtil implements Listener, Runnable
Set<CommandSender> ret = new LinkedHashSet<CommandSender>();
// Add Online Players
ret.addAll(Arrays.asList(Bukkit.getOnlinePlayers()));
ret.addAll(MUtil.getOnlinePlayers());
// Add Console
ret.add(getConsole());
@ -479,7 +478,6 @@ public class IdUtil implements Listener, Runnable
return getAsPlayer(getSender(senderObject));
}
@SuppressWarnings("deprecation")
public static CommandSender getSender(Object senderObject)
{
// Null Return
@ -875,7 +873,7 @@ public class IdUtil implements Listener, Runnable
long millis = System.currentTimeMillis();
for (Player player : Bukkit.getOnlinePlayers())
for (Player player : MUtil.getOnlinePlayers())
{
String id = getId(player);
if (id == null) throw new NullPointerException("id");

View File

@ -24,7 +24,6 @@ public class InventoryUtil
// UPDATES
// -------------------------------------------- //
@SuppressWarnings("deprecation")
public static void update(HumanEntity human)
{
if (!(human instanceof Player)) return;

View File

@ -2,6 +2,7 @@ package com.massivecraft.massivecore.util;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Method;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Arrays;
@ -22,6 +23,7 @@ import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
@ -67,6 +69,101 @@ import com.massivecraft.massivecore.util.extractor.ExtractorWorldName;
public class MUtil
{
// -------------------------------------------- //
// CONSTANTS
// -------------------------------------------- //
private static Method methodGetOnlinePlayers;
static
{
methodGetOnlinePlayers = getMethodGetOnlinePlayers();
}
// -------------------------------------------- //
// GET ONLINE PLAYERS
// -------------------------------------------- //
// It seems we can not always trust the Bukkit.getOnlinePlayers() method.
// Due to compilation issue this method might not exist in the form we compiled against.
// Spigot 1.8 and the 1.7 Bukkit might have been compiled slightly differently resulting in this issue.
// Issue Example: https://github.com/MassiveCraft/MassiveCore/issues/192
public static Method getMethodGetOnlinePlayers()
{
Method ret = null;
try
{
for (Method method : Bukkit.class.getDeclaredMethods())
{
// The method name must be getOnlinePlayers ...
if ( ! method.getName().equals("getOnlinePlayers")) continue;
// ... if we find such a method it's better than nothing ...
if (ret == null) ret = method;
// ... but if the method additionally returns a collection ...
if ( ! method.getReturnType().isAssignableFrom(Collection.class)) continue;
// ... that is preferable ...
ret = method;
// ... and we need not look any further.
break;
}
ret.setAccessible(true);
}
catch (Exception e)
{
// If we fail we do so silently.
// This method is probably almost never going to be used anyways.
}
return ret;
}
public static Collection<? extends Player> getOnlinePlayers()
{
try
{
return Bukkit.getOnlinePlayers();
}
catch (Exception e)
{
// We probably just caught a NoSuchMethodError.
}
try
{
Object playersObject = methodGetOnlinePlayers.invoke(null);
if (playersObject instanceof Player[])
{
System.out.println(1);
Player[] playersArray = (Player[])playersObject;
return Arrays.asList(playersArray);
}
else if (playersObject instanceof Collection<?>)
{
System.out.println(2);
@SuppressWarnings("unchecked")
Collection<? extends Player> playersCollection = (Collection<? extends Player>)playersObject;
return playersCollection;
}
else
{
System.out.println(3);
throw new RuntimeException("Unknown return type for getOnlinePlayers using reflection.");
}
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
// -------------------------------------------- //
// IS VALID PLAYER NAME
// -------------------------------------------- //

View File

@ -54,7 +54,7 @@ public class PlayerUtil extends EngineAbstract
idToArmSwingEvent.clear();
joinedPlayerIds.clear();
for (Player player : Bukkit.getOnlinePlayers())
for (Player player : MUtil.getOnlinePlayers())
{
joinedPlayerIds.add(player.getUniqueId());
}