Text update function working on v1.8 R1!
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
package net.jitse.npclib.hologram;
|
package net.jitse.npclib.hologram;
|
||||||
|
|
||||||
import org.bukkit.Bukkit;
|
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
@@ -56,31 +55,4 @@ public abstract class HologramBase implements Hologram, HologramPacketHandler {
|
|||||||
|
|
||||||
this.shown.remove(uuid);
|
this.shown.remove(uuid);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void updateText(List<String> text) {
|
|
||||||
if (this.text.size() != text.size())
|
|
||||||
throw new IllegalArgumentException("When updating the text, the old and new text should have the same amount of lines");
|
|
||||||
|
|
||||||
for (int i = 0; i < text.size(); i++) {
|
|
||||||
String oldLine = this.text.get(i);
|
|
||||||
String newLine = text.get(i);
|
|
||||||
|
|
||||||
if (oldLine.equals(newLine))
|
|
||||||
continue; // No need to update.
|
|
||||||
|
|
||||||
// Perhaps this should return an object so we can send all immediately to the shown players.
|
|
||||||
createTextUpdatePacket(oldLine, newLine);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (UUID uuid : shown) {
|
|
||||||
Player player = Bukkit.getPlayer(uuid);
|
|
||||||
if (player == null || !player.isOnline()) {
|
|
||||||
throw new IllegalStateException("Tried to update hologram for offline player");
|
|
||||||
}
|
|
||||||
|
|
||||||
// sendTextUpdatePackets(player, );
|
|
||||||
}
|
|
||||||
this.text = text;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,8 +9,4 @@ public interface HologramPacketHandler {
|
|||||||
void sendShowPackets(Player player);
|
void sendShowPackets(Player player);
|
||||||
|
|
||||||
void sendHidePackets(Player player);
|
void sendHidePackets(Player player);
|
||||||
|
|
||||||
void createTextUpdatePacket(String oldLine, String newLine);
|
|
||||||
|
|
||||||
void sendTextUpdatePackets(Player player);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import net.jitse.npclib.api.events.NPCShowEvent;
|
|||||||
import net.jitse.npclib.api.skin.Skin;
|
import net.jitse.npclib.api.skin.Skin;
|
||||||
import net.jitse.npclib.api.state.NPCSlot;
|
import net.jitse.npclib.api.state.NPCSlot;
|
||||||
import net.jitse.npclib.api.state.NPCState;
|
import net.jitse.npclib.api.state.NPCState;
|
||||||
|
import net.jitse.npclib.hologram.Hologram;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
import org.bukkit.World;
|
import org.bukkit.World;
|
||||||
@@ -39,6 +40,7 @@ public abstract class NPCBase implements NPC, NPCPacketHandler {
|
|||||||
protected List<String> text;
|
protected List<String> text;
|
||||||
protected Location location;
|
protected Location location;
|
||||||
protected Skin skin;
|
protected Skin skin;
|
||||||
|
protected Hologram hologram;
|
||||||
|
|
||||||
// offHand support in 1.9 R1 and later.
|
// offHand support in 1.9 R1 and later.
|
||||||
protected ItemStack helmet, chestplate, leggings, boots, inHand, offHand;
|
protected ItemStack helmet, chestplate, leggings, boots, inHand, offHand;
|
||||||
@@ -304,7 +306,7 @@ public abstract class NPCBase implements NPC, NPCPacketHandler {
|
|||||||
for (UUID shownUuid : shown) {
|
for (UUID shownUuid : shown) {
|
||||||
Player player = Bukkit.getPlayer(shownUuid);
|
Player player = Bukkit.getPlayer(shownUuid);
|
||||||
if (player != null && isShown(player)) {
|
if (player != null && isShown(player)) {
|
||||||
sendTextUpdatePackets(this.text, text, player);
|
hologram.updateText(text);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.text = text;
|
this.text = text;
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ package net.jitse.npclib.nms.v1_8_R1;
|
|||||||
|
|
||||||
import net.jitse.npclib.NPCLib;
|
import net.jitse.npclib.NPCLib;
|
||||||
import net.jitse.npclib.api.state.NPCSlot;
|
import net.jitse.npclib.api.state.NPCSlot;
|
||||||
import net.jitse.npclib.hologram.Hologram;
|
|
||||||
import net.jitse.npclib.internal.NPCBase;
|
import net.jitse.npclib.internal.NPCBase;
|
||||||
import net.jitse.npclib.nms.v1_8_R1.hologram.Hologram_v1_8_R1;
|
import net.jitse.npclib.nms.v1_8_R1.hologram.Hologram_v1_8_R1;
|
||||||
import net.jitse.npclib.nms.v1_8_R1.packets.*;
|
import net.jitse.npclib.nms.v1_8_R1.packets.*;
|
||||||
@@ -27,7 +26,6 @@ import java.util.UUID;
|
|||||||
*/
|
*/
|
||||||
public class NPC_v1_8_R1 extends NPCBase {
|
public class NPC_v1_8_R1 extends NPCBase {
|
||||||
|
|
||||||
private Hologram hologram;
|
|
||||||
private PacketPlayOutNamedEntitySpawn packetPlayOutNamedEntitySpawn;
|
private PacketPlayOutNamedEntitySpawn packetPlayOutNamedEntitySpawn;
|
||||||
private PacketPlayOutScoreboardTeam packetPlayOutScoreboardTeamRegister;
|
private PacketPlayOutScoreboardTeam packetPlayOutScoreboardTeamRegister;
|
||||||
private PacketPlayOutPlayerInfo packetPlayOutPlayerInfoAdd, packetPlayOutPlayerInfoRemove;
|
private PacketPlayOutPlayerInfo packetPlayOutPlayerInfoAdd, packetPlayOutPlayerInfoRemove;
|
||||||
|
|||||||
+69
-29
@@ -1,21 +1,23 @@
|
|||||||
package net.jitse.npclib.nms.v1_8_R1.hologram;
|
package net.jitse.npclib.nms.v1_8_R1.hologram;
|
||||||
|
|
||||||
import net.jitse.npclib.hologram.HologramBase;
|
import net.jitse.npclib.hologram.HologramBase;
|
||||||
import net.minecraft.server.v1_8_R1.EntityArmorStand;
|
import net.minecraft.server.v1_8_R1.*;
|
||||||
import net.minecraft.server.v1_8_R1.PacketPlayOutEntityDestroy;
|
import org.bukkit.Bukkit;
|
||||||
import net.minecraft.server.v1_8_R1.PacketPlayOutSpawnEntityLiving;
|
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
import org.bukkit.craftbukkit.v1_8_R1.CraftWorld;
|
import org.bukkit.craftbukkit.v1_8_R1.CraftWorld;
|
||||||
|
import org.bukkit.craftbukkit.v1_8_R1.entity.CraftPlayer;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
public class Hologram_v1_8_R1 extends HologramBase {
|
public class Hologram_v1_8_R1 extends HologramBase {
|
||||||
|
|
||||||
// Perhaps all logic methods should be placed in the base class instead then
|
private List<EntityArmorStand> nmsArmorStands;
|
||||||
// use a similar approach to the NPCBase class, with a HologramPacketHandler class...
|
private List<PacketPlayOutSpawnEntityLiving> showPackets;
|
||||||
// That way I can't mess up logic on different version whilst implementing new features.
|
private List<PacketPlayOutEntityDestroy> hidePackets;
|
||||||
// *cough cough* hologram text updates. I can already see this go wrong.
|
|
||||||
public Hologram_v1_8_R1(Location start, List<String> text) {
|
public Hologram_v1_8_R1(Location start, List<String> text) {
|
||||||
super(start, text);
|
super(start, text);
|
||||||
}
|
}
|
||||||
@@ -24,15 +26,14 @@ public class Hologram_v1_8_R1 extends HologramBase {
|
|||||||
public void createPackets() {
|
public void createPackets() {
|
||||||
Location top = start.clone().add(0, DELTA * text.size(), 0);
|
Location top = start.clone().add(0, DELTA * text.size(), 0);
|
||||||
|
|
||||||
for (String line : text) {
|
List<EntityArmorStand> nmsArmorStands = new ArrayList<>();
|
||||||
if (line.isEmpty()) {
|
List<PacketPlayOutSpawnEntityLiving> showPackets = new ArrayList<>();
|
||||||
top.subtract(0, DELTA, 0);
|
List<PacketPlayOutEntityDestroy> hidePackets = new ArrayList<>();
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
for (String line : text) {
|
||||||
EntityArmorStand armorStand = new EntityArmorStand(((CraftWorld) top.getWorld()).getHandle());
|
EntityArmorStand armorStand = new EntityArmorStand(((CraftWorld) top.getWorld()).getHandle());
|
||||||
armorStand.setLocation(top.getX(), top.getY(), top.getZ(), 0, 0);
|
armorStand.setLocation(top.getX(), top.getY(), top.getZ(), 0, 0);
|
||||||
armorStand.setCustomName(line); // Does this method update? Or should teams be used?
|
armorStand.setCustomName(line);
|
||||||
armorStand.setCustomNameVisible(true);
|
armorStand.setCustomNameVisible(true);
|
||||||
armorStand.setGravity(false);
|
armorStand.setGravity(false);
|
||||||
armorStand.setSmall(true);
|
armorStand.setSmall(true);
|
||||||
@@ -40,43 +41,82 @@ public class Hologram_v1_8_R1 extends HologramBase {
|
|||||||
armorStand.setBasePlate(false);
|
armorStand.setBasePlate(false);
|
||||||
armorStand.setArms(false);
|
armorStand.setArms(false);
|
||||||
|
|
||||||
|
nmsArmorStands.add(armorStand);
|
||||||
|
|
||||||
PacketPlayOutSpawnEntityLiving lineShowPacket = new PacketPlayOutSpawnEntityLiving(armorStand);
|
PacketPlayOutSpawnEntityLiving lineShowPacket = new PacketPlayOutSpawnEntityLiving(armorStand);
|
||||||
PacketPlayOutEntityDestroy lineHidePacket = new PacketPlayOutEntityDestroy(armorStand.getId());
|
PacketPlayOutEntityDestroy lineHidePacket = new PacketPlayOutEntityDestroy(armorStand.getId());
|
||||||
// TODO: Add packets and ArmorStand object to some sort of manager.
|
|
||||||
|
showPackets.add(lineShowPacket);
|
||||||
|
hidePackets.add(lineHidePacket);
|
||||||
|
|
||||||
top.subtract(0, DELTA, 0);
|
top.subtract(0, DELTA, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.nmsArmorStands = nmsArmorStands;
|
||||||
|
this.showPackets = showPackets;
|
||||||
|
this.hidePackets = hidePackets;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sendShowPackets(Player player) {
|
public void sendShowPackets(Player player) {
|
||||||
|
PlayerConnection connection = ((CraftPlayer) player).getHandle().playerConnection;
|
||||||
|
for (int i = 0; i < text.size(); i++) {
|
||||||
|
if (text.get(i).isEmpty()) continue; // No need to spawn the line.
|
||||||
|
connection.sendPacket(showPackets.get(i));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sendHidePackets(Player player) {
|
public void sendHidePackets(Player player) {
|
||||||
|
PlayerConnection connection = ((CraftPlayer) player).getHandle().playerConnection;
|
||||||
|
for (int i = 0; i < text.size(); i++) {
|
||||||
|
if (text.get(i).isEmpty()) continue; // No need to hide the line (as it was never spawned).
|
||||||
|
connection.sendPacket(hidePackets.get(i));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void createTextUpdatePacket(String oldLine, String newLine) {
|
public void updateText(List<String> text) {
|
||||||
|
List<Packet> toSend = new ArrayList<>();
|
||||||
|
|
||||||
}
|
if (this.text.size() != text.size())
|
||||||
|
throw new IllegalArgumentException("When updating the text, the old and new text should have the same amount of lines");
|
||||||
|
|
||||||
@Override
|
for (int i = 0; i < text.size(); i++) {
|
||||||
public void sendTextUpdatePackets(Player player) {
|
EntityArmorStand armorStand = nmsArmorStands.get(i);
|
||||||
|
String oldLine = this.text.get(i);
|
||||||
|
String newLine = text.get(i);
|
||||||
|
|
||||||
}
|
armorStand.getDataWatcher().watch(2, newLine); // Update the DataWatcher object.
|
||||||
|
|
||||||
@Override
|
if (oldLine.equals(newLine)) {
|
||||||
public void sendTextUpdatePackets(Player player, int index, String newLine) {
|
continue; // No need to update.
|
||||||
if (newLine.isEmpty()) {
|
} else if (newLine.isEmpty() && !oldLine.isEmpty()) {
|
||||||
// Check if line was empty before, if not, remove the hologram line.
|
// Check if line was empty before, if not, remove the hologram line.
|
||||||
} else {
|
toSend.add(hidePackets.get(i));
|
||||||
// Check if line was empty before, if it was, create the hologram line.
|
} else if (!newLine.isEmpty() && oldLine.isEmpty()) {
|
||||||
// If the line was not empty before and it isn't now, update its text.
|
// Check if line was empty before, if it was, create the hologram line.
|
||||||
|
PacketPlayOutSpawnEntityLiving lineShowPacket = new PacketPlayOutSpawnEntityLiving(armorStand);
|
||||||
|
toSend.add(lineShowPacket);
|
||||||
|
} else {
|
||||||
|
// If the line was not empty before and it isn't now, update its text.
|
||||||
|
PacketPlayOutEntityMetadata metadataPacket = new PacketPlayOutEntityMetadata(armorStand.getId(), armorStand.getDataWatcher(), true);
|
||||||
|
toSend.add(metadataPacket);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send the packets to all players that can see the hologram (i.e. shown set).
|
for (UUID uuid : shown) {
|
||||||
|
Player player = Bukkit.getPlayer(uuid);
|
||||||
|
if (player == null || !player.isOnline()) {
|
||||||
|
throw new IllegalStateException("Tried to update hologram for offline player");
|
||||||
|
}
|
||||||
|
|
||||||
|
PlayerConnection connection = ((CraftPlayer) player).getHandle().playerConnection;
|
||||||
|
for (Packet packet : toSend) {
|
||||||
|
connection.sendPacket(packet);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.text = text; // At last, update the text in this hologram object.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user