2011-09-03 01:49:10 +02:00
|
|
|
package org.bukkit.craftbukkit;
|
|
|
|
|
2011-12-12 18:39:52 +01:00
|
|
|
import java.io.File;
|
2011-10-10 22:10:48 +02:00
|
|
|
import java.util.LinkedHashMap;
|
|
|
|
import java.util.Map;
|
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;
|
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-09-03 01:49:10 +02:00
|
|
|
|
2011-10-10 22:10:48 +02:00
|
|
|
@SerializableAs("Player")
|
|
|
|
public class CraftOfflinePlayer implements OfflinePlayer, ConfigurationSerializable {
|
2011-09-03 01:49:10 +02:00
|
|
|
private final String name;
|
|
|
|
private final CraftServer server;
|
2011-12-12 18:39:52 +01:00
|
|
|
private final WorldNBTStorage storage;
|
2011-09-03 01:49:10 +02:00
|
|
|
|
|
|
|
protected CraftOfflinePlayer(CraftServer server, String name) {
|
|
|
|
this.server = server;
|
|
|
|
this.name = name;
|
2012-01-14 23:02:10 +01:00
|
|
|
this.storage = (WorldNBTStorage) (server.console.worlds.get(0).getDataManager());
|
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() {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Server getServer() {
|
|
|
|
return server;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isOp() {
|
2011-09-03 03:13:07 +02:00
|
|
|
return server.getHandle().isOp(getName().toLowerCase());
|
2011-09-03 01:49:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setOp(boolean value) {
|
|
|
|
if (value == isOp()) return;
|
|
|
|
|
|
|
|
if (value) {
|
2011-09-24 23:03:31 +02:00
|
|
|
server.getHandle().addOp(getName().toLowerCase());
|
2011-09-03 01:49:10 +02:00
|
|
|
} else {
|
2011-09-24 23:03:31 +02:00
|
|
|
server.getHandle().removeOp(getName().toLowerCase());
|
2011-09-03 03:13:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isBanned() {
|
|
|
|
return server.getHandle().banByName.contains(name.toLowerCase());
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setBanned(boolean value) {
|
|
|
|
if (value) {
|
2011-09-24 23:03:31 +02:00
|
|
|
server.getHandle().addUserBan(name.toLowerCase());
|
2011-09-03 03:13:07 +02:00
|
|
|
} else {
|
2011-09-24 23:03:31 +02:00
|
|
|
server.getHandle().removeUserBan(name.toLowerCase());
|
2011-09-03 01:49:10 +02:00
|
|
|
}
|
|
|
|
}
|
2011-09-03 16:57:54 +02:00
|
|
|
|
|
|
|
public boolean isWhitelisted() {
|
2011-09-24 23:03:31 +02:00
|
|
|
return server.getHandle().getWhitelisted().contains(name.toLowerCase());
|
2011-09-03 16:57:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setWhitelisted(boolean value) {
|
|
|
|
if (value) {
|
2011-09-24 23:03:31 +02:00
|
|
|
server.getHandle().addWhitelist(name.toLowerCase());
|
2011-09-03 16:57:54 +02:00
|
|
|
} else {
|
2011-09-24 23:03:31 +02:00
|
|
|
server.getHandle().removeWhitelist(name.toLowerCase());
|
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
|
|
|
|
2011-10-10 22:10:48 +02:00
|
|
|
result.put("name", name);
|
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) {
|
2012-01-14 23:02:10 +01:00
|
|
|
return Bukkit.getServer().getOfflinePlayer((String) args.get("name"));
|
2011-10-10 22:10:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return getClass().getSimpleName() + "[name=" + name + "]";
|
|
|
|
}
|
2011-10-11 15:52:07 +02:00
|
|
|
|
|
|
|
public Player getPlayer() {
|
2012-01-14 23:02:10 +01:00
|
|
|
for (Object obj : server.getHandle().players) {
|
|
|
|
EntityPlayer player = (EntityPlayer) obj;
|
2011-10-11 15:52:07 +02:00
|
|
|
if (player.name.equalsIgnoreCase(getName())) {
|
|
|
|
return (player.netServerHandler != null) ? player.netServerHandler.getPlayer() : null;
|
|
|
|
}
|
|
|
|
}
|
2012-01-14 23:02:10 +01:00
|
|
|
|
2011-10-11 15:52:07 +02:00
|
|
|
return null;
|
|
|
|
}
|
2011-12-12 16:34:49 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object obj) {
|
|
|
|
if (obj == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!(obj instanceof OfflinePlayer)) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-01-14 23:02:10 +01:00
|
|
|
OfflinePlayer other = (OfflinePlayer) obj;
|
2011-12-12 16:34:49 +01:00
|
|
|
if ((this.getName() == null) || (other.getName() == null)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return this.getName().equalsIgnoreCase(other.getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
int hash = 5;
|
|
|
|
hash = 97 * hash + (this.getName() != null ? this.getName().hashCode() : 0);
|
|
|
|
return hash;
|
|
|
|
}
|
2011-12-12 18:39:52 +01:00
|
|
|
|
|
|
|
private NBTTagCompound getData() {
|
|
|
|
return storage.getPlayerData(getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
private NBTTagCompound getBukkitData() {
|
|
|
|
NBTTagCompound result = getData();
|
|
|
|
|
|
|
|
if (result != null) {
|
|
|
|
if (!result.hasKey("bukkit")) {
|
|
|
|
result.setCompound("bukkit", new NBTTagCompound());
|
|
|
|
}
|
|
|
|
result = result.getCompound("bukkit");
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
private File getDataFile() {
|
|
|
|
return new File(storage.getPlayerDir(), name + ".dat");
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
if (player != null) return player.getFirstPlayed();
|
|
|
|
|
|
|
|
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();
|
|
|
|
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-09-03 01:49:10 +02:00
|
|
|
}
|