/
EntityConversionTime.java
149 lines (129 loc) · 5.02 KB
/
EntityConversionTime.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
package com.denizenscript.denizen.objects.properties.entity;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizencore.objects.Mechanism;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.DurationTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.properties.Property;
import com.denizenscript.denizencore.objects.properties.PropertyParser;
import org.bukkit.entity.*;
public class EntityConversionTime implements Property {
public static boolean describes(ObjectTag object) {
if (!(object instanceof EntityTag)) {
return false;
}
Entity entity = ((EntityTag) object).getBukkitEntity();
return (entity instanceof Zombie && !(entity instanceof PigZombie))
|| entity instanceof Skeleton;
}
public static EntityConversionTime getFrom(ObjectTag entity) {
if (!describes(entity)) {
return null;
}
else {
return new EntityConversionTime((EntityTag) entity);
}
}
public static final String[] handledMechs = new String[] {
"conversion_duration", "drowned_conversion_duration"
};
public EntityConversionTime(EntityTag ent) {
entity = ent;
}
EntityTag entity;
@Override
public String getPropertyString() {
if (isConverting()) {
return getConversionTime().identify();
}
return null;
}
@Override
public String getPropertyId() {
return "conversion_duration";
}
public static void register() {
// <--[tag]
// @attribute <EntityTag.conversion_duration>
// @returns DurationTag
// @mechanism EntityTag.conversion_duration
// @group properties
// @description
// Returns the duration of time until an entity completes a conversion. See <@link tag EntityTag.is_converting> for examples of conversions.
// When this value hits 0, the conversion completes.
// See also <@link tag EntityTag.in_water_duration>
// -->
PropertyParser.registerTag(EntityConversionTime.class, DurationTag.class, "conversion_duration", (attribute, object) -> {
if (!object.isConverting()) {
attribute.echoError("This entity is not converting!");
return null;
}
return object.getConversionTime();
}, "drowned_conversion_duration");
// <--[tag]
// @attribute <EntityTag.is_converting>
// @returns ElementTag(Boolean)
// @group properties
// @description
// Returns whether an entity is currently undergoing a conversion process. This can be:
// A zombie villager being cured,
// A zombie becoming a drowned (See also <@link tag EntityTag.in_water_duration>),
// A husk becoming a zombie, or
// A skeleton becoming a stray.
// -->
PropertyParser.registerTag(EntityConversionTime.class, ElementTag.class, "is_converting", (attribute, object) -> {
return new ElementTag(object.isConverting());
});
}
public boolean isZombie() {
return entity.getBukkitEntity() instanceof Zombie;
}
public boolean isSkeleton() {
return entity.getBukkitEntity() instanceof Skeleton;
}
public Zombie getZombie() {
return (Zombie) entity.getBukkitEntity();
}
public Skeleton getSkeleton() {
return (Skeleton) entity.getBukkitEntity();
}
public boolean isConverting() {
if (isZombie()) {
return getZombie().isConverting();
}
else if (isSkeleton()) {
return getSkeleton().isConverting();
}
return false;
}
public DurationTag getConversionTime() {
if (isZombie()) {
return new DurationTag((long) getZombie().getConversionTime());
}
else if (isSkeleton()) {
return new DurationTag((long) getSkeleton().getConversionTime());
}
return null;
}
@Override
public void adjust(Mechanism mechanism) {
// <--[mechanism]
// @object EntityTag
// @name conversion_duration
// @input DurationTag
// @description
// Sets the duration of time until an entity completes a conversion. See <@link tag EntityTag.is_converting> for examples of conversions.
// When this value hits 0, the conversion completes.
// @tags
// <EntityTag.conversion_duration>
// -->
if ((mechanism.matches("conversion_duration") || mechanism.matches("drowned_conversion_duration")) && mechanism.requireObject(DurationTag.class)) {
if (isZombie()) {
getZombie().setConversionTime(mechanism.valueAsType(DurationTag.class).getTicksAsInt());
}
else if (isSkeleton()) {
getSkeleton().setConversionTime(mechanism.valueAsType(DurationTag.class).getTicksAsInt());
}
}
}
}