/
ImprovedOfflinePlayerImpl.java
147 lines (127 loc) · 5.57 KB
/
ImprovedOfflinePlayerImpl.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
package com.denizenscript.denizen.nms.v1_18.impl;
import com.denizenscript.denizen.nms.abstracts.ImprovedOfflinePlayer;
import com.denizenscript.denizen.nms.util.jnbt.CompoundTag;
import com.denizenscript.denizen.nms.v1_18.impl.jnbt.CompoundTagImpl;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.NbtIo;
import net.minecraft.world.entity.ai.attributes.AttributeInstance;
import net.minecraft.world.entity.ai.attributes.AttributeMap;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.entity.ai.attributes.DefaultAttributes;
import net.minecraft.world.inventory.PlayerEnderChestContainer;
import org.bukkit.Bukkit;
import org.bukkit.craftbukkit.v1_18_R2.inventory.CraftInventory;
import org.bukkit.craftbukkit.v1_18_R2.inventory.CraftInventoryPlayer;
import org.bukkit.entity.HumanEntity;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.UUID;
public class ImprovedOfflinePlayerImpl extends ImprovedOfflinePlayer {
public ImprovedOfflinePlayerImpl(UUID playeruuid) {
super(playeruuid);
}
public static class OfflinePlayerInventory extends net.minecraft.world.entity.player.Inventory {
public OfflinePlayerInventory(net.minecraft.world.entity.player.Player entityhuman) {
super(entityhuman);
}
@Override
public InventoryHolder getOwner() {
return null;
}
}
public static class OfflineCraftInventoryPlayer extends CraftInventoryPlayer {
public OfflineCraftInventoryPlayer(net.minecraft.world.entity.player.Inventory inventory) {
super(inventory);
}
@Override
public HumanEntity getHolder() {
return null;
}
}
@Override
public org.bukkit.inventory.PlayerInventory getInventory() {
if (inventory == null) {
net.minecraft.world.entity.player.Inventory newInv = new OfflinePlayerInventory(null);
newInv.load(((CompoundTagImpl) this.compound).toNMSTag().getList("Inventory", 10));
inventory = new OfflineCraftInventoryPlayer(newInv);
}
return inventory;
}
@Override
public void setInventory(org.bukkit.inventory.PlayerInventory inventory) {
CraftInventoryPlayer inv = (CraftInventoryPlayer) inventory;
net.minecraft.nbt.CompoundTag nbtTagCompound = ((CompoundTagImpl) compound).toNMSTag();
nbtTagCompound.put("Inventory", inv.getInventory().save(new ListTag()));
this.compound = CompoundTagImpl.fromNMSTag(nbtTagCompound);
markModified();
}
@Override
public Inventory getEnderChest() {
if (enderchest == null) {
PlayerEnderChestContainer endchest = new PlayerEnderChestContainer(null);
endchest.fromTag(((CompoundTagImpl) this.compound).toNMSTag().getList("EnderItems", 10));
enderchest = new CraftInventory(endchest);
}
return enderchest;
}
@Override
public void setEnderChest(Inventory inventory) {
net.minecraft.nbt.CompoundTag nbtTagCompound = ((CompoundTagImpl) compound).toNMSTag();
nbtTagCompound.put("EnderItems", ((PlayerEnderChestContainer) ((CraftInventory) inventory).getInventory()).createTag());
this.compound = CompoundTagImpl.fromNMSTag(nbtTagCompound);
markModified();
}
@Override
public double getMaxHealth() {
AttributeInstance maxHealth = getAttributes().getInstance(Attributes.MAX_HEALTH);
return maxHealth == null ? Attributes.MAX_HEALTH.getDefaultValue() : maxHealth.getValue();
}
@Override
public void setMaxHealth(double input) {
AttributeMap attributes = getAttributes();
AttributeInstance maxHealth = attributes.getInstance(Attributes.MAX_HEALTH);
maxHealth.setBaseValue(input);
setAttributes(attributes);
}
private AttributeMap getAttributes() {
AttributeMap amb = new AttributeMap(DefaultAttributes.getSupplier(net.minecraft.world.entity.EntityType.PLAYER));
amb.load(((CompoundTagImpl) this.compound).toNMSTag().getList("Attributes", 10));
return amb;
}
public void setAttributes(AttributeMap attributes) {
net.minecraft.nbt.CompoundTag nbtTagCompound = ((CompoundTagImpl) compound).toNMSTag();
nbtTagCompound.put("Attributes", attributes.save());
this.compound = CompoundTagImpl.fromNMSTag(nbtTagCompound);
markModified();
}
@Override
protected boolean loadPlayerData(UUID uuid) {
try {
this.player = uuid;
for (org.bukkit.World w : Bukkit.getWorlds()) {
this.file = new File(w.getWorldFolder(), "playerdata" + File.separator + this.player + ".dat");
if (this.file.exists()) {
this.compound = CompoundTagImpl.fromNMSTag(NbtIo.readCompressed(new FileInputStream(this.file)));
return true;
}
}
}
catch (Exception e) {
Debug.echoError(e);
}
return false;
}
@Override
public void saveInternal(CompoundTag compound) {
try {
NbtIo.writeCompressed(((CompoundTagImpl) compound).toNMSTag(), new FileOutputStream(this.file));
}
catch (Exception e) {
Debug.echoError(e);
}
}
}