From f258c37171730ba0f0eaf914690c09884e119320 Mon Sep 17 00:00:00 2001 From: boy0001 Date: Tue, 10 Feb 2015 16:29:23 +1100 Subject: [PATCH] Compatability fixes for Bukkit 1.5 --- .../intellectualcrafters/plot/config/C.java | 2 +- .../plot/util/UUIDHandler.java | 8 ++-- .../plot/uuid/OfflineUUIDWrapper.java | 40 ++++++++++++++++++- 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/config/C.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/config/C.java index 457821e57..6c7412c70 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/config/C.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/config/C.java @@ -161,7 +161,7 @@ public enum C { /* * Core Stuff */ - PREFIX("&c[&6&Plot&c] "), + PREFIX("&c[&6Plot&c] "), ENABLED("&6PlotSquared is now enabled"), EXAMPLE_MESSAGE("&cThis is an example message &k!!!"), /* diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/UUIDHandler.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/UUIDHandler.java index 31b030933..3193e796f 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/UUIDHandler.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/UUIDHandler.java @@ -162,11 +162,9 @@ public class UUIDHandler { } // check online - for (Player player : Bukkit.getOnlinePlayers()) { - UUID u2 = UUIDHandler.uuidWrapper.getUUID(player); - if (uuid.equals(u2)) { - return player.getName(); - } + Player player = uuidWrapper.getPlayer(uuid); + if (player != null) { + return player.getName(); } // check cache diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/uuid/OfflineUUIDWrapper.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/uuid/OfflineUUIDWrapper.java index d6a6ca107..d21f2a502 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/uuid/OfflineUUIDWrapper.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/uuid/OfflineUUIDWrapper.java @@ -1,9 +1,12 @@ package com.intellectualcrafters.plot.uuid; +import java.lang.reflect.Method; +import java.util.Collection; import java.util.UUID; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; +import org.bukkit.Server; import org.bukkit.entity.Player; import com.google.common.base.Charsets; @@ -13,6 +16,19 @@ import com.intellectualcrafters.plot.util.UUIDHandler; public class OfflineUUIDWrapper extends UUIDWrapper { + private Method getOnline = null; + private Object[] arg = new Object[0]; + + public OfflineUUIDWrapper() { + try { + this.getOnline = Server.class.getMethod("getOnlinePlayers", new Class[0]); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } + } + @Override public UUID getUUID(final Player player) { return UUID.nameUUIDFromBytes(("OfflinePlayer:" + player.getName()).getBytes(Charsets.UTF_8)); @@ -46,9 +62,31 @@ public class OfflineUUIDWrapper extends UUIDWrapper { return null; } + public Player[] getOnlinePlayers() { + if (getOnline == null) { + return Bukkit.getOnlinePlayers().toArray(new Player[0]); + } + try { + Object players = getOnline.invoke(Bukkit.getServer(), arg); + if (players instanceof Player[]) { + return (Player[]) players; + } + else { + @SuppressWarnings("unchecked") + Collection p = (Collection) players; + return p.toArray(new Player[0]); + } + } + catch (Exception e) { + System.out.print("Failed to resolve online players"); + getOnline = null; + return Bukkit.getOnlinePlayers().toArray(new Player[0]); + } + } + @Override public Player getPlayer(final UUID uuid) { - for (final Player player : Bukkit.getOnlinePlayers()) { + for (final Player player : getOnlinePlayers()) { if (getUUID(player).equals(uuid)) { return player; }