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
|
|
|
|
|
|
|
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-02-23 02:32:27 +01:00
|
|
|
|
2015-02-10 06:29:23 +01:00
|
|
|
public OfflineUUIDWrapper() {
|
|
|
|
try {
|
|
|
|
this.getOnline = Server.class.getMethod("getOnlinePlayers", new Class[0]);
|
2015-02-20 07:34:19 +01:00
|
|
|
} catch (final NoSuchMethodException e) {
|
2015-02-10 06:29:23 +01:00
|
|
|
e.printStackTrace();
|
2015-02-20 07:34:19 +01:00
|
|
|
} catch (final SecurityException e) {
|
2015-02-10 06:29:23 +01:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2015-02-23 02:32:27 +01:00
|
|
|
|
2014-12-16 05:58:54 +01:00
|
|
|
@Override
|
2015-02-21 13:52:50 +01: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-02-23 02:32:27 +01:00
|
|
|
|
2014-12-16 05:58:54 +01:00
|
|
|
@Override
|
2015-02-26 12:13:17 +01: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-02-23 02:32:27 +01:00
|
|
|
|
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));
|
|
|
|
}
|
2015-02-23 02:32:27 +01:00
|
|
|
|
2014-12-16 05:58:54 +01:00
|
|
|
@Override
|
2015-02-26 12:13:17 +01: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;
|
|
|
|
try {
|
|
|
|
name = map.get(uuid).value;
|
2015-02-20 07:34:19 +01:00
|
|
|
} catch (final NullPointerException e) {
|
2014-12-27 13:28:10 +01:00
|
|
|
name = null;
|
|
|
|
}
|
2014-12-16 05:58:54 +01:00
|
|
|
if (name != null) {
|
2015-02-20 07:34:19 +01:00
|
|
|
final OfflinePlayer op = Bukkit.getOfflinePlayer(name);
|
2015-01-12 16:52:33 +01:00
|
|
|
if (op.hasPlayedBefore()) {
|
2015-02-21 13:52:50 +01:00
|
|
|
return new BukkitOfflinePlayer(op);
|
2014-12-16 05:58:54 +01:00
|
|
|
}
|
|
|
|
}
|
2015-01-12 16:52:33 +01:00
|
|
|
for (final OfflinePlayer player : Bukkit.getOfflinePlayers()) {
|
|
|
|
if (getUUID(player).equals(uuid)) {
|
2015-02-21 13:52:50 +01:00
|
|
|
return new BukkitOfflinePlayer(player);
|
2015-01-12 16:52:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
2014-12-16 05:58:54 +01:00
|
|
|
}
|
2015-02-23 02:32:27 +01:00
|
|
|
|
2015-02-10 06:29:23 +01:00
|
|
|
public Player[] getOnlinePlayers() {
|
2015-02-20 07:34:19 +01:00
|
|
|
if (this.getOnline == null) {
|
2015-02-10 06:29:23 +01:00
|
|
|
return Bukkit.getOnlinePlayers().toArray(new Player[0]);
|
|
|
|
}
|
|
|
|
try {
|
2015-02-20 07:34:19 +01:00
|
|
|
final Object players = this.getOnline.invoke(Bukkit.getServer(), this.arg);
|
2015-02-10 06:29:23 +01:00
|
|
|
if (players instanceof Player[]) {
|
|
|
|
return (Player[]) players;
|
2015-02-20 07:34:19 +01: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-02-20 07:34:19 +01:00
|
|
|
} catch (final Exception e) {
|
2015-07-30 16:25:16 +02:00
|
|
|
PS.debug("Failed to resolve online players");
|
2015-02-20 07:34:19 +01:00
|
|
|
this.getOnline = null;
|
2015-02-10 06:29:23 +01:00
|
|
|
return Bukkit.getOnlinePlayers().toArray(new Player[0]);
|
|
|
|
}
|
|
|
|
}
|
2015-02-23 02:32:27 +01:00
|
|
|
|
2015-01-29 23:16:31 +01:00
|
|
|
@Override
|
2015-02-20 07:34:19 +01: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-02-26 12:13:17 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public OfflinePlotPlayer[] getOfflinePlayers() {
|
|
|
|
OfflinePlayer[] ops = Bukkit.getOfflinePlayers();
|
|
|
|
BukkitOfflinePlayer[] toReturn = new BukkitOfflinePlayer[ops.length] ;
|
|
|
|
for (int i = 0; i < ops.length; i++) {
|
|
|
|
toReturn[i] = new BukkitOfflinePlayer(ops[i]);
|
|
|
|
}
|
|
|
|
return toReturn;
|
|
|
|
}
|
2014-12-16 05:58:54 +01:00
|
|
|
}
|