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;

    static {
        try {
            entityThrownTridentFieldAw = entityThrownTrident.getDeclaredField("aw");
            entityThrownTridentFieldAw.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);
        }
    }
}

@@ -506,16 +506,69 @@
 FieldAw;
+%0A    private static Field entityThrownTridentFieldAx;
 %0A%0A    st
@@ -729,24 +729,169 @@
 ible(true);%0A
+            entityThrownTridentFieldAx = entityThrownTrident.getDeclaredField(%22ax%22);%0A            entityThrownTridentFieldAx.setAccessible(true);%0A
         %7D ca
@@ -1778,32 +1778,32 @@
 opy(itemStack);%0A
-
             enti
@@ -1962,11 +1962,759 @@
  %7D%0A    %7D
+%0A%0A    public static boolean getTridentDealtDamage(Trident entity) %7B%0A        try %7B%0A            EntityThrownTrident thrownTrident = (EntityThrownTrident) ((CraftEntity) entity).getHandle();%0A            return (boolean) entityThrownTridentFieldAx.get(thrownTrident);%0A        %7D catch (IllegalAccessException e) %7B%0A            throw new RuntimeException(e);%0A        %7D%0A    %7D%0A%0A    public static void setTridentDealtDamage(Trident entity, boolean dealtDamage) %7B%0A        try %7B%0A            EntityThrownTrident thrownTrident = (EntityThrownTrident) ((CraftEntity) entity).getHandle();%0A            entityThrownTridentFieldAx.set(thrownTrident, dealtDamage);%0A        %7D catch (IllegalAccessException e) %7B%0A            throw new RuntimeException(e);%0A        %7D%0A    %7D
 %0A%7D%0A

Add trident damage dealt accessor