Search in sources :

Example 6 with EntityHook

use of com.bergerkiller.bukkit.common.internal.hooks.EntityHook in project BKCommonLib by bergerhealer.

the class EntityController method bind.

/**
 * Binds this Entity Controller to an Entity. This is called from elsewhere,
 * and should be ignored entirely.
 *
 * @param entity to bind with
 * @param handleAttachment whether to fire {@link #onAttached()}
 */
@SuppressWarnings("unchecked")
public final void bind(CommonEntity<?> entity, boolean handleAttachment) {
    if (entity == null && this.hook == null) {
        throw new RuntimeException("WTF");
    }
    if (entity != null && entity.getWorld() == null) {
        throw new RuntimeException("Can not bind to an Entity that has no world set");
    }
    if (this.entity != null) {
        this.onDetached();
    }
    this.entity = (T) entity;
    if (this.entity != null) {
        this.hook = EntityHook.get(this.entity.getHandle(), EntityHook.class);
        if (this.hook == null) {
            this.hook = new EntityHook();
            this.hook.mock(this.entity.getHandle());
        }
        this.hook.setController(this);
        if (handleAttachment) {
            this.onAttached();
        }
    }
}
Also used : EntityHook(com.bergerkiller.bukkit.common.internal.hooks.EntityHook)

Example 7 with EntityHook

use of com.bergerkiller.bukkit.common.internal.hooks.EntityHook in project BKCommonLib by bergerhealer.

the class CommonEntity method getController.

/**
 * Gets the Entity Controller currently assigned to this Entity, checking to make sure
 * the controller is of a certain Class type. When no custom controller is set, or the current
 * controller can not be assigned to the type specified, <i>null</i> is returned instead.<br>
 * <br>
 * This method offers performance benefits over {@link #getController()} by not instantiating a new
 * default controller when no controller is set.
 *
 * @param controllerType to get
 * @return the controller of controllerType, or <i>null</i> if not found
 */
@SuppressWarnings("unchecked")
public <C extends EntityController<?>> C getController(Class<? extends C> controllerType) {
    EntityHook hook = EntityHook.get(getHandle(), EntityHook.class);
    if (hook == null || !hook.hasController()) {
        return null;
    }
    EntityController<?> controller = hook.getController();
    if (controllerType.isAssignableFrom(controller.getClass())) {
        return (C) controller;
    } else {
        return null;
    }
}
Also used : EntityHook(com.bergerkiller.bukkit.common.internal.hooks.EntityHook)

Example 8 with EntityHook

use of com.bergerkiller.bukkit.common.internal.hooks.EntityHook in project BKCommonLib by bergerhealer.

the class CommonEntityType method createCommonEntityNull.

public <T extends Entity> CommonEntity<T> createCommonEntityNull() {
    EntityHook hook = new EntityHook();
    hook.setStack(new Throwable());
    Object handle = hook.createInstance(this.nmsType.getType());
    CommonEntity<T> entity = createCommonEntityFromHandle(handle);
    DefaultEntityController controller = new DefaultEntityController();
    controller.bind(entity, false);
    return entity;
}
Also used : EntityHook(com.bergerkiller.bukkit.common.internal.hooks.EntityHook) DefaultEntityController(com.bergerkiller.bukkit.common.controller.DefaultEntityController)

Example 9 with EntityHook

use of com.bergerkiller.bukkit.common.internal.hooks.EntityHook in project BKCommonLib by bergerhealer.

the class CommonEntityType method createNMSHookFromEntity.

public Object createNMSHookFromEntity(CommonEntity<?> entity) {
    EntityHook hook = new EntityHook();
    hook.setStack(new Throwable());
    return hook.hook(entity.getHandle());
}
Also used : EntityHook(com.bergerkiller.bukkit.common.internal.hooks.EntityHook)

Example 10 with EntityHook

use of com.bergerkiller.bukkit.common.internal.hooks.EntityHook in project BKCommonLib by bergerhealer.

the class CommonPlugin method notifyRemovedFromServer.

public void notifyRemovedFromServer(org.bukkit.World world, org.bukkit.entity.Entity e, boolean removeFromChangeSet) {
    // Also remove from the set tracking these changes
    if (removeFromChangeSet) {
        this.entitiesRemovedFromServer.remove(e);
    }
    // Remove from maps
    Iterator<SoftReference<EntityMap>> iter = this.maps.iterator();
    while (iter.hasNext()) {
        EntityMap map = iter.next().get();
        if (map == null) {
            iter.remove();
        } else {
            map.remove(e);
        }
    }
    // Fire events
    if (CommonUtil.hasHandlers(EntityRemoveFromServerEvent.getHandlerList())) {
        CommonUtil.callEvent(new EntityRemoveFromServerEvent(e));
    }
    // Remove any entity controllers set for the entities that were removed
    EntityHook hook = EntityHook.get(HandleConversion.toEntityHandle(e), EntityHook.class);
    if (hook != null && hook.hasController()) {
        hook.getController().getEntity().setController(null);
    }
}
Also used : EntityHook(com.bergerkiller.bukkit.common.internal.hooks.EntityHook) SoftReference(java.lang.ref.SoftReference) EntityMap(com.bergerkiller.bukkit.common.collections.EntityMap) EntityRemoveFromServerEvent(com.bergerkiller.bukkit.common.events.EntityRemoveFromServerEvent)

Aggregations

EntityHook (com.bergerkiller.bukkit.common.internal.hooks.EntityHook)10 DefaultEntityController (com.bergerkiller.bukkit.common.controller.DefaultEntityController)2 EntityMap (com.bergerkiller.bukkit.common.collections.EntityMap)1 EntityRemoveFromServerEvent (com.bergerkiller.bukkit.common.events.EntityRemoveFromServerEvent)1 NMSWorld (com.bergerkiller.reflection.net.minecraft.server.NMSWorld)1 SoftReference (java.lang.ref.SoftReference)1 World (org.bukkit.World)1