Paper/src/main/java/org/bukkit/craftbukkit/CraftOfflinePlayer.java

221 Zeilen
6.3 KiB
Java

package org.bukkit.craftbukkit;
import java.io.File;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import net.minecraft.server.EntityPlayer;
import net.minecraft.server.NBTTagCompound;
import net.minecraft.server.WorldNBTStorage;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.Server;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.bukkit.configuration.serialization.SerializableAs;
import org.bukkit.entity.Player;
import org.bukkit.metadata.MetadataValue;
import org.bukkit.plugin.Plugin;
@SerializableAs("Player")
public class CraftOfflinePlayer implements OfflinePlayer, ConfigurationSerializable {
private final String name;
private final CraftServer server;
private final WorldNBTStorage storage;
protected CraftOfflinePlayer(CraftServer server, String name) {
this.server = server;
this.name = name;
this.storage = (WorldNBTStorage) (server.console.worlds.get(0).getDataManager());
}
public boolean isOnline() {
return getPlayer() != null;
}
public String getName() {
return name;
}
public Server getServer() {
return server;
}
public boolean isOp() {
return server.getHandle().isOp(getName().toLowerCase());
}
public void setOp(boolean value) {
if (value == isOp()) return;
if (value) {
server.getHandle().addOp(getName().toLowerCase());
} else {
server.getHandle().removeOp(getName().toLowerCase());
}
}
public boolean isBanned() {
return server.getHandle().banByName.contains(name.toLowerCase());
}
public void setBanned(boolean value) {
if (value) {
server.getHandle().addUserBan(name.toLowerCase());
} else {
server.getHandle().removeUserBan(name.toLowerCase());
}
}
public boolean isWhitelisted() {
return server.getHandle().getWhitelisted().contains(name.toLowerCase());
}
public void setWhitelisted(boolean value) {
if (value) {
server.getHandle().addWhitelist(name.toLowerCase());
} else {
server.getHandle().removeWhitelist(name.toLowerCase());
}
}
public Map<String, Object> serialize() {
Map<String, Object> result = new LinkedHashMap<String, Object>();
result.put("name", name);
return result;
}
public static OfflinePlayer deserialize(Map<String, Object> args) {
return Bukkit.getServer().getOfflinePlayer((String) args.get("name"));
}
@Override
public String toString() {
return getClass().getSimpleName() + "[name=" + name + "]";
}
public Player getPlayer() {
for (Object obj : server.getHandle().players) {
EntityPlayer player = (EntityPlayer) obj;
if (player.name.equalsIgnoreCase(getName())) {
return (player.netServerHandler != null) ? player.netServerHandler.getPlayer() : null;
}
}
return null;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (!(obj instanceof OfflinePlayer)) {
return false;
}
OfflinePlayer other = (OfflinePlayer) obj;
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;
}
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.getLastPlayed();
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;
}
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;
}
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);
}
}