-
-
Notifications
You must be signed in to change notification settings - Fork 109
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 2 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,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. | ||
// | ||
// @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)); | ||
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; | ||
|
||
@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" -> entity.getName().equals(entity.getEntityType().toString()) ? null : new ElementTag(entity.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. this check logic is a very wonky hack 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. ... also this isn't even event data / from the event at all, why is this here? 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. 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. 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. You'd need to read the old name in the event block, not the getContext, to preserve it properly for |
||
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); | ||
} | ||
} |
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.
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?