Search in sources :

Example 1 with CraftInventoryHandle

use of com.bergerkiller.generated.org.bukkit.craftbukkit.inventory.CraftInventoryHandle in project BKCommonLib by bergerhealer.

the class CommonEntity method replaceEntity.

@SuppressWarnings("unchecked")
private void replaceEntity(final EntityHandle newInstance) {
    final EntityHandle oldInstance = this.handle;
    if (oldInstance.getRaw() == newInstance.getRaw()) {
        throw new RuntimeException("Can not replace an entity with itself!");
    }
    // Reset entity state
    oldInstance.setDead(true);
    newInstance.setDead(false);
    oldInstance.setValid(false);
    newInstance.setValid(true);
    // *** Bukkit Entity ***
    CraftEntityHandle craftEntity = CraftEntityHandle.createHandle(this.entity);
    craftEntity.setHandle(newInstance);
    if (entity instanceof InventoryHolder) {
        Inventory inv = ((InventoryHolder) entity).getInventory();
        if (CraftInventoryHandle.T.isAssignableFrom(inv)) {
            CraftInventoryHandle cInv = CraftInventoryHandle.createHandle(inv);
            if (IInventoryHandle.T.isAssignableFrom(newInstance.getRaw())) {
                IInventoryHandle iinvHandle = IInventoryHandle.createHandle(newInstance.getRaw());
                cInv.setHandleField(iinvHandle);
            }
        }
    }
    // *** Give the old entity a new Bukkit Entity ***
    oldInstance.setBukkitEntityField(CraftEntityHandle.createCraftEntity(Bukkit.getServer(), oldInstance));
    this.handle = newInstance;
    // *** Replace entity in passenger and vehicle fields ***
    EntityHandle vehicle = newInstance.getVehicle();
    if (vehicle != null) {
        replaceInList(vehicle.getPassengers(), newInstance);
    }
    if (newInstance.getPassengers() != null) {
        for (EntityHandle passenger : newInstance.getPassengers()) {
            if (oldInstance.getRaw() == passenger.getVehicle().getRaw()) {
                passenger.setVehicle(newInstance);
            }
        }
    }
    // *** DataWatcher field of the old Entity ***
    Object dataWatcher = EntityHandle.T.datawatcherField.raw.get(newInstance.getRaw());
    if (dataWatcher != null) {
        DataWatcherHandle.T.owner.set(dataWatcher, newInstance);
    }
    // *** Perform further replacement all over the place in the server ***
    replaceEntityInServer(oldInstance, newInstance);
    // *** Repeat the replacement in the server the next tick to make sure nothing lingers ***
    CommonUtil.nextTick(new Runnable() {

        @Override
        public void run() {
            replaceEntityInServer(oldInstance, newInstance);
        }
    });
    // *** Make sure a controller is set when hooked ***
    if (this.isHooked()) {
        DefaultEntityController controller = new DefaultEntityController();
        controller.bind(this);
    }
}
Also used : CraftEntityHandle(com.bergerkiller.generated.org.bukkit.craftbukkit.entity.CraftEntityHandle) IInventoryHandle(com.bergerkiller.generated.net.minecraft.server.IInventoryHandle) CraftEntityHandle(com.bergerkiller.generated.org.bukkit.craftbukkit.entity.CraftEntityHandle) EntityHandle(com.bergerkiller.generated.net.minecraft.server.EntityHandle) InventoryHolder(org.bukkit.inventory.InventoryHolder) CraftInventoryHandle(com.bergerkiller.generated.org.bukkit.craftbukkit.inventory.CraftInventoryHandle) Inventory(org.bukkit.inventory.Inventory)

Aggregations

EntityHandle (com.bergerkiller.generated.net.minecraft.server.EntityHandle)1 IInventoryHandle (com.bergerkiller.generated.net.minecraft.server.IInventoryHandle)1 CraftEntityHandle (com.bergerkiller.generated.org.bukkit.craftbukkit.entity.CraftEntityHandle)1 CraftInventoryHandle (com.bergerkiller.generated.org.bukkit.craftbukkit.inventory.CraftInventoryHandle)1 Inventory (org.bukkit.inventory.Inventory)1 InventoryHolder (org.bukkit.inventory.InventoryHolder)1