Skip to content

Commit

Permalink
Merge pull request #1885 from mergu/spawner_event
Browse files Browse the repository at this point in the history
Add spawner spawn event, fixes #1813
  • Loading branch information
mcmonkey4eva committed Jan 18, 2019
2 parents 89b264a + 516903c commit afa2783
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 3 deletions.
1 change: 1 addition & 0 deletions plugin/src/main/java/net/aufdemrand/denizen/Denizen.java
Expand Up @@ -624,6 +624,7 @@ public void onEnable() {
ScriptEvent.registerScriptEvent(new EntityResurrectScriptEvent());
}
ScriptEvent.registerScriptEvent(new EntityShootsBowEvent());
ScriptEvent.registerScriptEvent(new EntitySpawnerSpawnScriptEvent());
ScriptEvent.registerScriptEvent(new EntitySpawnScriptEvent());
ScriptEvent.registerScriptEvent(new EntityTamesScriptEvent());
ScriptEvent.registerScriptEvent(new EntityTargetsScriptEvent());
Expand Down
Expand Up @@ -58,7 +58,7 @@ public EntitySpawnScriptEvent() {
@Override
public boolean couldMatch(ScriptContainer scriptContainer, String s) {
String lower = CoreUtilities.toLowerCase(s);
return CoreUtilities.xthArgEquals(1, lower, "spawns") && !lower.startsWith("item");
return CoreUtilities.xthArgEquals(1, lower, "spawns") && !lower.startsWith("item") && !lower.startsWith("spawner");
}

@Override
Expand Down
@@ -0,0 +1,117 @@
package net.aufdemrand.denizen.events.entity;

import net.aufdemrand.denizen.BukkitScriptEntryData;
import net.aufdemrand.denizen.events.BukkitScriptEvent;
import net.aufdemrand.denizen.objects.dEntity;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.utilities.DenizenAPI;
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.entity.Entity;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.SpawnerSpawnEvent;

public class EntitySpawnerSpawnScriptEvent extends BukkitScriptEvent implements Listener {

// <--[event]
// @Events
// spawner spawns entity (in <area>)
// spawner spawns <entity> (in <area>)
//
// @Regex ^on spawner spawns [^\s]+( in ((notable (cuboid|ellipsoid))|([^\s]+)))?$
//
// @Cancellable true
//
// @Triggers when an entity spawns from a monster spawner.
//
// @Context
// <context.entity> returns the dEntity that spawned.
// <context.location> returns the dLocation the entity will spawn at.
// <context.spawner_location> returns the dLocation of the monster spawner.
//
// -->

public EntitySpawnerSpawnScriptEvent() {
instance = this;
}

public static EntitySpawnerSpawnScriptEvent instance;
private dEntity entity;
private dLocation location;
private dLocation spawnerLocation;
public SpawnerSpawnEvent event;

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

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

if (!tryEntity(entity, CoreUtilities.getXthArg(2, lower))) {
return false;
}

return runInCheck(scriptContainer, s, lower, location);
}

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

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

@Override
public void destroy() {
SpawnerSpawnEvent.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 dObject getContext(String name) {
switch (name) {
case "entity":
return entity;
case "location":
return location;
case "spawner_location":
return spawnerLocation;
default:
return super.getContext(name);
}
}

@EventHandler
public void onSpawnerSpawn(SpawnerSpawnEvent event) {
Entity entity = event.getEntity();
this.entity = new dEntity(entity);
location = new dLocation(event.getLocation());
spawnerLocation = new dLocation(event.getSpawner().getLocation());
cancelled = event.isCancelled();
this.event = event;
dEntity.rememberEntity(entity);
fire();
dEntity.forgetEntity(entity);
event.setCancelled(cancelled);
}
}
Expand Up @@ -50,8 +50,11 @@ public ItemSpawnsScriptEvent() {
@Override
public boolean couldMatch(ScriptContainer scriptContainer, String s) {
String lower = CoreUtilities.toLowerCase(s);
String cmd = CoreUtilities.getXthArg(1, lower);
return cmd.equals("spawns");
String arg = CoreUtilities.getXthArg(2, lower);
if (arg.length() > 0 && !arg.equals("in")) {
return false;
}
return CoreUtilities.xthArgEquals(1, lower, "spawns");
}

@Override
Expand Down

0 comments on commit afa2783

Please sign in to comment.