PlotSquared/Bukkit/src/main/java/com/plotsquared/bukkit/uuid/OfflineUUIDWrapper.java

111 lines
3.9 KiB
Java
Raw Normal View History

package com.plotsquared.bukkit.uuid;
2014-12-16 05:58:54 +01:00
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-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;
import com.intellectualcrafters.plot.uuid.UUIDWrapper;
2015-07-27 19:50:04 +02:00
import com.plotsquared.bukkit.object.BukkitOfflinePlayer;
2016-03-23 02:41:37 +01:00
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.Server;
import org.bukkit.entity.Player;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.UUID;
2014-12-16 05:58:54 +01:00
2015-09-13 06:04:31 +02:00
public class OfflineUUIDWrapper extends UUIDWrapper {
2015-02-20 07:34:19 +01:00
private final Object[] arg = new Object[0];
2016-03-23 02:41:37 +01:00
private Method getOnline = null;
2015-09-13 06:04:31 +02:00
public OfflineUUIDWrapper() {
try {
2016-03-23 02:41:37 +01:00
this.getOnline = Server.class.getMethod("getOnlinePlayers");
} catch (NoSuchMethodException | 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
2016-03-23 02:41:37 +01:00
public UUID getUUID(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
2016-03-23 02:41:37 +01:00
public UUID getUUID(OfflinePlotPlayer player) {
2015-02-21 13:52:50 +01:00
return UUID.nameUUIDFromBytes(("OfflinePlayer:" + player.getName()).getBytes(Charsets.UTF_8));
}
2016-03-23 02:41:37 +01:00
public UUID getUUID(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
2016-03-23 02:41:37 +01:00
public OfflinePlotPlayer getOfflinePlayer(UUID uuid) {
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;
2016-03-23 02:41:37 +01:00
} catch (NullPointerException e) {
2014-12-27 13:28:10 +01:00
name = null;
}
2015-09-13 06:04:31 +02:00
if (name != null) {
2016-03-23 02:41:37 +01:00
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
}
2016-03-23 02:41:37 +01:00
for (OfflinePlayer player : Bukkit.getOfflinePlayers()) {
2015-09-13 06:04:31 +02:00
if (getUUID(player).equals(uuid)) {
return new BukkitOfflinePlayer(player);
}
}
return null;
2014-12-16 05:58:54 +01:00
}
2015-09-13 06:04:31 +02:00
public Player[] getOnlinePlayers() {
2016-03-23 02:41:37 +01:00
if (this.getOnline == null) {
Collection<? extends Player> onlinePlayers = Bukkit.getOnlinePlayers();
return onlinePlayers.toArray(new Player[onlinePlayers.size()]);
2015-09-13 06:04:31 +02:00
}
try {
2016-03-23 02:41:37 +01:00
Object players = this.getOnline.invoke(Bukkit.getServer(), this.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 {
2016-03-23 02:41:37 +01:00
@SuppressWarnings("unchecked") Collection<? extends Player> p = (Collection<? extends Player>) players;
return p.toArray(new Player[p.size()]);
2015-02-10 06:29:23 +01:00
}
2016-03-23 02:41:37 +01:00
} catch (Exception e) {
2015-07-30 16:25:16 +02:00
PS.debug("Failed to resolve online players");
2016-03-23 02:41:37 +01:00
this.getOnline = null;
Collection<? extends Player> onlinePlayers = Bukkit.getOnlinePlayers();
return onlinePlayers.toArray(new Player[onlinePlayers.size()]);
2015-02-10 06:29:23 +01:00
}
}
2015-09-13 06:04:31 +02:00
@Override
2016-03-23 02:41:37 +01:00
public UUID getUUID(String name) {
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() {
2016-03-23 02:41:37 +01:00
OfflinePlayer[] ops = Bukkit.getOfflinePlayers();
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;
}
2016-02-10 19:59:51 +01:00
@Override
public OfflinePlotPlayer getOfflinePlayer(String name) {
return new BukkitOfflinePlayer(Bukkit.getOfflinePlayer(name));
}
2014-12-16 05:58:54 +01:00
}