-
-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add paper-only entity pathfind event
- Loading branch information
Showing
2 changed files
with
117 additions
and
0 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
116 changes: 116 additions & 0 deletions
116
paper/src/main/java/com/denizenscript/denizen/paper/events/EntityPathfindScriptEvent.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,116 @@ | ||
package com.denizenscript.denizen.paper.events; | ||
|
||
import com.denizenscript.denizen.events.BukkitScriptEvent; | ||
import com.denizenscript.denizen.objects.EntityTag; | ||
import com.denizenscript.denizen.objects.LocationTag; | ||
import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData; | ||
import com.denizenscript.denizencore.objects.ObjectTag; | ||
import com.denizenscript.denizencore.scripts.ScriptEntryData; | ||
import com.destroystokyo.paper.event.entity.EntityPathfindEvent; | ||
import org.bukkit.entity.Entity; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.Listener; | ||
|
||
public class EntityPathfindScriptEvent extends BukkitScriptEvent implements Listener { | ||
|
||
// <--[event] | ||
// @Events | ||
// entity pathfinds | ||
// <entity> pathfinds | ||
// | ||
// @Regex ^on [^\s]+ pathfinds$ | ||
// | ||
// @Switch in:<area> to only process the event if it occurred within a specified area. | ||
// @Switch to:<area> to only process the event if the entity is pathfinding into a specified area. | ||
// @Switch at:<entity> to only process the event when the entity is pathfinding at a specified entity. | ||
// | ||
// @Plugin Paper | ||
// | ||
// @Cancellable true | ||
// | ||
// @Triggers when an entity starts pathfinding towards a location or entity. | ||
// | ||
// @Context | ||
// <context.entity> returns the EntityTag that is pathfinding. | ||
// <context.location> returns the LocationTag that is being pathfound to. | ||
// <context.target> returns the EntityTag that is being targeted, if any. | ||
// | ||
// @Player when the target entity is a player. | ||
// | ||
// @NPC when the target entity is an NPC. | ||
// | ||
// --> | ||
|
||
public EntityPathfindScriptEvent() { | ||
instance = this; | ||
} | ||
|
||
public static EntityPathfindScriptEvent instance; | ||
|
||
public EntityTag entity; | ||
public EntityTag target; | ||
public EntityPathfindEvent event; | ||
|
||
@Override | ||
public boolean couldMatch(ScriptPath path) { | ||
return path.eventArgLowerAt(1).equals("pathfinds"); | ||
} | ||
|
||
@Override | ||
public boolean matches(ScriptPath path) { | ||
if (!tryEntity(entity, path.eventArgLowerAt(0))) { | ||
return false; | ||
} | ||
if (!runInCheck(path, entity.getLocation())) { | ||
return false; | ||
} | ||
if (!runInCheck(path, event.getLoc(), "to")) { | ||
return false; | ||
} | ||
String at = path.switches.get("at"); | ||
if (at != null && !tryEntity(target, at)) { | ||
return false; | ||
} | ||
return super.matches(path); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "EntityPathfinds"; | ||
} | ||
|
||
@Override | ||
public ScriptEntryData getScriptEntryData() { | ||
return new BukkitScriptEntryData( | ||
target != null && target.isPlayer() ? target.getDenizenPlayer() : null, | ||
target != null && target.isCitizensNPC() ? target.getDenizenNPC() : null); | ||
} | ||
|
||
@Override | ||
public ObjectTag getContext(String name) { | ||
if (name.equals("entity")) { | ||
return entity.getDenizenObject(); | ||
} | ||
else if (name.equals("target") && target != null) { | ||
return target.getDenizenObject(); | ||
} | ||
else if (name.equals("location")) { | ||
return new LocationTag(event.getLoc()); | ||
} | ||
return super.getContext(name); | ||
} | ||
|
||
@EventHandler | ||
public void onEntityPathfind(EntityPathfindEvent event) { | ||
this.event = event; | ||
Entity entity = event.getEntity(); | ||
Entity target = event.getTargetEntity(); | ||
this.entity = new EntityTag(entity); | ||
this.target = target != null ? new EntityTag(target) : null; | ||
EntityTag.rememberEntity(entity); | ||
EntityTag.rememberEntity(target); | ||
fire(event); | ||
EntityTag.forgetEntity(entity); | ||
EntityTag.forgetEntity(target); | ||
} | ||
} |