/
EntityTame.java
171 lines (144 loc) · 5.06 KB
/
EntityTame.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
package net.aufdemrand.denizen.objects.properties.entity;
import net.aufdemrand.denizen.objects.dEntity;
import net.aufdemrand.denizen.objects.dPlayer;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.objects.Mechanism;
import net.aufdemrand.denizencore.objects.dList;
import net.aufdemrand.denizencore.objects.dObject;
import net.aufdemrand.denizencore.objects.properties.Property;
import net.aufdemrand.denizencore.tags.Attribute;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Tameable;
public class EntityTame implements Property {
public static boolean describes(dObject entity) {
return entity instanceof dEntity &&
((dEntity) entity).getBukkitEntity() instanceof Tameable;
}
public static EntityTame getFrom(dObject entity) {
if (!describes(entity)) {
return null;
}
else {
return new EntityTame((dEntity) entity);
}
}
///////////////////
// Instance Fields and Methods
/////////////
private EntityTame(dEntity tame) {
entity = tame;
}
dEntity entity;
/////////
// Property Methods
///////
@Override
public String getPropertyString() {
if (((Tameable) entity.getBukkitEntity()).isTamed()) {
OfflinePlayer owner = (OfflinePlayer) ((Tameable) entity.getBukkitEntity()).getOwner();
if (owner == null) {
return "true";
}
else {
return "true|" + owner.getUniqueId();
}
}
else {
return null;
}
}
@Override
public String getPropertyId() {
return "tame";
}
///////////
// dObject Attributes
////////
@Override
public String getAttribute(Attribute attribute) {
if (attribute == null) {
return null;
}
// <--[tag]
// @attribute <e@entity.is_tamed>
// @returns Element(Boolean)
// @mechanism dEntity.tame
// @group properties
// @description
// Returns whether the entity has been tamed.
// -->
if (attribute.startsWith("is_tamed")) {
return new Element(((Tameable) entity.getBukkitEntity()).isTamed())
.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <e@entity.get_owner>
// @returns dPlayer
// @mechanism dEntity.owner
// @group properties
// @description
// Returns the owner of a tamed entity.
// -->
if (attribute.startsWith("get_owner")) {
if (((Tameable) entity.getBukkitEntity()).isTamed()) {
return new dPlayer((OfflinePlayer) ((Tameable) entity.getBukkitEntity()).getOwner())
.getAttribute(attribute.fulfill(1));
}
else {
return null;
}
}
return null;
}
@Override
public void adjust(Mechanism mechanism) {
// <--[mechanism]
// @object dEntity
// @name tame
// @input Element(Boolean)(|dPlayer)
// @description
// Sets whether the entity has been tamed.
// Also available: <@link mechanism dEntity.owner>
// @tags
// <e@entity.is_tamed>
// <e@entity.is_tameable>
// -->
if (mechanism.matches("tame")) {
dList list = mechanism.getValue().asType(dList.class);
if (list.size() == 0) {
dB.echoError("Missing value for 'tame' mechanism!");
return;
}
if (new Element(list.get(0)).isBoolean()) {
((Tameable) entity.getBukkitEntity()).setTamed(mechanism.getValue().asBoolean());
}
else {
dB.echoError("Invalid boolean value!");
}
if (list.size() > 1 && new Element(list.get(1)).matchesType(dPlayer.class)) {
((Tameable) entity.getBukkitEntity()).setOwner(new Element(list.get(1)).asType(dPlayer.class).getOfflinePlayer());
}
}
// <--[mechanism]
// @object dEntity
// @name owner
// @input dPlayer
// @description
// Sets the entity's owner. Use with no input to make it not have an owner.
// Also available: <@link mechanism dEntity.tame>
// @tags
// <e@entity.is_tamed>
// <e@entity.is_tameable>
// <e@entity.get_owner>
// -->
if (mechanism.matches("owner")) {
if (mechanism.hasValue() && mechanism.requireObject(dPlayer.class)) {
((Tameable) entity.getBukkitEntity()).setOwner(mechanism.getValue().asType(dPlayer.class).getOfflinePlayer());
}
else {
((Tameable) entity.getBukkitEntity()).setOwner(null);
}
}
}
}