New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Paper-only event player tracks|untracks <entity>
#2444
Changes from 2 commits
5cfab6a
5e3db02
751c5cc
7bc0432
922df5e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
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 io.papermc.paper.event.player.PlayerTrackEntityEvent; | ||
import io.papermc.paper.event.player.PlayerUntrackEntityEvent; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.Listener; | ||
|
||
public class PlayerTracksEntityScriptEvent extends BukkitScriptEvent implements Listener { | ||
|
||
// <--[event] | ||
// @Events | ||
// player tracks|untracks <entity> | ||
// | ||
// @Location true | ||
// | ||
// @Group Paper | ||
// | ||
// @Plugin Paper | ||
// | ||
// @Warning This event may fire very rapidly. | ||
// | ||
// @Triggers when a player starts or stop tracking an entity. An entity is tracked when it is visible to the client. An entity will be untracked when it is no longer visible, e.g. it despawns, dies, or is unloaded. | ||
// | ||
// @Context | ||
// <context.entity> returns an EntityTag of the entity being tracked. | ||
// <context.location> returns a LocationTag of the entity. | ||
// | ||
// @Player Always. | ||
// | ||
// @Example | ||
// # Narrate when the player tracks all entities except for item frames. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
// on player tracks !item_frame: | ||
// - narrate "You are now tracking <context.entity.name> at <context.location.simple>" | ||
// | ||
// @Example | ||
// on player untracks chicken: | ||
// - narrate "CHICKEN: No! Come back! :(" | ||
// --> | ||
|
||
public PlayerTracksEntityScriptEvent() { | ||
registerCouldMatcher("player tracks|untracks <entity>"); | ||
} | ||
|
||
public String type; | ||
public Player player; | ||
public EntityTag entity; | ||
public LocationTag location; | ||
|
||
@Override | ||
public boolean matches(ScriptPath path) { | ||
if (!runInCheck(path, location)) { | ||
return false; | ||
} | ||
if (!path.eventArgLowerAt(1).equals(type)) { | ||
return false; | ||
} | ||
if (!path.tryArgObject(2, entity)) { | ||
return false; | ||
} | ||
return super.matches(path); | ||
} | ||
|
||
@Override | ||
public ScriptEntryData getScriptEntryData() { | ||
return new BukkitScriptEntryData(player); | ||
} | ||
|
||
@Override | ||
public ObjectTag getContext(String name) { | ||
return switch (name) { | ||
case "entity" -> entity; | ||
case "location" -> location; | ||
default -> super.getContext(name); | ||
}; | ||
} | ||
|
||
@EventHandler | ||
public void playerTracksEntityEvent(PlayerTrackEntityEvent event) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should follow the |
||
entity = new EntityTag(event.getEntity()); | ||
location = new LocationTag(event.getEntity().getLocation()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this, the location can be pulled from the entity via a tag if needed |
||
player = event.getPlayer(); | ||
type = "tracks"; | ||
fire(event); | ||
} | ||
|
||
@EventHandler | ||
public void playerUntracksEntityEvent(PlayerUntrackEntityEvent event) { | ||
entity = new EntityTag(event.getEntity()); | ||
tal5 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
location = new LocationTag(event.getEntity().getLocation()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same with previous comment, this can be removed |
||
player = event.getPlayer(); | ||
type = "untracks"; | ||
fire(event); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These events were added in 1.19, need a version check here