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 spawns" event in new ScriptEvent format.
- Loading branch information
Showing
3 changed files
with
143 additions
and
159 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
158 changes: 0 additions & 158 deletions
158
src/main/java/net/aufdemrand/denizen/events/core/EntitySpawnSmartEvent.java
This file was deleted.
Oops, something went wrong.
142 changes: 142 additions & 0 deletions
142
src/main/java/net/aufdemrand/denizen/events/scriptevents/EntitySpawnScriptEvent.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,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); | ||
} | ||
|
||
} |