|
| 1 | +package com.denizenscript.denizen.paper.events; |
| 2 | + |
| 3 | +import com.denizenscript.denizen.events.BukkitScriptEvent; |
| 4 | +import com.denizenscript.denizen.objects.EntityTag; |
| 5 | +import com.denizenscript.denizen.paper.PaperModule; |
| 6 | +import com.denizenscript.denizen.utilities.PaperAPITools; |
| 7 | +import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData; |
| 8 | +import com.denizenscript.denizencore.objects.ObjectTag; |
| 9 | +import com.denizenscript.denizencore.objects.core.ElementTag; |
| 10 | +import com.denizenscript.denizencore.scripts.ScriptEntryData; |
| 11 | +import io.papermc.paper.event.player.PlayerNameEntityEvent; |
| 12 | +import net.md_5.bungee.api.ChatColor; |
| 13 | +import org.bukkit.event.EventHandler; |
| 14 | +import org.bukkit.event.Listener; |
| 15 | + |
| 16 | +public class PlayerNameEntityScriptEvent extends BukkitScriptEvent implements Listener { |
| 17 | + |
| 18 | + // <--[event] |
| 19 | + // @Events |
| 20 | + // player names <entity> |
| 21 | + // |
| 22 | + // @Location true |
| 23 | + // |
| 24 | + // @Plugin Paper |
| 25 | + // |
| 26 | + // @Group Paper |
| 27 | + // |
| 28 | + // @Cancellable true |
| 29 | + // |
| 30 | + // @Triggers when a player attempts to rename an entity with a name tag. |
| 31 | + // |
| 32 | + // @Context |
| 33 | + // <context.entity> returns an EntityTag of the renamed entity. |
| 34 | + // <context.old_name> returns the old name of the entity, if any. |
| 35 | + // <context.name> returns the new name of the entity. |
| 36 | + // <context.persistent> returns whether this will cause the entity to persist through server restarts. |
| 37 | + // |
| 38 | + // @Determine |
| 39 | + // "NAME:<ElementTag>" to set a different name for the entity. |
| 40 | + // "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>. |
| 41 | + // |
| 42 | + // @Player Always. |
| 43 | + // |
| 44 | + // --> |
| 45 | + |
| 46 | + public PlayerNameEntityScriptEvent() { |
| 47 | + registerCouldMatcher("player names <entity>"); |
| 48 | + this.<PlayerNameEntityScriptEvent, ElementTag>registerOptionalDetermination("persistent", ElementTag.class, (evt, context, determination) -> { |
| 49 | + if (determination.isBoolean()) { |
| 50 | + evt.event.setPersistent(determination.asBoolean()); |
| 51 | + return true; |
| 52 | + } |
| 53 | + return false; |
| 54 | + }); |
| 55 | + this.<PlayerNameEntityScriptEvent, ElementTag>registerDetermination("name", ElementTag.class, (evt, context, determination) -> { |
| 56 | + evt.event.setName(PaperModule.parseFormattedText(determination.toString(), ChatColor.WHITE)); |
| 57 | + }); |
| 58 | + } |
| 59 | + |
| 60 | + public PlayerNameEntityEvent event; |
| 61 | + public EntityTag entity; |
| 62 | + public ElementTag oldName; |
| 63 | + |
| 64 | + @Override |
| 65 | + public boolean matches(ScriptPath path) { |
| 66 | + if (!runInCheck(path, entity.getLocation())) { |
| 67 | + return false; |
| 68 | + } |
| 69 | + if (!path.tryArgObject(2, entity)) { |
| 70 | + return false; |
| 71 | + } |
| 72 | + return super.matches(path); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public ScriptEntryData getScriptEntryData() { |
| 77 | + return new BukkitScriptEntryData(event.getPlayer()); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public ObjectTag getContext(String name) { |
| 82 | + return switch (name) { |
| 83 | + case "entity" -> entity.getDenizenObject(); |
| 84 | + case "name" -> new ElementTag(PaperModule.stringifyComponent(event.getName()), true); |
| 85 | + case "old_name" -> oldName; |
| 86 | + case "persistent" -> new ElementTag(event.isPersistent()); |
| 87 | + default -> super.getContext(name); |
| 88 | + }; |
| 89 | + } |
| 90 | + |
| 91 | + @EventHandler |
| 92 | + public void playerNamesEntity(PlayerNameEntityEvent event) { |
| 93 | + this.event = event; |
| 94 | + entity = new EntityTag(event.getEntity()); |
| 95 | + String name = PaperAPITools.instance.getCustomName(entity.getBukkitEntity()); |
| 96 | + oldName = name == null ? null : new ElementTag(name, true); |
| 97 | + fire(event); |
| 98 | + } |
| 99 | +} |
0 commit comments