Skip to content

Added PlayerNameEntityScriptEvent #2708

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

Open
wants to merge 12 commits into
base: dev
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public static void init() {
ScriptEvent.registerScriptEvent(PlayerJumpsScriptEventPaperImpl.class);
ScriptEvent.registerScriptEvent(PlayerLecternPageChangeScriptEvent.class);
ScriptEvent.registerScriptEvent(PlayerLoomPatternSelectScriptEvent.class);
ScriptEvent.registerScriptEvent(PlayerNameEntityScriptEvent.class);
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_20)) {
ScriptEvent.registerScriptEvent(PlayerOpenSignScriptEvent.class);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package com.denizenscript.denizen.paper.events;

import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.paper.PaperModule;
import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.scripts.ScriptEntryData;
import io.papermc.paper.event.player.PlayerNameEntityEvent;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;

public class PlayerNameEntityScriptEvent extends BukkitScriptEvent implements Listener {

// <--[event]
// @Events
// player names <entity>
//
// @Location true
//
// @Plugin Paper
//
// @Group Paper
//
// @Cancellable true
//
// @Triggers when a player attempts to rename an entity with a name tag.
//
// @Context
// <context.entity> returns an EntityTag of the renamed entity.
// <context.old_name> returns the old name of the entity, if any.
// <context.name> returns the new name of the entity.
// <context.persistent> returns whether this will cause the entity to persist through server restarts.
//
// @Determine
// "NAME:<ElementTag>" to set a different name for the entity.
// "PERSISTENT:<ElementTag(Boolean)>" to set whether the entity should remain through server restarts.
Copy link
Member

Choose a reason for hiding this comment

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

Repeating my earlier question: Does false on this cause the entity to not remain persistent, or does this stop the event from modifying the persistence state?

//
// @Player Always.
//
// -->

public PlayerNameEntityScriptEvent() {
registerCouldMatcher("player names <entity>");
this.<PlayerNameEntityScriptEvent, ElementTag>registerDetermination("persistent", ElementTag.class, (evt, context, determination) -> {
event.getEntity().setPersistent(determination.asBoolean());
});
this.<PlayerNameEntityScriptEvent, ElementTag>registerDetermination("name", ElementTag.class, (evt, context, determination) -> {
event.setName(PaperModule.parseFormattedText(determination.toString(), ChatColor.WHITE));
Copy link
Member

Choose a reason for hiding this comment

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

Same here

});
}

public PlayerNameEntityEvent event;
public EntityTag entity;

@Override
public boolean matches(ScriptPath path) {
if (!runInCheck(path, entity.getLocation())) {
return false;
}
if (!path.tryArgObject(2, entity)) {
return false;
}
return super.matches(path);
}

@Override
public ScriptEntryData getScriptEntryData() {
return new BukkitScriptEntryData(event.getPlayer());
}

@Override
public ObjectTag getContext(String name) {
return switch (name) {
case "entity" -> entity.getDenizenObject();
case "name" -> new ElementTag(PaperModule.stringifyComponent(event.getName()));
Copy link
Member

Choose a reason for hiding this comment

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

Nitpick, but this can be a plain text element (, true)

case "old_name" -> entity.getName().equals(entity.getEntityType().toString()) ? null : new ElementTag(entity.getName());
Copy link
Member

Choose a reason for hiding this comment

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

this check logic is a very wonky hack

Copy link
Member

Choose a reason for hiding this comment

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

... also this isn't even event data / from the event at all, why is this here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think that with this event, having both what the name is being changed to and what it was would be beneficial. With the wonkiness, would you prefer that it just returned the entity type if there was no old name, because that's what it outputs in that scenario.

Copy link
Member

Choose a reason for hiding this comment

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

You'd need to read the old name in the event block, not the getContext, to preserve it properly for after events, and you'd need to use the correct method to get the name (look into the custom_name tag) not that getName method.

case "persistent" -> new ElementTag(event.getEntity().isPersistent());
default -> super.getContext(name);
};
}

@EventHandler
public void playerNamesEntity(PlayerNameEntityEvent event) {
this.event = event;
entity = new EntityTag(event.getEntity());
fire(event);
}
}