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