Skip to content

Commit

Permalink
Code flow improvements:
Browse files Browse the repository at this point in the history
Wrapped repetitive getEnabledModule() calls into convenience functions.
  • Loading branch information
Achille004 committed Oct 11, 2024
1 parent 32d8cef commit 6611883
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 42 deletions.
41 changes: 21 additions & 20 deletions src/main/java/meranha/mekaweapons/MekaWeaponsUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,44 +17,46 @@
import net.minecraft.world.item.ItemStack;

public class MekaWeaponsUtils {
public static long getTotalDamage(@NotNull ItemStack stack, @Nullable IModule<ModuleWeaponAttackAmplificationUnit> 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<ModuleWeaponAttackAmplificationUnit> attackAmplificationUnit, @NotNull CachedIntValue baseDamage, @NotNull CachedLongValue energyUsage) {
return getTotalDamage(weapon, attackAmplificationUnit, baseDamage.get(), energyUsage.get());
}

public static long getTotalDamage(@NotNull ItemStack stack, @Nullable IModule<ModuleWeaponAttackAmplificationUnit> attackAmplificationUnit, int baseDamage, long energyUsage) {
IEnergyContainer energyContainer = StorageUtils.getEnergyContainer(stack, 0);
public static long getTotalDamage(@NotNull ItemStack weapon, @Nullable IModule<ModuleWeaponAttackAmplificationUnit> 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<ModuleWeaponAttackAmplificationUnit> 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<ModuleWeaponAttackAmplificationUnit> attackAmplificationUnit, long energyUsage) {
public static long getEnergyNeeded(@Nullable ItemStack weaponStack, long energyUsage) {
IModule<ModuleWeaponAttackAmplificationUnit> attackAmplificationUnit = getEnabledModule(weaponStack, MekaWeapons.ATTACKAMPLIFICATION_UNIT);
if (attackAmplificationUnit != null) {
return getEnergyNeeded(attackAmplificationUnit.getCustomInstance().getDamage(), energyUsage);
}
Expand All @@ -75,8 +77,7 @@ public static int getBarCustomColor(@NotNull ItemStack stack, long energyUsage)
return MekanismConfig.client.hudDangerColor.get();
}

IModule<ModuleWeaponAttackAmplificationUnit> 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();
}
Expand All @@ -88,12 +89,12 @@ private static boolean hasNotEnoughEnergy(@Nullable IEnergyContainer energyConta
return energyContainer == null || energyContainer.getEnergy() < minEnergy;
}

@Nullable
public static <MODULE extends ICustomModule<MODULE>> IModule<MODULE> getEnabledModule(ItemStack stack, IModuleDataProvider<MODULE> typeProvider) {
@Nullable
public static <MODULE extends ICustomModule<MODULE>> IModule<MODULE> getEnabledModule(ItemStack stack, IModuleDataProvider<MODULE> typeProvider) {
return IModuleHelper.INSTANCE.getIfEnabled(stack, typeProvider);
}

public static boolean isModuleEnabled(ItemStack stack, IModuleDataProvider<?> type) {
return IModuleHelper.INSTANCE.isEnabled(stack, type);
}
}
}
14 changes: 4 additions & 10 deletions src/main/java/meranha/mekaweapons/items/ItemMekaBow.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,9 @@ public void appendHoverText(@NotNull ItemStack stack, @NotNull Item.TooltipConte
}

public void adjustAttributes(@NotNull ItemAttributeModifierEvent event) {
ItemStack stack = event.getItemStack();
IModule<ModuleWeaponAttackAmplificationUnit> 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);
}

Expand All @@ -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<ItemStack> 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<ModuleWeaponAttackAmplificationUnit> 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);
Expand All @@ -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<ModuleWeaponAttackAmplificationUnit> 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));
}

Expand Down
19 changes: 7 additions & 12 deletions src/main/java/meranha/mekaweapons/items/ItemMekaTana.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<ModuleWeaponAttackAmplificationUnit> 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);
Expand All @@ -89,10 +87,7 @@ public boolean hurtEnemy(@NotNull ItemStack stack, @NotNull LivingEntity target,
}

public void adjustAttributes(@NotNull ItemAttributeModifierEvent event) {
ItemStack stack = event.getItemStack();
IModule<ModuleWeaponAttackAmplificationUnit> 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);
Expand Down Expand Up @@ -141,6 +136,11 @@ public InteractionResultHolder<ItemStack> 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;
}
Expand All @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/assets/mekaweapons/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 6611883

Please sign in to comment.