Search in sources :

Example 1 with EntityHandle

use of com.bergerkiller.generated.net.minecraft.server.EntityHandle in project BKCommonLib by bergerhealer.

the class ExtendedEntity method setPassengersSilent.

/**
     * Sets the passengers of this Vehicle without raising any events
     *
     * @param newPassenger to set to
     */
public void setPassengersSilent(List<org.bukkit.entity.Entity> newPassengers) {
    final EntityHandle handle = this.handle;
    // Generate a difference view between the expected list of passengers, and the current
    List<EntityHandle> removedPassengers = new ArrayList<EntityHandle>(handle.getPassengers().size());
    List<org.bukkit.entity.Entity> keptPassengers = new ArrayList<org.bukkit.entity.Entity>(newPassengers.size());
    for (EntityHandle oldPassenger : handle.getPassengers()) {
        boolean found = false;
        for (org.bukkit.entity.Entity p : newPassengers) {
            if (oldPassenger.getRaw() == Conversion.toEntityHandle.convert(p)) {
                found = true;
                keptPassengers.add(p);
                break;
            }
        }
        if (!found) {
            removedPassengers.add(oldPassenger);
        }
    }
    // Remove vehicle information and passenger information for all removed passengers
    for (EntityHandle passenger : removedPassengers) {
        passenger.setVehicle(null);
        handle.getPassengers().remove(passenger);
    }
    // Add new passengers as required
    List<EntityHandle> currPassengers = handle.getPassengers();
    for (org.bukkit.entity.Entity p : newPassengers) {
        EntityHandle passengerHandle = EntityHandle.fromBukkit(p);
        if (!currPassengers.contains(passengerHandle)) {
            currPassengers.add(passengerHandle);
            passengerHandle.setVehicle(handle);
            // Send mount packet
            CommonPacket packet = PacketType.OUT_MOUNT.newInstance(entity, keptPassengers);
            PacketUtil.broadcastEntityPacket(entity, packet);
        }
    }
    CommonPacket packet = PacketType.OUT_MOUNT.newInstanceHandles(entity, currPassengers);
    PacketUtil.broadcastEntityPacket(entity, packet);
    // Synchronize entity tracker of the vehicle to make sure it does not try to synchronize a second time
    Object entry = WorldUtil.getTracker(entity.getWorld()).getEntry(entity);
    if (entry != null) {
        NMSEntityTrackerEntry.passengers.set(entry, newPassengers);
    }
}
Also used : ArrayList(java.util.ArrayList) CommonPacket(com.bergerkiller.bukkit.common.protocol.CommonPacket) EntityHandle(com.bergerkiller.generated.net.minecraft.server.EntityHandle)

Example 2 with EntityHandle

use of com.bergerkiller.generated.net.minecraft.server.EntityHandle in project BKCommonLib by bergerhealer.

the class CommonEventFactory method handleEntityMove.

/**
     * Fires Entity Move events for all entities that moved on the server
     */
public void handleEntityMove() {
    if (!CommonUtil.hasHandlers(EntityMoveEvent.getHandlerList())) {
        return;
    }
    for (World world : WorldUtil.getWorlds()) {
        entityMoveEntities.addAll(WorldHandle.fromBukkit(world).getEntityList());
    }
    for (EntityHandle entity : entityMoveEntities) {
        if (entity.getLocX() != entity.getLastX() || entity.getLocY() != entity.getLastY() || entity.getLocZ() != entity.getLastZ() || entity.getYaw() != entity.getLastYaw() || entity.getPitch() != entity.getLastPitch()) {
            entityMoveEvent.setEntity(entity);
            CommonUtil.callEvent(entityMoveEvent);
        }
    }
    entityMoveEntities.clear();
}
Also used : EntityHandle(com.bergerkiller.generated.net.minecraft.server.EntityHandle) World(org.bukkit.World)

Example 3 with EntityHandle

use of com.bergerkiller.generated.net.minecraft.server.EntityHandle 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)

Example 4 with EntityHandle

use of com.bergerkiller.generated.net.minecraft.server.EntityHandle in project BKCommonLib by bergerhealer.

the class CommonEntity method replaceInEntityTracker.

private static void replaceInEntityTracker(int entityId, EntityHandle newInstance) {
    final EntityTracker trackerMap = WorldUtil.getTracker(newInstance.getWorld().getWorld());
    Object entry = trackerMap.getEntry(entityId);
    if (entry != null) {
        EntityHandle tracker = EntityHandle.createHandle(NMSEntityTrackerEntry.tracker.getInternal(entry));
        if (tracker != null && tracker.getId() == newInstance.getId()) {
            NMSEntityTrackerEntry.tracker.setInternal(entry, newInstance.getRaw());
        }
        replaceInList((List<?>) NMSEntityTrackerEntry.passengers.getInternal(entry), newInstance);
    }
}
Also used : EntityTracker(com.bergerkiller.bukkit.common.wrappers.EntityTracker) NMSEntityTracker(com.bergerkiller.reflection.net.minecraft.server.NMSEntityTracker) CraftEntityHandle(com.bergerkiller.generated.org.bukkit.craftbukkit.entity.CraftEntityHandle) EntityHandle(com.bergerkiller.generated.net.minecraft.server.EntityHandle)

Example 5 with EntityHandle

use of com.bergerkiller.generated.net.minecraft.server.EntityHandle in project BKCommonLib by bergerhealer.

the class EntityMoveHandler method world_getCubes.

private List<AxisAlignedBBHandle> world_getCubes(EntityHandle entity, AxisAlignedBBHandle axisalignedbb) {
    // BKCommonLib edit: use cached list
    collisions_buffer.clear();
    // BKCommonLib start: replace world_getBlockCollisions call; use cached list instead
    //world.a(entity, axisalignedbb, false, arraylist);
    world_getBlockCollisions(entity, axisalignedbb, false);
    if (entity != null) {
        List<EntityHandle> list = entity.getWorld().getEntities(entity, axisalignedbb.growUniform(0.25D));
        for (int i = 0; i < list.size(); i++) {
            EntityHandle entity1 = list.get(i);
            if (!entity.isInSameVehicle(entity1)) {
                // BKCommonLib start: block collision event handler
                AxisAlignedBBHandle axisalignedbb1 = entity1.getOtherBoundingBox();
                if (axisalignedbb1 != null && axisalignedbb1.bbTransformA(axisalignedbb) && controller.onEntityCollision(entity1.getBukkitEntity())) {
                    collisions_buffer.add(axisalignedbb1);
                }
                axisalignedbb1 = entity.getEntityBoundingBox(entity1);
                if (axisalignedbb1 != null && axisalignedbb1.bbTransformA(axisalignedbb) && controller.onEntityCollision(entity1.getBukkitEntity())) {
                    collisions_buffer.add(axisalignedbb1);
                }
            /*
                    if ((axisalignedbb1 != null) && (axisalignedbb1.c(axisalignedbb))) {
                        list.add(axisalignedbb1);
                    }

                    axisalignedbb1 = entity.j(entity1);
                    if ((axisalignedbb1 != null) && (axisalignedbb1.c(axisalignedbb))) {
                        list.add(axisalignedbb1);
                    }
                    */
            // BKCommonLib end
            }
        }
    }
    return collisions_buffer;
}
Also used : AxisAlignedBBHandle(com.bergerkiller.generated.net.minecraft.server.AxisAlignedBBHandle) EntityHandle(com.bergerkiller.generated.net.minecraft.server.EntityHandle)

Aggregations

EntityHandle (com.bergerkiller.generated.net.minecraft.server.EntityHandle)12 CraftEntityHandle (com.bergerkiller.generated.org.bukkit.craftbukkit.entity.CraftEntityHandle)4 ArrayList (java.util.ArrayList)3 EntityTracker (com.bergerkiller.bukkit.common.wrappers.EntityTracker)2 AxisAlignedBBHandle (com.bergerkiller.generated.net.minecraft.server.AxisAlignedBBHandle)2 WorldHandle (com.bergerkiller.generated.net.minecraft.server.WorldHandle)2 NMSEntityTracker (com.bergerkiller.reflection.net.minecraft.server.NMSEntityTracker)2 ExtendedEntity (com.bergerkiller.bukkit.common.bases.ExtendedEntity)1 IntVector3 (com.bergerkiller.bukkit.common.bases.IntVector3)1 CommonLivingEntity (com.bergerkiller.bukkit.common.entity.type.CommonLivingEntity)1 CommonPlayer (com.bergerkiller.bukkit.common.entity.type.CommonPlayer)1 CommonPacket (com.bergerkiller.bukkit.common.protocol.CommonPacket)1 BlockData (com.bergerkiller.bukkit.common.wrappers.BlockData)1 BlockHandle (com.bergerkiller.generated.net.minecraft.server.BlockHandle)1 CrashReportHandle (com.bergerkiller.generated.net.minecraft.server.CrashReportHandle)1 CrashReportSystemDetailsHandle (com.bergerkiller.generated.net.minecraft.server.CrashReportSystemDetailsHandle)1 EntityInsentientHandle (com.bergerkiller.generated.net.minecraft.server.EntityInsentientHandle)1 EntityTrackerEntryHandle (com.bergerkiller.generated.net.minecraft.server.EntityTrackerEntryHandle)1 IInventoryHandle (com.bergerkiller.generated.net.minecraft.server.IInventoryHandle)1 WorldServerHandle (com.bergerkiller.generated.net.minecraft.server.WorldServerHandle)1