/
EntityItemProjectileImpl.java
95 lines (80 loc) · 3.29 KB
/
EntityItemProjectileImpl.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
package com.denizenscript.denizen.nms.v1_17.impl.entities;
import com.denizenscript.denizencore.utilities.ReflectionHelper;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.google.common.base.Preconditions;
import net.minecraft.network.syncher.EntityDataAccessor;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.projectile.ThrowableProjectile;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import org.bukkit.Location;
import java.lang.invoke.MethodHandle;
public class EntityItemProjectileImpl extends ThrowableProjectile {
public static MethodHandle setBukkitEntityMethod = ReflectionHelper.getFinalSetter(Entity.class, "bukkitEntity");
public static final EntityDataAccessor<ItemStack> ITEM;
static {
EntityDataAccessor<ItemStack> watcher = null;
try {
watcher = (EntityDataAccessor<ItemStack>) ReflectionHelper.getFields(EntityItem.class).get("ITEM").get(null);
}
catch (Throwable ex) {
Debug.echoError(ex);
}
ITEM = watcher;
}
public EntityItemProjectileImpl(Level world, Location location, ItemStack item) {
super((net.minecraft.world.entity.EntityType) net.minecraft.world.entity.EntityType.ITEM, world);
try {
setBukkitEntityMethod.invoke(this, new CraftItemProjectileImpl(world.getServer(), this));
}
catch (Throwable ex) {
Debug.echoError(ex);
}
setPositionRotation(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
setItemStack(item);
}
@Override
protected void initDatawatcher() {
this.getEntityData().register(ITEM, ItemStack.b);
}
public ItemStack getItemStack() {
return this.getEntityData().get(ITEM);
}
public void setItemStack(ItemStack itemstack) {
Preconditions.checkArgument(!itemstack.isEmpty(), "Cannot drop air");
this.getEntityData().set(ITEM, itemstack);
this.getEntityData().markDirty(ITEM);
}
@Override
protected void a(MovingObjectPositionBlock movingobjectpositionblock) {
super.a(movingobjectpositionblock);
die();
}
@Override
public void a(EntityDataAccessor<?> datawatcherobject) {
super.a(datawatcherobject);
if (ITEM.equals(datawatcherobject)) {
this.getItemStack().a(this);
}
}
@Override
public void saveData(net.minecraft.nbt.CompoundTag nbttagcompound) {
if (!this.getItemStack().isEmpty()) {
nbttagcompound.set("Item", this.getItemStack().save(new net.minecraft.nbt.CompoundTag()));
}
super.saveData(nbttagcompound);
}
@Override
public void loadData(net.minecraft.nbt.CompoundTag nbttagcompound) {
net.minecraft.nbt.CompoundTag nbttagcompound1 = nbttagcompound.getCompound("Item");
this.setItemStack(ItemStack.a(nbttagcompound1));
if (this.getItemStack().isEmpty()) {
this.die();
}
super.loadData(nbttagcompound);
}
@Override
public CraftItemProjectileImpl getBukkitEntity() {
return (CraftItemProjectileImpl) super.getBukkitEntity();
}
}