2011-09-03 01:49:10 +02:00
|
|
|
package org.bukkit.craftbukkit;
|
|
|
|
|
2014-11-25 22:32:16 +01:00
|
|
|
import com.mojang.authlib.GameProfile;
|
2011-12-12 18:39:52 +01:00
|
|
|
import java.io.File;
|
2011-10-10 22:10:48 +02:00
|
|
|
import java.util.LinkedHashMap;
|
2011-12-08 06:33:59 +01:00
|
|
|
import java.util.List;
|
2011-10-10 22:10:48 +02:00
|
|
|
import java.util.Map;
|
2014-03-29 05:48:15 +01:00
|
|
|
import java.util.UUID;
|
2012-07-29 09:33:13 +02:00
|
|
|
|
2011-10-11 15:52:07 +02:00
|
|
|
import net.minecraft.server.EntityPlayer;
|
2011-12-12 18:39:52 +01:00
|
|
|
import net.minecraft.server.NBTTagCompound;
|
|
|
|
import net.minecraft.server.WorldNBTStorage;
|
2012-07-29 09:33:13 +02:00
|
|
|
|
2014-02-05 04:52:50 +01:00
|
|
|
import org.bukkit.BanList;
|
2011-10-10 22:10:48 +02:00
|
|
|
import org.bukkit.Bukkit;
|
2012-01-17 22:35:31 +01:00
|
|
|
import org.bukkit.Location;
|
2011-09-03 01:49:10 +02:00
|
|
|
import org.bukkit.OfflinePlayer;
|
|
|
|
import org.bukkit.Server;
|
2011-10-10 22:10:48 +02:00
|
|
|
import org.bukkit.configuration.serialization.ConfigurationSerializable;
|
|
|
|
import org.bukkit.configuration.serialization.SerializableAs;
|
2011-10-11 15:52:07 +02:00
|
|
|
import org.bukkit.entity.Player;
|
2011-12-08 06:33:59 +01:00
|
|
|
import org.bukkit.metadata.MetadataValue;
|
|
|
|
import org.bukkit.plugin.Plugin;
|
2011-09-03 01:49:10 +02:00
|
|
|
|
2011-10-10 22:10:48 +02:00
|
|
|
@SerializableAs("Player")
|
|
|
|
public class CraftOfflinePlayer implements OfflinePlayer, ConfigurationSerializable {
|
2014-04-11 03:04:38 +02:00
|
|
|
private final GameProfile profile;
|
2011-09-03 01:49:10 +02:00
|
|
|
private final CraftServer server;
|
2011-12-12 18:39:52 +01:00
|
|
|
private final WorldNBTStorage storage;
|
2011-09-03 01:49:10 +02:00
|
|
|
|
2014-04-11 03:04:38 +02:00
|
|
|
protected CraftOfflinePlayer(CraftServer server, GameProfile profile) {
|
2011-09-03 01:49:10 +02:00
|
|
|
this.server = server;
|
2014-04-11 03:04:38 +02:00
|
|
|
this.profile = profile;
|
2012-01-14 23:02:10 +01:00
|
|
|
this.storage = (WorldNBTStorage) (server.console.worlds.get(0).getDataManager());
|
2014-04-11 03:04:38 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public GameProfile getProfile() {
|
|
|
|
return profile;
|
2011-09-03 01:49:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isOnline() {
|
2011-10-11 15:52:07 +02:00
|
|
|
return getPlayer() != null;
|
2011-09-03 01:49:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getName() {
|
2014-04-11 03:04:38 +02:00
|
|
|
Player player = getPlayer();
|
|
|
|
if (player != null) {
|
|
|
|
return player.getName();
|
2014-03-29 05:48:15 +01:00
|
|
|
}
|
|
|
|
|
2014-04-12 09:38:53 +02:00
|
|
|
// This might not match lastKnownName but if not it should be more correct
|
|
|
|
if (profile.getName() != null) {
|
|
|
|
return profile.getName();
|
|
|
|
}
|
|
|
|
|
2014-04-11 03:04:38 +02:00
|
|
|
NBTTagCompound data = getBukkitData();
|
|
|
|
|
|
|
|
if (data != null) {
|
|
|
|
if (data.hasKey("lastKnownName")) {
|
|
|
|
return data.getString("lastKnownName");
|
|
|
|
}
|
2014-03-29 05:48:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-04-11 03:04:38 +02:00
|
|
|
public UUID getUniqueId() {
|
|
|
|
return profile.getId();
|
|
|
|
}
|
|
|
|
|
2011-09-03 01:49:10 +02:00
|
|
|
public Server getServer() {
|
|
|
|
return server;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isOp() {
|
2014-04-11 03:04:38 +02:00
|
|
|
return server.getHandle().isOp(profile);
|
2011-09-03 01:49:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setOp(boolean value) {
|
2014-04-11 03:04:38 +02:00
|
|
|
if (value == isOp()) {
|
|
|
|
return;
|
|
|
|
}
|
2011-09-03 01:49:10 +02:00
|
|
|
|
|
|
|
if (value) {
|
2014-04-11 03:04:38 +02:00
|
|
|
server.getHandle().addOp(profile);
|
2011-09-03 01:49:10 +02:00
|
|
|
} else {
|
2014-04-11 03:04:38 +02:00
|
|
|
server.getHandle().removeOp(profile);
|
2011-09-03 03:13:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isBanned() {
|
2014-04-17 17:45:10 +02:00
|
|
|
if (getName() == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return server.getBanList(BanList.Type.NAME).isBanned(getName());
|
2011-09-03 03:13:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setBanned(boolean value) {
|
2014-04-17 17:45:10 +02:00
|
|
|
if (getName() == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-03 03:13:07 +02:00
|
|
|
if (value) {
|
2014-04-17 17:45:10 +02:00
|
|
|
server.getBanList(BanList.Type.NAME).addBan(getName(), null, null, null);
|
2011-09-03 03:13:07 +02:00
|
|
|
} else {
|
2014-04-17 17:45:10 +02:00
|
|
|
server.getBanList(BanList.Type.NAME).pardon(getName());
|
2011-09-03 01:49:10 +02:00
|
|
|
}
|
|
|
|
}
|
2011-09-03 16:57:54 +02:00
|
|
|
|
|
|
|
public boolean isWhitelisted() {
|
2014-04-12 14:45:32 +02:00
|
|
|
return server.getHandle().getWhitelist().isWhitelisted(profile);
|
2011-09-03 16:57:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setWhitelisted(boolean value) {
|
|
|
|
if (value) {
|
2014-04-11 03:04:38 +02:00
|
|
|
server.getHandle().addWhitelist(profile);
|
2011-09-03 16:57:54 +02:00
|
|
|
} else {
|
2014-04-11 03:04:38 +02:00
|
|
|
server.getHandle().removeWhitelist(profile);
|
2011-09-03 16:57:54 +02:00
|
|
|
}
|
|
|
|
}
|
2011-10-10 22:10:48 +02:00
|
|
|
|
|
|
|
public Map<String, Object> serialize() {
|
|
|
|
Map<String, Object> result = new LinkedHashMap<String, Object>();
|
2012-01-14 23:02:10 +01:00
|
|
|
|
2014-04-11 03:04:38 +02:00
|
|
|
result.put("UUID", profile.getId().toString());
|
2012-01-14 23:02:10 +01:00
|
|
|
|
2011-10-10 22:10:48 +02:00
|
|
|
return result;
|
|
|
|
}
|
2012-01-14 23:02:10 +01:00
|
|
|
|
2011-10-10 22:10:48 +02:00
|
|
|
public static OfflinePlayer deserialize(Map<String, Object> args) {
|
2014-04-11 03:04:38 +02:00
|
|
|
// Backwards comparability
|
|
|
|
if (args.get("name") != null) {
|
|
|
|
return Bukkit.getServer().getOfflinePlayer((String) args.get("name"));
|
|
|
|
}
|
|
|
|
|
|
|
|
return Bukkit.getServer().getOfflinePlayer(UUID.fromString((String) args.get("UUID")));
|
2011-10-10 22:10:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
2014-04-11 03:04:38 +02:00
|
|
|
return getClass().getSimpleName() + "[UUID=" + profile.getId() + "]";
|
2011-10-10 22:10:48 +02:00
|
|
|
}
|
2011-10-11 15:52:07 +02:00
|
|
|
|
|
|
|
public Player getPlayer() {
|
2015-07-13 10:54:08 +02:00
|
|
|
return server.getPlayer(getUniqueId());
|
2011-10-11 15:52:07 +02:00
|
|
|
}
|
2011-12-12 16:34:49 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object obj) {
|
2014-04-11 03:04:38 +02:00
|
|
|
if (obj == null || !(obj instanceof OfflinePlayer)) {
|
2011-12-12 16:34:49 +01:00
|
|
|
return false;
|
|
|
|
}
|
2014-04-11 03:04:38 +02:00
|
|
|
|
2012-01-14 23:02:10 +01:00
|
|
|
OfflinePlayer other = (OfflinePlayer) obj;
|
2014-04-11 03:04:38 +02:00
|
|
|
if ((this.getUniqueId() == null) || (other.getUniqueId() == null)) {
|
2011-12-12 16:34:49 +01:00
|
|
|
return false;
|
|
|
|
}
|
2014-04-11 03:04:38 +02:00
|
|
|
|
|
|
|
return this.getUniqueId().equals(other.getUniqueId());
|
2011-12-12 16:34:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
int hash = 5;
|
2014-04-13 11:29:01 +02:00
|
|
|
hash = 97 * hash + (this.getUniqueId() != null ? this.getUniqueId().hashCode() : 0);
|
2011-12-12 16:34:49 +01:00
|
|
|
return hash;
|
|
|
|
}
|
2011-12-12 18:39:52 +01:00
|
|
|
|
|
|
|
private NBTTagCompound getData() {
|
2014-04-11 03:04:38 +02:00
|
|
|
return storage.getPlayerData(getUniqueId().toString());
|
2011-12-12 18:39:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private NBTTagCompound getBukkitData() {
|
|
|
|
NBTTagCompound result = getData();
|
|
|
|
|
|
|
|
if (result != null) {
|
|
|
|
if (!result.hasKey("bukkit")) {
|
2013-11-04 14:07:38 +01:00
|
|
|
result.set("bukkit", new NBTTagCompound());
|
2011-12-12 18:39:52 +01:00
|
|
|
}
|
|
|
|
result = result.getCompound("bukkit");
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
private File getDataFile() {
|
2014-04-11 03:04:38 +02:00
|
|
|
return new File(storage.getPlayerDir(), getUniqueId() + ".dat");
|
2011-12-12 18:39:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public long getFirstPlayed() {
|
|
|
|
Player player = getPlayer();
|
|
|
|
if (player != null) return player.getFirstPlayed();
|
|
|
|
|
|
|
|
NBTTagCompound data = getBukkitData();
|
|
|
|
|
|
|
|
if (data != null) {
|
|
|
|
if (data.hasKey("firstPlayed")) {
|
|
|
|
return data.getLong("firstPlayed");
|
|
|
|
} else {
|
|
|
|
File file = getDataFile();
|
|
|
|
return file.lastModified();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getLastPlayed() {
|
|
|
|
Player player = getPlayer();
|
2012-02-27 16:31:52 +01:00
|
|
|
if (player != null) return player.getLastPlayed();
|
2011-12-12 18:39:52 +01:00
|
|
|
|
|
|
|
NBTTagCompound data = getBukkitData();
|
|
|
|
|
|
|
|
if (data != null) {
|
|
|
|
if (data.hasKey("lastPlayed")) {
|
|
|
|
return data.getLong("lastPlayed");
|
|
|
|
} else {
|
|
|
|
File file = getDataFile();
|
|
|
|
return file.lastModified();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasPlayedBefore() {
|
|
|
|
return getData() != null;
|
|
|
|
}
|
2012-01-17 22:35:31 +01:00
|
|
|
|
|
|
|
public Location getBedSpawnLocation() {
|
|
|
|
NBTTagCompound data = getData();
|
2012-07-25 06:23:33 +02:00
|
|
|
if (data == null) return null;
|
|
|
|
|
2012-01-17 22:35:31 +01:00
|
|
|
if (data.hasKey("SpawnX") && data.hasKey("SpawnY") && data.hasKey("SpawnZ")) {
|
|
|
|
String spawnWorld = data.getString("SpawnWorld");
|
|
|
|
if (spawnWorld.equals("")) {
|
|
|
|
spawnWorld = server.getWorlds().get(0).getName();
|
|
|
|
}
|
|
|
|
return new Location(server.getWorld(spawnWorld), data.getInt("SpawnX"), data.getInt("SpawnY"), data.getInt("SpawnZ"));
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2011-12-08 06:33:59 +01:00
|
|
|
|
|
|
|
public void setMetadata(String metadataKey, MetadataValue metadataValue) {
|
|
|
|
server.getPlayerMetadata().setMetadata(this, metadataKey, metadataValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<MetadataValue> getMetadata(String metadataKey) {
|
|
|
|
return server.getPlayerMetadata().getMetadata(this, metadataKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasMetadata(String metadataKey) {
|
|
|
|
return server.getPlayerMetadata().hasMetadata(this, metadataKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void removeMetadata(String metadataKey, Plugin plugin) {
|
|
|
|
server.getPlayerMetadata().removeMetadata(this, metadataKey, plugin);
|
|
|
|
}
|
2011-09-03 01:49:10 +02:00
|
|
|
}
|