/
EntityInfected.java
179 lines (149 loc) · 5.64 KB
/
EntityInfected.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
package net.aufdemrand.denizen.objects.properties.entity;
import net.aufdemrand.denizen.nms.NMSHandler;
import net.aufdemrand.denizen.nms.NMSVersion;
import net.aufdemrand.denizen.objects.dEntity;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.objects.Mechanism;
import net.aufdemrand.denizencore.objects.dObject;
import net.aufdemrand.denizencore.objects.properties.Property;
import net.aufdemrand.denizencore.tags.Attribute;
import net.citizensnpcs.api.npc.NPC;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Villager;
import org.bukkit.entity.Zombie;
import org.bukkit.entity.ZombieVillager;
public class EntityInfected implements Property {
public static boolean describes(dObject entity) {
if (!(entity instanceof dEntity)) {
return false;
}
// Check if a Villager or Zombie -- the only two EntityTypes that can be 'infected'
return ((dEntity) entity).getBukkitEntityType() == EntityType.ZOMBIE
|| ((dEntity) entity).getBukkitEntityType() == EntityType.VILLAGER;
}
public static EntityInfected getFrom(dObject entity) {
if (!describes(entity)) {
return null;
}
else {
return new EntityInfected((dEntity) entity);
}
}
public static final String[] handledTags = new String[] {
"is_infected"
};
public static final String[] handledMechs = new String[] {
"is_infected"
};
///////////////////
// Instance Fields and Methods
/////////////
private EntityInfected(dEntity item) {
infected = item;
}
dEntity infected;
public boolean isInfected() {
return infected.getBukkitEntity() instanceof Zombie
&& (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_11_R1)
? infected.getBukkitEntityType() == EntityType.ZOMBIE_VILLAGER
: ((Zombie) infected.getBukkitEntity()).isVillager());
}
public void setInfected(boolean bool) {
if (bool) {
if (infected.isCitizensNPC()) {
NPC infected_npc = infected.getDenizenNPC().getCitizen();
infected_npc.setBukkitEntityType(EntityType.ZOMBIE_VILLAGER);
}
// TODO: Improve upon.
else if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_11_R1)) {
LivingEntity entity = infected.getLivingEntity();
// Make a new entity
ZombieVillager infect = (ZombieVillager) entity.getLocation().getWorld().spawnEntity(infected.getLocation(), EntityType.ZOMBIE_VILLAGER);
// Set health
infect.setHealth(entity.getHealth());
// Set equipment
infect.getEquipment().setArmorContents(entity.getEquipment().getArmorContents());
// Remove the Villager
entity.remove();
// Set the dEntity to the new entity
infected.setEntity(infect);
}
// TODO: Should be bother allowing villager input at all?
// If it's a Villager, we need to spawn a Zombie instead.
// This is kind of messy, and can be improved upon.
// TODO: Improve upon.
else if (infected.getBukkitEntity() instanceof Villager) {
Villager villager = (Villager) infected.getBukkitEntity();
// Make a new entity
Zombie infect = (Zombie) villager.getLocation().getWorld().spawnEntity(infected.getLocation(), EntityType.ZOMBIE);
infect.setVillager(true);
// Set health
infect.setHealth(villager.getHealth());
// Set equipment
infect.getEquipment().setArmorContents(villager.getEquipment().getArmorContents());
// Remove the Villager
villager.remove();
// Set the dEntity to the new entity
infected.setEntity(infect);
}
// Much much easier
else if (infected.getBukkitEntity() instanceof Zombie) {
((Zombie) infected).setVillager(true);
}
}
}
/////////
// Property Methods
///////
@Override
public String getPropertyString() {
if (isInfected()) {
return "true";
}
else {
return null;
}
}
@Override
public String getPropertyId() {
return "infected";
}
///////////
// dObject Attributes
////////
@Override
public String getAttribute(Attribute attribute) {
if (attribute == null) {
return null;
}
// <--[tag]
// @attribute <e@entity.is_infected>
// @returns Element(Boolean)
// @group properties
// @description
// If the entity is infectable, returns whether the entity is infected.
// Currently only Zombie or Villager entities can be infected.
// -->
if (attribute.startsWith("is_infected")) {
return new Element(isInfected())
.getAttribute(attribute.fulfill(1));
}
return null;
}
@Override
public void adjust(Mechanism mechanism) {
// <--[mechanism]
// @object dEntity
// @name is_infected
// @input Element(Boolean)
// @description
// Sets whether the entity is infected.
// @tags
// <e@entity.is_infected>
// -->
if (mechanism.matches("is_infected") && mechanism.requireBoolean()) {
setInfected(mechanism.getValue().asBoolean());
}
}
}