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;
}
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;
}
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());
}
}
}
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();
}
}
}
}
Aggregations