Added Trigger on Mob Death

This commit is contained in:
Sn0wStorm 2013-08-03 13:47:50 +02:00
parent 13bff0801e
commit d0691d737a
4 changed files with 83 additions and 0 deletions

View File

@ -252,4 +252,8 @@ public class DMobType {
return null; return null;
} }
public String getName() {
return name;
}
} }

View File

@ -7,6 +7,7 @@ import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import com.dre.dungeonsxl.DMobType; import com.dre.dungeonsxl.DMobType;
import com.dre.dungeonsxl.trigger.MobTrigger;
public class DMob { public class DMob {
@ -33,6 +34,7 @@ public class DMob {
if (event.getEntity() instanceof LivingEntity) { if (event.getEntity() instanceof LivingEntity) {
LivingEntity victim = (LivingEntity) event.getEntity(); LivingEntity victim = (LivingEntity) event.getEntity();
GameWorld gworld = GameWorld.get(victim.getWorld()); GameWorld gworld = GameWorld.get(victim.getWorld());
String name = null;
if (gworld != null) { if (gworld != null) {
for (DMob dmob : gworld.dmobs) { for (DMob dmob : gworld.dmobs) {
@ -46,6 +48,14 @@ public class DMob {
event.getDrops().add(item); event.getDrops().add(item);
} }
} }
name = dmob.type.getName();
} else {
name = victim.getType().getName();
}
MobTrigger trigger = MobTrigger.get(name, gworld);
if (trigger != null) {
trigger.onTrigger();
} }
gworld.dmobs.remove(dmob); gworld.dmobs.remove(dmob);

View File

@ -0,0 +1,63 @@
package com.dre.dungeonsxl.trigger;
import java.util.Map;
import java.util.HashMap;
import java.util.ArrayList;
import com.dre.dungeonsxl.game.GameWorld;
public class MobTrigger extends Trigger {
private static Map<GameWorld, ArrayList<MobTrigger>> triggers = new HashMap<GameWorld, ArrayList<MobTrigger>>();
private String name;
public MobTrigger(String name) {
this.name = name;
}
public void onTrigger() {
triggered = true;
updateDSigns();
}
public void register(GameWorld gworld) {
if (!hasTriggers(gworld)) {
ArrayList<MobTrigger> list = new ArrayList<MobTrigger>();
list.add(this);
triggers.put(gworld, list);
} else {
triggers.get(gworld).add(this);
}
}
public void unregister(GameWorld gworld) {
if (hasTriggers(gworld)) {
triggers.get(gworld).remove(this);
}
}
public static MobTrigger getOrCreate(String name, GameWorld gworld) {
MobTrigger trigger = get(name, gworld);
if (trigger != null) {
return trigger;
}
return new MobTrigger(name);
}
public static MobTrigger get(String name, GameWorld gworld) {
if (hasTriggers(gworld)) {
for (MobTrigger trigger : triggers.get(gworld)) {
if (trigger.name.equalsIgnoreCase(name)) {
return trigger;
}
}
}
return null;
}
public static boolean hasTriggers(GameWorld gworld) {
return !triggers.isEmpty() && triggers.containsKey(gworld);
}
}

View File

@ -51,6 +51,12 @@ public abstract class Trigger {
trigger = InteractTrigger.getOrCreate(P.p.parseInt(splitted[1]), dsign.getGameWorld()); trigger = InteractTrigger.getOrCreate(P.p.parseInt(splitted[1]), dsign.getGameWorld());
} }
} else if (splitted[0].equalsIgnoreCase("M")) {
if (splitted.length > 1) {
trigger = MobTrigger.getOrCreate(splitted[1], dsign.getGameWorld());
}
} }
} }
return trigger; return trigger;