Search in sources :

Example 1 with EntityActivationRangeCategory

use of org.spongepowered.common.config.inheritable.EntityActivationRangeCategory in project SpongeCommon by SpongePowered.

the class EntityActivationRange method addEntityToConfig.

public static void addEntityToConfig(final boolean autoPopulate, final ResourceLocation key, final byte activationType, final String activationTypeName) {
    final InheritableConfigHandle<GlobalConfig> globalConfig = SpongeGameConfigs.getGlobalInheritable();
    final EntityActivationRangeCategory activationConfig = globalConfig.get().entityActivationRange;
    boolean requiresSave = false;
    final Integer defaultRange = activationConfig.globalRanges.getOrDefault(activationTypeName, 32);
    Integer range = defaultRange;
    EntityActivationRangeCategory.ModSubCategory modSubCategory = activationConfig.mods.get(key.getNamespace());
    if (autoPopulate && modSubCategory == null) {
        modSubCategory = new EntityActivationRangeCategory.ModSubCategory();
        activationConfig.mods.put(key.getNamespace(), modSubCategory);
        requiresSave = true;
    }
    if (modSubCategory != null) {
        final Integer modActivationRange = modSubCategory.defaultRanges.get(activationTypeName);
        if (autoPopulate && modActivationRange == null) {
            modSubCategory.defaultRanges.put(activationTypeName, defaultRange);
            requiresSave = true;
        } else if (modActivationRange != null && modActivationRange > range) {
            range = modActivationRange;
        }
        final Integer entityActivationRange = modSubCategory.entities.get(key.getPath());
        if (autoPopulate && entityActivationRange == null) {
            modSubCategory.entities.put(key.getPath(), modSubCategory.defaultRanges.get(activationTypeName));
            requiresSave = true;
        }
        if (entityActivationRange != null && entityActivationRange > range) {
            range = entityActivationRange;
        }
    }
    // check max ranges
    final int newRange = range;
    EntityActivationRange.maxActivationRanges.compute(activationType, (k, maxRange) -> maxRange == null || newRange > maxRange ? newRange : maxRange);
    if (autoPopulate && requiresSave) {
        globalConfig.save();
    }
}
Also used : GlobalConfig(org.spongepowered.common.config.inheritable.GlobalConfig) EntityActivationRangeCategory(org.spongepowered.common.config.inheritable.EntityActivationRangeCategory)

Example 2 with EntityActivationRangeCategory

use of org.spongepowered.common.config.inheritable.EntityActivationRangeCategory in project SpongeCommon by SpongePowered.

the class EntityActivationRange method initializeEntityActivationState.

/**
 * Initialize entity activation state.
 *
 * @param entity Entity to check
 */
public static void initializeEntityActivationState(final Entity entity) {
    final ActivationCapabilityBridge spongeEntity = (ActivationCapabilityBridge) entity;
    if (entity.level.isClientSide()) {
        return;
    }
    // types that should always be active
    if (entity instanceof Player && !((PlatformEntityBridge) entity).bridge$isFakePlayer() || entity instanceof ThrowableProjectile || entity instanceof EnderDragon || entity instanceof EnderDragonPart || entity instanceof WitherBoss || entity instanceof AbstractHurtingProjectile || entity instanceof LightningBolt || entity instanceof PrimedTnt || entity instanceof Painting || entity instanceof EndCrystal || entity instanceof FireworkRocketEntity || // Always tick falling blocks
    entity instanceof FallingBlockEntity) {
        return;
    }
    final InheritableConfigHandle<WorldConfig> configAdapter = SpongeGameConfigs.getForWorld(entity.level);
    final EntityActivationRangeCategory config = configAdapter.get().entityActivationRange;
    final EntityTypeBridge type = (EntityTypeBridge) entity.getType();
    final ResourceLocation key = EntityType.getKey(entity.getType());
    final byte activationType = spongeEntity.activation$getActivationType();
    final String activationTypeName = EntityActivationRange.activationTypeMappings.getOrDefault(activationType, "misc");
    if (!type.bridge$isActivationRangeInitialized()) {
        EntityActivationRange.addEntityToConfig(config.autoPopulate, key, activationType, activationTypeName);
        type.bridge$setActivationRangeInitialized(true);
    }
    final EntityActivationRangeCategory.ModSubCategory entityMod = config.mods.get(key.getNamespace());
    final int defaultActivationRange = config.globalRanges.get(activationTypeName);
    if (entityMod == null) {
        // use default activation range
        spongeEntity.activation$setActivationRange(defaultActivationRange);
        if (defaultActivationRange > 0) {
            spongeEntity.activation$setDefaultActivationState(false);
        }
    } else {
        if (!entityMod.enabled) {
            spongeEntity.activation$setDefaultActivationState(true);
            return;
        }
        final Integer defaultModActivationRange = entityMod.defaultRanges.get(activationTypeName);
        final Integer entityActivationRange = entityMod.entities.get(key.getPath());
        if (defaultModActivationRange != null && entityActivationRange == null) {
            spongeEntity.activation$setActivationRange(defaultModActivationRange);
            if (defaultModActivationRange > 0) {
                spongeEntity.activation$setDefaultActivationState(false);
            }
        } else if (entityActivationRange != null) {
            spongeEntity.activation$setActivationRange(entityActivationRange);
            if (entityActivationRange > 0) {
                spongeEntity.activation$setDefaultActivationState(false);
            }
        }
    }
}
Also used : PrimedTnt(net.minecraft.world.entity.item.PrimedTnt) FallingBlockEntity(net.minecraft.world.entity.item.FallingBlockEntity) Player(net.minecraft.world.entity.player.Player) ServerPlayer(net.minecraft.server.level.ServerPlayer) WorldConfig(org.spongepowered.common.config.inheritable.WorldConfig) EntityTypeBridge(org.spongepowered.common.bridge.world.entity.EntityTypeBridge) EndCrystal(net.minecraft.world.entity.boss.enderdragon.EndCrystal) FireworkRocketEntity(net.minecraft.world.entity.projectile.FireworkRocketEntity) Painting(net.minecraft.world.entity.decoration.Painting) LightningBolt(net.minecraft.world.entity.LightningBolt) EnderDragon(net.minecraft.world.entity.boss.enderdragon.EnderDragon) ActivationCapabilityBridge(org.spongepowered.common.bridge.activation.ActivationCapabilityBridge) WitherBoss(net.minecraft.world.entity.boss.wither.WitherBoss) EnderDragonPart(net.minecraft.world.entity.boss.EnderDragonPart) AbstractHurtingProjectile(net.minecraft.world.entity.projectile.AbstractHurtingProjectile) ResourceLocation(net.minecraft.resources.ResourceLocation) ThrowableProjectile(net.minecraft.world.entity.projectile.ThrowableProjectile) EntityActivationRangeCategory(org.spongepowered.common.config.inheritable.EntityActivationRangeCategory)

Aggregations

EntityActivationRangeCategory (org.spongepowered.common.config.inheritable.EntityActivationRangeCategory)2 ResourceLocation (net.minecraft.resources.ResourceLocation)1 ServerPlayer (net.minecraft.server.level.ServerPlayer)1 LightningBolt (net.minecraft.world.entity.LightningBolt)1 EnderDragonPart (net.minecraft.world.entity.boss.EnderDragonPart)1 EndCrystal (net.minecraft.world.entity.boss.enderdragon.EndCrystal)1 EnderDragon (net.minecraft.world.entity.boss.enderdragon.EnderDragon)1 WitherBoss (net.minecraft.world.entity.boss.wither.WitherBoss)1 Painting (net.minecraft.world.entity.decoration.Painting)1 FallingBlockEntity (net.minecraft.world.entity.item.FallingBlockEntity)1 PrimedTnt (net.minecraft.world.entity.item.PrimedTnt)1 Player (net.minecraft.world.entity.player.Player)1 AbstractHurtingProjectile (net.minecraft.world.entity.projectile.AbstractHurtingProjectile)1 FireworkRocketEntity (net.minecraft.world.entity.projectile.FireworkRocketEntity)1 ThrowableProjectile (net.minecraft.world.entity.projectile.ThrowableProjectile)1 ActivationCapabilityBridge (org.spongepowered.common.bridge.activation.ActivationCapabilityBridge)1 EntityTypeBridge (org.spongepowered.common.bridge.world.entity.EntityTypeBridge)1 GlobalConfig (org.spongepowered.common.config.inheritable.GlobalConfig)1 WorldConfig (org.spongepowered.common.config.inheritable.WorldConfig)1