Flesh out hologram impl more

This commit is contained in:
fullwall 2020-07-03 15:14:55 +08:00
parent ae0c3b7a6b
commit 878263359a
7 changed files with 142 additions and 43 deletions

View File

@ -78,6 +78,7 @@ public class Settings {
DEFAULT_DISTANCE_MARGIN("npc.pathfinding.default-distance-margin", 2),
DEFAULT_LOOK_CLOSE("npc.default.look-close.enabled", false),
DEFAULT_LOOK_CLOSE_RANGE("npc.default.look-close.range", 5),
DEFAULT_NPC_HOLOGRAM_LINE_HEIGHT("npc.hologram.default-line-height", 0.4D),
DEFAULT_NPC_LIMIT("npc.limits.default-limit", 10),
DEFAULT_PATH_DISTANCE_MARGIN("npc.pathfinding.default-path-distance-margin", 1),
DEFAULT_PATHFINDER_UPDATE_PATH_RATE("npc.pathfinding.update-path-rate", 20),

View File

@ -704,20 +704,42 @@ public class NPCCommands {
@Command(
aliases = { "npc" },
usage = "hologram [text]",
desc = "Controls NPC hologram",
usage = "hologram set [line #] [text] | add [text] | remove [line #] | lineheight [height]",
desc = "Controls NPC hologram text",
modifiers = { "hologram" },
min = 1,
max = -1,
permission = "citizens.npc.hologram")
public void hologram(CommandContext args, CommandSender sender, NPC npc) {
public void hologram(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
HologramTrait trait = npc.getTrait(HologramTrait.class);
if (args.argsLength() == 1) {
trait.setText(null);
Messaging.sendTr(sender, Messages.HOLOGRAM_TEXT_REMOVED);
} else {
trait.setText(args.getJoinedStrings(1));
Messaging.sendTr(sender, Messages.HOLOGRAM_TEXT_SET);
String output = Messaging.tr(Messages.HOLOGRAM_DESCRIBE_HEADER, npc.getName());
List<String> lines = trait.getLines();
for (int i = 0; i < lines.size(); i++) {
String line = lines.get(i);
output += "<br> [[" + i + "]] - " + line;
}
Messaging.send(sender, output);
} else if (args.getString(1).equalsIgnoreCase("set")) {
int idx = Math.max(0, args.getInteger(2));
if (idx > trait.getLines().size()) {
throw new CommandException(Messages.HOLOGRAM_INVALID_LINE);
}
trait.setLine(idx, args.getJoinedStrings(3));
Messaging.sendTr(sender, Messages.HOLOGRAM_LINE_SET, idx, args.getJoinedStrings(3));
} else if (args.getString(1).equalsIgnoreCase("add")) {
trait.addLine(args.getJoinedStrings(2));
Messaging.sendTr(sender, Messages.HOLOGRAM_LINE_ADD, args.getJoinedStrings(2));
} else if (args.getString(1).equalsIgnoreCase("remove")) {
int idx = Math.max(0, args.getInteger(2));
if (idx > trait.getLines().size()) {
throw new CommandException(Messages.HOLOGRAM_INVALID_LINE);
}
trait.removeLine(idx);
Messaging.sendTr(sender, Messages.HOLOGRAM_LINE_REMOVED, idx);
} else if (args.getString(1).equalsIgnoreCase("lineheight")) {
trait.setLineHeight(args.getDouble(2));
Messaging.sendTr(sender, Messages.HOLOGRAM_LINE_HEIGHT_SET, args.getDouble(2));
}
}

View File

@ -63,6 +63,12 @@ public class ArmorStandTrait extends Trait {
if (head != null) {
entity.setHeadPose(head);
}
entity.setVisible(visible);
entity.setGravity(gravity);
entity.setArms(hasarms);
entity.setBasePlate(hasbaseplate);
entity.setSmall(small);
entity.setMarker(marker);
}
}

View File

@ -1,8 +1,15 @@
package net.citizensnpcs.trait;
import java.util.List;
import org.bukkit.Location;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.EntityType;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
import com.google.common.collect.Lists;
import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.MemoryNPCDataStore;
import net.citizensnpcs.api.npc.NPC;
@ -17,55 +24,108 @@ import net.citizensnpcs.api.util.Placeholders;
*/
@TraitName("hologramtrait")
public class HologramTrait extends Trait {
private final NPCRegistry registry = CitizensAPI
.createAnonymousNPCRegistry(new MemoryNPCDataStore());
private NPC hologramNPC;
private Location currentLoc;
private final List<NPC> hologramNPCs = Lists.newArrayList();
@Persist
private String text;
private double lineHeight = -1;
@Persist
private final List<String> lines = Lists.newArrayList();
private final NPCRegistry registry = CitizensAPI.createAnonymousNPCRegistry(new MemoryNPCDataStore());
public HologramTrait() {
super("hologramtrait");
}
public void addLine(String text) {
lines.add(text);
unload();
load();
}
private double getHeight(int lineNumber) {
return (lineHeight == -1 ? Setting.DEFAULT_NPC_HOLOGRAM_LINE_HEIGHT.asDouble() : lineHeight) * lineNumber;
}
public List<String> getLines() {
return lines;
}
private void load() {
int i = 0;
currentLoc = npc.getStoredLocation();
for (String line : lines) {
NPC hologramNPC = registry.createNPC(EntityType.ARMOR_STAND, Placeholders.replace(line, null, npc));
ArmorStandTrait trait = hologramNPC.getTrait(ArmorStandTrait.class);
trait.setVisible(false);
trait.setSmall(true);
trait.setMarker(true);
trait.setGravity(false);
trait.setHasArms(false);
trait.setHasBaseplate(false);
hologramNPC.spawn(currentLoc.clone().add(0, npc.getEntity().getHeight() + getHeight(i), 0));
hologramNPC.getEntity().setInvulnerable(true);
hologramNPCs.add(hologramNPC);
i++;
}
}
@Override
public void onDespawn() {
if (hologramNPC != null) {
hologramNPC.destroy();
}
unload();
}
@Override
public void onSpawn() {
hologramNPC = registry.createNPC(EntityType.ARMOR_STAND, "");
ArmorStandTrait trait = hologramNPC.getTrait(ArmorStandTrait.class);
trait.setVisible(false);
trait.setSmall(true);
hologramNPC.spawn(npc.getStoredLocation());
hologramNPC.getEntity().setInvulnerable(true);
hologramNPC.getEntity().setGravity(false);
load();
}
public void removeLine(int idx) {
lines.remove(idx);
unload();
load();
}
@Override
public void run() {
if (!npc.isSpawned())
if (!npc.isSpawned()) {
onDespawn();
return;
ArmorStand hologram = (ArmorStand) hologramNPC.getEntity();
if (hologram == null)
return;
if (hologram.getVehicle() == null || hologram.getVehicle() != npc.getEntity()) {
if (hologram.getVehicle() != npc.getEntity()) {
hologram.leaveVehicle();
}
boolean update = currentLoc.distanceSquared(npc.getStoredLocation()) > 0.5;
if (update) {
currentLoc = npc.getStoredLocation();
}
for (int i = 0; i < hologramNPCs.size(); i++) {
NPC hologramNPC = hologramNPCs.get(i);
ArmorStand hologram = (ArmorStand) hologramNPC.getEntity();
if (hologram == null)
continue;
if (update) {
hologramNPC.teleport(currentLoc.clone().add(0, npc.getEntity().getHeight() + lineHeight * i, 0),
TeleportCause.PLUGIN);
}
String text = lines.get(i);
if (text != null && !text.isEmpty()) {
hologramNPC.setName(Placeholders.replace(text, null, npc));
} else {
}
npc.getEntity().addPassenger(hologram);
}
if (text != null && !text.isEmpty()) {
hologramNPC.setName(Placeholders.replace(text, null, npc));
} else {
}
}
public void setText(String text) {
this.text = text;
public void setLine(int idx, String text) {
lines.set(idx, text);
}
public void setLineHeight(double height) {
lineHeight = height;
}
private void unload() {
if (hologramNPCs.isEmpty())
return;
for (NPC npc : hologramNPCs) {
npc.destroy();
}
hologramNPCs.clear();
}
}

View File

@ -134,8 +134,13 @@ public class Messages {
public static final String GUIDED_WAYPOINT_EDITOR_ALREADY_TAKEN = "citizens.editors.waypoints.guided.already-taken";
public static final String GUIDED_WAYPOINT_EDITOR_BEGIN = "citizens.editors.waypoints.guided.begin";
public static final String GUIDED_WAYPOINT_EDITOR_END = "citizens.editors.waypoints.guided.end";
public static final String HOLOGRAM_DESCRIBE_HEADER = "citizens.commands.npc.hologram.text-describe-header";
public static final String HOLOGRAM_INVALID_LINE = "citizens.commands.npc.hologram.invalid-text-id";
public static final String HOLOGRAM_LINE_ADD = "citizens.commands.npc.hologram.line-add";
public static final String HOLOGRAM_LINE_HEIGHT_SET = "citizens.commands.npc.hologram.line-height-set";
public static final String HOLOGRAM_LINE_REMOVED = "citizens.commands.npc.hologram.line-removed";
public static final String HOLOGRAM_LINE_SET = "citizens.commands.npc.hologram.text-set";
public static final String HOLOGRAM_TEXT_REMOVED = "citizens.commands.npc.hologram.text-removed";
public static final String HOLOGRAM_TEXT_SET = "citizens.commands.npc.hologram.text-set";
public static final String HORSE_CHEST_SET = "citizens.commands.npc.horse.chest-set";
public static final String HORSE_CHEST_UNSET = "citizens.commands.npc.horse.chest-unset";
public static final String HORSE_COLOR_SET = "citizens.commands.npc.horse.color-set";

View File

@ -87,8 +87,13 @@ citizens.commands.npc.glowing.unset=[[{0}]] is no longer glowing.
citizens.commands.npc.glowing.color-set=[[{0}]]''s glowing color set to {1}]].
citizens.commands.npc.guardian.elder-unset=[[{0}]] is no longer an elder guardian.
citizens.commands.npc.guardian.elder-set=[[{0}]] is now an elder guardian.
citizens.commands.npc.hologram.text-set=Set hologram text to [[{0}]].
citizens.commands.npc.hologram.text-set=Set hologram text line [[{0}]] to [[{1}]].
citizens.commands.npc.hologram.text-removed=Removed hologram text.
citizens.commands.npc.hologram.text-describe-header=[[{0}]]''s hologram text (in bottom-up order):
citizens.commands.npc.hologram.invalid-text-id=Invalid line number.
citizens.commands.npc.hologram.line-height-set=Line height set to [[{0}]].
citizens.commands.npc.hologram.line-removed=Removed line [[{0}]].
citizens.commands.npc.hologram.line-add=Added a new hologram line: [[{0}]].
citizens.commands.npc.horse.chest-set=The horse is now carrying a chest.
citizens.commands.npc.horse.chest-unset=The horse is no longer carrying a chest.
citizens.commands.npc.horse.color-set=The horse''s color was set to [[{0}]].

View File

@ -45,11 +45,11 @@ public class PlayerlistTracker extends PlayerChunkMap.EntityTracker {
@Override
public void updatePlayer(final EntityPlayer entityplayer) {
if (!(entityplayer instanceof EntityHumanNPC)) {
// prevent updates to NPC "viewers"
this.lastUpdatedPlayer = entityplayer;
super.updatePlayer(entityplayer);
}
if (entityplayer instanceof EntityHumanNPC) // prevent updates to NPC "viewers"
return;
this.lastUpdatedPlayer = entityplayer;
super.updatePlayer(entityplayer);
}
private static int getD(EntityTracker entry) {