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,99 @@
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.PaperAPITools;
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 event will cause the entity to persist through restarts. NOTE: Entities may still persist for other reasons. To ensure they do not, use <@link mechanism EntityTag.force_no_persist>.
//
// @Player Always.
//
// -->

public PlayerNameEntityScriptEvent() {
registerCouldMatcher("player names <entity>");
this.<PlayerNameEntityScriptEvent, ElementTag>registerOptionalDetermination("persistent", ElementTag.class, (evt, context, determination) -> {
if (determination.isBoolean()) {
event.setPersistent(determination.asBoolean());
Copy link
Member

Choose a reason for hiding this comment

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

Need to access it from the evt parameter

return true;
}
return false;
});
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;
public ElementTag oldName;

@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" -> oldName;
case "persistent" -> new ElementTag(event.isPersistent());
default -> super.getContext(name);
};
}

@EventHandler
public void playerNamesEntity(PlayerNameEntityEvent event) {
this.event = event;
entity = new EntityTag(event.getEntity());
String name = PaperAPITools.instance.getCustomName(entity.getBukkitEntity());
oldName = name == null ? null : new ElementTag(name);
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

fire(event);
}
}