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

107 lines
3.6 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.PS;
2015-07-26 16:51:12 +02:00
import com.plotsquared.bukkit.object.BukkitOfflinePlayer;
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-26 18:14:34 +02:00
import com.plotsquared.bukkit.util.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;
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);
if (op.hasPlayedBefore()) {
2015-02-21 13:52:50 +01:00
return new BukkitOfflinePlayer(op);
2014-12-16 05:58:54 +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);
}
}
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) {
PS.log("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
@Override
2015-02-20 07:34:19 +01:00
public UUID getUUID(final String name) {
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
}