Search in sources :

Example 1 with IntHashMap

use of com.bergerkiller.bukkit.common.wrappers.IntHashMap in project BKCommonLib by bergerhealer.

the class CommonNMS method createDummyTrackerEntry.

/**
 * Creates an entity tracker entry with the right configuration without actually registering it inside the server.
 * This allows reading the network configuration such as view distance and update interval for an Entity.
 *
 * @param entity to create a dummy EntityTrackerEntry for
 * @return dummy EntityTrackerEntry
 */
@SuppressWarnings("unchecked")
public static EntityTrackerEntryHandle createDummyTrackerEntry(Entity entity) {
    EntityTrackerEntryHandle createdEntry = null;
    try {
        // We explicitly set this to an empty list to guarantee no spawn packets are produced
        if (dummyTrackerWorld == null) {
            dummyTrackerWorld = WorldServerHandle.T.newHandleNull();
            WorldHandle.T.players.raw.set(dummyTrackerWorld.getRaw(), Collections.emptyList());
        }
        // Initialize the dummy tracker without calling any methods/constructors
        if (dummyTracker == null) {
            dummyTracker = EntityTrackerHandle.T.newHandleNull();
            EntityTrackerHandle.T.entries.raw.set(dummyTracker.getRaw(), new HashSet<Object>());
            EntityTrackerHandle.T.world.raw.set(dummyTracker.getRaw(), dummyTrackerWorld.getRaw());
            dummyTracker.setTrackedEntities(new IntHashMap<Object>());
        }
        dummyTracker.getEntries().clear();
        // Track it!
        dummyTracker.setViewDistance((Bukkit.getViewDistance() - 1) * 16);
        IntHashMap<?> tracked = dummyTracker.getTrackedEntities();
        tracked.clear();
        dummyTracker.trackEntity(entity);
        // Retrieve it from the mapping
        List<IntHashMap.Entry<Object>> entries = ((IntHashMap<Object>) tracked).entries();
        if (!entries.isEmpty()) {
            createdEntry = EntityTrackerEntryHandle.createHandle(entries.get(0).getValue());
        } else {
            Logging.LOGGER_REFLECTION.once(Level.WARNING, "No dummy entry created for " + entity.getName() + ", resolving to defaults");
        }
    } catch (Throwable t) {
        Logging.LOGGER_REFLECTION.once(Level.SEVERE, "Failed to create dummy entry", t);
    }
    if (createdEntry == null) {
        // defaults
        createdEntry = EntityTrackerEntryHandle.createNew(entity, 80, (Bukkit.getViewDistance() - 1) * 16, 3, true);
    }
    // We must do this so that the next updatePlayer() update is properly synchronized
    if (EntityTrackerEntryHandle.T.opt_passengers.isAvailable()) {
        EntityTrackerEntryHandle.T.opt_passengers.set(createdEntry.getRaw(), (new ExtendedEntity<Entity>(entity)).getPassengers());
    }
    // We must do this so that the next updatePlayer() update is properly synchronized
    if (EntityTrackerEntryHandle.T.opt_vehicle.isAvailable()) {
        EntityTrackerEntryHandle.T.opt_vehicle.set(createdEntry.getRaw(), entity.getVehicle());
    }
    return createdEntry;
}
Also used : EntityTrackerEntryHandle(com.bergerkiller.generated.net.minecraft.server.EntityTrackerEntryHandle) ExtendedEntity(com.bergerkiller.bukkit.common.bases.ExtendedEntity) HumanEntity(org.bukkit.entity.HumanEntity) Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) IntHashMap(com.bergerkiller.bukkit.common.wrappers.IntHashMap)

Aggregations

ExtendedEntity (com.bergerkiller.bukkit.common.bases.ExtendedEntity)1 IntHashMap (com.bergerkiller.bukkit.common.wrappers.IntHashMap)1 EntityTrackerEntryHandle (com.bergerkiller.generated.net.minecraft.server.EntityTrackerEntryHandle)1 Entity (org.bukkit.entity.Entity)1 HumanEntity (org.bukkit.entity.HumanEntity)1 LivingEntity (org.bukkit.entity.LivingEntity)1