Search in sources :

Example 1 with EntityActivationRangeCategory

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();
    }
}
Also used : IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) EntityActivationRangeCategory(org.spongepowered.common.config.category.EntityActivationRangeCategory) EntityActivationModCategory(org.spongepowered.common.config.category.EntityActivationModCategory)

Example 2 with EntityActivationRangeCategory

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;
}
Also used : EntityThrowable(net.minecraft.entity.projectile.EntityThrowable) EntityDragon(net.minecraft.entity.boss.EntityDragon) IMixinEntity(org.spongepowered.common.interfaces.entity.IMixinEntity) Entity(net.minecraft.entity.Entity) IModData_Activation(org.spongepowered.common.mixin.plugin.entityactivation.interfaces.IModData_Activation) EntityFallingBlock(net.minecraft.entity.item.EntityFallingBlock) EntityTNTPrimed(net.minecraft.entity.item.EntityTNTPrimed) EntityWeatherEffect(net.minecraft.entity.effect.EntityWeatherEffect) EntityEnderCrystal(net.minecraft.entity.item.EntityEnderCrystal) MultiPartEntityPart(net.minecraft.entity.MultiPartEntityPart) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) SpongeEntityType(org.spongepowered.common.entity.SpongeEntityType) EntityType(org.spongepowered.api.entity.EntityType) IMixinWorld(org.spongepowered.common.interfaces.world.IMixinWorld) EntityFireworkRocket(net.minecraft.entity.item.EntityFireworkRocket) SpongeEntityType(org.spongepowered.common.entity.SpongeEntityType) EntityPlayer(net.minecraft.entity.player.EntityPlayer) EntityWither(net.minecraft.entity.boss.EntityWither) EntityActivationRangeCategory(org.spongepowered.common.config.category.EntityActivationRangeCategory) EntityActivationModCategory(org.spongepowered.common.config.category.EntityActivationModCategory) EntityFireball(net.minecraft.entity.projectile.EntityFireball)

Aggregations

EntityActivationModCategory (org.spongepowered.common.config.category.EntityActivationModCategory)2 EntityActivationRangeCategory (org.spongepowered.common.config.category.EntityActivationRangeCategory)2 IMixinWorldServer (org.spongepowered.common.interfaces.world.IMixinWorldServer)2 Entity (net.minecraft.entity.Entity)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 EntityEnderCrystal (net.minecraft.entity.item.EntityEnderCrystal)1 EntityFallingBlock (net.minecraft.entity.item.EntityFallingBlock)1 EntityFireworkRocket (net.minecraft.entity.item.EntityFireworkRocket)1 EntityTNTPrimed (net.minecraft.entity.item.EntityTNTPrimed)1 EntityPlayer (net.minecraft.entity.player.EntityPlayer)1 EntityFireball (net.minecraft.entity.projectile.EntityFireball)1 EntityThrowable (net.minecraft.entity.projectile.EntityThrowable)1 EntityType (org.spongepowered.api.entity.EntityType)1 SpongeEntityType (org.spongepowered.common.entity.SpongeEntityType)1 IMixinEntity (org.spongepowered.common.interfaces.entity.IMixinEntity)1 IMixinWorld (org.spongepowered.common.interfaces.world.IMixinWorld)1 IModData_Activation (org.spongepowered.common.mixin.plugin.entityactivation.interfaces.IModData_Activation)1