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