Search in sources :

Example 16 with IEnergyContainer

use of mekanism.api.energy.IEnergyContainer in project Mekanism by mekanism.

the class ModuleShearingUnit method onItemUse.

@Nonnull
@Override
public ActionResultType onItemUse(IModule<ModuleShearingUnit> module, ItemUseContext context) {
    IEnergyContainer energyContainer = module.getEnergyContainer();
    if (energyContainer == null || energyContainer.getEnergy().smallerThan(MekanismConfig.gear.mekaToolEnergyUsageShearBlock.get())) {
        return ActionResultType.PASS;
    }
    BlockState state = context.getLevel().getBlockState(context.getClickedPos());
    return MekanismUtils.performActions(carvePumpkin(energyContainer, context, state), () -> shearBeehive(energyContainer, context, state));
}
Also used : IEnergyContainer(mekanism.api.energy.IEnergyContainer) BlockState(net.minecraft.block.BlockState) Nonnull(javax.annotation.Nonnull)

Example 17 with IEnergyContainer

use of mekanism.api.energy.IEnergyContainer in project Mekanism by mekanism.

the class ModuleInhalationPurificationUnit method tickServer.

@Override
public void tickServer(IModule<ModuleInhalationPurificationUnit> module, PlayerEntity player) {
    FloatingLong usage = MekanismConfig.gear.mekaSuitEnergyUsagePotionTick.get();
    boolean free = usage.isZero() || player.isCreative();
    IEnergyContainer energyContainer = free ? null : module.getEnergyContainer();
    if (free || (energyContainer != null && energyContainer.getEnergy().greaterOrEqual(usage))) {
        // Gather all the active effects that we can handle, so that we have them in their own list and
        // don't run into any issues related to CMEs
        List<EffectInstance> effects = player.getActiveEffects().stream().filter(effect -> canHandle(effect.getEffect().getCategory())).collect(Collectors.toList());
        for (EffectInstance effect : effects) {
            if (free) {
                speedupEffect(player, effect);
            } else if (module.useEnergy(player, energyContainer, usage, true).isZero()) {
                // If we can't actually extract energy, exit
                break;
            } else {
                speedupEffect(player, effect);
                if (energyContainer.getEnergy().smallerThan(usage)) {
                    // If after using energy, our remaining energy is now smaller than how much we need to use, exit
                    break;
                }
            }
        }
    }
}
Also used : ModuleBooleanData(mekanism.api.gear.config.ModuleBooleanData) IModule(mekanism.api.gear.IModule) PlayerEntity(net.minecraft.entity.player.PlayerEntity) ICustomModule(mekanism.api.gear.ICustomModule) ModuleConfigItemCreator(mekanism.api.gear.config.ModuleConfigItemCreator) Collectors(java.util.stream.Collectors) DamageSource(net.minecraft.util.DamageSource) ParametersAreNonnullByDefault(javax.annotation.ParametersAreNonnullByDefault) IModuleConfigItem(mekanism.api.gear.config.IModuleConfigItem) MekanismUtils(mekanism.common.util.MekanismUtils) List(java.util.List) EffectInstance(net.minecraft.potion.EffectInstance) FloatingLong(mekanism.api.math.FloatingLong) EffectType(net.minecraft.potion.EffectType) IEnergyContainer(mekanism.api.energy.IEnergyContainer) MekanismLang(mekanism.common.MekanismLang) MekanismConfig(mekanism.common.config.MekanismConfig) Nullable(javax.annotation.Nullable) FloatingLong(mekanism.api.math.FloatingLong) IEnergyContainer(mekanism.api.energy.IEnergyContainer) EffectInstance(net.minecraft.potion.EffectInstance)

Example 18 with IEnergyContainer

use of mekanism.api.energy.IEnergyContainer in project Mekanism by mekanism.

the class ModuleMagneticAttractionUnit method tickServer.

@Override
public void tickServer(IModule<ModuleMagneticAttractionUnit> module, PlayerEntity player) {
    if (range.get() != Range.OFF) {
        float size = 4 + range.get().getRange();
        FloatingLong usage = MekanismConfig.gear.mekaSuitEnergyUsageItemAttraction.get().multiply(range.get().getRange());
        boolean free = usage.isZero() || player.isCreative();
        IEnergyContainer energyContainer = free ? null : module.getEnergyContainer();
        if (free || (energyContainer != null && energyContainer.getEnergy().greaterOrEqual(usage))) {
            // If the energy cost is free, or we have enough energy for at least one pull grab all the items that can be picked up.
            // Note: We check distance afterwards so that we aren't having to calculate a bunch of distances when we may run out
            // of energy, and calculating distance is a bit more expensive than just checking if it can be picked up
            List<ItemEntity> items = player.level.getEntitiesOfClass(ItemEntity.class, player.getBoundingBox().inflate(size, size, size), item -> !item.hasPickUpDelay());
            for (ItemEntity item : items) {
                if (item.distanceTo(player) > 0.001) {
                    if (free) {
                        pullItem(player, item);
                    } else if (module.useEnergy(player, energyContainer, usage, true).isZero()) {
                        // If we can't actually extract energy, exit
                        break;
                    } else {
                        pullItem(player, item);
                        if (energyContainer.getEnergy().smallerThan(usage)) {
                            // If after using energy, our energy is now smaller than how much we need to use, exit
                            break;
                        }
                    }
                }
            }
        }
    }
}
Also used : FloatingLong(mekanism.api.math.FloatingLong) ItemEntity(net.minecraft.entity.item.ItemEntity) IEnergyContainer(mekanism.api.energy.IEnergyContainer)

Example 19 with IEnergyContainer

use of mekanism.api.energy.IEnergyContainer in project Mekanism by mekanism.

the class ModuleChargeDistributionUnit method chargeSuit.

private void chargeSuit(PlayerEntity player) {
    FloatingLong total = FloatingLong.ZERO;
    EnergySaveTarget saveTarget = new EnergySaveTarget(4);
    for (ItemStack stack : player.inventory.armor) {
        IEnergyContainer energyContainer = StorageUtils.getEnergyContainer(stack, 0);
        if (energyContainer != null) {
            saveTarget.addDelegate(energyContainer);
            total = total.plusEqual(energyContainer.getEnergy());
        }
    }
    EmitUtils.sendToAcceptors(saveTarget, total);
    saveTarget.save();
}
Also used : FloatingLong(mekanism.api.math.FloatingLong) EnergySaveTarget(mekanism.common.content.network.distribution.EnergySaveTarget) IEnergyContainer(mekanism.api.energy.IEnergyContainer) ItemStack(net.minecraft.item.ItemStack)

Example 20 with IEnergyContainer

use of mekanism.api.energy.IEnergyContainer in project Mekanism by mekanism.

the class ModuleSolarRechargingUnit method tickServer.

@Override
public void tickServer(IModule<ModuleSolarRechargingUnit> module, PlayerEntity player) {
    IEnergyContainer energyContainer = module.getEnergyContainer();
    if (energyContainer != null && !energyContainer.getNeeded().isZero()) {
        // Use the position that is roughly where the solar panel is
        BlockPos pos = new BlockPos(player.getX(), player.getEyeY() + 0.2, player.getZ());
        // Based on how TileEntitySolarGenerator and the rest of our solar things do energy calculations
        if (WorldUtils.canSeeSun(player.level, pos)) {
            Biome b = player.level.getBiomeManager().getBiome(pos);
            boolean needsRainCheck = b.getPrecipitation() != RainType.NONE;
            // Consider the best temperature to be 0.8; biomes that are higher than that
            // will suffer an efficiency loss (semiconductors don't like heat); biomes that are cooler
            // get a boost. We scale the efficiency to around 30% so that it doesn't totally dominate
            float tempEff = 0.3F * (0.8F - b.getTemperature(pos));
            // Treat rainfall as a proxy for humidity; any humidity works as a drag on overall efficiency.
            // As with temperature, we scale it so that it doesn't overwhelm production. Note the signedness
            // on the scaling factor. Also note that we only use rainfall as a proxy if it CAN rain; some dimensions
            // (like the End) have rainfall set, but can't actually support rain.
            float humidityEff = needsRainCheck ? -0.3F * b.getDownfall() : 0.0F;
            FloatingLong peakOutput = MekanismConfig.gear.mekaSuitSolarRechargingRate.get().multiply(1.0F + tempEff + humidityEff);
            // Get the brightness of the sun; note that there are some implementations that depend on the base
            // brightness function which doesn't take into account the fact that rain can't occur in some biomes.
            float brightness = WorldUtils.getSunBrightness(player.level, 1.0F);
            // Production is a function of the peak possible output in this biome and sun's current brightness
            FloatingLong production = peakOutput.multiply(brightness);
            // If the generator is in a biome where it can rain, and it's raining penalize production by 80%
            if (needsRainCheck && (player.level.isRaining() || player.level.isThundering())) {
                production = production.timesEqual(RAIN_MULTIPLIER);
            }
            // Multiply actual production based on how many modules are installed
            energyContainer.insert(production.multiply(module.getInstalledCount()), Action.EXECUTE, AutomationType.MANUAL);
        }
    }
}
Also used : FloatingLong(mekanism.api.math.FloatingLong) IEnergyContainer(mekanism.api.energy.IEnergyContainer) Biome(net.minecraft.world.biome.Biome) BlockPos(net.minecraft.util.math.BlockPos)

Aggregations

IEnergyContainer (mekanism.api.energy.IEnergyContainer)34 FloatingLong (mekanism.api.math.FloatingLong)26 BlockPos (net.minecraft.util.math.BlockPos)13 PlayerEntity (net.minecraft.entity.player.PlayerEntity)12 ItemStack (net.minecraft.item.ItemStack)12 Nonnull (javax.annotation.Nonnull)10 Direction (net.minecraft.util.Direction)8 World (net.minecraft.world.World)8 BlockState (net.minecraft.block.BlockState)6 ServerPlayerEntity (net.minecraft.entity.player.ServerPlayerEntity)6 ArrayList (java.util.ArrayList)5 List (java.util.List)4 Map (java.util.Map)4 Nullable (javax.annotation.Nullable)4 TileEntityMekanism (mekanism.common.tile.base.TileEntityMekanism)4 TileEntity (net.minecraft.tileentity.TileEntity)4 SyncableFloatingLong (mekanism.common.inventory.container.sync.SyncableFloatingLong)3 EnumMap (java.util.EnumMap)2 HashMap (java.util.HashMap)2 Set (java.util.Set)2