Skip to content

Commit

Permalink
Rewrote "on entity spawns" event in new ScriptEvent format.
Browse files Browse the repository at this point in the history
  • Loading branch information
Talamar1 committed Jun 13, 2015
1 parent ad39540 commit c257e54
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 159 deletions.
2 changes: 1 addition & 1 deletion src/main/java/net/aufdemrand/denizen/Denizen.java
Expand Up @@ -569,7 +569,6 @@ public void onEnable() {
// Register all the 'Core' SmartEvents.
OldEventManager.registerSmartEvent(new CommandSmartEvent());
OldEventManager.registerSmartEvent(new CuboidEnterExitSmartEvent());
OldEventManager.registerSmartEvent(new EntitySpawnSmartEvent());
OldEventManager.registerSmartEvent(new FlagSmartEvent());
OldEventManager.registerSmartEvent(new NPCNavigationSmartEvent());
OldEventManager.registerSmartEvent(new PlayerEquipsArmorSmartEvent());
Expand All @@ -590,6 +589,7 @@ public void onEnable() {
ScriptEvent.registerScriptEvent(new EntityDespawnScriptEvent());
ScriptEvent.registerScriptEvent(new EntityInteractScriptEvent());
ScriptEvent.registerScriptEvent(new EntityKilledScriptEvent());
ScriptEvent.registerScriptEvent(new EntitySpawnScriptEvent());
ScriptEvent.registerScriptEvent(new EntityTeleportScriptEvent());
ScriptEvent.registerScriptEvent(new ItemMoveScriptEvent());
ScriptEvent.registerScriptEvent(new ItemScrollScriptEvent());
Expand Down

This file was deleted.

@@ -0,0 +1,142 @@
package net.aufdemrand.denizen.events.scriptevents;

import net.aufdemrand.denizen.objects.dCuboid;
import net.aufdemrand.denizen.objects.dEllipsoid;
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.Element;
import net.aufdemrand.denizencore.objects.dList;
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.CreatureSpawnEvent;

import java.util.HashMap;

public class EntitySpawnScriptEvent extends ScriptEvent implements Listener {

// <--[event]
// @Events
// entity spawns
// entity spawns (in <notable cuboid>) (because <cause>)
// <entity> spawns
// <entity> spawns (in <notable cuboid>) (because <cause>)
//
// @Cancellable true
//
// @Warning This event may fire very rapidly.
//
// @Triggers when an entity spawns.
//
// @Context
// <context.entity> returns the dEntity that spawned.
// <context.location> returns the location the entity will spawn at.
// <context.cuboids> returns a list of cuboids that the entity spawned inside.
// <context.reason> returns the reason the entity spawned.
// Reasons: <@link url https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/event/entity/CreatureSpawnEvent.SpawnReason.html>
//
// -->

public EntitySpawnScriptEvent() {
instance = this;
}
public static EntitySpawnScriptEvent instance;
public dEntity entity;
public dLocation location;
public dList cuboids;
public Element reason;
public CreatureSpawnEvent event;

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

@Override
public boolean matches(ScriptContainer scriptContainer, String s) {
String lower = CoreUtilities.toLowerCase(s);

if (!entity.matchesEntity(CoreUtilities.getXthArg(0, lower))) {
return false;
}

if (CoreUtilities.xthArgEquals(2, lower, "in")) {
String it = CoreUtilities.getXthArg(3, lower);
if (dCuboid.matches(it)) {
dCuboid cuboid = dCuboid.valueOf(it);
if (!cuboid.isInsideCuboid(location)) {
return false;
}
}
else if (dEllipsoid.matches(it)) {
dEllipsoid ellipsoid = dEllipsoid.valueOf(it);
if (!ellipsoid.contains(location)) {
return false;
}
}
else {
dB.echoError("Invalid event 'IN ...' check [BlockPhysics]: '" + s + "' for " + scriptContainer.getName());
return false;
}
}

if (CoreUtilities.xthArgEquals(4, lower, "because")) {
return CoreUtilities.getXthArg(5, lower).equals(reason.toString());
}
return true;
}

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

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

@Override
public void destroy() {
CreatureSpawnEvent.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();
context.put("entity", entity);
context.put("location", location);
context.put("cuboids", cuboids);
context.put("reason", reason);
return context;
}

@EventHandler
public void onEntityInteract(CreatureSpawnEvent event) {
entity = new dEntity(event.getEntity());
location = new dLocation(event.getLocation());
cuboids = new dList();
for (dCuboid cuboid: dCuboid.getNotableCuboidsContaining(location)) {
cuboids.add(cuboid.identifySimple());
}
reason = new Element(event.getSpawnReason().name());
cancelled = event.isCancelled();
this.event = event;
fire();
event.setCancelled(cancelled);
}

}

0 comments on commit c257e54

Please sign in to comment.