PlotSquared/PlotSquared/src/main/java/com/intellectualcrafters/plot/uuid/OfflineUUIDWrapper.java

103 lines
3.1 KiB
Java
Raw Normal View History

2014-12-16 05:58:54 +01:00
package com.intellectualcrafters.plot.uuid;
2015-02-10 06:29:23 +01:00
import java.lang.reflect.Method;
import java.util.Collection;
2015-01-13 17:38:15 +01:00
import java.util.UUID;
2014-12-18 03:15:11 +01:00
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
2015-02-10 06:29:23 +01:00
import org.bukkit.Server;
2014-12-18 03:15:11 +01:00
import org.bukkit.entity.Player;
2015-01-13 17:38:15 +01:00
import com.google.common.base.Charsets;
import com.google.common.collect.BiMap;
import com.intellectualcrafters.plot.object.StringWrapper;
2015-02-20 07:28:21 +01:00
import com.intellectualcrafters.plot.util.bukkit.UUIDHandler;
2014-12-16 05:58:54 +01:00
public class OfflineUUIDWrapper extends UUIDWrapper {
2015-02-10 06:29:23 +01:00
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();
}
}
2014-12-16 05:58:54 +01:00
@Override
2014-12-16 06:03:20 +01:00
public UUID getUUID(final Player player) {
2014-12-16 05:58:54 +01:00
return UUID.nameUUIDFromBytes(("OfflinePlayer:" + player.getName()).getBytes(Charsets.UTF_8));
}
@Override
2014-12-16 06:03:20 +01:00
public UUID getUUID(final OfflinePlayer player) {
2014-12-16 05:58:54 +01:00
return UUID.nameUUIDFromBytes(("OfflinePlayer:" + player.getName()).getBytes(Charsets.UTF_8));
}
@Override
2014-12-16 06:03:20 +01:00
public OfflinePlayer getOfflinePlayer(final UUID uuid) {
final BiMap<UUID, StringWrapper> map = UUIDHandler.getUuidMap().inverse();
2014-12-27 13:28:10 +01:00
String name;
try {
name = map.get(uuid).value;
} catch (NullPointerException e) {
name = null;
}
2014-12-16 05:58:54 +01:00
if (name != null) {
OfflinePlayer op = Bukkit.getOfflinePlayer(name);
if (op.hasPlayedBefore()) {
return op;
2014-12-16 05:58:54 +01:00
}
}
for (final OfflinePlayer player : Bukkit.getOfflinePlayers()) {
if (getUUID(player).equals(uuid)) {
return player;
}
}
return null;
2014-12-16 05:58:54 +01:00
}
2015-02-10 06:29:23 +01:00
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<? extends Player> p = (Collection<? extends Player>) 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]);
}
}
2014-12-16 05:58:54 +01:00
@Override
2014-12-16 06:03:20 +01:00
public Player getPlayer(final UUID uuid) {
2015-02-10 06:29:23 +01:00
for (final Player player : getOnlinePlayers()) {
2014-12-16 05:58:54 +01:00
if (getUUID(player).equals(uuid)) {
return player;
}
}
return null;
}
2014-12-16 06:03:20 +01:00
@Override
public UUID getUUID(String name) {
return UUID.nameUUIDFromBytes(("OfflinePlayer:" + name).getBytes(Charsets.UTF_8));
}
2014-12-16 05:58:54 +01:00
}