diff --git a/pom.xml b/pom.xml index 841ef47f..539cef58 100644 --- a/pom.xml +++ b/pom.xml @@ -40,9 +40,9 @@ - org.bukkit - bukkit - 1.7.9-R0.2 + org.spigotmc + spigot-api + 1.8-R0.1-SNAPSHOT net.milkbowl.vault @@ -52,10 +52,6 @@ - - bukkit-repo - http://repo.bukkit.org/content/groups/public/ - vault-repo http://nexus.theyeticave.net/content/repositories/pub_releases diff --git a/src/main/java/com/massivecraft/massivecore/MetricsLite.java b/src/main/java/com/massivecraft/massivecore/MetricsLite.java index fecf93c9..d0307616 100644 --- a/src/main/java/com/massivecraft/massivecore/MetricsLite.java +++ b/src/main/java/com/massivecraft/massivecore/MetricsLite.java @@ -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 diff --git a/src/main/java/com/massivecraft/massivecore/particleeffect/ParticleEffect.java b/src/main/java/com/massivecraft/massivecore/particleeffect/ParticleEffect.java index daf11768..34ffcc1d 100644 --- a/src/main/java/com/massivecraft/massivecore/particleeffect/ParticleEffect.java +++ b/src/main/java/com/massivecraft/massivecore/particleeffect/ParticleEffect.java @@ -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; /** * ParticleEffect Library @@ -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; } diff --git a/src/main/java/com/massivecraft/massivecore/util/IdUtil.java b/src/main/java/com/massivecraft/massivecore/util/IdUtil.java index d0531b1d..3b470baa 100644 --- a/src/main/java/com/massivecraft/massivecore/util/IdUtil.java +++ b/src/main/java/com/massivecraft/massivecore/util/IdUtil.java @@ -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 ret = new LinkedHashSet(); // 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"); diff --git a/src/main/java/com/massivecraft/massivecore/util/InventoryUtil.java b/src/main/java/com/massivecraft/massivecore/util/InventoryUtil.java index af813592..1106425c 100644 --- a/src/main/java/com/massivecraft/massivecore/util/InventoryUtil.java +++ b/src/main/java/com/massivecraft/massivecore/util/InventoryUtil.java @@ -24,7 +24,6 @@ public class InventoryUtil // UPDATES // -------------------------------------------- // - @SuppressWarnings("deprecation") public static void update(HumanEntity human) { if (!(human instanceof Player)) return; diff --git a/src/main/java/com/massivecraft/massivecore/util/MUtil.java b/src/main/java/com/massivecraft/massivecore/util/MUtil.java index fde0d292..af59dae4 100644 --- a/src/main/java/com/massivecraft/massivecore/util/MUtil.java +++ b/src/main/java/com/massivecraft/massivecore/util/MUtil.java @@ -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 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 playersCollection = (Collection)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 // -------------------------------------------- // diff --git a/src/main/java/com/massivecraft/massivecore/util/PlayerUtil.java b/src/main/java/com/massivecraft/massivecore/util/PlayerUtil.java index db11830b..81059146 100644 --- a/src/main/java/com/massivecraft/massivecore/util/PlayerUtil.java +++ b/src/main/java/com/massivecraft/massivecore/util/PlayerUtil.java @@ -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()); }