001/*
002 * Trident - A Multithreaded Server Alternative
003 * Copyright 2014 The TridentSDK Team
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *    http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package net.tridentsdk.server.entity;
019
020import net.tridentsdk.Trident;
021import net.tridentsdk.base.Position;
022import net.tridentsdk.docs.InternalUseOnly;
023import net.tridentsdk.entity.Entity;
024import net.tridentsdk.util.TridentLogger;
025
026import javax.annotation.concurrent.ThreadSafe;
027import java.util.Iterator;
028import java.util.Map;
029import java.util.concurrent.ConcurrentHashMap;
030
031/**
032 * Manages server entities and provides registration procedures
033 *
034 * @author The TridentSDK Team
035 */
036@ThreadSafe
037public final class EntityHandler {
038    private static final Map<Integer, Entity> entities = new ConcurrentHashMap<>();
039
040    @InternalUseOnly
041    private EntityHandler() {
042        if (!Trident.isTrident())
043            TridentLogger.get().error(new IllegalAccessException("EntityManager can only be initalized by TridentSDK!"));
044    }
045
046    /**
047     * Constructs the EntityManager for use by the server ONLY
048     *
049     * <p>In other words, DON'T USE IT</p>
050     */
051    public static EntityHandler create() {
052        return new EntityHandler();
053    }
054
055    /**
056     * Starts entity management and tracks the entity
057     *
058     * @param entity the entity to manage
059     */
060    public void register(Entity entity) {
061        entities.put(entity.entityId(), entity);
062    }
063
064    /**
065     * Removes the entity from management
066     *
067     * @param entity the entity to remove
068     */
069    public void removeEntity(Entity entity) {
070        entities.remove(entity.entityId());
071    }
072
073    /**
074     * Tracks the movement of the entity, not for teleportation
075     *
076     * @param entity the entity to track
077     * @param from   the original location
078     * @param to     the new location
079     */
080    public void trackMovement(Entity entity, Position from, Position to) {
081    }
082
083    /**
084     * Gets the entity with the given ID
085     *
086     * @param id the ID to find the entity by
087     * @return the entity with the ID specified
088     */
089    public Entity entityBy(int id) {
090        return entities.get(id);
091    }
092
093    /**
094     * Gets all entities with the given type class
095     *
096     * @param type the type to search for entities
097     * @param <T>  the entity type
098     * @return the list of entities with the specified type
099     */
100    public <T extends Entity> Iterator<Entity> entities(final Class<T> type) {
101        return entities.values().stream()
102                .filter((e) -> type.getClass().equals(e.getClass()))
103                .iterator();
104    }
105}