Fix NoSuchMethodError with pre-1.7.9 versions

Fixes #104
This commit is contained in:
Dan Mulloy 2015-07-24 12:36:26 -04:00
parent 7226e199e5
commit 0159752687
5 changed files with 31 additions and 8 deletions

View File

@ -7,10 +7,10 @@ import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import com.comphenix.protocol.utility.SafeCacheBuilder;
import com.comphenix.protocol.utility.Util;
import com.google.common.base.Function;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.RemovalListener;
@ -166,7 +166,7 @@ public class ConcurrentPlayerMap<TValue> extends AbstractMap<Player, TValue> imp
* @return The player with the given key, or NULL if not found.
*/
protected Player findOnlinePlayer(Object key) {
for (Player player : Bukkit.getOnlinePlayers()) {
for (Player player : Util.getOnlinePlayers()) {
if (key.equals(keyMethod.apply(player))) {
return player;
}

View File

@ -737,7 +737,7 @@ public final class PacketFilterManager implements ProtocolManager, ListenerInvok
Location recycle = origin.clone();
// Only broadcast the packet to nearby players
for (Player player : server.getOnlinePlayers()) {
for (Player player : Util.getOnlinePlayers()) {
if (world.equals(player.getWorld()) &&
getDistanceSquared(origin, recycle, player) <= maxDistance) {

View File

@ -22,7 +22,6 @@ import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@ -159,7 +158,7 @@ public class ChatExtensions {
throw new IllegalArgumentException("message cannot be NULL.");
// Send this message to every online player
for (Player player : Bukkit.getServer().getOnlinePlayers()) {
for (Player player : Util.getOnlinePlayers()) {
if (permission == null || player.hasPermission(permission)) {
sendMessageSilently(player, message);
}

View File

@ -30,19 +30,26 @@ import com.comphenix.protocol.reflect.accessors.MethodAccessor;
/**
* General utility class
*
* @author dmulloy2
*/
public class Util {
private static MethodAccessor getOnlinePlayers;
private static boolean reflectionRequired;
private static boolean isUsingSpigot;
static {
try {
Method method = Bukkit.class.getMethod("getOnlinePlayers");
getOnlinePlayers = Accessors.getMethodAccessor(method);
reflectionRequired = !method.getReturnType().isAssignableFrom(Collection.class);
try {
Class.forName("org.bukkit.entity.Player.Spigot");
isUsingSpigot = true;
} catch (ClassNotFoundException ex) {
isUsingSpigot = false;
}
} catch (Throwable ex) {
throw new RuntimeException("Failed to obtain getOnlinePlayers method.", ex);
}
@ -50,8 +57,7 @@ public class Util {
/**
* Gets a list of online {@link Player}s. This also provides backwards
* compatibility as Bukkit changed <code>getOnlinePlayers</code>.
*
* compatibility, since Bukkit changed getOnlinePlayers in 1.7.9.
* @return A list of currently online Players
*/
@SuppressWarnings("unchecked")
@ -63,12 +69,27 @@ public class Util {
return (List<Player>) Bukkit.getOnlinePlayers();
}
/**
* Converts a variable argument array into a List.
* @param elements Array to convert
* @return The list
*/
public static <E> List<E> asList(E... elements) {
List<E> list = new ArrayList<E>(elements.length);
for (E element : elements) {
list.add(element);
}
Arrays.asList(elements);
return list;
}
/**
* Whether or not this server is running Spigot. This works by checking
* for a Spigot-specific API class, in this case {@link Player.Spigot}.
* @return True if it is, false if not.
*/
public static boolean isUsingSpigot() {
return isUsingSpigot;
}
}

View File

@ -28,6 +28,9 @@ import net.md_5.bungee.chat.ComponentSerializer;
public final class ComponentConverter {
private ComponentConverter() {
}
/**
* Converts a {@link WrappedChatComponent} into an array of {@link BaseComponent}s
* @param wrapper ProtocolLib wrapper