Search in sources :

Example 6 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 7 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)

Example 8 with IModData_Activation

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

the class MixinChunk_TileEntityActivation method onAddTileEntity.

@Inject(method = "addTileEntity(Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/tileentity/TileEntity;)V", at = @At("RETURN"))
public void onAddTileEntity(BlockPos pos, net.minecraft.tileentity.TileEntity tileEntityIn, CallbackInfo ci) {
    if (tileEntityIn.getWorld() == null) {
        tileEntityIn.setWorld(this.world);
    }
    if (!(tileEntityIn instanceof ITickable)) {
        return;
    }
    if (((IMixinWorldInfo) this.world.getWorldInfo()).isValid()) {
        IModData_Activation spongeTile = (IModData_Activation) tileEntityIn;
        spongeTile.setDefaultActivationState(TileEntityActivation.initializeTileEntityActivationState(tileEntityIn));
        if (!spongeTile.getDefaultActivationState()) {
            TileEntityActivation.addTileEntityToConfig(this.world, (SpongeTileEntityType) ((TileEntity) tileEntityIn).getType());
        }
    }
}
Also used : TileEntity(org.spongepowered.api.block.tileentity.TileEntity) IModData_Activation(org.spongepowered.common.mixin.plugin.entityactivation.interfaces.IModData_Activation) ITickable(net.minecraft.util.ITickable) IMixinWorldInfo(org.spongepowered.common.interfaces.world.IMixinWorldInfo) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 9 with IModData_Activation

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

the class MixinWorld_Activation method updateEntityWithOptionalForce.

@Overwrite
public void updateEntityWithOptionalForce(Entity entityIn, boolean forceUpdate) {
    // Sponge start - area is handled in ActivationRange
    // int i = MathHelper.floor_double(entityIn.posX);
    // int j = MathHelper.floor_double(entityIn.posZ);
    // boolean isForced = getPersistentChunks().containsKey(new net.minecraft.util.math.ChunkPos(i >> 4, j >> 4));
    // int k = isForced ? 0 : 32;
    // boolean canUpdate = !forceUpdate || this.isAreaLoaded(i - k, 0, j - k, i + k, 0, j + k, true);
    boolean canUpdate = EntityActivationRange.checkIfActive(entityIn);
    // Allow forge mods to force an update
    if (!canUpdate)
        canUpdate = net.minecraftforge.event.ForgeEventFactory.canEntityUpdate(entityIn);
    if (!canUpdate) {
        entityIn.ticksExisted++;
        ((IModData_Activation) entityIn).inactiveTick();
        return;
    }
    // Sponge end
    entityIn.lastTickPosX = entityIn.posX;
    entityIn.lastTickPosY = entityIn.posY;
    entityIn.lastTickPosZ = entityIn.posZ;
    entityIn.prevRotationYaw = entityIn.rotationYaw;
    entityIn.prevRotationPitch = entityIn.rotationPitch;
    if (forceUpdate && entityIn.addedToChunk) {
        ++entityIn.ticksExisted;
        // Sponge
        ++co.aikar.timings.TimingHistory.activatedEntityTicks;
        if (entityIn.isRiding()) {
            entityIn.updateRidden();
        } else {
            entityIn.onUpdate();
        }
    }
    if (Double.isNaN(entityIn.posX) || Double.isInfinite(entityIn.posX)) {
        entityIn.posX = entityIn.lastTickPosX;
    }
    if (Double.isNaN(entityIn.posY) || Double.isInfinite(entityIn.posY)) {
        entityIn.posY = entityIn.lastTickPosY;
    }
    if (Double.isNaN(entityIn.posZ) || Double.isInfinite(entityIn.posZ)) {
        entityIn.posZ = entityIn.lastTickPosZ;
    }
    if (Double.isNaN(entityIn.rotationPitch) || Double.isInfinite(entityIn.rotationPitch)) {
        entityIn.rotationPitch = entityIn.prevRotationPitch;
    }
    if (Double.isNaN(entityIn.rotationYaw) || Double.isInfinite(entityIn.rotationYaw)) {
        entityIn.rotationYaw = entityIn.prevRotationYaw;
    }
    int l = MathHelper.floor(entityIn.posX / 16.0D);
    int i1 = MathHelper.floor(entityIn.posY / 16.0D);
    int j1 = MathHelper.floor(entityIn.posZ / 16.0D);
    if (!entityIn.addedToChunk || entityIn.chunkCoordX != l || entityIn.chunkCoordY != i1 || entityIn.chunkCoordZ != j1) {
        // Sponge start - use cached chunk
        final Chunk activeChunk = (Chunk) ((IMixinEntity) entityIn).getActiveChunk();
        if (activeChunk != null) {
            activeChunk.removeEntityAtIndex(entityIn, entityIn.chunkCoordY);
        }
        // Sponge end
        final IMixinChunk newChunk = (IMixinChunk) ((IMixinChunkProviderServer) entityIn.world.getChunkProvider()).getLoadedChunkWithoutMarkingActive(l, j1);
        final boolean isPositionDirty = entityIn.setPositionNonDirty();
        if (newChunk == null || (!isPositionDirty && newChunk.isQueuedForUnload() && !newChunk.isPersistedChunk())) {
            entityIn.addedToChunk = false;
        } else {
            ((net.minecraft.world.chunk.Chunk) newChunk).addEntity(entityIn);
        }
    }
    if (forceUpdate && entityIn.addedToChunk) {
        for (Entity entity : entityIn.getPassengers()) {
            if (!entity.isDead && entity.getRidingEntity() == entityIn) {
                this.updateEntity(entity);
            } else {
                entity.dismountRidingEntity();
            }
        }
    }
}
Also used : IModData_Activation(org.spongepowered.common.mixin.plugin.entityactivation.interfaces.IModData_Activation) Entity(net.minecraft.entity.Entity) IMixinEntity(org.spongepowered.common.interfaces.entity.IMixinEntity) IMixinChunk(org.spongepowered.common.interfaces.IMixinChunk) IMixinChunk(org.spongepowered.common.interfaces.IMixinChunk) Chunk(net.minecraft.world.chunk.Chunk) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Aggregations

IModData_Activation (org.spongepowered.common.mixin.plugin.entityactivation.interfaces.IModData_Activation)9 ITickable (net.minecraft.util.ITickable)4 IMixinChunk (org.spongepowered.common.interfaces.IMixinChunk)4 IMixinEntity (org.spongepowered.common.interfaces.entity.IMixinEntity)4 Entity (net.minecraft.entity.Entity)3 IMixinTileEntity (org.spongepowered.common.interfaces.block.tile.IMixinTileEntity)3 IMixinWorldServer (org.spongepowered.common.interfaces.world.IMixinWorldServer)3 EntityFireworkRocket (net.minecraft.entity.item.EntityFireworkRocket)2 EntityPlayer (net.minecraft.entity.player.EntityPlayer)2 TileEntity (net.minecraft.tileentity.TileEntity)2 Chunk (net.minecraft.world.chunk.Chunk)2 EntityType (org.spongepowered.api.entity.EntityType)2 SpongeEntityType (org.spongepowered.common.entity.SpongeEntityType)2 IMixinWorld (org.spongepowered.common.interfaces.world.IMixinWorld)2 Vector3i (com.flowpowered.math.vector.Vector3i)1 Map (java.util.Map)1 MultiPartEntityPart (net.minecraft.entity.MultiPartEntityPart)1 EntityDragon (net.minecraft.entity.boss.EntityDragon)1 EntityWither (net.minecraft.entity.boss.EntityWither)1 EntityWeatherEffect (net.minecraft.entity.effect.EntityWeatherEffect)1