Skip to content

Commit

Permalink
Rewrote "on creeper powered" 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 79e7db4 commit 4b803f1
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 37 deletions.
1 change: 1 addition & 0 deletions src/main/java/net/aufdemrand/denizen/Denizen.java
Expand Up @@ -592,6 +592,7 @@ public void onEnable() {
ScriptEvent.registerScriptEvent(new ChatScriptEvent());
ScriptEvent.registerScriptEvent(new ChunkLoadScriptEvent());
ScriptEvent.registerScriptEvent(new ChunkUnloadScriptEvent());
ScriptEvent.registerScriptEvent(new CreeperPoweredScriptEvent());
ScriptEvent.registerScriptEvent(new EntityBreaksHangingScriptEvent());
ScriptEvent.registerScriptEvent(new EntityCombustsScriptEvent());
ScriptEvent.registerScriptEvent(new EntityDamagedScriptEvent());
Expand Down
@@ -0,0 +1,97 @@
package net.aufdemrand.denizen.events.scriptevents;

import net.aufdemrand.denizen.objects.dEntity;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizencore.events.ScriptEvent;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.objects.dObject;
import net.aufdemrand.denizencore.scripts.containers.ScriptContainer;
import net.aufdemrand.denizencore.utilities.CoreUtilities;

import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.CreeperPowerEvent;

import java.util.HashMap;

public class CreeperPoweredScriptEvent extends ScriptEvent implements Listener {

// <--[event]
// @Events
// creeper powered (because <cause>)
//
// @Cancellable true
//
// @Triggers when a creeper is struck by lightning and turned into a powered creeper.
//
// @Context
// <context.entity> returns the dEntity of the creeper.
// <context.lightning> returns the dEntity of the lightning.
// <context.cause> returns an Element of the cause for the creeper being powered.
//
// -->

public CreeperPoweredScriptEvent() {
instance = this;
}
public static CreeperPoweredScriptEvent instance;
public dEntity lightning;
public dEntity entity;
public Element cause;
public CreeperPowerEvent event;

@Override
public boolean couldMatch(ScriptContainer scriptContainer, String s) {
String lower = CoreUtilities.toLowerCase(s);
return lower.startsWith("creeper powered");
}

@Override
public boolean matches(ScriptContainer scriptContainer, String s) {
String reason = CoreUtilities.getXthArg(3,CoreUtilities.toLowerCase(s));
return reason.length() == 0 || reason.equals(cause.toString());
}

@Override
public String getName() {
return "CreeperPowered";
}

@Override
public void init() {
Bukkit.getServer().getPluginManager().registerEvents(this, DenizenAPI.getCurrentInstance());
}

@Override
public void destroy() {
CreeperPowerEvent.getHandlerList().unregister(this);
}

@Override
public boolean applyDetermination(ScriptContainer container, String determination) {
return super.applyDetermination(container, determination);
}

@Override
public HashMap<String, dObject> getContext() {
HashMap<String, dObject> context = super.getContext();
if (lightning != null) {
context.put("lightning", lightning);
}
context.put("entity", entity);
context.put("cause", cause);
return context;
}

@EventHandler
public void onCreeperPowered(CreeperPowerEvent event) {
lightning = new dEntity(event.getLightning());
entity = new dEntity(event.getEntity());
cause = new Element(event.getCause().name());
cancelled = event.isCancelled();
this.event = event;
fire();
event.setCancelled(cancelled);
}
}
Expand Up @@ -140,43 +140,6 @@ public void timeEvent() {
// ENTITY EVENTS
/////////////////


// <--[event]
// @Events
// creeper powered (because <cause>)
//
// @Triggers when a creeper is struck by lightning and turned into a powered creeper.
// @Context
// <context.entity> returns the dEntity of the creeper.
// <context.lightning> returns the dEntity of the lightning.
// <context.cause> returns an Element of the cause for the creeper being powered.
//
// @Determine
// "CANCELLED" to stop the creeper from being powered.
//
// -->
@EventHandler
public void creeperPower(CreeperPowerEvent event) {

Map<String, dObject> context = new HashMap<String, dObject>();
dEntity entity = new dEntity(event.getEntity());
dEntity lightning = event.getLightning() == null ? null: new dEntity(event.getLightning());
String cause = event.getCause().name();

context.put("entity", entity);
if (lightning != null)
context.put("lightning", lightning);
context.put("cause", new Element(cause));

String determination = doEvents(Arrays.asList
("creeper powered",
"creeper powered because " + cause),
null, null, context);

if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
}

// <--[event]
// @Events
// entity combusts
Expand Down

0 comments on commit 4b803f1

Please sign in to comment.