|
|
@ -49,6 +49,10 @@ public abstract class NPCBase implements NPC, NPCPacketHandler { |
|
|
|
|
|
|
|
protected final Map<NPCSlot, ItemStack> items = new EnumMap<>(NPCSlot.class); |
|
|
|
|
|
|
|
// Storage for per-player text; |
|
|
|
protected final Map<UUID, List<String>> uniqueText = new HashMap<>(); |
|
|
|
protected final Map<UUID, Hologram> textDisplayHolograms = new HashMap<>(); |
|
|
|
|
|
|
|
public NPCBase(NPCLib instance, List<String> text) { |
|
|
|
this.instance = instance; |
|
|
|
this.text = text == null ? Collections.emptyList() : text; |
|
|
@ -60,6 +64,42 @@ public abstract class NPCBase implements NPC, NPCPacketHandler { |
|
|
|
return instance; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public Hologram getPlayerHologram(Player player){ |
|
|
|
Hologram playerHologram = textDisplayHolograms.getOrDefault(player.getUniqueId(), null); |
|
|
|
return playerHologram; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public NPC setPlayerLines(List<String> uniqueLines, Player targetPlayer) { |
|
|
|
uniqueText.put(targetPlayer.getUniqueId(), uniqueLines); |
|
|
|
return this; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public NPC setPlayerLines(List<String> uniqueLines, Player targetPlayer, boolean update) { |
|
|
|
List<String> originalLines = getPlayerLines(targetPlayer); |
|
|
|
setPlayerLines(uniqueLines, targetPlayer); |
|
|
|
if (update){ |
|
|
|
if (originalLines.size() != uniqueLines.size()){ // recreate the entire hologram |
|
|
|
Hologram originalhologram = getPlayerHologram(targetPlayer); |
|
|
|
originalhologram.hide(targetPlayer); // essentially destroy the hologram |
|
|
|
textDisplayHolograms.remove(targetPlayer.getUniqueId()); // remove the old obj |
|
|
|
} |
|
|
|
|
|
|
|
Hologram hologram = getPlayerHologram(targetPlayer); // |
|
|
|
List<Object> updatePackets = hologram.getUpdatePackets(getPlayerLines(targetPlayer)); |
|
|
|
hologram.update(targetPlayer, updatePackets); |
|
|
|
hologram.show(targetPlayer); |
|
|
|
} |
|
|
|
return this; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public List<String> getPlayerLines(Player targetPlayer) { |
|
|
|
return uniqueText.getOrDefault(targetPlayer.getUniqueId(), text); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public UUID getUniqueId() { |
|
|
|
return uuid; |
|
|
@ -303,11 +343,16 @@ public abstract class NPCBase implements NPC, NPCPacketHandler { |
|
|
|
|
|
|
|
@Override |
|
|
|
public NPC setText(List<String> text) { |
|
|
|
List<Object> updatePackets = hologram.getUpdatePackets(text); |
|
|
|
uniqueText.clear(); |
|
|
|
|
|
|
|
for (UUID shownUuid : shown) { |
|
|
|
Player player = Bukkit.getPlayer(shownUuid); |
|
|
|
if (player != null && isShown(player)) { |
|
|
|
Hologram originalhologram = getPlayerHologram(player); |
|
|
|
originalhologram.hide(player); // essentially destroy the hologram |
|
|
|
textDisplayHolograms.remove(player.getUniqueId()); // remove the old obj |
|
|
|
Hologram hologram = getPlayerHologram(player); // let it regenerate |
|
|
|
List<Object> updatePackets = hologram.getUpdatePackets(getPlayerLines(player)); |
|
|
|
hologram.update(player, updatePackets); |
|
|
|
} |
|
|
|
} |
|
|
|