Skip to content

Commit

Permalink
Rewrote "on entity explodes" event in new ScriptEvent format.
Browse files Browse the repository at this point in the history
Removed from BukkitWorldScriptHelper.
  • Loading branch information
Talamar1 committed Jun 16, 2015
1 parent 1638f3e commit 211c464
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 68 deletions.
1 change: 1 addition & 0 deletions src/main/java/net/aufdemrand/denizen/Denizen.java
Expand Up @@ -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());
Expand Down
@@ -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
// <entity> explodes
//
// @Cancellable true
//
// @Triggers when an entity explodes.
//
// @Context
// <context.blocks> returns a dList of blocks that the entity blew up.
// <context.entity> returns the dEntity that exploded.
// <context.location> returns the dLocation the entity blew up at.
// <context.strength> 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<String> 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<String> 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<String, dObject> getContext() {
HashMap<String, dObject> 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);
}
}
Expand Up @@ -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.*;
Expand Down Expand Up @@ -136,73 +135,6 @@ public void timeEvent() {
}
}

/////////////////////
// ENTITY EVENTS
/////////////////

// <--[event]
// @Events
// entity explodes
// <entity> explodes
//
// @Triggers when an entity explodes.
// @Context
// <context.blocks> returns a dList of blocks that the entity blew up.
// <context.entity> returns the dEntity that exploded.
// <context.location> returns the dLocation the entity blew up at.
// <contect.strength> 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<String, dObject> context = new HashMap<String, dObject>();
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 <cause>)
Expand Down

0 comments on commit 211c464

Please sign in to comment.