Skip to content

Commit

Permalink
Add spawner:<location> switch (#2551)
Browse files Browse the repository at this point in the history
* Add `spawner:<location>` switch

* `'s`
  • Loading branch information
BreadcrumbIsTaken committed Oct 22, 2023
1 parent 425dd75 commit 366353b
Showing 1 changed file with 12 additions and 11 deletions.
Expand Up @@ -25,6 +25,8 @@ public class EntitySpawnerSpawnScriptEvent extends BukkitScriptEvent implements
//
// @Triggers when an entity spawns from a monster spawner.
//
// @Switch spawner:<location> to only process the event if the spawner's location matches.
//
// @Context
// <context.entity> returns the EntityTag that spawned.
// <context.location> returns the LocationTag the entity will spawn at.
Expand All @@ -34,18 +36,21 @@ public class EntitySpawnerSpawnScriptEvent extends BukkitScriptEvent implements

public EntitySpawnerSpawnScriptEvent() {
registerCouldMatcher("spawner spawns <entity>");
registerSwitches("spawner");
}

private EntityTag entity;
private LocationTag location;
private LocationTag spawnerLocation;
public SpawnerSpawnEvent event;

@Override
public boolean matches(ScriptPath path) {
if (!path.tryArgObject(2, entity)) {
return false;
}
if (!path.tryObjectSwitch("spawner", spawnerLocation)) {
return false;
}
if (!runInCheck(path, location)) {
return false;
}
Expand All @@ -59,15 +64,12 @@ public ScriptEntryData getScriptEntryData() {

@Override
public ObjectTag getContext(String name) {
switch (name) {
case "entity":
return entity;
case "location":
return location;
case "spawner_location":
return spawnerLocation;
}
return super.getContext(name);
return switch (name) {
case "entity" -> entity;
case "location" -> location;
case "spawner_location" -> spawnerLocation;
default -> super.getContext(name);
};
}

@EventHandler
Expand All @@ -76,7 +78,6 @@ public void onSpawnerSpawn(SpawnerSpawnEvent event) {
this.entity = new EntityTag(entity);
location = new LocationTag(event.getLocation());
spawnerLocation = new LocationTag(event.getSpawner().getLocation());
this.event = event;
EntityTag.rememberEntity(entity);
fire(event);
EntityTag.forgetEntity(entity);
Expand Down

0 comments on commit 366353b

Please sign in to comment.