Search in sources :

Example 1 with WorldServerHandle

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

the class EntityAddRemoveHandler_1_14_to_1_16_5 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();
    // *** Remove from the entities to add queue ***
    Queue<Object> entitiesToAdd = this.entitiesToAddField.get(oldEntity.getWorld().getRaw());
    entitiesToAdd.remove(oldEntity.getRaw());
    // *** Entities By UUID Map ***
    {
        Map<UUID, Object> entitiesByUUID = this.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.getIdField());
        if (storedEntityHandle != null && storedEntityHandle != newEntity.getRaw()) {
            if (oldEntity.getIdField() != newEntity.getIdField()) {
                entitiesById.remove(oldEntity.getIdField());
            }
            entitiesById.put(newEntity.getIdField(), newEntity.getRaw());
        }
    }
    // *** Tuinity WorldServer EntityList field ***
    if (tuinitySwapEntityInWorldEntityListMethod.isAvailable()) {
        tuinitySwapEntityInWorldEntityListMethod.invoke(worldHandle, oldEntity.getRaw(), newEntity.getRaw());
    }
    // *** Tuinity WorldServer entitiesForIteration field ***
    if (tuinitySwapEntityInWorldEntityIterationSetMethod.isAvailable()) {
        tuinitySwapEntityInWorldEntityIterationSetMethod.invoke(worldHandle, oldEntity.getRaw(), newEntity.getRaw());
    }
    // *** EntityTrackerEntry ***
    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);
        }
    }
    // *** Entity Current Chunk ***
    final int chunkX = newEntity.getChunkX();
    final int chunkZ = newEntity.getChunkZ();
    PlayerChunkMapHandle playerChunks = WorldServerHandle.T.getPlayerChunkMap.invoke(worldHandle);
    Chunk loadedChunk = WorldUtil.getChunk(newEntity.getBukkitWorld(), chunkX, chunkZ);
    if (loadedChunk != null) {
        replaceInChunk(loadedChunk, oldEntity, newEntity);
    } else {
        // Chunk isn't loaded at this time. This gets difficult!
        // It might still be in the updating chunks mapping
        PlayerChunkHandle updatingChunk = playerChunks.getUpdatingChunk(chunkX, chunkZ);
        Chunk loadedUpdatingChunk = (updatingChunk == null) ? null : updatingChunk.getChunkIfLoaded();
        if (loadedUpdatingChunk == null && updatingChunk != null) {
            // Try hard time! This allows any status the chunk is in.
            loadedUpdatingChunk = PlayerChunkHandle.T.opt_getChunkTryHard_1_14.invoke(updatingChunk.getRaw());
        }
        // Let's go!
        replaceInChunk(loadedUpdatingChunk, 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(oldEntity.getRaw());
}
Also used : IntHashMapHandle(com.bergerkiller.generated.net.minecraft.util.IntHashMapHandle) PlayerChunkMapHandle(com.bergerkiller.generated.net.minecraft.server.level.PlayerChunkMapHandle) WorldServerHandle(com.bergerkiller.generated.net.minecraft.server.level.WorldServerHandle) PlayerChunkHandle(com.bergerkiller.generated.net.minecraft.server.level.PlayerChunkHandle) EntityHandle(com.bergerkiller.generated.net.minecraft.world.entity.EntityHandle) Chunk(org.bukkit.Chunk) Map(java.util.Map)

Example 2 with WorldServerHandle

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

the class EntityUtil method addEntity.

/**
 * Adds a single entity to the server
 *
 * @param entity to add
 */
public static void addEntity(org.bukkit.entity.Entity entity) {
    EntityHandle nmsentity = CommonNMS.getHandle(entity);
    WorldServerHandle nmsworld = nmsentity.getWorldServer();
    entity.getWorld().getChunkAt(MathUtil.toChunk(nmsentity.getLocX()), MathUtil.toChunk(nmsentity.getLocZ()));
    nmsentity.setDestroyed(false);
    // Remove an entity tracker for this entity if it was present
    nmsworld.getEntityTracker().stopTracking(entity);
    // Add the entity to the world
    nmsworld.addEntity(nmsentity);
    // Process add-related events right now
    EntityAddRemoveHandler.INSTANCE.processEvents();
}
Also used : WorldServerHandle(com.bergerkiller.generated.net.minecraft.server.level.WorldServerHandle) EntityHandle(com.bergerkiller.generated.net.minecraft.world.entity.EntityHandle)

Example 3 with WorldServerHandle

use of com.bergerkiller.generated.net.minecraft.server.level.WorldServerHandle 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 4 with WorldServerHandle

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

WorldServerHandle (com.bergerkiller.generated.net.minecraft.server.level.WorldServerHandle)4 EntityHandle (com.bergerkiller.generated.net.minecraft.world.entity.EntityHandle)4 IntHashMapHandle (com.bergerkiller.generated.net.minecraft.util.IntHashMapHandle)2 Map (java.util.Map)2 PlayerChunkHandle (com.bergerkiller.generated.net.minecraft.server.level.PlayerChunkHandle)1 PlayerChunkMapHandle (com.bergerkiller.generated.net.minecraft.server.level.PlayerChunkMapHandle)1 UUID (java.util.UUID)1 Chunk (org.bukkit.Chunk)1