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. If no
* custom controller is set, this method returns a new
* {@link DefaultEntityController} instance.
*
* @return Entity Controller
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public EntityController<CommonEntity<T>> getController() {
EntityHook hook = EntityHook.get(getHandle(), EntityHook.class);
final EntityController controller;
if (hook == null) {
controller = new DefaultEntityController();
controller.bind(this, false);
} else if (hook.hasController()) {
controller = hook.getController();
} else {
// This should not occur. Return some dummy controller for now.
controller = new DefaultEntityController();
controller.bind(this, false);
}
return controller;
}
use of com.bergerkiller.bukkit.common.internal.hooks.EntityHook in project BKCommonLib by bergerhealer.
the class CommonEntity method get.
/**
* Obtains a (new) {@link CommonEntity} instance providing additional
* methods for the Entity specified. This method never returns null, unless
* the input Entity is null.
*
* @param entity to get a CommonEntity for
* @return a (new) CommonEntity instance for the Entity
*/
@SuppressWarnings("unchecked")
public static <T extends org.bukkit.entity.Entity> CommonEntity<T> get(T entity) {
if (entity == null) {
return null;
}
final Object handle = HandleConversion.toEntityHandle(entity);
if (handle == null) {
return null;
}
EntityHook hook = EntityHook.get(handle, EntityHook.class);
if (hook != null && hook.hasController()) {
return (CommonEntity<T>) hook.getController().getEntity();
}
return CommonEntityType.byNMSEntity(handle).createCommonEntity(entity);
}
use of com.bergerkiller.bukkit.common.internal.hooks.EntityHook in project BKCommonLib by bergerhealer.
the class CommonEntity method clearControllers.
/**
* Clears possible network or Entity controllers from the Entity. This
* should be called when a specific Entity should default back to all
* default behaviours.
*
* @param entity to clear the controllers of
*/
public static void clearControllers(org.bukkit.entity.Entity entity) {
CommonEntity<?> commonEntity = get(entity);
Object oldInstance = commonEntity.getHandle();
EntityHook oldHook = EntityHook.get(oldInstance, EntityHook.class);
// Detach controller and undo hook Entity replacement
if (oldHook != null) {
try {
CommonEntityController<?> controller = oldHook.getController();
if (controller != null) {
controller.onDetached();
}
} catch (Throwable t) {
Logging.LOGGER.log(Level.SEVERE, "Failed to handle controller detachment:");
t.printStackTrace();
}
try {
// Transfer data and replace
Object newInstance = EntityHook.unhook(oldInstance);
commonEntity.replaceEntity(EntityHandle.createHandle(newInstance));
} catch (Throwable t) {
Logging.LOGGER.log(Level.SEVERE, "Failed to unhook Common Entity Controller:");
t.printStackTrace();
}
}
// Unhook network controller
EntityNetworkController<?> controller = commonEntity.getNetworkController();
if (controller != null && !(controller instanceof DefaultEntityNetworkController)) {
commonEntity.setNetworkController(new DefaultEntityNetworkController());
}
}
use of com.bergerkiller.bukkit.common.internal.hooks.EntityHook in project BKCommonLib by bergerhealer.
the class CommonEntityType method createNMSHookEntity.
public <T extends Entity> CommonEntity<T> createNMSHookEntity(Location location) {
World world = location.getWorld();
double x = location.getX();
double y = location.getY();
double z = location.getZ();
EntityHook hook = new EntityHook();
hook.setStack(new Throwable());
Object handle = hook.constructInstance(this.nmsType.getType(), new Class<?>[] { NMSWorld.T.getType(), double.class, double.class, double.class }, new Object[] { Conversion.toWorldHandle.convert(world), x, y, z });
CommonEntity<T> entity = createCommonEntityFromHandle(handle);
entity.loc.set(entity.last.set(location));
DefaultEntityController controller = new DefaultEntityController();
controller.bind(entity, false);
return entity;
}
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
*/
@SuppressWarnings("unchecked")
public final void bind(CommonEntity<?> entity) {
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);
this.onAttached();
}
}
Aggregations