use of com.denizenscript.denizen.objects.EntityFormObject in project Denizen-For-Bukkit by DenizenScript.
the class RenameCommand method execute.
@Override
public void execute(final ScriptEntry scriptEntry) {
final ElementTag name = scriptEntry.getElement("name");
ElementTag perPlayer = scriptEntry.getElement("per_player");
ElementTag listNameOnly = scriptEntry.getElement("list_name_only");
ListTag targets = scriptEntry.getObjectTag("targets");
List<PlayerTag> players = (List<PlayerTag>) scriptEntry.getObject("players");
if (perPlayer != null && perPlayer.asBoolean()) {
NetworkInterceptHelper.enable();
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), name, targets, perPlayer, listNameOnly, db("for", players));
}
for (ObjectTag target : targets.objectForms) {
EntityTag entity = target.asType(EntityTag.class, CoreUtilities.noDebugContext);
if (entity != null) {
Entity bukkitEntity = entity.getBukkitEntity();
if (bukkitEntity == null) {
Debug.echoError("Invalid entity in rename command.");
continue;
}
if (name.asString().equals("cancel")) {
customNames.remove(bukkitEntity.getUniqueId());
if (bukkitEntity.isCustomNameVisible()) {
if (players == null) {
for (Player player : NMSHandler.getEntityHelper().getPlayersThatSee(bukkitEntity)) {
NMSHandler.getPacketHelper().sendRename(player, bukkitEntity, bukkitEntity.getCustomName(), false);
}
} else {
for (PlayerTag player : players) {
NMSHandler.getPacketHelper().sendRename(player.getPlayerEntity(), bukkitEntity, bukkitEntity.getCustomName(), false);
}
}
} else {
bukkitEntity.setCustomNameVisible(true);
// Force a metadata update
bukkitEntity.setCustomNameVisible(false);
}
} else {
final BukkitTagContext originalContext = (BukkitTagContext) scriptEntry.context.clone();
HashMap<UUID, RenameData> playerToFuncMap = customNames.computeIfAbsent(bukkitEntity.getUniqueId(), k -> new HashMap<>());
Function<Player, String> nameGetter = p -> {
originalContext.player = new PlayerTag(p);
return TagManager.tag(name.asString(), originalContext);
};
RenameData renamer = new RenameData();
renamer.nameFunction = nameGetter;
renamer.listOnly = listNameOnly != null && listNameOnly.asBoolean();
if (players == null) {
playerToFuncMap.put(null, renamer);
} else {
for (PlayerTag player : players) {
playerToFuncMap.put(player.getUUID(), renamer);
}
}
if (players == null) {
for (Player player : NMSHandler.getEntityHelper().getPlayersThatSee(bukkitEntity)) {
NMSHandler.getPacketHelper().sendRename(player, bukkitEntity, "", renamer.listOnly);
}
} else {
for (PlayerTag player : players) {
NMSHandler.getPacketHelper().sendRename(player.getPlayerEntity(), bukkitEntity, "", renamer.listOnly);
}
}
}
}
}
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(), db("name", nameString), listNameOnly, targets);
}
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 == null) {
Debug.echoError("Invalid entity in rename command.");
continue;
}
if (entity instanceof NPCTag) {
NPC npc = ((NPCTag) entity).getCitizen();
if (npc.isSpawned()) {
Location prev = npc.getStoredLocation().clone();
npc.despawn(DespawnReason.PENDING_RESPAWN);
npc.setName(nameString);
npc.spawn(prev);
} else {
npc.setName(nameString);
}
} else if (entity instanceof PlayerTag) {
if (listNameOnly != null && listNameOnly.asBoolean()) {
AdvancedTextImpl.instance.setPlayerListName(((PlayerTag) entity).getPlayerEntity(), nameString);
} else {
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.getUniqueId());
bukkitEntity.setCustomName(nameString);
bukkitEntity.setCustomNameVisible(true);
}
}
}
Aggregations