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
Merged
mcmonkey4eva
merged 5 commits into
DenizenScript:dev
from
BreadcrumbIsTaken:player_tracks_stops_tracking_entity
Mar 20, 2023
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5cfab6a
Add paper-only event `player tracks|untracks <entity>`
BreadcrumbIsTaken 5e3db02
Explain when an entity is tracked or untracked.
BreadcrumbIsTaken 751c5cc
Meta + code improvements
BreadcrumbIsTaken 7bc0432
Remove `<context.location>` and change order of `runInCheck`
BreadcrumbIsTaken 922df5e
Change event handler names
BreadcrumbIsTaken File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
97 changes: 97 additions & 0 deletions
97
...r/src/main/java/com/denizenscript/denizen/paper/events/PlayerTracksEntityScriptEvent.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,97 @@ | ||
package com.denizenscript.denizen.paper.events; | ||
|
||
import com.denizenscript.denizen.events.BukkitScriptEvent; | ||
import com.denizenscript.denizen.objects.EntityTag; | ||
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 stops tracking an entity. An entity is tracked/untracked by a player's client when the player moves in/out of its <@link mechanism EntityTag.tracking_range>. | ||
// | ||
// @Context | ||
// <context.entity> returns an EntityTag of the entity being tracked or untracked. | ||
// | ||
// @Player Always. | ||
// | ||
// @Example | ||
// # Narrate when the player tracks all entities except for item frames. | ||
// on player tracks !item_frame: | ||
// - narrate "You are now tracking <context.entity.name> at <context.entity.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; | ||
|
||
@Override | ||
public boolean matches(ScriptPath path) { | ||
if (!path.eventArgLowerAt(1).equals(type)) { | ||
return false; | ||
} | ||
if (!path.tryArgObject(2, entity)) { | ||
return false; | ||
} | ||
if (!runInCheck(path, entity.getLocation())) { | ||
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; | ||
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()); | ||
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
|
||
player = event.getPlayer(); | ||
type = "untracks"; | ||
EntityTag.rememberEntity(event.getEntity()); | ||
fire(event); | ||
EntityTag.forgetEntity(event.getEntity()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
any
notall