Skip to content
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
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -64,6 +64,7 @@ public static void init() {
ScriptEvent.registerScriptEvent(PlayerSelectsStonecutterRecipeScriptEvent.class);
ScriptEvent.registerScriptEvent(PlayerSpectatesEntityScriptEvent.class);
ScriptEvent.registerScriptEvent(PlayerStopsSpectatingScriptEvent.class);
ScriptEvent.registerScriptEvent(PlayerTracksEntityScriptEvent.class);
Copy link
Member

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

ScriptEvent.registerScriptEvent(PlayerTradesWithMerchantScriptEvent.class);
ScriptEvent.registerScriptEvent(PreEntitySpawnScriptEvent.class);
ScriptEvent.registerScriptEvent(ProjectileCollideScriptEvent.class);
Expand Down
@@ -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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any not all

// 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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should follow the onX naming convention, I.e. onPlayerTracksEntity (same for the other listener)

entity = new EntityTag(event.getEntity());
location = new LocationTag(event.getEntity().getLocation());
Copy link
Contributor

Choose a reason for hiding this comment

The 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());
Copy link
Contributor

Choose a reason for hiding this comment

The 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);
}
}