use of mekanism.api.math.FloatingLong in project Mekanism by mekanism.
the class CommonPlayerTickHandler method handleDamage.
private boolean handleDamage(LivingHurtEvent event, @Nullable IEnergyContainer energyContainer, FloatSupplier absorptionRatio, FloatingLongSupplier energyCost) {
if (energyContainer != null) {
float absorption = absorptionRatio.getAsFloat();
float amount = event.getAmount() * absorption;
FloatingLong energyRequirement = energyCost.get().multiply(amount);
float ratioAbsorbed;
if (energyRequirement.isZero()) {
// No energy is actually needed to absorb the damage, either because of the config
// or how small the amount to absorb is
ratioAbsorbed = absorption;
} else {
ratioAbsorbed = absorption * energyContainer.extract(energyRequirement, Action.EXECUTE, AutomationType.MANUAL).divide(amount).floatValue();
}
if (ratioAbsorbed > 0) {
float damageRemaining = event.getAmount() * Math.max(0, 1 - ratioAbsorbed);
if (damageRemaining <= 0) {
event.setCanceled(true);
return true;
} else {
event.setAmount(damageRemaining);
}
}
}
return false;
}
use of mekanism.api.math.FloatingLong in project Mekanism by mekanism.
the class CommonPlayerTickHandler method tryAbsorbAll.
private boolean tryAbsorbAll(LivingAttackEvent event, @Nullable IEnergyContainer energyContainer, FloatSupplier absorptionRatio, FloatingLongSupplier energyCost) {
if (energyContainer != null && absorptionRatio.getAsFloat() == 1) {
FloatingLong energyRequirement = energyCost.get().multiply(event.getAmount());
if (energyRequirement.isZero()) {
// No energy is actually needed to absorb the damage, either because of the config
// or how small the amount to absorb is
event.setCanceled(true);
return true;
}
FloatingLong simulatedExtract = energyContainer.extract(energyRequirement, Action.SIMULATE, AutomationType.MANUAL);
if (simulatedExtract.equals(energyRequirement)) {
// If we could fully negate the damage cancel the event and extract it
energyContainer.extract(energyRequirement, Action.EXECUTE, AutomationType.MANUAL);
event.setCanceled(true);
return true;
}
}
return false;
}
use of mekanism.api.math.FloatingLong in project Mekanism by mekanism.
the class CommonPlayerTickHandler method onLivingJump.
@SubscribeEvent
public void onLivingJump(LivingJumpEvent event) {
if (event.getEntityLiving() instanceof PlayerEntity) {
PlayerEntity player = (PlayerEntity) event.getEntityLiving();
IModule<ModuleHydraulicPropulsionUnit> module = MekanismAPI.getModuleHelper().load(player.getItemBySlot(EquipmentSlotType.FEET), MekanismModules.HYDRAULIC_PROPULSION_UNIT);
if (module != null && module.isEnabled() && Mekanism.keyMap.has(player.getUUID(), KeySync.BOOST)) {
float boost = module.getCustomInstance().getBoost();
FloatingLong usage = MekanismConfig.gear.mekaSuitBaseJumpEnergyUsage.get().multiply(boost / 0.1F);
IEnergyContainer energyContainer = module.getEnergyContainer();
if (module.canUseEnergy(player, energyContainer, usage, false)) {
// if we're sprinting with the boost module, limit the height
IModule<ModuleLocomotiveBoostingUnit> boostModule = MekanismAPI.getModuleHelper().load(player.getItemBySlot(EquipmentSlotType.LEGS), MekanismModules.LOCOMOTIVE_BOOSTING_UNIT);
if (boostModule != null && boostModule.isEnabled() && boostModule.getCustomInstance().canFunction(boostModule, player)) {
boost = (float) Math.sqrt(boost);
}
player.setDeltaMovement(player.getDeltaMovement().add(0, boost, 0));
module.useEnergy(player, energyContainer, usage, true);
}
}
}
}
use of mekanism.api.math.FloatingLong in project Mekanism by mekanism.
the class PlayerState method updateFlightInfo.
public void updateFlightInfo(PlayerEntity player) {
boolean isFlyingGameMode = !MekanismUtils.isPlayingMode(player);
boolean hasGravitationalModulator = CommonPlayerTickHandler.isGravitationalModulationReady(player);
FlightInfo flightInfo = flightInfoMap.computeIfAbsent(player.getUUID(), uuid -> new FlightInfo());
if (isFlyingGameMode || hasGravitationalModulator) {
// The player can fly
if (!flightInfo.hadFlightItem) {
// If they did not have a flight item
if (!player.abilities.mayfly) {
// and they are not already allowed to fly, then enable it
updateClientServerFlight(player, true);
}
// and mark that they had a flight item
flightInfo.hadFlightItem = true;
} else if (flightInfo.wasFlyingGameMode && !isFlyingGameMode) {
// The player was in a game mode that allowed flight, but no longer is, though they still are allowed to fly
// Sync the fact to the client. Also passes wasFlying so that if they were flying previously,
// and are still allowed to the game mode change doesn't force them out of it
updateClientServerFlight(player, true, flightInfo.wasFlying);
} else if (flightInfo.wasFlyingAllowed && !player.abilities.mayfly) {
// If we were allowed to fly but something changed that state (such as another mod)
// Re-enable flying and set the player back into flying if they were flying
updateClientServerFlight(player, true, flightInfo.wasFlying);
}
// Update flight info states
flightInfo.wasFlyingGameMode = isFlyingGameMode;
flightInfo.wasFlying = player.abilities.flying;
flightInfo.wasFlyingAllowed = player.abilities.mayfly;
if (player.abilities.flying && hasGravitationalModulator) {
// If the player is actively flying (not just allowed to), and has the gravitational modulator ready then apply movement boost if active, and use energy
IModule<ModuleGravitationalModulatingUnit> module = MekanismAPI.getModuleHelper().load(player.getItemBySlot(EquipmentSlotType.CHEST), MekanismModules.GRAVITATIONAL_MODULATING_UNIT);
if (module != null) {
// Should not be null but double check
FloatingLong usage = MekanismConfig.gear.mekaSuitEnergyUsageGravitationalModulation.get();
if (Mekanism.keyMap.has(player.getUUID(), KeySync.BOOST)) {
FloatingLong boostUsage = usage.multiply(4);
if (module.canUseEnergy(player, boostUsage, false)) {
float boost = module.getCustomInstance().getBoost();
if (boost > 0) {
player.moveRelative(boost, new Vector3d(0, 0, 1));
usage = boostUsage;
}
}
}
module.useEnergy(player, usage);
}
}
} else {
if (flightInfo.hadFlightItem) {
if (player.abilities.mayfly) {
updateClientServerFlight(player, false);
}
flightInfo.hadFlightItem = false;
}
flightInfo.wasFlyingGameMode = false;
flightInfo.wasFlying = player.abilities.flying;
flightInfo.wasFlyingAllowed = player.abilities.mayfly;
}
}
use of mekanism.api.math.FloatingLong in project Mekanism by mekanism.
the class MekanismJEI method getEnergyComponent.
private static String getEnergyComponent(ItemStack stack) {
Optional<IStrictEnergyHandler> capability = stack.getCapability(Capabilities.STRICT_ENERGY_CAPABILITY).resolve();
if (capability.isPresent()) {
IStrictEnergyHandler energyHandlerItem = capability.get();
String component = "";
int containers = energyHandlerItem.getEnergyContainerCount();
for (int container = 0; container < containers; container++) {
FloatingLong neededEnergy = energyHandlerItem.getNeededEnergy(container);
if (neededEnergy.isZero()) {
component = addInterpretation(component, "filled");
} else if (containers > 1) {
component = addInterpretation(component, "empty");
}
}
return component;
}
return IIngredientSubtypeInterpreter.NONE;
}
Aggregations