package de.craften.plugins.rpgplus.components.entitymanager;

import de.craften.plugins.managedentities.ManagedEntityBase;
import de.craften.plugins.managedentities.behavior.SecondNameBehavior;
import de.craften.plugins.managedentities.behavior.VisibleNameBehavior;
import org.bukkit.Location;
import org.bukkit.entity.Creature;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Monster;
import org.bukkit.entity.Player;

/**
 * A basic managed entity without any special logic.
 */
public abstract class RpgPlusEntity<T extends Entity> extends ManagedEntityBase<T> {
    private boolean isTakingDamage = true;
    private boolean nameVisible = true;

    public RpgPlusEntity(Location location) {
        super(location);

        addBehavior(new VisibleNameBehavior());
        addBehavior(new SecondNameBehavior());
    }

    public String getName() {
        return getProperty(VisibleNameBehavior.NAME_PROPERTY_KEY);
    }

    public void setName(String name) {
        setProperty(VisibleNameBehavior.NAME_PROPERTY_KEY, name);
    }

    public String getSecondName() {
        return getProperty(SecondNameBehavior.NAME_PROPERTY_KEY);
    }

    public void setSecondName(String secondName) {
        setProperty(SecondNameBehavior.NAME_PROPERTY_KEY, secondName);
    }

    public boolean isTakingDamage() {
        return isTakingDamage;
    }

    public void setTakingDamage(boolean isTakingDamage) {
        this.isTakingDamage = isTakingDamage;
        //TODO this has no effect right now
    }

    public boolean isNameVisible() {
        return nameVisible;
    }

    public void setNameVisible(boolean nameVisible) {
        if (nameVisible != this.nameVisible) {
            this.nameVisible = nameVisible;
            if (nameVisible) {
                removeBehavior(getBehaviors(VisibleNameBehavior.class).iterator().next());
            } else {
                addBehavior(new VisibleNameBehavior());
            }
        }
    }

    public Object getTarget() {
        Entity entity = getEntity();
        if (entity instanceof Creature) {
            return ((Creature) entity).getTarget();
        }
        return null;
    }

    public void setTarget(Player player) {
        Entity entity = getEntity();
        if (entity instanceof Creature) {
            ((Monster) entity).setTarget(player);
        }
        //TODO remember the target if the entity is not spawned yet
    }

    @Override
    public void teleport(Location location) {
        Location old = getLocation();
        if (old != null) {
            super.teleport(location.clone().setDirection(location.toVector().subtract(old.toVector())));
        } else {
            super.teleport(location);
        }
    }
}

@@ -270,95 +270,65 @@
 ort 
-org.bukkit.Location;%0Aimport org.bukkit.entity.Creature;%0Aimport org.bukkit.entity.
+de.craften.plugins.managedentities.util.nms.Nms
 Entity
+Util
 ;%0Aim
@@ -347,22 +347,16 @@
 kit.
-entity.Monster
+Location
 ;%0Aim
@@ -378,22 +378,17 @@
 .entity.
-Player
+*
 ;%0A%0A/**%0A 
@@ -778,32 +778,260 @@
 vior());%0A    %7D%0A%0A
+    @Override%0A    public void spawn() %7B%0A        super.spawn();%0A%0A        if (getEntity() instanceof LivingEntity && !isTakingDamage) %7B%0A            NmsEntityUtil.setInvulnerable((LivingEntity) getEntity(), true);%0A        %7D%0A    %7D%0A%0A
     public Strin
@@ -1648,24 +1648,25 @@
 Damage;%0A
+%0A
         
 //TODO t
@@ -1661,43 +1661,148 @@
     
-//TODO this has no effect right now
+if (getEntity() instanceof LivingEntity) %7B%0A            NmsEntityUtil.setInvulnerable((LivingEntity) getEntity(), !isTakingDamage);%0A        %7D
 %0A   

Make invulnerable entities actually invulnerable.