From 66118832d9dc28849853838f0c251c0f4d8fb6af Mon Sep 17 00:00:00 2001 From: Francesco Marras <2004marras@gmail.com> Date: Fri, 11 Oct 2024 20:49:33 +0200 Subject: [PATCH] Code flow improvements: Wrapped repetitive getEnabledModule() calls into convenience functions. --- .../meranha/mekaweapons/MekaWeaponsUtils.java | 41 ++++++++++--------- .../mekaweapons/items/ItemMekaBow.java | 14 ++----- .../mekaweapons/items/ItemMekaTana.java | 19 ++++----- .../assets/mekaweapons/lang/en_us.json | 1 + 4 files changed, 33 insertions(+), 42 deletions(-) diff --git a/src/main/java/meranha/mekaweapons/MekaWeaponsUtils.java b/src/main/java/meranha/mekaweapons/MekaWeaponsUtils.java index 7b892b2..6aae169 100644 --- a/src/main/java/meranha/mekaweapons/MekaWeaponsUtils.java +++ b/src/main/java/meranha/mekaweapons/MekaWeaponsUtils.java @@ -17,44 +17,46 @@ import net.minecraft.world.item.ItemStack; public class MekaWeaponsUtils { - public static long getTotalDamage(@NotNull ItemStack stack, @Nullable IModule attackAmplificationUnit, @NotNull CachedIntValue baseDamage, @NotNull CachedLongValue energyUsage) { - return getTotalDamage(stack, attackAmplificationUnit, baseDamage.get(), energyUsage.get()); + public static long getTotalDamage(@NotNull ItemStack weapon, @NotNull CachedIntValue baseDamage, @NotNull CachedLongValue energyUsage) { + return getTotalDamage(weapon, getEnabledModule(weapon, MekaWeapons.ATTACKAMPLIFICATION_UNIT), baseDamage, energyUsage); + } + + public static long getTotalDamage(@NotNull ItemStack weapon, @Nullable IModule attackAmplificationUnit, @NotNull CachedIntValue baseDamage, @NotNull CachedLongValue energyUsage) { + return getTotalDamage(weapon, attackAmplificationUnit, baseDamage.get(), energyUsage.get()); } - public static long getTotalDamage(@NotNull ItemStack stack, @Nullable IModule attackAmplificationUnit, int baseDamage, long energyUsage) { - IEnergyContainer energyContainer = StorageUtils.getEnergyContainer(stack, 0); + public static long getTotalDamage(@NotNull ItemStack weapon, @Nullable IModule attackAmplificationUnit, int baseDamage, long energyUsage) { + IEnergyContainer energyContainer = StorageUtils.getEnergyContainer(weapon, 0); long energy = energyContainer != null ? energyContainer.getEnergy() : 0; if(energy < energyUsage) { return -1; } - double damage = baseDamage; + long damage = baseDamage; if (attackAmplificationUnit != null) { int unitDamage = attackAmplificationUnit.getCustomInstance().getDamage(); if (unitDamage > 0) { - double additionalDamage = baseDamage * attackAmplificationUnit.getCustomInstance().getDamageMultiplicator(); + long additionalDamage = MathUtils.clampToLong(baseDamage * attackAmplificationUnit.getCustomInstance().getDamageMultiplicator()); long energyCost = getEnergyNeeded(unitDamage, energyUsage); // todo always max damage if in creative - if (energy < energyCost){ + if (energy < energyCost) { //If we don't have enough power use it at a reduced power level (this will be false the majority of the time) - damage += additionalDamage * MathUtils.divideToLevel(energy - energyUsage, energyCost - energyUsage); + damage += Math.round(additionalDamage * MathUtils.divideToLevel(energy - energyUsage, energyCost - energyUsage)); } else { damage += additionalDamage; } } } - return Math.round(damage) - 1; + return damage - 1; } - public static long getEnergyNeeded(@Nullable IModule attackAmplificationUnit, CachedLongValue energyUsage) { - if (attackAmplificationUnit != null) { - return getEnergyNeeded(attackAmplificationUnit.getCustomInstance().getDamage(), energyUsage.get()); - } - return -1; + public static long getEnergyNeeded(@Nullable ItemStack weaponStack, @NotNull CachedLongValue energyUsage) { + return getEnergyNeeded(weaponStack, energyUsage.get()); } - public static long getEnergyNeeded(@Nullable IModule attackAmplificationUnit, long energyUsage) { + public static long getEnergyNeeded(@Nullable ItemStack weaponStack, long energyUsage) { + IModule attackAmplificationUnit = getEnabledModule(weaponStack, MekaWeapons.ATTACKAMPLIFICATION_UNIT); if (attackAmplificationUnit != null) { return getEnergyNeeded(attackAmplificationUnit.getCustomInstance().getDamage(), energyUsage); } @@ -75,8 +77,7 @@ public static int getBarCustomColor(@NotNull ItemStack stack, long energyUsage) return MekanismConfig.client.hudDangerColor.get(); } - IModule attackAmplificationUnit = getEnabledModule(stack, MekaWeapons.ATTACKAMPLIFICATION_UNIT); - long energyNeeded = MekaWeaponsUtils.getEnergyNeeded(attackAmplificationUnit, energyUsage); + long energyNeeded = getEnergyNeeded(stack, energyUsage); if (hasNotEnoughEnergy(energyContainer, energyNeeded)) { return MekanismConfig.client.hudWarningColor.get(); } @@ -88,12 +89,12 @@ private static boolean hasNotEnoughEnergy(@Nullable IEnergyContainer energyConta return energyContainer == null || energyContainer.getEnergy() < minEnergy; } - @Nullable - public static > IModule getEnabledModule(ItemStack stack, IModuleDataProvider typeProvider) { + @Nullable + public static > IModule getEnabledModule(ItemStack stack, IModuleDataProvider typeProvider) { return IModuleHelper.INSTANCE.getIfEnabled(stack, typeProvider); } public static boolean isModuleEnabled(ItemStack stack, IModuleDataProvider type) { return IModuleHelper.INSTANCE.isEnabled(stack, type); } -} +} \ No newline at end of file diff --git a/src/main/java/meranha/mekaweapons/items/ItemMekaBow.java b/src/main/java/meranha/mekaweapons/items/ItemMekaBow.java index 5c05af1..e8ddccb 100644 --- a/src/main/java/meranha/mekaweapons/items/ItemMekaBow.java +++ b/src/main/java/meranha/mekaweapons/items/ItemMekaBow.java @@ -73,12 +73,9 @@ public void appendHoverText(@NotNull ItemStack stack, @NotNull Item.TooltipConte } public void adjustAttributes(@NotNull ItemAttributeModifierEvent event) { - ItemStack stack = event.getItemStack(); - IModule attackAmplificationUnit = getEnabledModule(stack, MekaWeapons.ATTACKAMPLIFICATION_UNIT); - double totalDamage = MekaWeaponsUtils.getTotalDamage(stack, attackAmplificationUnit, MekaWeapons.general.mekaBowBaseDamage, MekaWeapons.general.mekaBowEnergyUsage); - + long totalDamage = MekaWeaponsUtils.getTotalDamage(event.getItemStack(), MekaWeapons.general.mekaBowBaseDamage, MekaWeapons.general.mekaBowEnergyUsage); event.addModifier(Attributes.ATTACK_DAMAGE, new AttributeModifier(BASE_ATTACK_DAMAGE_ID, totalDamage, Operation.ADD_VALUE), EquipmentSlotGroup.MAINHAND); - //event.addModifier(Attributes.ATTACK_SPEED, new AttributeModifier(BASE_ATTACK_SPEED_ID, (5 * installedModules) -9, Operation.ADD_VALUE), EquipmentSlotGroup.MAINHAND); todo? + // event.addModifier(Attributes.ATTACK_SPEED, new AttributeModifier(BASE_ATTACK_SPEED_ID, (5 * installedModules) -9, Operation.ADD_VALUE), EquipmentSlotGroup.MAINHAND); todo? IRadialModuleContainerItem.super.adjustAttributes(event); } @@ -104,9 +101,7 @@ public void releaseUsing(@NotNull ItemStack bow, @NotNull Level world, @NotNull protected void shoot(@NotNull ServerLevel world, @NotNull LivingEntity entity, @NotNull InteractionHand hand, @NotNull ItemStack bow, @NotNull List potentialAmmo, float velocity, float inaccuracy, boolean critical, @Nullable LivingEntity target) { super.shoot(world, entity, hand, bow, potentialAmmo, velocity, inaccuracy, critical, target); if(entity instanceof Player player && !player.isCreative()) { - IModule attackAmplificationUnit = getEnabledModule(bow, MekaWeapons.ATTACKAMPLIFICATION_UNIT); - long energyNeeded = MekaWeaponsUtils.getEnergyNeeded(attackAmplificationUnit, MekaWeapons.general.mekaBowEnergyUsage); - + long energyNeeded = MekaWeaponsUtils.getEnergyNeeded(bow, MekaWeapons.general.mekaBowEnergyUsage); IEnergyContainer energyContainer = StorageUtils.getEnergyContainer(bow, 0); if(energyContainer != null) { energyContainer.extract(energyNeeded, Action.EXECUTE, AutomationType.MANUAL); @@ -116,8 +111,7 @@ protected void shoot(@NotNull ServerLevel world, @NotNull LivingEntity entity, @ @NotNull public AbstractArrow customArrow(@NotNull AbstractArrow arrow, @NotNull ItemStack projectileStack, @NotNull ItemStack weaponStack) { - IModule attackAmplificationUnit = getEnabledModule(weaponStack, MekaWeapons.ATTACKAMPLIFICATION_UNIT); - long totalDamage = MekaWeaponsUtils.getTotalDamage(weaponStack, attackAmplificationUnit, MekaWeapons.general.mekaBowBaseDamage, MekaWeapons.general.mekaBowEnergyUsage); + long totalDamage = MekaWeaponsUtils.getTotalDamage(weaponStack, MekaWeapons.general.mekaBowBaseDamage, MekaWeapons.general.mekaBowEnergyUsage); return new MekaArrowEntity(arrow.level(), arrow.getX(), arrow.getY(), arrow.getZ(), projectileStack, weaponStack, MathUtils.clampToInt(totalDamage)); } diff --git a/src/main/java/meranha/mekaweapons/items/ItemMekaTana.java b/src/main/java/meranha/mekaweapons/items/ItemMekaTana.java index 6579b55..5df912a 100644 --- a/src/main/java/meranha/mekaweapons/items/ItemMekaTana.java +++ b/src/main/java/meranha/mekaweapons/items/ItemMekaTana.java @@ -77,9 +77,7 @@ public void appendHoverText(@NotNull ItemStack stack, @NotNull Item.TooltipConte public boolean hurtEnemy(@NotNull ItemStack stack, @NotNull LivingEntity target, @NotNull LivingEntity attacker) { if(attacker instanceof Player player && !player.isCreative()) { - IModule attackAmplificationUnit = getEnabledModule(stack, MekaWeapons.ATTACKAMPLIFICATION_UNIT); - long energyNeeded = MekaWeaponsUtils.getEnergyNeeded(attackAmplificationUnit, MekaWeapons.general.mekaTanaEnergyUsage); - + long energyNeeded = MekaWeaponsUtils.getEnergyNeeded(stack, MekaWeapons.general.mekaTanaEnergyUsage); IEnergyContainer energyContainer = StorageUtils.getEnergyContainer(stack, 0); if(energyContainer != null) { energyContainer.extract(energyNeeded, Action.EXECUTE, AutomationType.MANUAL); @@ -89,10 +87,7 @@ public boolean hurtEnemy(@NotNull ItemStack stack, @NotNull LivingEntity target, } public void adjustAttributes(@NotNull ItemAttributeModifierEvent event) { - ItemStack stack = event.getItemStack(); - IModule attackAmplificationUnit = getEnabledModule(stack, MekaWeapons.ATTACKAMPLIFICATION_UNIT); - double totalDamage = MekaWeaponsUtils.getTotalDamage(stack, attackAmplificationUnit, MekaWeapons.general.mekaTanaBaseDamage, MekaWeapons.general.mekaTanaEnergyUsage); - + long totalDamage = MekaWeaponsUtils.getTotalDamage(event.getItemStack(), MekaWeapons.general.mekaTanaBaseDamage, MekaWeapons.general.mekaTanaEnergyUsage); event.addModifier(Attributes.ATTACK_DAMAGE, new AttributeModifier(BASE_ATTACK_DAMAGE_ID, totalDamage, Operation.ADD_VALUE), EquipmentSlotGroup.MAINHAND); event.addModifier(Attributes.ATTACK_SPEED, new AttributeModifier(BASE_ATTACK_SPEED_ID, MekaWeapons.general.mekaTanaAttackSpeed.get(), Operation.ADD_VALUE), EquipmentSlotGroup.MAINHAND); IRadialModuleContainerItem.super.adjustAttributes(event); @@ -141,6 +136,11 @@ public InteractionResultHolder use(@NotNull Level world, @NotNull Pla return InteractionResultHolder.pass(stack); } + private boolean isValidDestinationBlock(@NotNull Level world, BlockPos pos) { + BlockState blockState = world.getBlockState(pos); + return blockState.isAir() || MekanismUtils.isLiquidBlock(blockState.getBlock()); + } + public boolean isBarVisible(@NotNull ItemStack stack) { return true; } @@ -149,11 +149,6 @@ public int getBarColor(@NotNull ItemStack stack) { return MekaWeaponsUtils.getBarCustomColor(stack, MekaWeapons.general.mekaTanaEnergyUsage); } - private boolean isValidDestinationBlock(@NotNull Level world, BlockPos pos) { - BlockState blockState = world.getBlockState(pos); - return blockState.isAir() || MekanismUtils.isLiquidBlock(blockState.getBlock()); - } - public boolean isEnchantable(@NotNull ItemStack stack) { return false; } diff --git a/src/main/resources/assets/mekaweapons/lang/en_us.json b/src/main/resources/assets/mekaweapons/lang/en_us.json index a8d435e..1118201 100644 --- a/src/main/resources/assets/mekaweapons/lang/en_us.json +++ b/src/main/resources/assets/mekaweapons/lang/en_us.json @@ -9,6 +9,7 @@ "tooltip.mekaweapons.autofire_mode_change": "Auto Fire Mode", "tooltip.mekaweapons.arrowenergy_mode": "Energy Arrows Mode: %s", "tooltip.mekaweapons.magnetizer": "Displays the Meka-Bow and the Meka-Tana in player's back if they are in inventory. (Curios needed)", + "radial.mekaweapons.attack_damage": "Attack Damage", "radial.mekaweapons.attack_damage.extreme": "Extreme", "radial.mekaweapons.attack_damage.high": "High", "radial.mekaweapons.attack_damage.low": "Low",