-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
EntityItem.java
189 lines (164 loc) · 6.85 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
package com.denizenscript.denizen.objects.properties.entity;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.NMSVersion;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.objects.ItemTag;
import com.denizenscript.denizencore.objects.Mechanism;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.properties.Property;
import com.denizenscript.denizencore.objects.properties.PropertyParser;
import com.denizenscript.denizencore.tags.Attribute;
import net.citizensnpcs.api.npc.NPC;
import org.bukkit.Material;
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.*;
import org.bukkit.inventory.ItemStack;
public class EntityItem implements Property {
public static boolean describes(ObjectTag object) {
if (!(object instanceof EntityTag)) {
return false;
}
Entity entity = ((EntityTag) object).getBukkitEntity();
return entity instanceof Item
|| entity instanceof Enderman
|| entity instanceof Trident // TODO: 1.15: supported in ThrowableProjectile now, remove this part when 1.14 is dropped
|| (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_15) && entity instanceof ThrowableProjectile)
|| (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_16) && entity instanceof EnderSignal);
}
public static EntityItem getFrom(ObjectTag entity) {
if (!describes(entity)) {
return null;
}
else {
return new EntityItem((EntityTag) entity);
}
}
public static final String[] handledTags = new String[] {
"item"
};
public static final String[] handledMechs = new String[] {
"item"
};
private EntityItem(EntityTag entity) {
item = entity;
}
EntityTag item;
public ItemTag getItem() {
if (isDroppedItem()) {
return new ItemTag(getDroppedItem().getItemStack());
}
else if (isEnderman()) {
BlockData data = getEnderman().getCarriedBlock();
if (data == null) {
return new ItemTag(Material.AIR);
}
return new ItemTag(data.getMaterial());
}
else if (isTrident()) {
// TODO: 1.15: supported by ThrowableProjectile now, remove this part when 1.14 is dropped
return new ItemTag(NMSHandler.getEntityHelper().getItemFromTrident(item.getBukkitEntity()));
}
else if (isThrowableProjectile()) {
return new ItemTag(((ThrowableProjectile) item.getBukkitEntity()).getItem()); // TODO: 1.15
}
else if (isEnderSignal()) {
return new ItemTag(getEnderSignal().getItem());
}
return null; // Unreachable
}
public boolean isDroppedItem() {
return item.getBukkitEntity() instanceof Item;
}
public boolean isEnderman() {
return item.getBukkitEntity() instanceof Enderman;
}
public boolean isTrident() {
return item.getBukkitEntity() instanceof Trident;
}
public boolean isThrowableProjectile() {
return NMSHandler.getVersion().isAtLeast(NMSVersion.v1_15) && item.getBukkitEntity() instanceof ThrowableProjectile;
}
public boolean isEnderSignal() {
return NMSHandler.getVersion().isAtLeast(NMSVersion.v1_16) && item.getBukkitEntity() instanceof EnderSignal;
}
public Item getDroppedItem() {
return (Item) item.getBukkitEntity();
}
public Enderman getEnderman() {
return (Enderman) item.getBukkitEntity();
}
public Trident getTrident() {
return (Trident) item.getBukkitEntity();
}
public EnderSignal getEnderSignal() {
return (EnderSignal) item.getBukkitEntity();
}
/*public ThrowableProjectile getThrowableProjectile() { // TODO: 1.15
return (ThrowableProjectile) item.getBukkitEntity();
}*/
@Override
public String getPropertyString() {
ItemTag item = getItem();
if (item.getBukkitMaterial() != Material.AIR) {
return item.identify();
}
return null;
}
@Override
public String getPropertyId() {
return "item";
}
public static void registerTags() {
// <--[tag]
// @attribute <EntityTag.item>
// @returns ItemTag
// @mechanism EntityTag.item
// @group properties
// @description
// If the entity is a dropped item, returns the item represented by the entity.
// If the entity is a trident, returns the trident item represented by the entity.
// If the entity is a throwable projectile, returns the display item for that projectile.
// If the entity is an ender signal, returns the item to be displayed and dropped by it.
// -->
PropertyParser.<EntityItem, ItemTag>registerTag(ItemTag.class, "item", (attribute, object) -> {
return object.getItem();
});
}
@Override
public void adjust(Mechanism mechanism) {
// <--[mechanism]
// @object EntityTag
// @name item
// @input ItemTag
// @description
// If the entity is a dropped item, sets the item represented by the entity.
// If the entity is a trident, sets the trident item represented by the entity.
// If the item is a throwable projectile, sets the display item for that projectile.
// If the entity is an ender signal, sets the item to be displayed and dropped by it.
// @tags
// <EntityTag.item>
// -->
if (mechanism.matches("item") && mechanism.requireObject(ItemTag.class)) {
ItemStack itemStack = mechanism.valueAsType(ItemTag.class).getItemStack();
if (item.isCitizensNPC()) {
item.getDenizenNPC().getCitizen().data().setPersistent(NPC.ITEM_ID_METADATA, itemStack.getType().name());
}
if (isDroppedItem()) {
getDroppedItem().setItemStack(itemStack);
}
else if (isEnderman()) {
getEnderman().setCarriedBlock(itemStack.getType().createBlockData());
}
else if (isTrident()) {
// TODO: 1.15: supported by ThrowableProjectile now, remove this part when 1.14 is dropped
NMSHandler.getEntityHelper().setItemForTrident(item.getBukkitEntity(), itemStack);
}
else if (isThrowableProjectile()) {
((ThrowableProjectile) item.getBukkitEntity()).setItem(itemStack); // TODO: 1.15
}
else if (isEnderSignal()) {
getEnderSignal().setItem(itemStack);
}
}
}
}