1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
|
package org.bukkit.craftbukkit;
import java.io.File;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import net.minecraft.server.EntityPlayer;
import net.minecraft.server.NBTTagCompound;
import net.minecraft.server.WorldNBTStorage;
import net.minecraft.util.com.mojang.authlib.GameProfile;
import org.bukkit.BanList;
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 GameProfile profile;
private final CraftServer server;
private final WorldNBTStorage storage;
protected CraftOfflinePlayer(CraftServer server, GameProfile profile) {
this.server = server;
this.profile = profile;
this.storage = (WorldNBTStorage) (server.console.worlds.get(0).getDataManager());
}
public GameProfile getProfile() {
return profile;
}
public boolean isOnline() {
return getPlayer() != null;
}
public String getName() {
Player player = getPlayer();
if (player != null) {
return player.getName();
}
// This might not match lastKnownName but if not it should be more correct
if (profile.getName() != null) {
return profile.getName();
}
NBTTagCompound data = getBukkitData();
if (data != null) {
if (data.hasKey("lastKnownName")) {
return data.getString("lastKnownName");
}
}
return null;
}
public UUID getUniqueId() {
return profile.getId();
}
public Server getServer() {
return server;
}
public boolean isOp() {
return server.getHandle().isOp(profile);
}
public void setOp(boolean value) {
if (value == isOp()) {
return;
}
if (value) {
server.getHandle().addOp(profile);
} else {
server.getHandle().removeOp(profile);
}
}
public boolean isBanned() {
return server.getBanList(BanList.Type.UUID).isBanned(getUniqueId().toString());
}
public void setBanned(boolean value) {
if (value) {
server.getBanList(BanList.Type.UUID).addBan(getUniqueId().toString(), null, null, null);
} else {
server.getBanList(BanList.Type.UUID).pardon(getUniqueId().toString());
}
}
public boolean isWhitelisted() {
return server.getHandle().isWhitelisted(profile);
}
public void setWhitelisted(boolean value) {
if (value) {
server.getHandle().addWhitelist(profile);
} else {
server.getHandle().removeWhitelist(profile);
}
}
public Map<String, Object> serialize() {
Map<String, Object> result = new LinkedHashMap<String, Object>();
result.put("UUID", profile.getId().toString());
return result;
}
public static OfflinePlayer deserialize(Map<String, Object> args) {
// 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")));
}
@Override
public String toString() {
return getClass().getSimpleName() + "[UUID=" + profile.getId() + "]";
}
public Player getPlayer() {
for (Object obj : server.getHandle().players) {
EntityPlayer player = (EntityPlayer) obj;
if (player.getUniqueID().equals(getUniqueId())) {
return (player.playerConnection != null) ? player.playerConnection.getPlayer() : null;
}
}
return null;
}
@Override
public boolean equals(Object obj) {
if (obj == null || !(obj instanceof OfflinePlayer)) {
return false;
}
OfflinePlayer other = (OfflinePlayer) obj;
if ((this.getUniqueId() == null) || (other.getUniqueId() == null)) {
return false;
}
return this.getUniqueId().equals(other.getUniqueId());
}
@Override
public int hashCode() {
int hash = 5;
hash = 97 * hash + (this.getUniqueId() != null ? this.getUniqueId().toString().hashCode() : 0);
return hash;
}
private NBTTagCompound getData() {
return storage.getPlayerData(getUniqueId().toString());
}
private NBTTagCompound getBukkitData() {
NBTTagCompound result = getData();
if (result != null) {
if (!result.hasKey("bukkit")) {
result.set("bukkit", new NBTTagCompound());
}
result = result.getCompound("bukkit");
}
return result;
}
private File getDataFile() {
return new File(storage.getPlayerDir(), getUniqueId() + ".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 == null) return null;
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);
}
}
|