Search in sources :

Example 11 with EntityHandle

use of com.bergerkiller.generated.net.minecraft.world.entity.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!");
    }
    // *** 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) {
        List<EntityHandle> passengers = new ArrayList<EntityHandle>(vehicle.getPassengers());
        replaceInList(passengers, newInstance);
        vehicle.setPassengers(passengers);
    }
    for (EntityHandle passenger : newInstance.getPassengers()) {
        if (oldInstance.getRaw() == passenger.getVehicle().getRaw()) {
            passenger.setVehicle(newInstance);
        }
    }
    // *** Replace data that is stored in the entity itself ***
    newInstance.assignEntityReference();
    // *** Perform further replacement all over the place in the server ***
    EntityAddRemoveHandler.INSTANCE.replace(oldInstance, newInstance);
    // *** Reset entity state ***
    oldInstance.setRemovedPassive();
    newInstance.setValid(true);
    // *** Repeat the replacement in the server the next tick to make sure nothing lingers ***
    CommonUtil.nextTick(() -> EntityAddRemoveHandler.INSTANCE.replace(oldInstance, newInstance));
    // *** Make sure a controller is set when hooked ***
    if (this.isHooked()) {
        DefaultEntityController controller = new DefaultEntityController();
        controller.bind(this, true);
    }
}
Also used : CraftEntityHandle(com.bergerkiller.generated.org.bukkit.craftbukkit.entity.CraftEntityHandle) IInventoryHandle(com.bergerkiller.generated.net.minecraft.world.IInventoryHandle) ArrayList(java.util.ArrayList) EntityHandle(com.bergerkiller.generated.net.minecraft.world.entity.EntityHandle) CraftEntityHandle(com.bergerkiller.generated.org.bukkit.craftbukkit.entity.CraftEntityHandle) InventoryHolder(org.bukkit.inventory.InventoryHolder) CraftInventoryHandle(com.bergerkiller.generated.org.bukkit.craftbukkit.inventory.CraftInventoryHandle) Inventory(org.bukkit.inventory.Inventory)

Example 12 with EntityHandle

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

the class MapPlayerInput method updateInterception.

private void updateInterception(boolean intercept) {
    // No receivers, do not do input interception
    if (!intercept) {
        updateInputInterception(false);
        return;
    }
    // If the player is already inside a vehicle, we can not fake-mount him
    if (player.isInsideVehicle()) {
        updateInputInterception(false);
        return;
    }
    // Since Minecraft 1.16, it is not possible to be on the mount while sneaking
    if (!CommonCapabilities.VEHICLE_EXIT_CANCELLABLE && player.isSneaking()) {
        updateInputInterception(false);
        return;
    }
    // Verify the player isn't flying, it results in a kick
    if (!player.isFlying() && !player.isOnGround()) {
        // Check if there is a block below the player, which means onGround is actually true
        // This is because, when the player is 'floating' while intercepting, onGround stays false
        EntityHandle playerHandle = EntityHandle.fromBukkit(player);
        double half_width = 0.5 * (double) playerHandle.getWidth();
        double below = 0.1;
        AxisAlignedBBHandle below_bounds = AxisAlignedBBHandle.createNew(playerHandle.getLocX() - half_width, playerHandle.getLocY() - below, playerHandle.getLocZ() - half_width, playerHandle.getLocX() + half_width, playerHandle.getLocY(), playerHandle.getLocZ() + half_width);
        if (WorldHandle.fromBukkit(player.getWorld()).isNotCollidingWithBlocks(playerHandle, below_bounds)) {
            updateInputInterception(false);
            return;
        }
    }
    // Allowed
    this.updateInputInterception(true);
}
Also used : AxisAlignedBBHandle(com.bergerkiller.generated.net.minecraft.world.phys.AxisAlignedBBHandle) EntityHandle(com.bergerkiller.generated.net.minecraft.world.entity.EntityHandle)

Example 13 with EntityHandle

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

the class ExtendedEntity method getLeashHolder.

/**
 * Gets the Entity that is holding this Entity by a leash. If this Entity
 * does not support leashing, or the Entity is not on a leash, null is
 * returned instead.
 *
 * @return Leash holder
 */
public org.bukkit.entity.Entity getLeashHolder() {
    if (handle.isInstanceOf(EntityInsentientHandle.T)) {
        EntityInsentientHandle insHandle = EntityInsentientHandle.createHandle(handle.getRaw());
        EntityHandle holder = insHandle.getLeashHolder();
        if (holder != null) {
            return holder.getBukkitEntity();
        }
    }
    return null;
}
Also used : EntityHandle(com.bergerkiller.generated.net.minecraft.world.entity.EntityHandle) EntityInsentientHandle(com.bergerkiller.generated.net.minecraft.world.entity.EntityInsentientHandle)

Example 14 with EntityHandle

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

the class EntityAddRemoveHandler_1_17 method replace.

@Override
public void replace(EntityHandle oldEntity, EntityHandle newEntity) {
    WorldServerHandle world = oldEntity.getWorldServer();
    if (newEntity == null) {
        if (world != null) {
            world.removeEntity(oldEntity);
            world.getEntityTracker().stopTracking(oldEntity.getBukkitEntity());
        }
    }
    // *** EntityTrackerEntry ***
    if (newEntity == null) {
        // If still tracked despite removal, wipe the tracker
        world.getEntityTracker().removeEntry(oldEntity.getIdField());
    } else {
        // Replacement
        replaceInEntityTracker(oldEntity, oldEntity, newEntity);
        if (oldEntity.getVehicle() != null) {
            replaceInEntityTracker(oldEntity.getVehicle(), oldEntity, newEntity);
        }
        if (oldEntity.getPassengers() != null) {
            for (EntityHandle passenger : oldEntity.getPassengers()) {
                replaceInEntityTracker(passenger, oldEntity, newEntity);
            }
        }
    }
    Object newEntityRaw = Handle.getRaw(newEntity);
    if (world != null) {
        removeHandler.replaceInWorldStorage(world.getRaw(), oldEntity.getRaw(), newEntityRaw);
    }
    removeHandler.replaceInSectionStorage(oldEntity.getRaw(), newEntityRaw);
    removeHandler.replaceInChunkStorage(oldEntity.getRaw(), newEntityRaw);
// See where the object is still referenced to check we aren't missing any places to replace
// This is SLOW, do not ever have this enabled on a release version!
// com.bergerkiller.bukkit.common.utils.DebugUtil.logInstances(oldEntity.getRaw());
}
Also used : WorldServerHandle(com.bergerkiller.generated.net.minecraft.server.level.WorldServerHandle) EntityHandle(com.bergerkiller.generated.net.minecraft.world.entity.EntityHandle)

Example 15 with EntityHandle

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

the class EntityAddRemoveHandler_1_8_to_1_13_2 method replace.

@Override
public void replace(EntityHandle oldEntity, EntityHandle newEntity) {
    WorldServerHandle world = oldEntity.getWorldServer();
    if (newEntity == null) {
        if (world != null) {
            world.removeEntity(oldEntity);
            world.getEntityTracker().stopTracking(oldEntity.getBukkitEntity());
        }
        // Works fine, no need to clean up any more
        return;
    }
    Object worldHandle = world.getRaw();
    // *** Entities By UUID Map ***
    {
        Map<UUID, Object> entitiesByUUID = entitiesByUUIDField.get(worldHandle);
        Object storedEntityHandle = entitiesByUUID.get(oldEntity.getUniqueID());
        if (storedEntityHandle != null && storedEntityHandle != newEntity.getRaw()) {
            if (!oldEntity.getUniqueID().equals(newEntity.getUniqueID())) {
                entitiesByUUID.remove(oldEntity.getUniqueID());
            }
            entitiesByUUID.put(newEntity.getUniqueID(), newEntity.getRaw());
        }
    }
    // *** Entities by Id Map ***
    {
        IntHashMapHandle entitiesById = IntHashMapHandle.createHandle(this.entitiesByIdField.get(worldHandle));
        Object storedEntityHandle = entitiesById.get(oldEntity.getId());
        if (storedEntityHandle != null && storedEntityHandle != newEntity.getRaw()) {
            if (oldEntity.getId() != newEntity.getId()) {
                entitiesById.remove(oldEntity.getId());
            }
            entitiesById.put(newEntity.getId(), newEntity.getRaw());
        }
    }
    // *** Entities By UUID Map ***
    final Map<UUID, Object> entitiesByUUID = entitiesByUUIDField.get(worldHandle);
    entitiesByUUID.put(newEntity.getUniqueID(), newEntity.getRaw());
    // *** Entities by Id Map ***
    IntHashMapHandle entitiesById = IntHashMapHandle.createHandle(this.entitiesByIdField.get(worldHandle));
    entitiesById.put(newEntity.getId(), newEntity.getRaw());
    // *** EntityTrackerEntry ***
    replaceInEntityTracker(newEntity.getId(), newEntity);
    if (newEntity.getVehicle() != null) {
        replaceInEntityTracker(newEntity.getVehicle().getId(), newEntity);
    }
    if (newEntity.getPassengers() != null) {
        for (EntityHandle passenger : newEntity.getPassengers()) {
            replaceInEntityTracker(passenger.getId(), newEntity);
        }
    }
    // *** World ***
    replaceInList(entityListField.get(worldHandle), newEntity);
    // Fixes for PaperSpigot
    // if (!Common.IS_PAPERSPIGOT_SERVER) {
    // replaceInList(WorldRef.entityRemovalList.get(oldInstance.world), newInstance);
    // }
    // *** Entity Current Chunk ***
    final int chunkX = newEntity.getChunkX();
    final int chunkZ = newEntity.getChunkZ();
    Object chunkHandle = HandleConversion.toChunkHandle(WorldUtil.getChunk(newEntity.getWorld().getWorld(), chunkX, chunkZ));
    if (chunkHandle != null) {
        this.chunkEntitySliceHandler.replace(chunkHandle, oldEntity, newEntity);
    }
// See where the object is still referenced to check we aren't missing any places to replace
// This is SLOW, do not ever have this enabled on a release version!
// com.bergerkiller.bukkit.common.utils.DebugUtil.logInstances(oldInstance.getRaw());
}
Also used : IntHashMapHandle(com.bergerkiller.generated.net.minecraft.util.IntHashMapHandle) WorldServerHandle(com.bergerkiller.generated.net.minecraft.server.level.WorldServerHandle) EntityHandle(com.bergerkiller.generated.net.minecraft.world.entity.EntityHandle) UUID(java.util.UUID) Map(java.util.Map)

Aggregations

EntityHandle (com.bergerkiller.generated.net.minecraft.world.entity.EntityHandle)22 AxisAlignedBBHandle (com.bergerkiller.generated.net.minecraft.world.phys.AxisAlignedBBHandle)6 ArrayList (java.util.ArrayList)6 EntityTracker (com.bergerkiller.bukkit.common.wrappers.EntityTracker)4 EntityTrackerEntryHandle (com.bergerkiller.generated.net.minecraft.server.level.EntityTrackerEntryHandle)4 WorldServerHandle (com.bergerkiller.generated.net.minecraft.server.level.WorldServerHandle)4 EntityTrackerEntryStateHandle (com.bergerkiller.generated.net.minecraft.server.level.EntityTrackerEntryStateHandle)2 IntHashMapHandle (com.bergerkiller.generated.net.minecraft.util.IntHashMapHandle)2 WorldHandle (com.bergerkiller.generated.net.minecraft.world.level.WorldHandle)2 VoxelShapeHandle (com.bergerkiller.generated.net.minecraft.world.phys.shapes.VoxelShapeHandle)2 CraftEntityHandle (com.bergerkiller.generated.org.bukkit.craftbukkit.entity.CraftEntityHandle)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Map (java.util.Map)2 Location (org.bukkit.Location)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 EntityTrackerHook (com.bergerkiller.bukkit.common.internal.hooks.EntityTrackerHook)1