Potential packet fix

This commit is contained in:
Jitse Boonstra 2020-08-08 18:39:42 +02:00
parent 0a66b9dcef
commit 4eb7c8238d
4 changed files with 22 additions and 12 deletions

View File

@ -20,15 +20,13 @@ 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 uniqueLines The text that the targetPlayer will see
* @param targetPlayer The target player
* @return object instance
* @author Gatt
@ -36,17 +34,15 @@ public interface NPC {
NPC setPlayerLines(List<String> uniqueLines, Player targetPlayer);
/**
*
* @param uniqueLines The text that the targetPlayer will see
* @param uniqueLines The text that the targetPlayer will see
* @param targetPlayer The target player
* @param update whether or not to send the update packets
* @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.
*/
@ -91,6 +87,13 @@ public interface NPC {
*/
NPC create();
/**
* Check whether the NPCs packets have already been generated.
*
* @return Whether NPC#create has been called yet.
*/
boolean isCreated();
/**
* Get the ID of the NPC.
*

View File

@ -41,6 +41,7 @@ public abstract class NPCBase implements NPC, NPCPacketHandler {
protected UUID uuid = new UUID(new Random().nextLong(), 0);
protected String name = uuid.toString().replace("-", "").substring(0, 10);
protected GameProfile gameProfile = new GameProfile(uuid, name);
protected boolean created = false;
protected NPCLib instance;
protected List<String> text;
@ -177,7 +178,7 @@ public abstract class NPCBase implements NPC, NPCPacketHandler {
@Override
public boolean isShown(Player player) {
if (player == null) return false;
Objects.requireNonNull(player, "Player object cannot be null");
return shown.contains(player.getUniqueId()) && !autoHidden.contains(player.getUniqueId());
}
@ -190,9 +191,15 @@ public abstract class NPCBase implements NPC, NPCPacketHandler {
@Override
public NPC create() {
createPackets();
this.created = true;
return this;
}
@Override
public boolean isCreated() {
return created;
}
public void onLogout(Player player) {
getAutoHidden().remove(player.getUniqueId());
getShown().remove(player.getUniqueId()); // Don't need to use NPC#hide since the entity is not registered in the NMS server.

View File

@ -49,7 +49,7 @@ public class PacketListener {
}
private boolean handleInteractPacket(Player player, Object packet) {
if (!packetPlayInUseEntityClazz.isInstance(packet))
if (!packetPlayInUseEntityClazz.isInstance(packet) || player == null)
return true; // We aren't handling the packet.
NPCBase npc = null;
@ -62,7 +62,7 @@ public class PacketListener {
// ~ Kneesnap, 9 / 20 / 2019.
for (NPCBase testNPC : NPCManager.getAllNPCs()) {
if (testNPC.isShown(player) && testNPC.getEntityId() == packetEntityId) {
if (testNPC.isCreated() && testNPC.isShown(player) && testNPC.getEntityId() == packetEntityId) {
npc = testNPC;
break;
}

View File

@ -36,17 +36,17 @@ public class NPC_v1_16_R1 extends NPCBase {
public NPC_v1_16_R1(NPCLib instance, List<String> lines) {
super(instance, lines);
}
@Override
public Hologram getPlayerHologram(Player player) {
Hologram holo = super.getPlayerHologram(player);
if (holo == null){
if (holo == null) {
holo = new Hologram(MinecraftVersion.V1_16_R1, location.clone().add(0, 0.5, 0), getPlayerLines(player));
}
super.textDisplayHolograms.put(player.getUniqueId(), holo);
return holo;
}
@Override
public void createPackets() {
Bukkit.getOnlinePlayers().forEach(this::createPackets);