Merge pull request #105 from RealGatt/feature/per-player-names

Feature to add Per-Player Name Holograms to NPCs
This commit is contained in:
Jitse Boonstra
2020-07-20 13:00:52 +02:00
committed by GitHub
33 changed files with 320 additions and 57 deletions
@@ -8,6 +8,7 @@ import net.jitse.npclib.api.skin.Skin;
import net.jitse.npclib.api.state.NPCAnimation;
import net.jitse.npclib.api.state.NPCSlot;
import net.jitse.npclib.api.state.NPCState;
import net.jitse.npclib.hologram.Hologram;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
@@ -18,6 +19,39 @@ import java.util.UUID;
public interface NPC {
/**
*
* @param player
* @return unique hologram for that user
*/
Hologram getPlayerHologram(Player player);
/**
*
* @param uniqueLines The text that the targetPlayer will see
* @param targetPlayer The target player
* @return object instance
* @author Gatt
*/
NPC setPlayerLines(List<String> uniqueLines, Player targetPlayer);
/**
*
* @param uniqueLines The text that the targetPlayer will see
* @param targetPlayer The target player
* @param update whether or not to send the update packets
* @return object instance
* @author Gatt
*/
NPC setPlayerLines(List<String> uniqueLines, Player targetPlayer, boolean update);
/**
*
* @param targetPlayer The target player
* @return the lines that the targetPlayer will see, if null; default lines.
*/
List<String> getPlayerLines(Player targetPlayer);
/**
* Set the NPC's location.
* Use this method before using {@link NPC#create}.
@@ -16,7 +16,8 @@ public enum MinecraftVersion {
V1_13_R1,
V1_13_R2,
V1_14_R1,
V1_15_R1;
V1_15_R1,
V1_16_R1;
public boolean isAboveOrEqual(MinecraftVersion compare) {
return ordinal() >= compare.ordinal();
@@ -45,10 +45,15 @@ public abstract class NPCBase implements NPC, NPCPacketHandler {
protected List<String> text;
protected Location location;
protected Skin skin;
protected Hologram hologram;
//protected Hologram hologram;
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 +65,43 @@ 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
}
if (isShown(targetPlayer)) { //only show hologram if the player is in range
Hologram hologram = getPlayerHologram(targetPlayer);
List<Object> updatePackets = hologram.getUpdatePackets(getPlayerLines(targetPlayer));
hologram.update(targetPlayer, updatePackets);
}
}
return this;
}
@Override
public List<String> getPlayerLines(Player targetPlayer) {
return uniqueText.getOrDefault(targetPlayer.getUniqueId(), text);
}
@Override
public UUID getUniqueId() {
return uuid;
@@ -90,8 +132,9 @@ public abstract class NPCBase implements NPC, NPCPacketHandler {
if (autoHidden.contains(uuid)) {
continue;
}
hide(Bukkit.getPlayer(uuid), true);
Player plyr = Bukkit.getPlayer(uuid); // destroy the per player holograms
getPlayerHologram(plyr).hide(plyr);
hide(plyr, true);
}
}
@@ -303,11 +346,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);
}
}
@@ -15,6 +15,8 @@ interface NPCPacketHandler {
void createPackets();
void createPackets(Player player);
void sendShowPackets(Player player);
void sendHidePackets(Player player);