Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrote "on entity explodes" event in new ScriptEvent format.
Removed from BukkitWorldScriptHelper.
- Loading branch information
Showing
3 changed files
with
156 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
src/main/java/net/aufdemrand/denizen/events/scriptevents/EntityExplodesScriptEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters