-
-
Notifications
You must be signed in to change notification settings - Fork 108
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
base: dev
Are you sure you want to change the base?
Changes from 11 commits
00eaf70
59f6502
20f84a5
6d2f246
3a50939
359715e
8cc87ff
58c49e4
81fd881
37633bc
f54b31a
ee06197
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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()); | ||
return true; | ||
} | ||
return false; | ||
}); | ||
this.<PlayerNameEntityScriptEvent, ElementTag>registerDetermination("name", ElementTag.class, (evt, context, determination) -> { | ||
event.setName(PaperModule.parseFormattedText(determination.toString(), ChatColor.WHITE)); | ||
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. 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())); | ||
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. Nitpick, but this can be a plain text element ( |
||
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); | ||
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. Same here |
||
fire(event); | ||
} | ||
} |
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.
Need to access it from the
evt
parameter