diff --git a/src/main/java/net/aufdemrand/denizen/Denizen.java b/src/main/java/net/aufdemrand/denizen/Denizen.java index cdc2717308..d607c28037 100644 --- a/src/main/java/net/aufdemrand/denizen/Denizen.java +++ b/src/main/java/net/aufdemrand/denizen/Denizen.java @@ -599,6 +599,7 @@ public void onEnable() { ScriptEvent.registerScriptEvent(new EntityDamagedScriptEvent()); ScriptEvent.registerScriptEvent(new EntityDeathScriptEvent()); ScriptEvent.registerScriptEvent(new EntityDespawnScriptEvent()); + ScriptEvent.registerScriptEvent(new EntityExplodesScriptEvent()); ScriptEvent.registerScriptEvent(new EntityFormsBlock()); ScriptEvent.registerScriptEvent(new EntityInteractScriptEvent()); ScriptEvent.registerScriptEvent(new EntityKilledScriptEvent()); diff --git a/src/main/java/net/aufdemrand/denizen/events/scriptevents/EntityExplodesScriptEvent.java b/src/main/java/net/aufdemrand/denizen/events/scriptevents/EntityExplodesScriptEvent.java new file mode 100644 index 0000000000..a3c1f29a41 --- /dev/null +++ b/src/main/java/net/aufdemrand/denizen/events/scriptevents/EntityExplodesScriptEvent.java @@ -0,0 +1,155 @@ +package net.aufdemrand.denizen.events.scriptevents; + +import net.aufdemrand.denizen.BukkitScriptEntryData; +import net.aufdemrand.denizen.objects.dEntity; +import net.aufdemrand.denizen.objects.dLocation; +import net.aufdemrand.denizen.utilities.DenizenAPI; +import net.aufdemrand.denizen.utilities.debugging.dB; +import net.aufdemrand.denizencore.events.ScriptEvent; +import net.aufdemrand.denizencore.objects.*; +import net.aufdemrand.denizencore.scripts.ScriptEntryData; +import net.aufdemrand.denizencore.scripts.containers.ScriptContainer; +import net.aufdemrand.denizencore.utilities.CoreUtilities; +import org.bukkit.Bukkit; +import org.bukkit.block.Block; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.entity.EntityExplodeEvent; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; + +public class EntityExplodesScriptEvent extends ScriptEvent implements Listener { + + // <--[event] + // @Events + // entity explodes + // explodes + // + // @Cancellable true + // + // @Triggers when an entity explodes. + // + // @Context + // returns a dList of blocks that the entity blew up. + // returns the dEntity that exploded. + // returns the dLocation the entity blew up at. + // returns an Element(Decimal) of the strength of the explosion. + // + // @Determine + // dList(dLocation) to set a new lists of blocks that are to be affected by the explosion. + // Element(Decimal) to change the strength of the explosion. + // + // --> + + public EntityExplodesScriptEvent() { + instance = this; + } + public static EntityExplodesScriptEvent instance; + public dEntity entity; + public dList blocks; + public dLocation location; + public Float strength; + private Boolean blockSet; + public EntityExplodeEvent event; + + @Override + public boolean couldMatch(ScriptContainer scriptContainer, String s) { + String lower = CoreUtilities.toLowerCase(s); + String cmd = CoreUtilities.getXthArg(1, lower); + String entOne = CoreUtilities.getXthArg(0, lower); + List types = Arrays.asList("entity", "player", "npc"); + return (types.contains(entOne) || dEntity.matches(entOne)) + && cmd.equals("explodes"); + } + + @Override + public boolean matches(ScriptContainer scriptContainer, String s) { + String target = CoreUtilities.getXthArg(0,CoreUtilities.toLowerCase(s)); + List types = Arrays.asList("entity", "player", "npc"); + return (types.contains(target) || entity.matchesEntity(target)); + } + + @Override + public String getName() { + return "EntityExplodes"; + } + + @Override + public void init() { + Bukkit.getServer().getPluginManager().registerEvents(this, DenizenAPI.getCurrentInstance()); + } + + @Override + public void destroy() { + EntityExplodeEvent.getHandlerList().unregister(this); + } + + @Override + public boolean applyDetermination(ScriptContainer container, String determination) { + if (aH.Argument.valueOf(determination) + .matchesPrimitive(aH.PrimitiveType.Float)) { + strength = aH.getFloatFrom(determination); + return true; + } + if (dList.matches(determination)) { + blocks = new dList(); + blockSet = true; + for (String loc: dList.valueOf(determination)) { + dLocation location = dLocation.valueOf(loc); + if (location == null) { + dB.echoError("Invalid location '" + loc + "' check [" + getName() + "]: ' for " + container.getName()); + } + else { + blocks.add(location.identifySimple()); + } + } + return true; + } + return super.applyDetermination(container, determination); + } + + @Override + public ScriptEntryData getScriptEntryData() { + return new BukkitScriptEntryData(entity.isPlayer() ? dEntity.getPlayerFrom(event.getEntity()): null, + entity.isCitizensNPC() ? dEntity.getNPCFrom(event.getEntity()): null); + } + + @Override + public HashMap getContext() { + HashMap context = super.getContext(); + context.put("entity", entity); + context.put("location", location); + context.put("blocks", blocks); + context.put("strength", new Element(strength)); + return context; + } + + @EventHandler + public void onEntityExplodes(EntityExplodeEvent event) { + entity = new dEntity(event.getEntity()); + location = new dLocation(event.getLocation()); + strength = event.getYield(); + blocks = new dList(); + blockSet = false; + for (Block block : event.blockList()) { + blocks.add(new dLocation(block.getLocation()).identifySimple()); + } + cancelled = event.isCancelled(); + this.event = event; + fire(); + event.setCancelled(cancelled); + if (blockSet) { + event.blockList().clear(); + if (blocks.size() > 0) { + event.blockList().clear(); + for (String loc : blocks) { + dLocation location = dLocation.valueOf(loc); + event.blockList().add(location.getWorld().getBlockAt(location)); + } + } + } + event.setYield(strength); + } +} diff --git a/src/main/java/net/aufdemrand/denizen/scripts/containers/core/BukkitWorldScriptHelper.java b/src/main/java/net/aufdemrand/denizen/scripts/containers/core/BukkitWorldScriptHelper.java index d3e6e52cc3..21550d49f5 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/containers/core/BukkitWorldScriptHelper.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/containers/core/BukkitWorldScriptHelper.java @@ -27,7 +27,6 @@ import org.bukkit.event.block.*; import org.bukkit.event.enchantment.*; import org.bukkit.event.player.*; -import org.bukkit.event.hanging.*; import org.bukkit.event.inventory.*; import org.bukkit.event.vehicle.*; import org.bukkit.event.weather.*; @@ -136,73 +135,6 @@ public void timeEvent() { } } - ///////////////////// - // ENTITY EVENTS - ///////////////// - - // <--[event] - // @Events - // entity explodes - // explodes - // - // @Triggers when an entity explodes. - // @Context - // returns a dList of blocks that the entity blew up. - // returns the dEntity that exploded. - // returns the dLocation the entity blew up at. - // returns an Element(Decimal) of the strength of the explosion. - // - // @Determine - // "CANCELLED" to stop the entity from exploding. - // dList(dLocation) to set a new lists of blocks that are to be affected by the explosion. - // Element(Decimal) to change the strength of the explosion. - // - // --> - @EventHandler - public void entityExplode(EntityExplodeEvent event) { - - Map context = new HashMap(); - if (event.getEntity() == null) { - return; // Fix for other plugins doing weird stuff. - } - dEntity entity = new dEntity(event.getEntity()); - - context.put("entity", entity.getDenizenObject()); - context.put("location", new dLocation(event.getLocation())); - context.put("strength", new Element(event.getYield())); - - String blocks = ""; - for (Block block : event.blockList()) { - blocks = blocks + new dLocation(block.getLocation()) + "|"; - } - context.put("blocks", new dList(blocks)); - - String determination = doEvents(Arrays.asList - ("entity explodes", - entity.identifyType() + " explodes", - entity.identifySimple() + " explodes"), - null, null, context, true); - - if (determination.toUpperCase().startsWith("CANCELLED")) - event.setCancelled(true); - - else if (aH.matchesDouble(determination)) { - event.setYield(new Element(determination).asFloat()); - } - - else if (determination.length() > 0 && !determination.equalsIgnoreCase("none")) { - dList list = dList.valueOf(determination); - event.blockList().clear(); - for (String loc: list) { - dLocation location = dLocation.valueOf(loc); - if (location == null) - dB.echoError("Invalid location '" + loc + "'"); - else - event.blockList().add(location.getWorld().getBlockAt(location)); - } - } - } - // <--[event] // @Events // entity heals (because )