/
ImprovedOfflinePlayer.java
482 lines (403 loc) · 15.4 KB
/
ImprovedOfflinePlayer.java
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
package com.denizenscript.denizen.nms.abstracts;
/*
* ImprovedOfflinePlayer, a library for Bukkit.
* Copyright (C) 2013 one4me@github.com
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* @name ImprovedOfflinePlayer
* @version 1.6.0
* @author one4me
*/
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.util.jnbt.*;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.util.Vector;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
public abstract class ImprovedOfflinePlayer {
protected UUID player;
protected File file;
protected CompoundTag compound;
protected boolean exists;
protected boolean autosave = true;
public static Map<UUID, PlayerInventory> offlineInventories = new HashMap<>();
public static Map<UUID, Inventory> offlineEnderChests = new HashMap<>();
public String getRawFlagMap() {
CompoundTag tag = (CompoundTag) this.compound.getValue().get("BukkitValues");
if (tag == null) {
return "";
}
else {
return tag.getString("denizen:flag_tracker");
}
}
public void setRawFlagMap(String flags) {
CompoundTag tag = (CompoundTag) this.compound.getValue().get("BukkitValues");
if (tag == null) {
tag = NMSHandler.getInstance().createCompoundTag(new HashMap<>());
}
tag = tag.createBuilder().put("denizen:flag_tracker", new StringTag(flags)).build();
this.compound = compound.createBuilder().put("BukkitValues", tag).build();
if (this.autosave) {
savePlayerData();
}
}
public ImprovedOfflinePlayer(UUID playeruuid) {
this.exists = loadPlayerData(playeruuid);
}
public abstract PlayerInventory getInventory();
public abstract void setInventory(PlayerInventory inventory);
public abstract Inventory getEnderChest();
public abstract void setEnderChest(Inventory inventory);
public Location getLocation() {
JNBTListTag position = this.compound.getListTag("Pos");
JNBTListTag rotation = this.compound.getListTag("Rotation");
return new Location(
Bukkit.getWorld(new UUID(this.compound.getLong("WorldUUIDMost"),
this.compound.getLong("WorldUUIDLeast"))),
position.getDouble(0),
position.getDouble(1),
position.getDouble(2),
rotation.getFloat(0),
rotation.getFloat(1)
);
}
public void setLocation(Location location) {
World w = location.getWorld();
UUID uuid = w.getUID();
List<DoubleTag> position = new ArrayList<>();
position.add(new DoubleTag(location.getX()));
position.add(new DoubleTag(location.getY()));
position.add(new DoubleTag(location.getZ()));
List<FloatTag> rotation = new ArrayList<>();
rotation.add(new FloatTag(location.getYaw()));
rotation.add(new FloatTag(location.getPitch()));
this.compound = this.compound.createBuilder()
.putLong("WorldUUIDMost", uuid.getMostSignificantBits())
.putLong("WorldUUIDLeast", uuid.getLeastSignificantBits())
.putInt("Dimension", w.getEnvironment().ordinal())
.put("Pos", new JNBTListTag(DoubleTag.class, position))
.put("Rotation", new JNBTListTag(FloatTag.class, rotation)).build();
if (this.autosave) {
savePlayerData();
}
}
public float getHealthFloat() {
return this.compound.getFloat("Health");
}
public void setHealthFloat(float input) {
this.compound = compound.createBuilder().putFloat("Health", input).build();
if (this.autosave) {
savePlayerData();
}
}
public abstract double getMaxHealth();
public abstract void setMaxHealth(double input);
protected abstract boolean loadPlayerData(UUID uuid);
public abstract void savePlayerData();
public boolean exists() {
return this.exists;
}
public boolean getAutoSave() {
return this.autosave;
}
public void setAutoSave(boolean autosave) {
this.autosave = autosave;
}
public float getAbsorptionAmount() {
return this.compound.getFloat("AbsorptionAmount");
}
public void setAbsorptionAmount(float input) {
this.compound = compound.createBuilder().putFloat("AbsorptionAmount", input).build();
if (this.autosave) {
savePlayerData();
}
}
public Location getBedSpawnLocation() {
return new Location(
Bukkit.getWorld(this.compound.getString("SpawnWorld")),
this.compound.getInt("SpawnX"),
this.compound.getInt("SpawnY"),
this.compound.getInt("SpawnZ")
);
}
public boolean isSpawnForced() {
return this.compound.getBoolean("SpawnForced");
}
public void setBedSpawnLocation(Location location, Boolean override) {
this.compound = compound.createBuilder()
.putInt("SpawnX", (int) location.getX())
.putInt("SpawnY", (int) location.getY())
.putInt("SpawnZ", (int) location.getZ())
.putString("SpawnWorld", location.getWorld().getName())
.putBoolean("SpawnForced", override == null ? false : override).build();
if (this.autosave) {
savePlayerData();
}
}
public float getExhaustion() {
return this.compound.getFloat("foodExhaustionLevel");
}
public void setExhaustion(float input) {
this.compound = compound.createBuilder().putFloat("foodExhaustionLevel", input).build();
if (this.autosave) {
savePlayerData();
}
}
public float getExp() {
return this.compound.getFloat("XpP");
}
public void setExp(float input) {
this.compound = compound.createBuilder().putFloat("XpP", input).build();
if (this.autosave) {
savePlayerData();
}
}
public float getFallDistance() {
return this.compound.getFloat("FallDistance");
}
public void setFallDistance(float input) {
this.compound = compound.createBuilder().putFloat("FallDistance", input).build();
if (this.autosave) {
savePlayerData();
}
}
public int getFireTicks() {
return this.compound.getShort("Fire");
}
public void setFireTicks(int input) {
this.compound = compound.createBuilder().putShort("Fire", (short) input).build();
if (this.autosave) {
savePlayerData();
}
}
public float getFlySpeed() {
return ((CompoundTag) this.compound.getValue().get("abilities")).getFloat("flySpeed");
}
public void setFlySpeed(float speed) {
CompoundTag compoundTag = (CompoundTag) this.compound.getValue().get("abilities");
compoundTag = compoundTag.createBuilder().putFloat("flySpeed", speed).build();
this.compound = compound.createBuilder().put("abilities", compoundTag).build();
if (this.autosave) {
savePlayerData();
}
}
public int getFoodLevel() {
return this.compound.getInt("foodLevel");
}
public void setFoodLevel(int input) {
this.compound = compound.createBuilder().putInt("foodLevel", input).build();
if (this.autosave) {
savePlayerData();
}
}
public int getFoodTickTimer() {
return this.compound.getInt("foodTickTimer");
}
public void setFoodTickTimer(int input) {
this.compound = compound.createBuilder().putInt("foodTickTimer", input).build();
if (this.autosave) {
savePlayerData();
}
}
public GameMode getGameMode() {
return GameMode.getByValue(this.compound.getInt("playerGameType"));
}
@SuppressWarnings("deprecation")//Will most likely break in 1.7
public void setGameMode(GameMode input) {
this.compound = compound.createBuilder().putInt("playerGameType", input.getValue()).build();
if (this.autosave) {
savePlayerData();
}
}
public int getHealthInt() {
return this.compound.getShort("Health");
}
public void setHealthInt(int input) {
this.compound = compound.createBuilder().putShort("Health", (short) input).build();
if (this.autosave) {
savePlayerData();
}
}
public boolean getIsInvulnerable() {
return compound.getBoolean("Invulnerable");
}
public void setIsInvulnerable(boolean input) {
this.compound = compound.createBuilder().putBoolean("Invulnerable", input).build();
if (this.autosave) {
savePlayerData();
}
}
public boolean getIsOnGround() {
return compound.getBoolean("OnGround");
}
public void setIsOnGround(boolean input) {
this.compound = compound.createBuilder().putBoolean("OnGround", input).build();
if (this.autosave) {
savePlayerData();
}
}
public boolean getIsSleeping() {
return this.compound.getBoolean("Sleeping");
}
public void setIsSleeping(boolean input) {
this.compound = compound.createBuilder().putBoolean("Sleeping", input).build();
if (this.autosave) {
savePlayerData();
}
}
public int getItemInHand() {
return this.compound.getInt("SelectedItemSlot");
}
public void setItemInHand(int input) {
this.compound = compound.createBuilder().putInt("SelectedItemSlot", input).build();
if (this.autosave) {
savePlayerData();
}
}
public int getLevel() {
return this.compound.getInt("XpLevel");
}
public void setLevel(int input) {
this.compound = compound.createBuilder().putInt("XpLevel", input).build();
if (this.autosave) {
savePlayerData();
}
}
public UUID getUniqueId() {
return this.player;
}
public int getPortalCooldown() {
return this.compound.getInt("PortalCooldown");
}
public void setPortalCooldown(int input) {
this.compound = compound.createBuilder().putInt("PortalCooldown", input).build();
if (this.autosave) {
savePlayerData();
}
}
public int getRemainingAir() {
return this.compound.getShort("Air");
}
public void setRemainingAir(int input) {
this.compound = compound.createBuilder().putShort("Air", (short) input).build();
if (this.autosave) {
savePlayerData();
}
}
public float getSaturation() {
return this.compound.getFloat("foodSaturationLevel");
}
public void setSaturation(float input) {
this.compound = compound.createBuilder().putFloat("foodSaturationLevel", input).build();
if (this.autosave) {
savePlayerData();
}
}
public float getScore() {
return this.compound.getFloat("foodSaturationLevel");
}
public void setScore(int input) {
this.compound = compound.createBuilder().putInt("Score", input).build();
if (this.autosave) {
savePlayerData();
}
}
public short getTimeAttack() {
return this.compound.getShort("AttackTime");
}
public void setTimeAttack(short input) {
this.compound = compound.createBuilder().putShort("AttackTime", input).build();
if (this.autosave) {
savePlayerData();
}
}
public short getTimeDeath() {
return this.compound.getShort("DeathTime");
}
public void setTimeDeath(short input) {
this.compound = compound.createBuilder().putShort("DeathTime", input).build();
if (this.autosave) {
savePlayerData();
}
}
public short getTimeHurt() {
return this.compound.getShort("HurtTime");
}
public void setTimeHurt(short input) {
this.compound = compound.createBuilder().putShort("HurtTime", input).build();
if (this.autosave) {
savePlayerData();
}
}
public short getTimeSleep() {
return this.compound.getShort("SleepTimer");
}
public void setTimeSleep(short input) {
this.compound = compound.createBuilder().putShort("SleepTimer", input).build();
if (this.autosave) {
savePlayerData();
}
}
public int getTotalExperience() {
return this.compound.getInt("XpTotal");
}
public void setTotalExperience(int input) {
this.compound = compound.createBuilder().putInt("XpTotal", input).build();
if (this.autosave) {
savePlayerData();
}
}
public Vector getVelocity() {
JNBTListTag list = this.compound.getListTag("Motion");
return new Vector(list.getDouble(0), list.getDouble(1), list.getDouble(2));
}
public void setVelocity(Vector vector) {
List<DoubleTag> motion = new ArrayList<>();
motion.add(new DoubleTag(vector.getX()));
motion.add(new DoubleTag(vector.getY()));
motion.add(new DoubleTag(vector.getZ()));
this.compound = compound.createBuilder().put("Motion", new JNBTListTag(DoubleTag.class, motion)).build();
if (this.autosave) {
savePlayerData();
}
}
public float getWalkSpeed() {
return ((CompoundTag) this.compound.getValue().get("abilities")).getFloat("walkSpeed");
}
public void setWalkSpeed(float speed) {
CompoundTag compoundTag = (CompoundTag) this.compound.getValue().get("abilities");
compoundTag = compoundTag.createBuilder().putFloat("walkSpeed", speed).build();
this.compound = compound.createBuilder().put("abilities", compoundTag).build();
if (this.autosave) {
savePlayerData();
}
}
}