Search in sources :

Example 1 with IModData_Activation

use of org.spongepowered.common.mixin.plugin.entityactivation.interfaces.IModData_Activation 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(World world) {
    if (((IMixinWorld) world).isFake()) {
        return;
    }
    for (EntityPlayer player : world.playerEntities) {
        int maxRange = 0;
        for (Integer range : maxActivationRanges.values()) {
            if (range > maxRange) {
                maxRange = range;
            }
        }
        maxRange = Math.min((((org.spongepowered.api.world.World) world).getViewDistance() << 4) - 8, maxRange);
        ((IModData_Activation) player).setActivatedTick(SpongeImpl.getServer().getTickCounter());
        growBb(maxBB, player.getEntityBoundingBox(), maxRange, 256, maxRange);
        int i = MathHelper.floor(maxBB.minX / 16.0D);
        int j = MathHelper.floor(maxBB.maxX / 16.0D);
        int k = MathHelper.floor(maxBB.minZ / 16.0D);
        int l = MathHelper.floor(maxBB.maxZ / 16.0D);
        for (int i1 = i; i1 <= j; ++i1) {
            for (int j1 = k; j1 <= l; ++j1) {
                WorldServer worldserver = (WorldServer) world;
                Chunk chunk = ((IMixinChunkProviderServer) worldserver.getChunkProvider()).getLoadedChunkWithoutMarkingActive(i1, j1);
                if (chunk != null) {
                    activateChunkEntities(player, chunk);
                }
            }
        }
    }
}
Also used : IModData_Activation(org.spongepowered.common.mixin.plugin.entityactivation.interfaces.IModData_Activation) IMixinWorld(org.spongepowered.common.interfaces.world.IMixinWorld) EntityPlayer(net.minecraft.entity.player.EntityPlayer) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) WorldServer(net.minecraft.world.WorldServer) IMixinChunk(org.spongepowered.common.interfaces.IMixinChunk) Chunk(net.minecraft.world.chunk.Chunk) IMixinChunkProviderServer(org.spongepowered.common.interfaces.world.gen.IMixinChunkProviderServer)

Example 2 with IModData_Activation

use of org.spongepowered.common.mixin.plugin.entityactivation.interfaces.IModData_Activation 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(EntityPlayer player, Chunk chunk) {
    for (int i = 0; i < chunk.getEntityLists().length; ++i) {
        for (Object o : chunk.getEntityLists()[i]) {
            Entity entity = (Entity) o;
            EntityType type = ((org.spongepowered.api.entity.Entity) entity).getType();
            long currentTick = SpongeImpl.getServer().getTickCounter();
            if (type == EntityTypes.UNKNOWN) {
                ((IModData_Activation) entity).setActivatedTick(currentTick);
                continue;
            }
            if (currentTick > ((IModData_Activation) entity).getActivatedTick()) {
                if (((IModData_Activation) entity).getDefaultActivationState()) {
                    ((IModData_Activation) entity).setActivatedTick(currentTick);
                    continue;
                }
                IModData_Activation spongeEntity = (IModData_Activation) entity;
                // check if activation cache needs to be updated
                if (spongeEntity.requiresActivationCacheRefresh()) {
                    EntityActivationRange.initializeEntityActivationState(entity);
                    spongeEntity.requiresActivationCacheRefresh(false);
                }
                // check for entity type overrides
                byte activationType = ((IModData_Activation) entity).getActivationType();
                int bbActivationRange = ((IModData_Activation) entity).getActivationRange();
                if (activationType == 5) {
                    growBb(miscBB, player.getEntityBoundingBox(), bbActivationRange, 256, bbActivationRange);
                } else if (activationType == 4) {
                    growBb(ambientBB, player.getEntityBoundingBox(), bbActivationRange, 256, bbActivationRange);
                } else if (activationType == 3) {
                    growBb(aquaticBB, player.getEntityBoundingBox(), bbActivationRange, 256, bbActivationRange);
                } else if (activationType == 2) {
                    growBb(creatureBB, player.getEntityBoundingBox(), bbActivationRange, 256, bbActivationRange);
                } else {
                    growBb(monsterBB, player.getEntityBoundingBox(), bbActivationRange, 256, bbActivationRange);
                }
                switch(((IModData_Activation) entity).getActivationType()) {
                    case 1:
                        if (monsterBB.intersects(entity.getEntityBoundingBox())) {
                            ((IModData_Activation) entity).setActivatedTick(currentTick);
                        }
                        break;
                    case 2:
                        if (creatureBB.intersects(entity.getEntityBoundingBox())) {
                            ((IModData_Activation) entity).setActivatedTick(currentTick);
                        }
                        break;
                    case 3:
                        if (aquaticBB.intersects(entity.getEntityBoundingBox())) {
                            ((IModData_Activation) entity).setActivatedTick(currentTick);
                        }
                        break;
                    case 4:
                        if (ambientBB.intersects(entity.getEntityBoundingBox())) {
                            ((IModData_Activation) entity).setActivatedTick(currentTick);
                        }
                        break;
                    case 5:
                    default:
                        if (miscBB.intersects(entity.getEntityBoundingBox())) {
                            ((IModData_Activation) entity).setActivatedTick(currentTick);
                        }
                }
            }
        }
    }
}
Also used : SpongeEntityType(org.spongepowered.common.entity.SpongeEntityType) EntityType(org.spongepowered.api.entity.EntityType) IMixinEntity(org.spongepowered.common.interfaces.entity.IMixinEntity) Entity(net.minecraft.entity.Entity) IModData_Activation(org.spongepowered.common.mixin.plugin.entityactivation.interfaces.IModData_Activation)

Example 3 with IModData_Activation

use of org.spongepowered.common.mixin.plugin.entityactivation.interfaces.IModData_Activation in project SpongeCommon by SpongePowered.

the class TileEntityActivation method activateChunkTileEntities.

/**
 * Checks for the activation state of all tileentities in this chunk.
 *
 * @param chunk Chunk to check for activation
 */
private static void activateChunkTileEntities(EntityPlayer player, Chunk chunk) {
    final Vector3i playerPos = VecHelper.toVector3i(player.getPosition());
    final long currentTick = SpongeImpl.getServer().getTickCounter();
    for (Map.Entry<BlockPos, TileEntity> mapEntry : chunk.getTileEntityMap().entrySet()) {
        final TileEntity tileEntity = mapEntry.getValue();
        if (!(tileEntity instanceof ITickable) || ((IModData_Activation) tileEntity).getActivatedTick() == currentTick) {
            // already activated
            continue;
        }
        final Vector3i tilePos = VecHelper.toVector3i(tileEntity.getPos());
        if (currentTick > ((IModData_Activation) tileEntity).getActivatedTick()) {
            if (((IModData_Activation) tileEntity).getDefaultActivationState()) {
                ((IModData_Activation) tileEntity).setActivatedTick(currentTick);
                continue;
            }
            IModData_Activation spongeEntity = (IModData_Activation) tileEntity;
            // check if activation cache needs to be updated
            if (spongeEntity.requiresActivationCacheRefresh()) {
                TileEntityActivation.initializeTileEntityActivationState(tileEntity);
                spongeEntity.requiresActivationCacheRefresh(false);
            }
            int bbActivationRange = ((IModData_Activation) tileEntity).getActivationRange();
            int blockDistance = Math.round(tilePos.distance(playerPos));
            if (blockDistance <= bbActivationRange) {
                ((IModData_Activation) tileEntity).setActivatedTick(currentTick);
            }
        }
    }
}
Also used : IMixinTileEntity(org.spongepowered.common.interfaces.block.tile.IMixinTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) IModData_Activation(org.spongepowered.common.mixin.plugin.entityactivation.interfaces.IModData_Activation) ITickable(net.minecraft.util.ITickable) Vector3i(com.flowpowered.math.vector.Vector3i) BlockPos(net.minecraft.util.math.BlockPos) PlayerChunkMap(net.minecraft.server.management.PlayerChunkMap) Map(java.util.Map)

Example 4 with IModData_Activation

use of org.spongepowered.common.mixin.plugin.entityactivation.interfaces.IModData_Activation in project SpongeCommon by SpongePowered.

the class TileEntityActivation method checkIfActive.

/**
 * Checks if the tileentity is active for this tick.
 *
 * @param tileEntity The tileentity to check for activity
 * @return Whether the given tileentity should be active
 */
public static boolean checkIfActive(TileEntity tileEntity) {
    if (tileEntity.getWorld() == null || tileEntity.getWorld().isRemote || !(tileEntity instanceof ITickable)) {
        return true;
    }
    final World world = tileEntity.getWorld();
    final IMixinChunk activeChunk = ((IMixinTileEntity) tileEntity).getActiveChunk();
    if (activeChunk == null) {
        // Should never happen but just in case for mods, always tick
        return true;
    }
    long currentTick = SpongeImpl.getServer().getTickCounter();
    IModData_Activation spongeTileEntity = (IModData_Activation) tileEntity;
    boolean isActive = activeChunk.isPersistedChunk() || spongeTileEntity.getActivatedTick() >= currentTick || spongeTileEntity.getDefaultActivationState();
    // Should this entity tick?
    if (!isActive) {
        if (spongeTileEntity.getActivatedTick() == Integer.MIN_VALUE) {
            // Has not come across a player
            return false;
        }
    }
    // check tick rate
    if (isActive && world.getWorldInfo().getWorldTotalTime() % spongeTileEntity.getSpongeTickRate() != 0L) {
        isActive = false;
    }
    return isActive;
}
Also used : IModData_Activation(org.spongepowered.common.mixin.plugin.entityactivation.interfaces.IModData_Activation) IMixinChunk(org.spongepowered.common.interfaces.IMixinChunk) IMixinTileEntity(org.spongepowered.common.interfaces.block.tile.IMixinTileEntity) ITickable(net.minecraft.util.ITickable) World(net.minecraft.world.World)

Example 5 with IModData_Activation

use of org.spongepowered.common.mixin.plugin.entityactivation.interfaces.IModData_Activation in project SpongeCommon by SpongePowered.

the class TileEntityActivation method initializeTileEntityActivationState.

/**
 * These tileentities are excluded from Activation range checks.
 *
 * @param tileEntity The tileentity to check
 * @return boolean If it should always tick.
 */
public static boolean initializeTileEntityActivationState(TileEntity tileEntity) {
    if (tileEntity.getWorld() == null || tileEntity.getWorld().isRemote || !(tileEntity instanceof ITickable)) {
        return true;
    }
    TileEntityActivationCategory config = ((IMixinWorldServer) tileEntity.getWorld()).getActiveConfig().getConfig().getTileEntityActivationRange();
    TileEntityType type = ((org.spongepowered.api.block.tileentity.TileEntity) tileEntity).getType();
    IModData_Activation spongeTileEntity = (IModData_Activation) tileEntity;
    SpongeTileEntityType spongeType = (SpongeTileEntityType) type;
    if (spongeType == null || spongeType.getModId() == null) {
        return true;
    }
    TileEntityActivationModCategory tileEntityMod = config.getModList().get(spongeType.getModId().toLowerCase());
    int defaultActivationRange = config.getDefaultBlockRange();
    int defaultTickRate = config.getDefaultTickRate();
    if (tileEntityMod == null) {
        // use default activation range
        spongeTileEntity.setActivationRange(defaultActivationRange);
        if (defaultActivationRange <= 0) {
            return true;
        }
        return false;
    } else if (!tileEntityMod.isEnabled()) {
        spongeTileEntity.setActivationRange(defaultActivationRange);
        spongeTileEntity.setSpongeTickRate(defaultTickRate);
        return true;
    }
    Integer defaultModActivationRange = tileEntityMod.getDefaultBlockRange();
    Integer tileEntityActivationRange = tileEntityMod.getTileEntityRangeList().get(type.getName().toLowerCase());
    if (defaultModActivationRange != null && tileEntityActivationRange == null) {
        spongeTileEntity.setActivationRange(defaultModActivationRange);
        if (defaultModActivationRange <= 0) {
            return true;
        }
    } else if (tileEntityActivationRange != null) {
        spongeTileEntity.setActivationRange(tileEntityActivationRange);
        if (tileEntityActivationRange <= 0) {
            return true;
        }
    }
    Integer defaultModTickRate = tileEntityMod.getDefaultTickRate();
    Integer tileEntityTickRate = tileEntityMod.getTileEntityTickRateList().get(type.getName().toLowerCase());
    if (defaultModTickRate != null && tileEntityTickRate == null) {
        spongeTileEntity.setSpongeTickRate(defaultModTickRate);
        if (defaultModTickRate <= 0) {
            return true;
        }
        return false;
    } else if (tileEntityTickRate != null) {
        spongeTileEntity.setSpongeTickRate(tileEntityTickRate);
        if (tileEntityTickRate <= 0) {
            return true;
        }
    }
    return false;
}
Also used : IMixinTileEntity(org.spongepowered.common.interfaces.block.tile.IMixinTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) IModData_Activation(org.spongepowered.common.mixin.plugin.entityactivation.interfaces.IModData_Activation) TileEntityActivationModCategory(org.spongepowered.common.config.category.TileEntityActivationModCategory) ITickable(net.minecraft.util.ITickable) SpongeTileEntityType(org.spongepowered.common.data.type.SpongeTileEntityType) SpongeTileEntityType(org.spongepowered.common.data.type.SpongeTileEntityType) TileEntityType(org.spongepowered.api.block.tileentity.TileEntityType) TileEntityActivationCategory(org.spongepowered.common.config.category.TileEntityActivationCategory) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer)

Aggregations

IModData_Activation (org.spongepowered.common.mixin.plugin.entityactivation.interfaces.IModData_Activation)7 ITickable (net.minecraft.util.ITickable)4 IMixinChunk (org.spongepowered.common.interfaces.IMixinChunk)3 IMixinTileEntity (org.spongepowered.common.interfaces.block.tile.IMixinTileEntity)3 Entity (net.minecraft.entity.Entity)2 TileEntity (net.minecraft.tileentity.TileEntity)2 Chunk (net.minecraft.world.chunk.Chunk)2 IMixinEntity (org.spongepowered.common.interfaces.entity.IMixinEntity)2 IMixinWorldServer (org.spongepowered.common.interfaces.world.IMixinWorldServer)2 Vector3i (com.flowpowered.math.vector.Vector3i)1 Map (java.util.Map)1 EntityPlayer (net.minecraft.entity.player.EntityPlayer)1 PlayerChunkMap (net.minecraft.server.management.PlayerChunkMap)1 BlockPos (net.minecraft.util.math.BlockPos)1 World (net.minecraft.world.World)1 WorldServer (net.minecraft.world.WorldServer)1 TileEntity (org.spongepowered.api.block.tileentity.TileEntity)1 TileEntityType (org.spongepowered.api.block.tileentity.TileEntityType)1 EntityType (org.spongepowered.api.entity.EntityType)1 Overwrite (org.spongepowered.asm.mixin.Overwrite)1