Search in sources :

Example 1 with ActivationCapabilityBridge

use of org.spongepowered.common.bridge.activation.ActivationCapabilityBridge in project SpongeCommon by SpongePowered.

the class EntityActivationRange method activateEntities.

/**
 * Find what entities are in range of the players in the world and set
 * active if in range.
 *
 * @param world The world to perform activation checks in
 */
public static void activateEntities(final ServerLevel world) {
    if (((LevelBridge) world).bridge$isFake()) {
        return;
    }
    for (final ServerPlayer player : world.players()) {
        int maxRange = 0;
        for (final Integer range : EntityActivationRange.maxActivationRanges.values()) {
            if (range > maxRange) {
                maxRange = range;
            }
        }
        maxRange = Math.min((((ServerWorld) world).properties().viewDistance() << 4) - 8, maxRange);
        ((ActivationCapabilityBridge) player).activation$setActivatedTick(SpongeCommon.server().getTickCount());
        final AABB aabb = EntityActivationRange.maxBB;
        EntityActivationRange.growBb(aabb, player.getBoundingBox(), maxRange, 256, maxRange);
        final int i = Mth.floor(aabb.minX / 16.0D);
        final int j = Mth.floor(aabb.maxX / 16.0D);
        final int k = Mth.floor(aabb.minZ / 16.0D);
        final int l = Mth.floor(aabb.maxZ / 16.0D);
        for (int i1 = i; i1 <= j; ++i1) {
            for (int j1 = k; j1 <= l; ++j1) {
                final LevelChunk chunk = world.getChunkSource().getChunkNow(i1, j1);
                if (chunk != null) {
                    EntityActivationRange.activateChunkEntities(player, chunk);
                }
            }
        }
    }
}
Also used : ServerWorld(org.spongepowered.api.world.server.ServerWorld) LevelChunk(net.minecraft.world.level.chunk.LevelChunk) LevelBridge(org.spongepowered.common.bridge.world.level.LevelBridge) ActivationCapabilityBridge(org.spongepowered.common.bridge.activation.ActivationCapabilityBridge) ServerPlayer(net.minecraft.server.level.ServerPlayer) AABB(net.minecraft.world.phys.AABB)

Example 2 with ActivationCapabilityBridge

use of org.spongepowered.common.bridge.activation.ActivationCapabilityBridge in project SpongeCommon by SpongePowered.

the class EntityActivationRange method checkIfActive.

/**
 * Checks if the entity is active for this tick.
 *
 * @param entity The entity to check for activity
 * @return Whether the given entity should be active
 */
public static boolean checkIfActive(final Entity entity) {
    // Never safe to skip fireworks or entities not yet added to chunk
    if (entity instanceof Player || entity.level.isClientSide() || !entity.inChunk || entity instanceof FireworkRocketEntity) {
        return true;
    }
    final LevelChunkBridge activeChunk = ((ActiveChunkReferantBridge) entity).bridge$getActiveChunk();
    if (activeChunk == null) {
        // Should never happen but just in case for mods, always tick
        return true;
    }
    if (!activeChunk.bridge$isActive()) {
        return false;
    }
    // If in forced chunk or is player
    if (activeChunk.bridge$isPersistedChunk() || ((PlatformEntityBridge) entity).bridge$isFakePlayer() && entity instanceof ServerPlayer) {
        return true;
    }
    final long currentTick = SpongeCommon.server().getTickCount();
    final ActivationCapabilityBridge spongeEntity = (ActivationCapabilityBridge) entity;
    boolean isActive = spongeEntity.activation$getActivatedTick() >= currentTick || spongeEntity.activation$getDefaultActivationState();
    // Should this entity tick?
    if (!isActive) {
        if ((currentTick - spongeEntity.activation$getActivatedTick() - 1) % 20 == 0) {
            // Check immunities every 20 ticks.
            if (EntityActivationRange.checkEntityImmunities(entity)) {
                // Triggered some sort of immunity, give 20 full ticks before we check again.
                spongeEntity.activation$setActivatedTick(currentTick + 20);
            }
            isActive = true;
        }
    // Add a little performance juice to active entities. Skip 1/4 if not immune.
    } else if (!spongeEntity.activation$getDefaultActivationState() && entity.tickCount % 4 == 0 && !EntityActivationRange.checkEntityImmunities(entity)) {
        isActive = false;
    }
    if (isActive && !activeChunk.bridge$areNeighborsLoaded()) {
        isActive = false;
    }
    return isActive;
}
Also used : Player(net.minecraft.world.entity.player.Player) ServerPlayer(net.minecraft.server.level.ServerPlayer) ActivationCapabilityBridge(org.spongepowered.common.bridge.activation.ActivationCapabilityBridge) ServerPlayer(net.minecraft.server.level.ServerPlayer) FireworkRocketEntity(net.minecraft.world.entity.projectile.FireworkRocketEntity) LevelChunkBridge(org.spongepowered.common.bridge.world.level.chunk.LevelChunkBridge) PlatformEntityBridge(org.spongepowered.common.bridge.world.entity.PlatformEntityBridge) ActiveChunkReferantBridge(org.spongepowered.common.bridge.world.level.chunk.ActiveChunkReferantBridge)

Example 3 with ActivationCapabilityBridge

use of org.spongepowered.common.bridge.activation.ActivationCapabilityBridge in project SpongeCommon by SpongePowered.

the class EntityActivationRange method activateChunkEntities.

/**
 * Checks for the activation state of all entities in this chunk.
 *
 * @param chunk Chunk to check for activation
 */
private static void activateChunkEntities(final Player player, final LevelChunk chunk) {
    for (final ClassInstanceMultiMap<Entity> entitySection : chunk.getEntitySections()) {
        for (final Entity entity : entitySection) {
            final ActivationCapabilityBridge spongeEntity = (ActivationCapabilityBridge) entity;
            final long currentTick = SpongeCommon.server().getTickCount();
            if (!((TrackableBridge) entity).bridge$shouldTick()) {
                continue;
            }
            if (currentTick <= spongeEntity.activation$getActivatedTick()) {
                continue;
            }
            if (spongeEntity.activation$getDefaultActivationState()) {
                spongeEntity.activation$setActivatedTick(currentTick);
                continue;
            }
            // check if activation cache needs to be updated
            if (spongeEntity.activation$requiresActivationCacheRefresh()) {
                EntityActivationRange.initializeEntityActivationState(entity);
                spongeEntity.activation$requiresActivationCacheRefresh(false);
            }
            // check for entity type overrides
            final AABB aabb;
            switch(spongeEntity.activation$getActivationType()) {
                case 5:
                    aabb = EntityActivationRange.miscBB;
                    break;
                case 4:
                    aabb = EntityActivationRange.ambientBB;
                    break;
                case 3:
                    aabb = EntityActivationRange.aquaticBB;
                    break;
                case 2:
                    aabb = EntityActivationRange.creatureBB;
                    break;
                default:
                    aabb = EntityActivationRange.monsterBB;
                    break;
            }
            final int bbActivationRange = spongeEntity.activation$getActivationRange();
            EntityActivationRange.growBb(aabb, player.getBoundingBox(), bbActivationRange, 256, bbActivationRange);
            if (aabb.intersects(entity.getBoundingBox())) {
                spongeEntity.activation$setActivatedTick(currentTick);
            }
        }
    }
}
Also used : FallingBlockEntity(net.minecraft.world.entity.item.FallingBlockEntity) LivingEntity(net.minecraft.world.entity.LivingEntity) FireworkRocketEntity(net.minecraft.world.entity.projectile.FireworkRocketEntity) Entity(net.minecraft.world.entity.Entity) ActivationCapabilityBridge(org.spongepowered.common.bridge.activation.ActivationCapabilityBridge) AABB(net.minecraft.world.phys.AABB)

Example 4 with ActivationCapabilityBridge

use of org.spongepowered.common.bridge.activation.ActivationCapabilityBridge in project SpongeCommon by SpongePowered.

the class EntityActivationRange method initializeEntityActivationState.

/**
 * Initialize entity activation state.
 *
 * @param entity Entity to check
 */
public static void initializeEntityActivationState(final Entity entity) {
    final ActivationCapabilityBridge spongeEntity = (ActivationCapabilityBridge) entity;
    if (entity.level.isClientSide()) {
        return;
    }
    // types that should always be active
    if (entity instanceof Player && !((PlatformEntityBridge) entity).bridge$isFakePlayer() || entity instanceof ThrowableProjectile || entity instanceof EnderDragon || entity instanceof EnderDragonPart || entity instanceof WitherBoss || entity instanceof AbstractHurtingProjectile || entity instanceof LightningBolt || entity instanceof PrimedTnt || entity instanceof Painting || entity instanceof EndCrystal || entity instanceof FireworkRocketEntity || // Always tick falling blocks
    entity instanceof FallingBlockEntity) {
        return;
    }
    final InheritableConfigHandle<WorldConfig> configAdapter = SpongeGameConfigs.getForWorld(entity.level);
    final EntityActivationRangeCategory config = configAdapter.get().entityActivationRange;
    final EntityTypeBridge type = (EntityTypeBridge) entity.getType();
    final ResourceLocation key = EntityType.getKey(entity.getType());
    final byte activationType = spongeEntity.activation$getActivationType();
    final String activationTypeName = EntityActivationRange.activationTypeMappings.getOrDefault(activationType, "misc");
    if (!type.bridge$isActivationRangeInitialized()) {
        EntityActivationRange.addEntityToConfig(config.autoPopulate, key, activationType, activationTypeName);
        type.bridge$setActivationRangeInitialized(true);
    }
    final EntityActivationRangeCategory.ModSubCategory entityMod = config.mods.get(key.getNamespace());
    final int defaultActivationRange = config.globalRanges.get(activationTypeName);
    if (entityMod == null) {
        // use default activation range
        spongeEntity.activation$setActivationRange(defaultActivationRange);
        if (defaultActivationRange > 0) {
            spongeEntity.activation$setDefaultActivationState(false);
        }
    } else {
        if (!entityMod.enabled) {
            spongeEntity.activation$setDefaultActivationState(true);
            return;
        }
        final Integer defaultModActivationRange = entityMod.defaultRanges.get(activationTypeName);
        final Integer entityActivationRange = entityMod.entities.get(key.getPath());
        if (defaultModActivationRange != null && entityActivationRange == null) {
            spongeEntity.activation$setActivationRange(defaultModActivationRange);
            if (defaultModActivationRange > 0) {
                spongeEntity.activation$setDefaultActivationState(false);
            }
        } else if (entityActivationRange != null) {
            spongeEntity.activation$setActivationRange(entityActivationRange);
            if (entityActivationRange > 0) {
                spongeEntity.activation$setDefaultActivationState(false);
            }
        }
    }
}
Also used : PrimedTnt(net.minecraft.world.entity.item.PrimedTnt) FallingBlockEntity(net.minecraft.world.entity.item.FallingBlockEntity) Player(net.minecraft.world.entity.player.Player) ServerPlayer(net.minecraft.server.level.ServerPlayer) WorldConfig(org.spongepowered.common.config.inheritable.WorldConfig) EntityTypeBridge(org.spongepowered.common.bridge.world.entity.EntityTypeBridge) EndCrystal(net.minecraft.world.entity.boss.enderdragon.EndCrystal) FireworkRocketEntity(net.minecraft.world.entity.projectile.FireworkRocketEntity) Painting(net.minecraft.world.entity.decoration.Painting) LightningBolt(net.minecraft.world.entity.LightningBolt) EnderDragon(net.minecraft.world.entity.boss.enderdragon.EnderDragon) ActivationCapabilityBridge(org.spongepowered.common.bridge.activation.ActivationCapabilityBridge) WitherBoss(net.minecraft.world.entity.boss.wither.WitherBoss) EnderDragonPart(net.minecraft.world.entity.boss.EnderDragonPart) AbstractHurtingProjectile(net.minecraft.world.entity.projectile.AbstractHurtingProjectile) ResourceLocation(net.minecraft.resources.ResourceLocation) ThrowableProjectile(net.minecraft.world.entity.projectile.ThrowableProjectile) EntityActivationRangeCategory(org.spongepowered.common.config.inheritable.EntityActivationRangeCategory)

Aggregations

ActivationCapabilityBridge (org.spongepowered.common.bridge.activation.ActivationCapabilityBridge)4 ServerPlayer (net.minecraft.server.level.ServerPlayer)3 FireworkRocketEntity (net.minecraft.world.entity.projectile.FireworkRocketEntity)3 FallingBlockEntity (net.minecraft.world.entity.item.FallingBlockEntity)2 Player (net.minecraft.world.entity.player.Player)2 AABB (net.minecraft.world.phys.AABB)2 ResourceLocation (net.minecraft.resources.ResourceLocation)1 Entity (net.minecraft.world.entity.Entity)1 LightningBolt (net.minecraft.world.entity.LightningBolt)1 LivingEntity (net.minecraft.world.entity.LivingEntity)1 EnderDragonPart (net.minecraft.world.entity.boss.EnderDragonPart)1 EndCrystal (net.minecraft.world.entity.boss.enderdragon.EndCrystal)1 EnderDragon (net.minecraft.world.entity.boss.enderdragon.EnderDragon)1 WitherBoss (net.minecraft.world.entity.boss.wither.WitherBoss)1 Painting (net.minecraft.world.entity.decoration.Painting)1 PrimedTnt (net.minecraft.world.entity.item.PrimedTnt)1 AbstractHurtingProjectile (net.minecraft.world.entity.projectile.AbstractHurtingProjectile)1 ThrowableProjectile (net.minecraft.world.entity.projectile.ThrowableProjectile)1 LevelChunk (net.minecraft.world.level.chunk.LevelChunk)1 ServerWorld (org.spongepowered.api.world.server.ServerWorld)1