2015-07-27 22:27:44 +02:00
|
|
|
package com.plotsquared.bukkit.uuid;
|
2014-12-16 05:58:54 +01:00
|
|
|
|
2015-07-30 16:25:16 +02:00
|
|
|
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;
|
|
|
|
|
2015-01-13 17:38:15 +01:00
|
|
|
import com.google.common.base.Charsets;
|
|
|
|
import com.google.common.collect.BiMap;
|
2015-07-03 14:15:20 +02:00
|
|
|
import com.intellectualcrafters.plot.PS;
|
2015-02-26 12:13:17 +01:00
|
|
|
import com.intellectualcrafters.plot.object.OfflinePlotPlayer;
|
2015-02-21 13:52:50 +01:00
|
|
|
import com.intellectualcrafters.plot.object.PlotPlayer;
|
2015-01-13 17:38:15 +01:00
|
|
|
import com.intellectualcrafters.plot.object.StringWrapper;
|
2015-07-27 09:26:50 +02:00
|
|
|
import com.intellectualcrafters.plot.util.UUIDHandler;
|
2015-07-27 22:27:44 +02:00
|
|
|
import com.intellectualcrafters.plot.uuid.UUIDWrapper;
|
2015-07-27 19:50:04 +02:00
|
|
|
import com.plotsquared.bukkit.object.BukkitOfflinePlayer;
|
2014-12-16 05:58:54 +01:00
|
|
|
|
2015-09-13 06:04:31 +02:00
|
|
|
public class OfflineUUIDWrapper extends UUIDWrapper {
|
2015-02-10 06:29:23 +01:00
|
|
|
private Method getOnline = null;
|
2015-02-20 07:34:19 +01:00
|
|
|
private final Object[] arg = new Object[0];
|
2015-09-13 06:04:31 +02:00
|
|
|
|
|
|
|
public OfflineUUIDWrapper() {
|
|
|
|
try {
|
2015-09-11 12:09:22 +02:00
|
|
|
getOnline = Server.class.getMethod("getOnlinePlayers", new Class[0]);
|
2015-09-13 06:04:31 +02:00
|
|
|
} catch (final NoSuchMethodException e) {
|
2015-02-10 06:29:23 +01:00
|
|
|
e.printStackTrace();
|
2015-09-13 06:04:31 +02:00
|
|
|
} catch (final SecurityException e) {
|
2015-02-10 06:29:23 +01:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2015-09-13 06:04:31 +02:00
|
|
|
|
2014-12-16 05:58:54 +01:00
|
|
|
@Override
|
2015-09-13 06:04:31 +02:00
|
|
|
public UUID getUUID(final PlotPlayer player) {
|
2014-12-16 05:58:54 +01:00
|
|
|
return UUID.nameUUIDFromBytes(("OfflinePlayer:" + player.getName()).getBytes(Charsets.UTF_8));
|
|
|
|
}
|
2015-09-13 06:04:31 +02:00
|
|
|
|
2014-12-16 05:58:54 +01:00
|
|
|
@Override
|
2015-09-13 06:04:31 +02:00
|
|
|
public UUID getUUID(final OfflinePlotPlayer player) {
|
2015-02-21 13:52:50 +01:00
|
|
|
return UUID.nameUUIDFromBytes(("OfflinePlayer:" + player.getName()).getBytes(Charsets.UTF_8));
|
|
|
|
}
|
2015-09-13 06:04:31 +02: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));
|
|
|
|
}
|
2015-09-13 06:04:31 +02:00
|
|
|
|
2014-12-16 05:58:54 +01:00
|
|
|
@Override
|
2015-09-13 06:04:31 +02:00
|
|
|
public OfflinePlotPlayer getOfflinePlayer(final UUID uuid) {
|
2014-12-16 06:03:20 +01:00
|
|
|
final BiMap<UUID, StringWrapper> map = UUIDHandler.getUuidMap().inverse();
|
2014-12-27 13:28:10 +01:00
|
|
|
String name;
|
2015-09-13 06:04:31 +02:00
|
|
|
try {
|
2014-12-27 13:28:10 +01:00
|
|
|
name = map.get(uuid).value;
|
2015-09-13 06:04:31 +02:00
|
|
|
} catch (final NullPointerException e) {
|
2014-12-27 13:28:10 +01:00
|
|
|
name = null;
|
|
|
|
}
|
2015-09-13 06:04:31 +02:00
|
|
|
if (name != null) {
|
2015-02-20 07:34:19 +01:00
|
|
|
final OfflinePlayer op = Bukkit.getOfflinePlayer(name);
|
2015-09-13 06:04:31 +02:00
|
|
|
if (op.hasPlayedBefore()) {
|
|
|
|
return new BukkitOfflinePlayer(op);
|
|
|
|
}
|
2014-12-16 05:58:54 +01:00
|
|
|
}
|
2015-09-13 06:04:31 +02:00
|
|
|
for (final OfflinePlayer player : Bukkit.getOfflinePlayers()) {
|
|
|
|
if (getUUID(player).equals(uuid)) {
|
|
|
|
return new BukkitOfflinePlayer(player);
|
|
|
|
}
|
2015-01-12 16:52:33 +01:00
|
|
|
}
|
|
|
|
return null;
|
2014-12-16 05:58:54 +01:00
|
|
|
}
|
2015-09-13 06:04:31 +02:00
|
|
|
|
|
|
|
public Player[] getOnlinePlayers() {
|
|
|
|
if (getOnline == null) {
|
|
|
|
return Bukkit.getOnlinePlayers().toArray(new Player[0]);
|
|
|
|
}
|
|
|
|
try {
|
2015-09-11 12:09:22 +02:00
|
|
|
final Object players = getOnline.invoke(Bukkit.getServer(), arg);
|
2015-09-13 06:04:31 +02:00
|
|
|
if (players instanceof Player[]) {
|
2015-02-10 06:29:23 +01:00
|
|
|
return (Player[]) players;
|
2015-09-13 06:04:31 +02:00
|
|
|
} else {
|
2015-02-10 06:29:23 +01:00
|
|
|
@SuppressWarnings("unchecked")
|
2015-02-20 07:34:19 +01:00
|
|
|
final Collection<? extends Player> p = (Collection<? extends Player>) players;
|
2015-02-10 06:29:23 +01:00
|
|
|
return p.toArray(new Player[0]);
|
|
|
|
}
|
2015-09-13 06:04:31 +02:00
|
|
|
} catch (final Exception e) {
|
2015-07-30 16:25:16 +02:00
|
|
|
PS.debug("Failed to resolve online players");
|
2015-09-11 12:09:22 +02:00
|
|
|
getOnline = null;
|
2015-02-10 06:29:23 +01:00
|
|
|
return Bukkit.getOnlinePlayers().toArray(new Player[0]);
|
|
|
|
}
|
|
|
|
}
|
2015-09-13 06:04:31 +02:00
|
|
|
|
2015-01-29 23:16:31 +01:00
|
|
|
@Override
|
2015-09-13 06:04:31 +02:00
|
|
|
public UUID getUUID(final String name) {
|
2015-01-29 23:16:31 +01:00
|
|
|
return UUID.nameUUIDFromBytes(("OfflinePlayer:" + name).getBytes(Charsets.UTF_8));
|
|
|
|
}
|
2015-09-13 06:04:31 +02:00
|
|
|
|
2015-02-26 12:13:17 +01:00
|
|
|
@Override
|
2015-09-13 06:04:31 +02:00
|
|
|
public OfflinePlotPlayer[] getOfflinePlayers() {
|
2015-09-11 12:09:22 +02:00
|
|
|
final OfflinePlayer[] ops = Bukkit.getOfflinePlayers();
|
|
|
|
final BukkitOfflinePlayer[] toReturn = new BukkitOfflinePlayer[ops.length];
|
2015-09-13 06:04:31 +02:00
|
|
|
for (int i = 0; i < ops.length; i++) {
|
2015-02-26 12:13:17 +01:00
|
|
|
toReturn[i] = new BukkitOfflinePlayer(ops[i]);
|
|
|
|
}
|
|
|
|
return toReturn;
|
|
|
|
}
|
2014-12-16 05:58:54 +01:00
|
|
|
}
|