-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
RenameCommand.java
181 lines (171 loc) · 7.89 KB
/
RenameCommand.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package com.denizenscript.denizen.scripts.commands.npc;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.objects.EntityFormObject;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.objects.NPCTag;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizen.tags.BukkitTagContext;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.ArgumentHelper;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import com.denizenscript.denizencore.tags.TagManager;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import net.citizensnpcs.api.event.DespawnReason;
import net.citizensnpcs.api.npc.NPC;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import java.util.HashMap;
import java.util.function.Function;
public class RenameCommand extends AbstractCommand {
public RenameCommand() {
setName("rename");
setSyntax("rename [<name>] (t:<entity>|...) (per_player)");
setRequiredArguments(1, 3);
setParseArgs(false);
isProcedural = false;
}
// <--[command]
// @Name Rename
// @Syntax rename [<name>] (t:<entity>|...) (per_player)
// @Required 1
// @Maximum 3
// @Plugin Citizens
// @Short Renames the linked NPC or list of entities.
// @Group npc
//
// @Description
// Renames the linked NPC or list of entities.
// Functions like the '/npc rename' command.
//
// Can rename a spawned or unspawned NPC to any name up to 256 characters.
//
// Can rename a vanilla entity to any name up to 256 characters, and will automatically make the nameplate visible.
//
// Can rename a player to any name up to 16 characters. This will affect only the player's nameplate.
//
// Optionally specify per_player to reprocess the input tags for each player when renaming a vanilla entity
// (meaning, if you use "- rename <player.name> t:<[someent]> per_player", every player will see their own name on that entity).
// A per_player rename will remain active until the entity is renamed again or the server is restarted.
//
// @Tags
// <NPCTag.name>
// <NPCTag.nickname>
//
// @Usage
// Use to rename the linked NPC.
// - rename Bob
//
// @Usage
// Use to rename a different NPC.
// - rename Bob t:<[some_npc]>
// -->
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry.getProcessedArgs()) {
if (!scriptEntry.hasObject("targets")
&& arg.matchesPrefix("t", "target", "targets")) {
scriptEntry.addObject("targets", ListTag.getListFor(TagManager.tagObject(arg.getValue(), scriptEntry.getContext()), scriptEntry.getContext()));
}
else if (!scriptEntry.hasObject("per_player")
&& arg.matches("per_player")) {
scriptEntry.addObject("per_player", new ElementTag(true));
}
else if (!scriptEntry.hasObject("name")) {
scriptEntry.addObject("name", new ElementTag(arg.raw_value));
}
else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("name")) {
throw new InvalidArgumentsException("Must specify a name!");
}
if (!scriptEntry.hasObject("targets")) {
if (Utilities.getEntryNPC(scriptEntry) == null || !Utilities.getEntryNPC(scriptEntry).isValid()) {
throw new InvalidArgumentsException("Must have an NPC attached, or specify a list of targets to rename!");
}
scriptEntry.addObject("targets", new ListTag(Utilities.getEntryNPC(scriptEntry)));
}
}
@Override
public void execute(final ScriptEntry scriptEntry) {
final ElementTag name = scriptEntry.getElement("name");
ElementTag perPlayer = scriptEntry.getElement("per_player");
ListTag targets = scriptEntry.getObjectTag("targets");
if (perPlayer != null && perPlayer.asBoolean()) {
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), name.debug()
+ targets.debug()
+ perPlayer.debug());
}
for (ObjectTag target : targets.objectForms) {
EntityTag entity = target.asType(EntityTag.class, CoreUtilities.noDebugContext);
if (entity != null) {
final BukkitTagContext originalContext = (BukkitTagContext) scriptEntry.context.clone();
customNames.put(entity.getBukkitEntity().getEntityId(), p -> {
originalContext.player = new PlayerTag(p);
return TagManager.tag(name.asString(), originalContext);
});
for (Player player : NMSHandler.getEntityHelper().getPlayersThatSee(entity.getBukkitEntity())) {
NMSHandler.getPacketHelper().sendRename(player, entity.getBukkitEntity(), "");
}
}
}
return;
}
String nameString = TagManager.tag(name.asString(), scriptEntry.context);
if (nameString.length() > 256) {
nameString = nameString.substring(0, 256);
}
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), ArgumentHelper.debugObj("name", nameString)
+ targets.debug());
}
for (ObjectTag target : targets.objectForms) {
EntityFormObject entity = target.asType(EntityTag.class, CoreUtilities.noDebugContext);
if (entity == null) {
entity = target.asType(NPCTag.class, scriptEntry.context);
}
else {
entity = ((EntityTag) entity).getDenizenObject();
}
if (entity instanceof NPCTag) {
NPC npc = ((NPCTag) entity).getCitizen();
if (npc.isSpawned()) {
Location prev = npc.getEntity().getLocation();
npc.despawn(DespawnReason.PENDING_RESPAWN);
npc.setName(nameString);
npc.spawn(prev);
}
else {
npc.setName(nameString);
}
}
else if (entity instanceof PlayerTag) {
String limitedName = nameString.length() > 16 ? nameString.substring(0, 16) : nameString;
NMSHandler.getInstance().getProfileEditor().setPlayerName(((PlayerTag) entity).getPlayerEntity(), limitedName);
}
else {
Entity bukkitEntity = entity.getDenizenEntity().getBukkitEntity();
customNames.remove(bukkitEntity.getEntityId());
bukkitEntity.setCustomName(nameString);
bukkitEntity.setCustomNameVisible(true);
}
}
}
public static HashMap<Integer, Function<Player, String>> customNames = new HashMap<>();
public static boolean hasAnyDynamicRenames() {
return !customNames.isEmpty();
}
public static Function<Player, String> getCustomNameFor(int eid) {
return customNames.get(eid);
}
}