package de.craften.plugins.managedentities;

import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.plugin.Plugin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * A manager for entities.
 */
public class EntityManager {
    private final Plugin plugin;
    private final Map<UUID, ManagedEntityBase> entities = new HashMap<>();

    public EntityManager(Plugin plugin) {
        this.plugin = plugin;

        plugin.getServer().getScheduler().runTaskTimer(plugin, new Runnable() {
            @Override
            public void run() {
                for (ManagedEntityBase entity : new ArrayList<>(entities.values())) {
                    entity.tick();
                }
            }
        }, 0, 1);
    }

    public <T extends Entity> ManagedEntity<T> spawn(Location location, Class<T> type) {
        ManagedEntityBase<T> entity = new GenericManagedEntity<>(location, type);
        entities.put(entity.getUniqueId(), entity);
        return entity;
    }

    /**
     * Gets the managed entity for the given entity, if it is managed.
     *
     * @param entity entity
     * @param <T>    type of the entity
     * @return managed entity for the given entity or null if the given entity is not managed
     */
    public <T extends Entity> ManagedEntity<T> getEntity(T entity) {
        return entities.get(entity.getUniqueId());
    }

    /**
     * Removes all entities.
     */
    public void removeAll() {
        for (ManagedEntity entity : entities.values()) {
            entity.remove();
        }
        entities.clear();
    }
}

@@ -842,13 +842,14 @@
 %3CT%3E 
-spawn
+create
 (Loc
@@ -963,16 +963,215 @@
  type);%0A
+        addEntity(entity);%0A        return entity;%0A    %7D%0A%0A    /**%0A     * Adds the given entity.%0A     *%0A     * @param entity entity to add%0A     */%0A    public void addEntity(ManagedEntityBase entity) %7B%0A
         
@@ -1218,33 +1218,276 @@
 y);%0A    
+%7D%0A%0A
     
-return entity
+/**%0A     * Reoves the given entity from this manager and from the world.%0A     *%0A     * @param entity entity to remove%0A     */%0A    public void removeEntity(ManagedEntityBase entity) %7B%0A        entities.remove(entity.getUniqueId());%0A        entity.remove()
 ;%0A    %7D%0A

Add methods to add and remove managed entities manually.