Skip to content

Commit c9a2770

Browse files
Fix up EntityData for better cross version support.
Fix null pointer exception in right click item listener.
1 parent 710ec1d commit c9a2770

File tree

5 files changed

+100
-22
lines changed

5 files changed

+100
-22
lines changed

build.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,15 @@
6060
<mkdir dir="${dist.dir}"/>
6161
</target>
6262

63+
<!-- this just runs an echo for now, but will be used for ensuring resources -->
64+
<target name="get-resources">
65+
<echo>
66+
Running the get resources phase
67+
</echo>
68+
</target>
69+
6370
<!-- Compile the core -->
64-
<target name="core-compile" depends="clean">
71+
<target name="core-compile" depends="get-resources, clean">
6572
<mkdir dir="${core.build.dir}"/>
6673
<javac srcdir="${core.src.dir}" destdir="${core.build.dir}" debug="true" source="${java.core.version}" target="${java.core.version}">
6774
<classpath>

src/com/nisovin/magicspells/spells/passive/RightClickItemListener.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ public class RightClickItemListener extends PassiveListener {
3232

3333
@Override
3434
public void registerSpell(PassiveSpell spell, PassiveTrigger trigger, String var) {
35+
if (var == null) {
36+
MagicSpells.error(trigger.getName() + " cannot accept a null variable");
37+
return;
38+
}
3539
Set<Material> materialSetAddTo = null;
3640
Map<MagicMaterial, List<PassiveSpell>> typesMapAddTo = null;
3741
if (isMainHand(trigger)) {

src/com/nisovin/magicspells/spells/targeted/SpawnMonsterSpell.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ void retarget(LivingEntity ignore) {
526526
LivingEntity t = null;
527527
int r = retargetRange * retargetRange;
528528
for (Entity e : monster.getNearbyEntities(retargetRange, retargetRange, retargetRange)) {
529-
if (e instanceof LivingEntity && validTargetList.canTarget(caster, (LivingEntity)e) && e != caster && e != ignore) {
529+
if (e instanceof LivingEntity && validTargetList.canTarget(caster, e) && e != caster && e != ignore) {
530530
if (e instanceof Player) {
531531
Player p = (Player)e;
532532
if (p.getGameMode() == GameMode.CREATIVE || p.getGameMode() == GameMode.SPECTATOR) {
@@ -570,7 +570,7 @@ public void run() {
570570
List<Entity> list = entity.getNearbyEntities(targetRange, targetRange, targetRange);
571571
List<LivingEntity> targetable = new ArrayList<LivingEntity>();
572572
for (Entity e : list) {
573-
if (e instanceof LivingEntity && validTargetList.canTarget(caster, (LivingEntity)e)) {
573+
if (e instanceof LivingEntity && validTargetList.canTarget(caster, e)) {
574574
targetable.add((LivingEntity)e);
575575
}
576576
}

src/com/nisovin/magicspells/util/EntityData.java

Lines changed: 59 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,17 @@ public EntityData(String type) {
2626
if (type.equalsIgnoreCase("human") || type.equalsIgnoreCase("player")) {
2727
type = "player";
2828
} else if (type.equalsIgnoreCase("wither skeleton")) {
29+
if (V1_11EntityTypeHandler.newEntityTypesPresent()) {
30+
entityType = EntityType.WITHER_SKELETON;
31+
} else {
32+
entityType = EntityType.SKELETON;
33+
}
2934
type = "skeleton";
3035
flag = true;
3136
} else if (type.equalsIgnoreCase("zombie villager") || type.equalsIgnoreCase("villager zombie")) {
37+
if (V1_11EntityTypeHandler.newEntityTypesPresent()) {
38+
entityType = EntityType.ZOMBIE_VILLAGER;
39+
}
3240
type = "zombie";
3341
var1 = 1;
3442
} else if (type.equalsIgnoreCase("powered creeper")) {
@@ -147,14 +155,29 @@ public EntityData(String type) {
147155
String t = data.remove(0).toLowerCase();
148156
if (t.equals("donkey")) {
149157
var1 = 1;
158+
if (V1_11EntityTypeHandler.newEntityTypesPresent()) {
159+
entityType = EntityType.DONKEY;
160+
}
150161
} else if (t.equals("mule")) {
151162
var1 = 2;
163+
if (V1_11EntityTypeHandler.newEntityTypesPresent()) {
164+
entityType = EntityType.MULE;
165+
}
152166
} else if (t.equals("skeleton") || t.equals("skeletal")) {
153167
var1 = 4;
168+
if (V1_11EntityTypeHandler.newEntityTypesPresent()) {
169+
entityType = EntityType.SKELETON_HORSE;
170+
}
154171
} else if (t.equals("zombie") || t.equals("undead")) {
155172
var1 = 3;
173+
if (V1_11EntityTypeHandler.newEntityTypesPresent()) {
174+
entityType = EntityType.ZOMBIE_HORSE;
175+
}
156176
} else {
157177
var1 = 0;
178+
if (V1_11EntityTypeHandler.newEntityTypesPresent()) {
179+
entityType = EntityType.HORSE;
180+
}
158181
}
159182
data.remove(0);
160183
}
@@ -174,10 +197,16 @@ public EntityData(String type) {
174197
} else if (type.equalsIgnoreCase("mule")) {
175198
var1 = 2;
176199
type = "entityhorse";
200+
if (V1_11EntityTypeHandler.newEntityTypesPresent()) {
201+
entityType = EntityType.MULE;
202+
}
177203
} else if (type.equalsIgnoreCase("donkey")) {
178204
var1 = 1;
179205
type = "entityhorse";
180206
} else if (type.equalsIgnoreCase("elder guardian")) {
207+
if (V1_11EntityTypeHandler.newEntityTypesPresent()) {
208+
entityType = EntityType.ELDER_GUARDIAN;
209+
}
181210
flag = true;
182211
type = "guardian";
183212
}
@@ -196,10 +225,12 @@ public EntityData(String type) {
196225
if (type.equals("magmacube")) type = "lavaslime";
197226
var1 = Integer.parseInt(data[1]);
198227
}
199-
if (type.equals("player")) {
200-
entityType = EntityType.PLAYER;
201-
} else {
202-
entityType = EntityType.fromName(type);
228+
if (entityType == null) {
229+
if (type.equals("player")) {
230+
entityType = EntityType.PLAYER;
231+
} else {
232+
entityType = EntityType.fromName(type);
233+
}
203234
}
204235
}
205236

@@ -224,16 +255,21 @@ public int getVar3() {
224255
}
225256

226257
public Entity spawn(Location loc) {
258+
227259
Entity entity = loc.getWorld().spawnEntity(loc, entityType);
228260
if (entity instanceof Ageable && flag) {
229261
((Ageable)entity).setBaby();
230262
}
231263
if (entityType == EntityType.ZOMBIE) {
232264
((Zombie)entity).setBaby(flag);
233-
((Zombie)entity).setVillager(var1 == 1);
265+
if (!V1_11EntityTypeHandler.newEntityTypesPresent()) {
266+
((Zombie)entity).setVillager(var1 == 1); // this is safe due to version checks
267+
}
234268
} else if (entityType == EntityType.SKELETON) {
235-
if (flag) {
236-
((Skeleton)entity).setSkeletonType(Skeleton.SkeletonType.WITHER);
269+
if (!V1_11EntityTypeHandler.newEntityTypesPresent()) {
270+
if (flag) {
271+
((Skeleton)entity).setSkeletonType(Skeleton.SkeletonType.WITHER); // this is safe due to version checks
272+
}
237273
}
238274
} else if (entityType == EntityType.CREEPER) {
239275
if (flag) {
@@ -295,20 +331,24 @@ public Entity spawn(Location loc) {
295331
((Rabbit)entity).setRabbitType(Rabbit.Type.THE_KILLER_BUNNY);
296332
}*/
297333
} else if (entityType == EntityType.GUARDIAN) {
298-
if (flag) {
299-
((Guardian)entity).setElder(true);
334+
if (!V1_11EntityTypeHandler.newEntityTypesPresent()) {
335+
if (flag) {
336+
((Guardian)entity).setElder(true); // this is safe due to version checks
337+
}
300338
}
301339
} else if (entityType == EntityType.HORSE) {
302-
if (var1 == 0) {
303-
((Horse)entity).setVariant(Horse.Variant.HORSE);
304-
} else if (var1 == 1) {
305-
((Horse)entity).setVariant(Horse.Variant.DONKEY);
306-
} else if (var1 == 2) {
307-
((Horse)entity).setVariant(Horse.Variant.MULE);
308-
} else if (var1 == 3) {
309-
((Horse)entity).setVariant(Horse.Variant.UNDEAD_HORSE);
310-
} else if (var1 == 4) {
311-
((Horse)entity).setVariant(Horse.Variant.SKELETON_HORSE);
340+
if (!V1_11EntityTypeHandler.newEntityTypesPresent()) {
341+
if (var1 == 0) {
342+
((Horse)entity).setVariant(Horse.Variant.HORSE); // this is safe due to version checks
343+
} else if (var1 == 1) {
344+
((Horse)entity).setVariant(Horse.Variant.DONKEY); // this is safe due to version checks
345+
} else if (var1 == 2) {
346+
((Horse)entity).setVariant(Horse.Variant.MULE); // this is safe due to version checks
347+
} else if (var1 == 3) {
348+
((Horse)entity).setVariant(Horse.Variant.UNDEAD_HORSE); // this is safe due to version checks
349+
} else if (var1 == 4) {
350+
((Horse)entity).setVariant(Horse.Variant.SKELETON_HORSE); // this is safe due to version checks
351+
}
312352
}
313353
}
314354
return entity;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.nisovin.magicspells.util;
2+
3+
import org.bukkit.entity.EntityType;
4+
5+
public class V1_11EntityTypeHandler {
6+
7+
private static boolean initialized = false;
8+
private static boolean v_1_11API = false;
9+
10+
private static void initialize() {
11+
if (initialized) return;
12+
13+
EntityType type = EntityType.valueOf("STRAY");
14+
if (type != null) v_1_11API = true;
15+
16+
initialized = true;
17+
}
18+
19+
public static boolean newEntityTypesPresent() {
20+
initialize();
21+
return v_1_11API;
22+
}
23+
24+
25+
26+
27+
}

0 commit comments

Comments
 (0)