use of org.spongepowered.common.config.category.EntityActivationRangeCategory in project SpongeCommon by SpongePowered.
the class EntityActivationRange method addEntityToConfig.
public static void addEntityToConfig(World world, SpongeEntityType type, byte activationType) {
checkNotNull(world, "world");
checkNotNull(type, "type");
SpongeConfig<? extends GeneralConfigBase> config = ((IMixinWorldServer) world).getActiveConfig();
if (config == null || type == null) {
return;
}
final boolean autoPopulate = config.getConfig().getEntityActivationRange().autoPopulateData();
boolean requiresSave = false;
String entityType = "misc";
entityType = EntityActivationRange.activationTypeMappings.get(activationType);
final String entityModId = type.getModId().toLowerCase();
final String entityId = type.getName().toLowerCase();
EntityActivationRangeCategory activationCategory = config.getConfig().getEntityActivationRange();
EntityActivationModCategory entityMod = activationCategory.getModList().get(entityModId);
Integer defaultActivationRange = activationCategory.getDefaultRanges().get(entityType);
if (defaultActivationRange == null) {
defaultActivationRange = 32;
}
Integer activationRange = activationCategory.getDefaultRanges().get(entityType);
if (autoPopulate && entityMod == null) {
entityMod = new EntityActivationModCategory();
activationCategory.getModList().put(entityModId, entityMod);
requiresSave = true;
}
if (entityMod != null) {
final Integer modActivationRange = entityMod.getDefaultRanges().get(entityType);
if (autoPopulate && modActivationRange == null) {
entityMod.getDefaultRanges().put(entityType, defaultActivationRange);
requiresSave = true;
} else if (modActivationRange != null && modActivationRange > activationRange) {
activationRange = modActivationRange;
}
final Integer entityActivationRange = entityMod.getEntityList().get(entityId);
if (autoPopulate && entityActivationRange == null) {
entityMod.getEntityList().put(entityId, entityMod.getDefaultRanges().get(entityType));
requiresSave = true;
}
if (entityActivationRange != null && entityActivationRange > activationRange) {
activationRange = entityActivationRange;
}
}
// check max ranges
Integer maxRange = maxActivationRanges.get(activationType);
if (maxRange == null) {
maxActivationRanges.put(activationType, activationRange);
} else if (activationRange > maxRange) {
maxActivationRanges.put(activationType, activationRange);
}
if (autoPopulate && requiresSave) {
config.save();
}
}
use of org.spongepowered.common.config.category.EntityActivationRangeCategory in project SpongeCommon by SpongePowered.
the class EntityActivationRange method initializeEntityActivationState.
/**
* These entities are excluded from Activation range checks.
*
* @param entity Entity to check
* @return boolean If it should always tick.
*/
public static boolean initializeEntityActivationState(Entity entity) {
if (((IMixinWorld) entity.world).isFake()) {
return true;
}
// types that should always be active
if (entity instanceof EntityPlayer && !SpongeImplHooks.isFakePlayer(entity) || entity instanceof EntityThrowable || entity instanceof EntityDragon || entity instanceof MultiPartEntityPart || entity instanceof EntityWither || entity instanceof EntityFireball || entity instanceof EntityWeatherEffect || entity instanceof EntityTNTPrimed || entity instanceof EntityEnderCrystal || entity instanceof EntityFireworkRocket || // Always tick falling blocks
entity instanceof EntityFallingBlock) {
return true;
}
EntityActivationRangeCategory config = ((IMixinWorldServer) entity.world).getActiveConfig().getConfig().getEntityActivationRange();
EntityType type = ((org.spongepowered.api.entity.Entity) entity).getType();
IModData_Activation spongeEntity = (IModData_Activation) entity;
if (type == EntityTypes.UNKNOWN || !(type instanceof SpongeEntityType)) {
return false;
}
final SpongeEntityType spongeType = (SpongeEntityType) type;
final byte activationType = spongeEntity.getActivationType();
if (!spongeType.isActivationRangeInitialized()) {
addEntityToConfig(entity.world, spongeType, activationType);
spongeType.setActivationRangeInitialized(true);
}
EntityActivationModCategory entityMod = config.getModList().get(spongeType.getModId().toLowerCase());
int defaultActivationRange = config.getDefaultRanges().get(activationTypeMappings.get(activationType));
if (entityMod == null) {
// use default activation range
spongeEntity.setActivationRange(defaultActivationRange);
if (defaultActivationRange <= 0) {
return true;
}
return false;
} else if (!entityMod.isEnabled()) {
spongeEntity.setActivationRange(defaultActivationRange);
return true;
}
Integer defaultModActivationRange = entityMod.getDefaultRanges().get(activationTypeMappings.get(activationType));
Integer entityActivationRange = entityMod.getEntityList().get(type.getName().toLowerCase());
if (defaultModActivationRange != null && entityActivationRange == null) {
spongeEntity.setActivationRange(defaultModActivationRange);
if (defaultModActivationRange <= 0) {
return true;
}
return false;
} else if (entityActivationRange != null) {
spongeEntity.setActivationRange(entityActivationRange);
if (entityActivationRange <= 0) {
return true;
}
}
return false;
}
Aggregations