This repository has been archived by the owner on Mar 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 275
/
EntityItem.java
271 lines (213 loc) · 7.24 KB
/
EntityItem.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
package cn.nukkit.entity.item;
import cn.nukkit.Player;
import cn.nukkit.entity.Entity;
import cn.nukkit.event.entity.EntityDamageEvent;
import cn.nukkit.event.entity.EntityDamageEvent.DamageCause;
import cn.nukkit.event.entity.ItemDespawnEvent;
import cn.nukkit.event.entity.ItemSpawnEvent;
import cn.nukkit.item.Item;
import cn.nukkit.level.format.FullChunk;
import cn.nukkit.nbt.NBTIO;
import cn.nukkit.nbt.tag.CompoundTag;
import cn.nukkit.network.protocol.AddItemEntityPacket;
/**
* @author MagicDroidX
*/
public class EntityItem extends Entity {
public static final int NETWORK_ID = 64;
public static final int DATA_SOURCE_ID = 17;
public EntityItem(FullChunk chunk, CompoundTag nbt) {
super(chunk, nbt);
}
@Override
public int getNetworkId() {
return NETWORK_ID;
}
protected String owner;
protected String thrower;
protected Item item;
protected int pickupDelay;
@Override
public float getWidth() {
return 0.25f;
}
@Override
public float getLength() {
return 0.25f;
}
@Override
public float getHeight() {
return 0.25f;
}
@Override
public float getGravity() {
return 0.04f;
}
@Override
public float getDrag() {
return 0.02f;
}
@Override
protected float getBaseOffset() {
return 0.125f;
}
@Override
public boolean canCollide() {
return false;
}
@Override
protected void initEntity() {
super.initEntity();
this.setMaxHealth(5);
this.setHealth(this.namedTag.getShort("Health"));
if (this.namedTag.contains("Age")) {
this.age = this.namedTag.getShort("Age");
}
if (this.namedTag.contains("PickupDelay")) {
this.pickupDelay = this.namedTag.getShort("PickupDelay");
}
if (this.namedTag.contains("Owner")) {
this.owner = this.namedTag.getString("Owner");
}
if (this.namedTag.contains("Thrower")) {
this.thrower = this.namedTag.getString("Thrower");
}
if (!this.namedTag.contains("Item")) {
this.close();
return;
}
this.item = NBTIO.getItemHelper(this.namedTag.getCompound("Item"));
this.setDataFlag(DATA_FLAGS, DATA_FLAG_IMMOBILE, true);
this.server.getPluginManager().callEvent(new ItemSpawnEvent(this));
}
@Override
public boolean attack(EntityDamageEvent source) {
return (source.getCause() == DamageCause.VOID ||
source.getCause() == DamageCause.FIRE_TICK ||
source.getCause() == DamageCause.ENTITY_EXPLOSION ||
source.getCause() == DamageCause.BLOCK_EXPLOSION)
&& super.attack(source);
}
@Override
public boolean onUpdate(int currentTick) {
if (this.closed) {
return false;
}
int tickDiff = currentTick - this.lastUpdate;
if (tickDiff <= 0 && !this.justCreated) {
return true;
}
this.lastUpdate = currentTick;
this.timing.startTiming();
boolean hasUpdate = this.entityBaseTick(tickDiff);
if (this.isAlive()) {
if (this.pickupDelay > 0 && this.pickupDelay < 32767) {
this.pickupDelay -= tickDiff;
if (this.pickupDelay < 0) {
this.pickupDelay = 0;
}
} else {
for (Entity entity : this.level.getNearbyEntities(this.boundingBox.grow(1, 0.5, 1), this)) {
if (entity instanceof Player) {
if (((Player) entity).pickupEntity(this, true)) {
return true;
}
}
}
}
this.motionY -= this.getGravity();
if (this.checkObstruction(this.x, this.y, this.z)) {
hasUpdate = true;
}
this.move(this.motionX, this.motionY, this.motionZ);
double friction = 1 - this.getDrag();
if (this.onGround && (Math.abs(this.motionX) > 0.00001 || Math.abs(this.motionZ) > 0.00001)) {
friction *= this.getLevel().getBlock(this.temporalVector.setComponents((int) Math.floor(this.x), (int) Math.floor(this.y - 1), (int) Math.floor(this.z) - 1)).getFrictionFactor();
}
this.motionX *= friction;
this.motionY *= 1 - this.getDrag();
this.motionZ *= friction;
if (this.onGround) {
this.motionY *= -0.5;
}
this.updateMovement();
if (this.age > 6000) {
ItemDespawnEvent ev = new ItemDespawnEvent(this);
this.server.getPluginManager().callEvent(ev);
if (ev.isCancelled()) {
this.age = 0;
} else {
this.kill();
hasUpdate = true;
}
}
}
this.timing.stopTiming();
return hasUpdate || !this.onGround || Math.abs(this.motionX) > 0.00001 || Math.abs(this.motionY) > 0.00001 || Math.abs(this.motionZ) > 0.00001;
}
@Override
public void saveNBT() {
super.saveNBT();
if (this.item != null) { // Yes, a item can be null... I don't know what causes this, but it can happen.
this.namedTag.putCompound("Item", NBTIO.putItemHelper(this.item, -1));
this.namedTag.putShort("Health", (int) this.getHealth());
this.namedTag.putShort("Age", this.age);
this.namedTag.putShort("PickupDelay", this.pickupDelay);
if (this.owner != null) {
this.namedTag.putString("Owner", this.owner);
}
if (this.thrower != null) {
this.namedTag.putString("Thrower", this.thrower);
}
}
}
@Override
public String getName() {
return this.hasCustomName() ? this.getNameTag() : (this.item.hasCustomName() ? this.item.getCustomName() : this.item.getName());
}
public Item getItem() {
return item;
}
@Override
public boolean canCollideWith(Entity entity) {
return false;
}
public int getPickupDelay() {
return pickupDelay;
}
public void setPickupDelay(int pickupDelay) {
this.pickupDelay = pickupDelay;
}
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
public String getThrower() {
return thrower;
}
public void setThrower(String thrower) {
this.thrower = thrower;
}
@Override
public void spawnTo(Player player) {
AddItemEntityPacket pk = new AddItemEntityPacket();
pk.entityUniqueId = this.getId();
pk.entityRuntimeId = this.getId();
pk.x = (float) this.x;
pk.y = (float) this.y;
pk.z = (float) this.z;
pk.speedX = (float) this.motionX;
pk.speedY = (float) this.motionY;
pk.speedZ = (float) this.motionZ;
pk.metadata = this.dataProperties;
pk.item = this.getItem();
player.dataPacket(pk);
super.spawnTo(player);
}
@Override
public boolean doesTriggerPressurePlate() {
return true;
}
}