Skip to content

Commit

Permalink
NPC matcher-switch (#2544)
Browse files Browse the repository at this point in the history
* Add `npc` and `cause` switches to `npc spawns` event

* Change switch name from `cause` to `reason`

* Add support for other events
  • Loading branch information
BreadcrumbIsTaken committed Oct 6, 2023
1 parent 9744310 commit 0bc3b4d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 28 deletions.
Expand Up @@ -24,6 +24,8 @@ public class NPCNavigationScriptEvent extends BukkitScriptEvent implements Liste
//
// @Triggers when an NPC begins, finishes, or cancels navigating.
//
// @Switch npc:<npc> to only process the event if the spawned NPC matches.
//
// @Context
// None
//
Expand All @@ -33,13 +35,17 @@ public class NPCNavigationScriptEvent extends BukkitScriptEvent implements Liste

public NPCNavigationScriptEvent() {
registerCouldMatcher("npc begins|completes|cancels navigation");
registerSwitches("npc");
}

public NPCTag npc;
public String type;

@Override
public boolean matches(ScriptPath path) {
if (!path.tryObjectSwitch("npc", npc)) {
return false;
}
if (!runInCheck(path, npc.getLocation())) {
return false;
}
Expand Down
Expand Up @@ -26,6 +26,8 @@ public class NPCOpensScriptEvent extends BukkitScriptEvent implements Listener {
//
// @Triggers when an NPC opens a door or gate.
//
// @Switch npc:<npc> to only process the event if the spawned NPC matches.
//
// @Context
// <context.location> returns the location of the door or gate opened.
//
Expand All @@ -35,13 +37,17 @@ public class NPCOpensScriptEvent extends BukkitScriptEvent implements Listener {

public NPCOpensScriptEvent() {
registerCouldMatcher("npc opens <block>");
registerSwitches("npc");
}

public NPCTag npc;
public LocationTag location;

@Override
public boolean matches(ScriptPath path) {
if (!path.tryObjectSwitch("npc", npc)) {
return false;
}
if (!runInCheck(path, location)) {
return false;
}
Expand All @@ -58,10 +64,10 @@ public ScriptEntryData getScriptEntryData() {

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

@EventHandler
Expand Down
Expand Up @@ -5,6 +5,7 @@
import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData;
import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.scripts.ScriptEntryData;
import net.citizensnpcs.api.event.NPCSpawnEvent;
import org.bukkit.event.EventHandler;
Expand All @@ -24,15 +25,20 @@ public class NPCSpawnScriptEvent extends BukkitScriptEvent implements Listener {
//
// @Triggers when an NPC spawns.
//
// @Switch npc:<npc> to only process the event if the spawned NPC matches.
// @Switch reason:<reason> to only process the event if the NPC's spawn reason matches. See <@link url https://jd.citizensnpcs.co/net/citizensnpcs/api/event/SpawnReason.html> for a list of reasons.
//
// @Context
// <context.location> returns the location the entity will spawn at.
// <context.reason> returns the reason of the spawn.
//
// @NPC Always.
//
// -->

public NPCSpawnScriptEvent() {
registerCouldMatcher("npc spawns");
registerSwitches("npc", "reason");
}

public NPCTag npc;
Expand All @@ -41,10 +47,15 @@ public NPCSpawnScriptEvent() {

@Override
public boolean matches(ScriptPath path) {
if (!path.tryObjectSwitch("npc", npc)) {
return false;
}
if (!path.tryObjectSwitch("reason", new ElementTag(event.getReason()))) {
return false;
}
if (!runInCheck(path, location)) {
return false;
}

return super.matches(path);
}

Expand All @@ -55,10 +66,11 @@ public ScriptEntryData getScriptEntryData() {

@Override
public ObjectTag getContext(String name) {
switch (name) {
case "location": return location;
}
return super.getContext(name);
return switch (name) {
case "location" -> location;
case "reason" -> new ElementTag(event.getReason());
default -> super.getContext(name);
};
}

@EventHandler
Expand Down
Expand Up @@ -24,6 +24,8 @@ public class NPCStuckScriptEvent extends BukkitScriptEvent implements Listener {
//
// @Triggers when an NPC's navigator is stuck.
//
// @Switch npc:<npc> to only process the event if the spawned NPC matches.
//
// @Context
// <context.action> returns 'teleport' or 'none'
//
Expand All @@ -37,17 +39,23 @@ public class NPCStuckScriptEvent extends BukkitScriptEvent implements Listener {

public NPCStuckScriptEvent() {
registerCouldMatcher("npc stuck");
registerSwitches("npc");
this.<NPCStuckScriptEvent, ElementTag>registerDetermination(null, ElementTag.class, (evt, context, action) -> {
evt.event.setAction(action.asLowerString().equals("none") ? null : TeleportStuckAction.INSTANCE);
});
}

public NavigationStuckEvent event;
public NPCTag npc;

@Override
public boolean matches(ScriptPath path) {
if (!path.tryObjectSwitch("npc", npc)) {
return false;
}
if (!runInCheck(path, npc.getLocation())) {
return false;
}

return super.matches(path);
}

Expand All @@ -56,26 +64,12 @@ public ScriptEntryData getScriptEntryData() {
return new BukkitScriptEntryData(null, npc);
}

@Override
public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) {
String lowVal = CoreUtilities.toLowerCase(determinationObj.toString());
if (lowVal.equals("none")) {
event.setAction(null);
return true;
}
else if (lowVal.equals("teleport")) {
event.setAction(TeleportStuckAction.INSTANCE);
return true;
}
return super.applyDetermination(path, determinationObj);
}

@Override
public ObjectTag getContext(String name) {
switch (name) {
case "action": return new ElementTag(event.getAction() == TeleportStuckAction.INSTANCE ? "teleport" : "none");
}
return super.getContext(name);
return switch (name) {
case "action" -> new ElementTag(event.getAction() == TeleportStuckAction.INSTANCE ? "teleport" : "none");
default -> super.getContext(name);
};
}

@EventHandler
Expand Down

0 comments on commit 0bc3b4d

Please sign in to comment.