Skip to content

Commit

Permalink
Rewrote "on entity exits portal" 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 17, 2015
1 parent 2121568 commit 62ac82c
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 32 deletions.
1 change: 1 addition & 0 deletions src/main/java/net/aufdemrand/denizen/Denizen.java
Expand Up @@ -600,6 +600,7 @@ public void onEnable() {
ScriptEvent.registerScriptEvent(new EntityDeathScriptEvent());
ScriptEvent.registerScriptEvent(new EntityDespawnScriptEvent());
ScriptEvent.registerScriptEvent(new EntityEntersPortalScriptEvent());
ScriptEvent.registerScriptEvent(new EntityExitsPortalScriptEvent());
ScriptEvent.registerScriptEvent(new EntityExplodesScriptEvent());
ScriptEvent.registerScriptEvent(new EntityFormsBlock());
ScriptEvent.registerScriptEvent(new EntityHealsScriptEvent());
Expand Down
@@ -0,0 +1,104 @@
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.denizencore.events.ScriptEvent;
import net.aufdemrand.denizencore.objects.dObject;
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.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityPortalExitEvent;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

public class EntityExitsPortalScriptEvent extends ScriptEvent implements Listener {

// <--[event]
// @Events
// entity exits portal
// <entity> exits portal
//
// @Cancellable false
//
// @Triggers when an entity enters a portal.
//
// @Context
// <context.entity> returns the dEntity.
// <context.location> returns the dLocation of the portal block touched by the entity.
//
// -->

public EntityExitsPortalScriptEvent() {
instance = this;
}
public static EntityExitsPortalScriptEvent instance;
public dEntity entity;
public dLocation location;
public EntityPortalExitEvent event;

@Override
public boolean couldMatch(ScriptContainer scriptContainer, String s) {
String lower = CoreUtilities.toLowerCase(s);
String entOne = CoreUtilities.getXthArg(0, lower);
List<String> types = Arrays.asList("entity", "player", "npc");
return (types.contains(entOne) || dEntity.matches(entOne))
&& lower.contains("exits portal");
}

@Override
public boolean matches(ScriptContainer scriptContainer, String s) {
String lower = CoreUtilities.toLowerCase(s);
String target = CoreUtilities.getXthArg(0,lower);
List<String> types = Arrays.asList("entity", "player", "npc");
return types.contains(target) || entity.matchesEntity(target);
}

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

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

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

@Override
public boolean applyDetermination(ScriptContainer container, String determination) {
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);
return context;
}

@EventHandler
public void onEntityExitsPortal(EntityPortalExitEvent event) {
entity = new dEntity(event.getEntity());
location = new dLocation(event.getTo());
this.event = event;
fire();
}
}
Expand Up @@ -139,38 +139,6 @@ public void timeEvent() {
// Additional EVENTS
/////////////////

// <--[event]
// @Events
// entity exits portal
// <entity> exits portal
//
// @Triggers when an entity exits a portal.
// @Context
// <context.entity> returns the dEntity.
// <context.location> returns the dLocation of the portal block touched by the entity.
//
// -->
@EventHandler
public void entityPortalExit(EntityPortalExitEvent event) {

dPlayer player = null;
dNPC npc = null;

Map<String, dObject> context = new HashMap<String, dObject>();
dEntity entity = new dEntity(event.getEntity());

context.put("location", new dLocation(event.getTo()));
context.put("entity", entity.getDenizenObject());

if (entity.isCitizensNPC()) npc = entity.getDenizenNPC();
else if (entity.isPlayer()) player = entity.getDenizenPlayer();

doEvents(Arrays.asList
("entity exits portal",
entity.identifyType() + " exits portal"),
npc, player, context, true);
}

// <--[event]
// @Events
// player uses portal
Expand Down

0 comments on commit 62ac82c

Please sign in to comment.