package cat.nyaa.nyaacore.utils;

import net.minecraft.server.v1_13_R2.EntityThrownTrident;
import org.bukkit.craftbukkit.v1_13_R2.entity.CraftEntity;
import org.bukkit.craftbukkit.v1_13_R2.inventory.CraftItemStack;
import org.bukkit.entity.Trident;
import org.bukkit.inventory.ItemStack;

import java.lang.reflect.Field;

public final class TridentUtils {

    private static Class<?> entityThrownTrident = ReflectionUtils.getNMSClass("EntityThrownTrident");

    private static Field entityThrownTridentFieldAw;
    private static Field entityThrownTridentFieldAx;

    static {
        try {
            entityThrownTridentFieldAw = entityThrownTrident.getDeclaredField("aw");
            entityThrownTridentFieldAw.setAccessible(true);
            entityThrownTridentFieldAx = entityThrownTrident.getDeclaredField("ax");
            entityThrownTridentFieldAx.setAccessible(true);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
    }

    public static ItemStack getTridentItemStack(Trident entity) {
        try {
            EntityThrownTrident thrownTrident = (EntityThrownTrident) ((CraftEntity) entity).getHandle();
            net.minecraft.server.v1_13_R2.ItemStack nmsItemStack = (net.minecraft.server.v1_13_R2.ItemStack) entityThrownTridentFieldAw.get(thrownTrident);
            return CraftItemStack.asBukkitCopy(nmsItemStack);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public static void setTridentItemStack(Trident entity, ItemStack itemStack) {
        try {
            EntityThrownTrident thrownTrident = (EntityThrownTrident) ((CraftEntity) entity).getHandle();
            net.minecraft.server.v1_13_R2.ItemStack nmsItemStack = CraftItemStack.asNMSCopy(itemStack);
            entityThrownTridentFieldAw.set(thrownTrident, nmsItemStack);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public static boolean getTridentDealtDamage(Trident entity) {
        try {
            EntityThrownTrident thrownTrident = (EntityThrownTrident) ((CraftEntity) entity).getHandle();
            return (boolean) entityThrownTridentFieldAx.get(thrownTrident);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public static void setTridentDealtDamage(Trident entity, boolean dealtDamage) {
        try {
            EntityThrownTrident thrownTrident = (EntityThrownTrident) ((CraftEntity) entity).getHandle();
            entityThrownTridentFieldAx.set(thrownTrident, dealtDamage);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
}

@@ -508,236 +508,38 @@
 eldA
-w;%0A    private static Field entityThrownTridentFieldAx;%0A%0A    static %7B%0A        try %7B%0A            entityThrownTridentFieldAw = entityThrownTrident.getDeclaredField(%22aw%22);%0A            entityThrownTridentFieldAw.setAccessible(true);
+x;%0A%0A    static %7B%0A        try %7B
 %0A   
@@ -839,34 +839,16 @@
 tity) %7B%0A
-        try %7B%0A    
         
@@ -941,36 +941,32 @@
 ndle();%0A        
-    
 net.minecraft.se
@@ -1008,101 +1008,31 @@
 k = 
-(net.minecraft.server.v1_13_R2.ItemStack) entityThrownTridentFieldAw.get(thrownT
+thrownTrident.t
 rident
-)
 ;%0A
-    
     
@@ -1089,106 +1089,8 @@
 k);%0A
-        %7D catch (IllegalAccessException e) %7B%0A            throw new RuntimeException(e);%0A        %7D%0A
     
@@ -1174,34 +1174,16 @@
 tack) %7B%0A
-        try %7B%0A    
         
@@ -1288,274 +1288,68 @@
     
-    net.minecraft.server.v1_13_R2.ItemStack nmsItemStack = CraftItemStack.asNMSCopy(itemStack);%0A            entityThrownTridentFieldAw.set(thrownTrident, nmsItemStack);%0A        %7D catch (IllegalAccessException e) %7B%0A            throw new RuntimeException(e);%0A        %7D
+thrownTrident.trident = CraftItemStack.asNMSCopy(itemStack);
 %0A   

fix trident utils