13
0
geforkt von Mirrors/Paper
Paper/src/main/java/org/bukkit/craftbukkit/CraftOfflinePlayer.java

123 Zeilen
3.3 KiB
Java

package org.bukkit.craftbukkit;
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-10-10 22:10:48 +02:00
import org.bukkit.Bukkit;
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-10-10 22:10:48 +02:00
@SerializableAs("Player")
public class CraftOfflinePlayer implements OfflinePlayer, ConfigurationSerializable {
private final String name;
private final CraftServer server;
protected CraftOfflinePlayer(CraftServer server, String name) {
this.server = server;
this.name = name;
}
public boolean isOnline() {
2011-10-11 15:52:07 +02:00
return getPlayer() != null;
}
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());
}
public void setOp(boolean value) {
if (value == isOp()) return;
if (value) {
server.getHandle().addOp(getName().toLowerCase());
} else {
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) {
server.getHandle().addUserBan(name.toLowerCase());
2011-09-03 03:13:07 +02:00
} 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());
}
}
2011-10-10 22:10:48 +02:00
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 + "]";
}
2011-10-11 15:52:07 +02:00
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;
}
}