From c8c649e8001ee352b2d96cf817826cdc53d11bf5 Mon Sep 17 00:00:00 2001
From: Pyrbu <pyrmcserver@gmail.com>
Date: Sun, 21 May 2023 12:32:25 +0100
Subject: [PATCH] make it less static

---
 .../pyr/znpcsplus/packets/V1_10PacketFactory.java  |  6 ++++--
 .../pyr/znpcsplus/packets/V1_14PacketFactory.java  |  6 ++++--
 .../pyr/znpcsplus/packets/V1_19PacketFactory.java  |  6 ++++--
 .../pyr/znpcsplus/packets/V1_8PacketFactory.java   | 14 +++++++++-----
 .../pyr/znpcsplus/packets/V1_9PacketFactory.java   |  6 ++++--
 5 files changed, 25 insertions(+), 13 deletions(-)

diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_10PacketFactory.java b/plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_10PacketFactory.java
index fbcdf59..f2ef0dc 100644
--- a/plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_10PacketFactory.java
+++ b/plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_10PacketFactory.java
@@ -1,17 +1,19 @@
 package lol.pyr.znpcsplus.packets;
 
+import com.github.retrooper.packetevents.PacketEventsAPI;
 import com.github.retrooper.packetevents.protocol.entity.data.EntityData;
 import lol.pyr.znpcsplus.api.entity.PropertyHolder;
 import lol.pyr.znpcsplus.entity.PacketEntity;
 import lol.pyr.znpcsplus.metadata.MetadataFactory;
 import lol.pyr.znpcsplus.scheduling.TaskScheduler;
 import org.bukkit.entity.Player;
+import org.bukkit.plugin.Plugin;
 
 import java.util.Map;
 
 public class V1_10PacketFactory extends V1_9PacketFactory {
-    public V1_10PacketFactory(TaskScheduler scheduler, MetadataFactory metadataFactory) {
-        super(scheduler, metadataFactory);
+    public V1_10PacketFactory(TaskScheduler scheduler, MetadataFactory metadataFactory, PacketEventsAPI<Plugin> packetEvents) {
+        super(scheduler, metadataFactory, packetEvents);
     }
 
     @Override
diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_14PacketFactory.java b/plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_14PacketFactory.java
index eff475c..3947ff6 100644
--- a/plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_14PacketFactory.java
+++ b/plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_14PacketFactory.java
@@ -1,5 +1,6 @@
 package lol.pyr.znpcsplus.packets;
 
+import com.github.retrooper.packetevents.PacketEventsAPI;
 import com.github.retrooper.packetevents.util.Vector3d;
 import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerSpawnEntity;
 import lol.pyr.znpcsplus.api.entity.PropertyHolder;
@@ -8,12 +9,13 @@ import lol.pyr.znpcsplus.metadata.MetadataFactory;
 import lol.pyr.znpcsplus.scheduling.TaskScheduler;
 import lol.pyr.znpcsplus.util.ZLocation;
 import org.bukkit.entity.Player;
+import org.bukkit.plugin.Plugin;
 
 import java.util.Optional;
 
 public class V1_14PacketFactory extends V1_10PacketFactory {
-    public V1_14PacketFactory(TaskScheduler scheduler, MetadataFactory metadataFactory) {
-        super(scheduler, metadataFactory);
+    public V1_14PacketFactory(TaskScheduler scheduler, MetadataFactory metadataFactory, PacketEventsAPI<Plugin> packetEvents) {
+        super(scheduler, metadataFactory, packetEvents);
     }
 
     @Override
diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_19PacketFactory.java b/plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_19PacketFactory.java
index f7a4163..0108060 100644
--- a/plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_19PacketFactory.java
+++ b/plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_19PacketFactory.java
@@ -1,5 +1,6 @@
 package lol.pyr.znpcsplus.packets;
 
+import com.github.retrooper.packetevents.PacketEventsAPI;
 import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes;
 import com.github.retrooper.packetevents.protocol.player.GameMode;
 import com.github.retrooper.packetevents.protocol.player.UserProfile;
@@ -11,13 +12,14 @@ import lol.pyr.znpcsplus.metadata.MetadataFactory;
 import lol.pyr.znpcsplus.scheduling.TaskScheduler;
 import net.kyori.adventure.text.Component;
 import org.bukkit.entity.Player;
+import org.bukkit.plugin.Plugin;
 
 import java.util.EnumSet;
 import java.util.concurrent.CompletableFuture;
 
 public class V1_19PacketFactory extends V1_14PacketFactory {
-    public V1_19PacketFactory(TaskScheduler scheduler, MetadataFactory metadataFactory) {
-        super(scheduler, metadataFactory);
+    public V1_19PacketFactory(TaskScheduler scheduler, MetadataFactory metadataFactory, PacketEventsAPI<Plugin> packetEvents) {
+        super(scheduler, metadataFactory, packetEvents);
     }
 
     @Override
diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_8PacketFactory.java b/plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_8PacketFactory.java
index f1e7391..57e8013 100644
--- a/plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_8PacketFactory.java
+++ b/plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_8PacketFactory.java
@@ -1,6 +1,6 @@
 package lol.pyr.znpcsplus.packets;
 
-import com.github.retrooper.packetevents.PacketEvents;
+import com.github.retrooper.packetevents.PacketEventsAPI;
 import com.github.retrooper.packetevents.protocol.entity.data.EntityData;
 import com.github.retrooper.packetevents.protocol.entity.type.EntityType;
 import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes;
@@ -20,6 +20,7 @@ import lol.pyr.znpcsplus.util.ZLocation;
 import net.kyori.adventure.text.Component;
 import net.kyori.adventure.text.format.NamedTextColor;
 import org.bukkit.entity.Player;
+import org.bukkit.plugin.Plugin;
 
 import java.util.*;
 import java.util.concurrent.CompletableFuture;
@@ -27,10 +28,12 @@ import java.util.concurrent.CompletableFuture;
 public class V1_8PacketFactory implements PacketFactory {
     protected final TaskScheduler scheduler;
     protected final MetadataFactory metadataFactory;
+    protected final PacketEventsAPI<Plugin> packetEvents;
 
-    public V1_8PacketFactory(TaskScheduler scheduler, MetadataFactory metadataFactory) {
+    public V1_8PacketFactory(TaskScheduler scheduler, MetadataFactory metadataFactory, PacketEventsAPI<Plugin> packetEvents) {
         this.scheduler = scheduler;
         this.metadataFactory = metadataFactory;
+        this.packetEvents = packetEvents;
     }
 
     @Override
@@ -50,7 +53,7 @@ public class V1_8PacketFactory implements PacketFactory {
     public void spawnEntity(Player player, PacketEntity entity, PropertyHolder properties) {
         ZLocation location = entity.getLocation();
         EntityType type = entity.getType();
-        ClientVersion clientVersion = PacketEvents.getAPI().getServerManager().getVersion().toClientVersion();
+        ClientVersion clientVersion = packetEvents.getServerManager().getVersion().toClientVersion();
         sendPacket(player, type.getLegacyId(clientVersion) == -1 ?
                 new WrapperPlayServerSpawnLivingEntity(entity.getEntityId(), entity.getUuid(), type, location.toVector3d(),
                         location.getYaw(), location.getPitch(), location.getPitch(), new Vector3d(), Collections.emptyList()) :
@@ -70,6 +73,7 @@ public class V1_8PacketFactory implements PacketFactory {
     public void teleportEntity(Player player, PacketEntity entity) {
         ZLocation location = entity.getLocation();
         sendPacket(player, new WrapperPlayServerEntityTeleport(entity.getEntityId(), location.toVector3d(), location.getYaw(), location.getPitch(), true));
+        if (entity.getType() == EntityTypes.PLAYER) sendPacket(player, new WrapperPlayServerEntityHeadLook(entity.getEntityId(), location.getYaw()));
     }
 
     @Override
@@ -128,11 +132,11 @@ public class V1_8PacketFactory implements PacketFactory {
 
     @Override
     public void sendMetadata(Player player, PacketEntity entity, List<EntityData> data) {
-        PacketEvents.getAPI().getPlayerManager().sendPacket(player, new WrapperPlayServerEntityMetadata(entity.getEntityId(), data));
+        packetEvents.getPlayerManager().sendPacket(player, new WrapperPlayServerEntityMetadata(entity.getEntityId(), data));
     }
 
     protected void sendPacket(Player player, PacketWrapper<?> packet) {
-        PacketEvents.getAPI().getPlayerManager().sendPacket(player, packet);
+        packetEvents.getPlayerManager().sendPacket(player, packet);
     }
 
     protected CompletableFuture<UserProfile> skinned(Player player, PropertyHolder properties, UserProfile profile) {
diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_9PacketFactory.java b/plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_9PacketFactory.java
index 86c8236..4507894 100644
--- a/plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_9PacketFactory.java
+++ b/plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_9PacketFactory.java
@@ -1,5 +1,6 @@
 package lol.pyr.znpcsplus.packets;
 
+import com.github.retrooper.packetevents.PacketEventsAPI;
 import com.github.retrooper.packetevents.protocol.entity.data.EntityData;
 import lol.pyr.znpcsplus.api.entity.PropertyHolder;
 import lol.pyr.znpcsplus.entity.EntityPropertyImpl;
@@ -7,12 +8,13 @@ import lol.pyr.znpcsplus.entity.PacketEntity;
 import lol.pyr.znpcsplus.metadata.MetadataFactory;
 import lol.pyr.znpcsplus.scheduling.TaskScheduler;
 import org.bukkit.entity.Player;
+import org.bukkit.plugin.Plugin;
 
 import java.util.Map;
 
 public class V1_9PacketFactory extends V1_8PacketFactory {
-    public V1_9PacketFactory(TaskScheduler scheduler, MetadataFactory metadataFactory) {
-        super(scheduler, metadataFactory);
+    public V1_9PacketFactory(TaskScheduler scheduler, MetadataFactory metadataFactory, PacketEventsAPI<Plugin> packetEvents) {
+        super(scheduler, metadataFactory, packetEvents);
     }
 
     @Override