package de.craften.plugins.managedentities.util.nms;

import net.minecraft.server.v1_8_R3.Entity;
import net.minecraft.server.v1_8_R3.EntityLiving;
import net.minecraft.server.v1_8_R3.NBTTagCompound;
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftEntity;
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftLivingEntity;
import org.bukkit.entity.LivingEntity;

/**
 * NMS utility methods for entities.
 */
public class NmsEntityUtil {
    /**
     * Disables the AI for the given entity.
     *
     * @param entity entity
     */
    public static void disableAi(LivingEntity entity) {
        Entity nmsEntity = ((CraftEntity) entity).getHandle();
        NBTTagCompound tag = nmsEntity.getNBTTag();
        if (tag == null) {
            tag = new NBTTagCompound();
        }
        nmsEntity.c(tag);
        tag.setInt("NoAI", 1);
        nmsEntity.f(tag);
    }

    /**
     * Sets whether the entity is invulnerable. This only blocks players in survival or adventure mode from hurting
     * the entity.
     *
     * @param entity       entity
     * @param invulnerable whether the entity should be invulnerable
     */
    public static void setInvulnerable(LivingEntity entity, boolean invulnerable) {
        Entity nmsEntity = ((CraftEntity) entity).getHandle();
        NBTTagCompound tag = nmsEntity.getNBTTag();
        if (tag == null) {
            tag = new NBTTagCompound();
        }
        nmsEntity.c(tag);
        tag.setInt("Invulnerable", invulnerable ? 1 : 0);
        nmsEntity.f(tag);
    }

    public static void setHeadRotation(LivingEntity entity, float yaw, float pitch) {
        EntityLiving nmsEntity = ((CraftLivingEntity) entity).getHandle();
        nmsEntity.f(clampYaw(yaw));
        nmsEntity.g(pitch);
    }

    private static float clampYaw(float yaw) {
        while (yaw < -180.0F) {
            yaw += 360.0F;
        }

        while (yaw >= 180.0F) {
            yaw -= 360.0F;
        }

        return yaw;
    }
}

@@ -527,62 +527,447 @@
    *
-/%0A    public static void disableAi(LivingEntity entity
+ @deprecated use %7B@link #setAi(LivingEntity, boolean)%7D instead%0A     */%0A    @Deprecated%0A    public static void disableAi(LivingEntity entity) %7B%0A        setAi(entity, false);%0A    %7D%0A%0A    /**%0A     * Sets whether the AI for the given entity is enabled. This also toggles gravity.%0A     *%0A     * @param entity entity%0A     * @param hasAi  whether the entity should have an AI%0A     */%0A    public static void setAi(LivingEntity entity, boolean hasAi
 ) %7B%0A
@@ -1210,16 +1210,28 @@
 (%22NoAI%22,
+ hasAi ? 0 :
  1);%0A   
@@ -1910,24 +1910,210 @@
 ag);%0A    %7D%0A%0A
+    /**%0A     * Sets the head rotation of the given entity.%0A     *%0A     * @param entity entity%0A     * @param yaw    head yaw, in degree%0A     * @param pitch  head pitch, in degree%0A     */%0A
     public s

Add setAi utility method and deprecate disableAi method. Add javadoc.