/
EntityEquipment.java
187 lines (162 loc) · 5.81 KB
/
EntityEquipment.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
package com.denizenscript.denizen.objects.properties.entity;
import com.denizenscript.denizen.objects.dEntity;
import com.denizenscript.denizen.objects.dItem;
import com.denizenscript.denizencore.objects.Mechanism;
import com.denizenscript.denizencore.objects.dList;
import com.denizenscript.denizencore.objects.dObject;
import com.denizenscript.denizencore.objects.properties.Property;
import com.denizenscript.denizencore.tags.Attribute;
import org.bukkit.Material;
import org.bukkit.entity.LivingEntity;
import org.bukkit.inventory.ItemStack;
public class EntityEquipment implements Property {
public static boolean describes(dObject entity) {
return entity instanceof dEntity
&& ((dEntity) entity).getBukkitEntity() instanceof LivingEntity;
}
public static EntityEquipment getFrom(dObject entity) {
if (!describes(entity)) {
return null;
}
else {
return new EntityEquipment((dEntity) entity);
}
}
public static final String[] handledTags = new String[] {
"equipment"
};
public static final String[] handledMechs = new String[] {
"equipment"
};
///////////////////
// Instance Fields and Methods
/////////////
private EntityEquipment(dEntity ent) {
entity = ent;
}
dEntity entity;
/////////
// Property Methods
///////
@Override
public String getPropertyString() {
return entity.getEquipment().identify();
}
@Override
public String getPropertyId() {
return "equipment";
}
///////////
// dObject Attributes
////////
@Override
public String getAttribute(Attribute attribute) {
if (attribute == null) {
return null;
}
// <--[tag]
// @attribute <e@entity.equipment.boots>
// @returns dItem
// @group inventory
// @description
// Returns the item the entity is wearing as boots.
// -->
if (attribute.startsWith("equipment.boots")) {
if (entity.getLivingEntity().getEquipment().getBoots() != null) {
return new dItem(entity.getLivingEntity().getEquipment().getBoots())
.getAttribute(attribute.fulfill(2));
}
else {
return new dItem(Material.AIR)
.getAttribute(attribute.fulfill(2));
}
}
// <--[tag]
// @attribute <e@entity.equipment.chestplate>
// @returns dItem
// @group inventory
// @description
// Returns the item the entity is wearing as a chestplate.
// -->
else if (attribute.startsWith("equipment.chestplate") ||
attribute.startsWith("equipment.chest")) {
if (entity.getLivingEntity().getEquipment().getChestplate() != null) {
return new dItem(entity.getLivingEntity().getEquipment().getChestplate())
.getAttribute(attribute.fulfill(2));
}
else {
return new dItem(Material.AIR)
.getAttribute(attribute.fulfill(2));
}
}
// <--[tag]
// @attribute <e@entity.equipment.helmet>
// @returns dItem
// @group inventory
// @description
// Returns the item the entity is wearing as a helmet.
// -->
else if (attribute.startsWith("equipment.helmet") ||
attribute.startsWith("equipment.head")) {
if (entity.getLivingEntity().getEquipment().getHelmet() != null) {
return new dItem(entity.getLivingEntity().getEquipment().getHelmet())
.getAttribute(attribute.fulfill(2));
}
else {
return new dItem(Material.AIR)
.getAttribute(attribute.fulfill(2));
}
}
// <--[tag]
// @attribute <e@entity.equipment.leggings>
// @returns dItem
// @group inventory
// @description
// Returns the item the entity is wearing as leggings.
// -->
else if (attribute.startsWith("equipment.leggings") ||
attribute.startsWith("equipment.legs")) {
if (entity.getLivingEntity().getEquipment().getLeggings() != null) {
return new dItem(entity.getLivingEntity().getEquipment().getLeggings())
.getAttribute(attribute.fulfill(2));
}
else {
return new dItem(Material.AIR)
.getAttribute(attribute.fulfill(2));
}
}
// <--[tag]
// @attribute <e@entity.equipment>
// @returns dList
// @group inventory
// @description
// Returns a dList containing the entity's equipment.
// Output list is boots|leggings|chestplate|helmet
// -->
else if (attribute.startsWith("equipment")) {
return entity.getEquipment().getAttribute(attribute.fulfill(1));
}
return null;
}
@Override
public void adjust(Mechanism mechanism) {
// <--[mechanism]
// @object dEntity
// @name equipment
// @input dList(dItem)
// @description
// Sets the entity's worn equipment.
// Input list is boots|leggings|chestplate|helmet
// @tags
// <e@entity.equipment>
// -->
if (mechanism.matches("equipment")) {
dList list = dList.valueOf(mechanism.getValue().asString());
ItemStack[] stacks = new ItemStack[list.size()];
for (int i = 0; i < list.size(); i++) {
stacks[i] = dItem.valueOf(list.get(i), mechanism.context).getItemStack();
}
entity.getLivingEntity().getEquipment().setArmorContents(stacks);
}
}
}