Skip to content

Commit

Permalink
Renewed energy bar coloring:
Browse files Browse the repository at this point in the history
  - green when gun operates normally
  - yellow when energy is not enough for a full shot
  - red when energy is not enough to use
Made projectiles pickup-able in survival when shot from an actual arrow.
  • Loading branch information
Achille004 committed Sep 23, 2024
1 parent fafa50e commit 32d8cef
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 41 deletions.
57 changes: 53 additions & 4 deletions src/main/java/meranha/mekaweapons/MekaWeaponsUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,23 @@
import org.jetbrains.annotations.Nullable;

import mekanism.api.energy.IEnergyContainer;
import mekanism.api.gear.ICustomModule;
import mekanism.api.gear.IModule;
import mekanism.api.gear.IModuleHelper;
import mekanism.api.math.MathUtils;
import mekanism.api.providers.IModuleDataProvider;
import mekanism.common.config.MekanismConfig;
import mekanism.common.config.value.CachedIntValue;
import mekanism.common.config.value.CachedLongValue;
import mekanism.common.util.StorageUtils;
import meranha.mekaweapons.items.ModuleWeaponAttackAmplificationUnit;
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 stack, @Nullable IModule<ModuleWeaponAttackAmplificationUnit> attackAmplificationUnit, int baseDamage, long energyUsage) {
IEnergyContainer energyContainer = StorageUtils.getEnergyContainer(stack, 0);
long energy = energyContainer != null ? energyContainer.getEnergy() : 0;
Expand All @@ -23,7 +33,7 @@ public static long getTotalDamage(@NotNull ItemStack stack, @Nullable IModule<Mo
int unitDamage = attackAmplificationUnit.getCustomInstance().getDamage();
if (unitDamage > 0) {
double additionalDamage = baseDamage * attackAmplificationUnit.getCustomInstance().getDamageMultiplicator();
long energyCost = getEnergyNeeded(energyUsage, unitDamage);
long energyCost = getEnergyNeeded(unitDamage, energyUsage);
// todo always max damage if in creative
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)
Expand All @@ -37,14 +47,53 @@ public static long getTotalDamage(@NotNull ItemStack stack, @Nullable IModule<Mo
return Math.round(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 IModule<ModuleWeaponAttackAmplificationUnit> attackAmplificationUnit, long energyUsage) {
if (attackAmplificationUnit != null) {
return getEnergyNeeded(energyUsage, attackAmplificationUnit.getCustomInstance().getDamage());
return getEnergyNeeded(attackAmplificationUnit.getCustomInstance().getDamage(), energyUsage);
}
return MekaWeapons.general.mekaBowEnergyUsage.get();
return -1;
}

private static long getEnergyNeeded(double energyUsage, int unitDamage) {
public static long getEnergyNeeded(int unitDamage, long energyUsage) {
return MathUtils.clampToLong(energyUsage * (1 + unitDamage / 4F));
}

public static int getBarCustomColor(@NotNull ItemStack stack, @NotNull CachedLongValue energyUsage) {
return getBarCustomColor(stack, energyUsage.get());
}

public static int getBarCustomColor(@NotNull ItemStack stack, long energyUsage) {
IEnergyContainer energyContainer = StorageUtils.getEnergyContainer(stack, 0);
if(hasNotEnoughEnergy(energyContainer, energyUsage)) {
return MekanismConfig.client.hudDangerColor.get();
}

IModule<ModuleWeaponAttackAmplificationUnit> attackAmplificationUnit = getEnabledModule(stack, MekaWeapons.ATTACKAMPLIFICATION_UNIT);
long energyNeeded = MekaWeaponsUtils.getEnergyNeeded(attackAmplificationUnit, energyUsage);
if (hasNotEnoughEnergy(energyContainer, energyNeeded)) {
return MekanismConfig.client.hudWarningColor.get();
}

return MekanismConfig.client.energyColor.get();
}

private static boolean hasNotEnoughEnergy(@Nullable IEnergyContainer energyContainer, long minEnergy) {
return energyContainer == null || energyContainer.getEnergy() < minEnergy;
}

@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);
}
}
37 changes: 17 additions & 20 deletions src/main/java/meranha/mekaweapons/items/ItemMekaBow.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import mekanism.client.key.MekKeyHandler;
import mekanism.client.key.MekanismKeyHandler;
import mekanism.common.MekanismLang;
import mekanism.common.config.MekanismConfig;
import mekanism.common.content.gear.IRadialModuleContainerItem;
import mekanism.common.content.gear.ModuleHelper;
import mekanism.common.util.StorageUtils;
Expand Down Expand Up @@ -76,7 +75,7 @@ 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.get(), MekaWeapons.general.mekaBowEnergyUsage.get());
double totalDamage = MekaWeaponsUtils.getTotalDamage(stack, attackAmplificationUnit, 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?
Expand All @@ -102,34 +101,24 @@ public void releaseUsing(@NotNull ItemStack bow, @NotNull Level world, @NotNull
super.releaseUsing(bow, world, entity, timeLeft);
}

private boolean hasEnoughEnergy(IEnergyContainer energyContainer) {
return energyContainer != null && energyContainer.getEnergy() >= MekaWeapons.general.mekaBowEnergyUsage.get();
}

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);

IEnergyContainer energyContainer = StorageUtils.getEnergyContainer(bow, 0);
if (!hasEnoughEnergy(energyContainer)) {
// todo warn no energy
return;
}
if(potentialAmmo.isEmpty() && !isModuleEnabled(bow, MekaWeapons.ARROWENERGY_UNIT)) {
// todo warn no ammo
return;
if(energyContainer != null) {
energyContainer.extract(energyNeeded, Action.EXECUTE, AutomationType.MANUAL);
}

IModule<ModuleWeaponAttackAmplificationUnit> attackAmplificationUnit = getEnabledModule(bow, MekaWeapons.ATTACKAMPLIFICATION_UNIT);
long energyNeeded = MekaWeaponsUtils.getEnergyNeeded(attackAmplificationUnit, MekaWeapons.general.mekaBowEnergyUsage.get());
energyContainer.extract(energyNeeded, Action.EXECUTE, AutomationType.MANUAL);
}
}

@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.get(), MekaWeapons.general.mekaBowEnergyUsage.get());
return new MekaArrowEntity(arrow.level(), arrow.getX(), arrow.getY(), arrow.getZ(), projectileStack, isModuleEnabled(weaponStack, MekaWeapons.GRAVITYDAMPENER_UNIT), MathUtils.clampToInt(totalDamage));
long totalDamage = MekaWeaponsUtils.getTotalDamage(weaponStack, attackAmplificationUnit, MekaWeapons.general.mekaBowBaseDamage, MekaWeapons.general.mekaBowEnergyUsage);
return new MekaArrowEntity(arrow.level(), arrow.getX(), arrow.getY(), arrow.getZ(), projectileStack, weaponStack, MathUtils.clampToInt(totalDamage));
}

public boolean shouldCauseReequipAnimation(@NotNull ItemStack oldStack, @NotNull ItemStack newStack, boolean slotChanged) {
Expand All @@ -149,7 +138,7 @@ public int getBarWidth(@NotNull ItemStack stack) {
}

public int getBarColor(@NotNull ItemStack stack) {
return MekanismConfig.client.energyColor.get();
return MekaWeaponsUtils.getBarCustomColor(stack, MekaWeapons.general.mekaBowEnergyUsage);
}

public boolean shouldCauseBlockBreakReset(@NotNull ItemStack oldStack, @NotNull ItemStack newStack) {
Expand All @@ -165,6 +154,14 @@ public float getUseTick(@NotNull ItemStack stack) {
return useTick;
}

public boolean isEnchantable(@NotNull ItemStack stack) {
return false;
}

public boolean isBookEnchantable(@NotNull ItemStack stack, @NotNull ItemStack book) {
return false;
}

public ResourceLocation getRadialIdentifier() {
return RADIAL_ID;
}
Expand Down
32 changes: 18 additions & 14 deletions src/main/java/meranha/mekaweapons/items/ItemMekaTana.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,35 +67,31 @@ public void onDestroyed(@NotNull ItemEntity item, @NotNull DamageSource damageSo
public void appendHoverText(@NotNull ItemStack stack, @NotNull Item.TooltipContext context, @NotNull List<Component> tooltip, @NotNull TooltipFlag flag) {
if (MekKeyHandler.isKeyPressed(MekanismKeyHandler.detailsKey)) {
addModuleDetails(stack, tooltip);
} else {
StorageUtils.addStoredEnergy(stack, tooltip, true);
tooltip.add(MekanismLang.HOLD_FOR_MODULES.translateColored(EnumColor.GRAY, EnumColor.INDIGO, MekanismKeyHandler.detailsKey.getTranslatedKeyMessage()));
return;
}
}

private boolean hasEnoughEnergy(IEnergyContainer energyContainer) {
return energyContainer != null && energyContainer.getEnergy() >= MekaWeapons.general.mekaTanaEnergyUsage.get();
StorageUtils.addStoredEnergy(stack, tooltip, true);
tooltip.add(MekanismLang.HOLD_FOR_MODULES.translateColored(EnumColor.GRAY, EnumColor.INDIGO, MekanismKeyHandler.detailsKey.getTranslatedKeyMessage()));
}


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);

IEnergyContainer energyContainer = StorageUtils.getEnergyContainer(stack, 0);
if(!hasEnoughEnergy(energyContainer)) {
// todo warn no energy
return false;
if(energyContainer != null) {
energyContainer.extract(energyNeeded, Action.EXECUTE, AutomationType.MANUAL);
}

IModule<ModuleWeaponAttackAmplificationUnit> attackAmplificationUnit = getEnabledModule(stack, MekaWeapons.ATTACKAMPLIFICATION_UNIT);
long energyNeeded = MekaWeaponsUtils.getEnergyNeeded(attackAmplificationUnit, MekaWeapons.general.mekaTanaEnergyUsage.get());
energyContainer.extract(energyNeeded, Action.EXECUTE, AutomationType.MANUAL);
}
return true;
}

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.get(), MekaWeapons.general.mekaTanaEnergyUsage.get());
double totalDamage = MekaWeaponsUtils.getTotalDamage(stack, attackAmplificationUnit, 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);
Expand Down Expand Up @@ -145,6 +141,14 @@ public InteractionResultHolder<ItemStack> use(@NotNull Level world, @NotNull Pla
return InteractionResultHolder.pass(stack);
}

public boolean isBarVisible(@NotNull ItemStack stack) {
return true;
}

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());
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/meranha/mekaweapons/items/MekaArrowEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.jetbrains.annotations.NotNull;

import meranha.mekaweapons.MekaWeapons;
import meranha.mekaweapons.MekaWeaponsUtils;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.projectile.AbstractArrow;
import net.minecraft.world.item.ItemStack;
Expand All @@ -14,9 +15,10 @@ public MekaArrowEntity(EntityType<? extends MekaArrowEntity> entityType, Level l
super(entityType, level);
}

public MekaArrowEntity(Level level, double x, double y, double z, ItemStack itemStack, boolean noGravity, int damage) {
super(MekaWeapons.MEKA_ARROW.get(), x, y, z, level, itemStack, null);
this.setNoGravity(noGravity);
public MekaArrowEntity(Level level, double x, double y, double z, ItemStack projectileStack, ItemStack weaponStack, int damage) {
super(MekaWeapons.MEKA_ARROW.get(), x, y, z, level, projectileStack, null);
this.setPickup(!MekaWeaponsUtils.isModuleEnabled(weaponStack, MekaWeapons.ARROWENERGY_UNIT));
this.setNoGravity(MekaWeaponsUtils.isModuleEnabled(weaponStack, MekaWeapons.GRAVITYDAMPENER_UNIT));
this.setBaseDamage(damage);
}

Expand All @@ -32,6 +34,10 @@ public void tick() {
}
}

public void setPickup(boolean pickup) {
this.pickup = pickup ? Pickup.ALLOWED : Pickup.CREATIVE_ONLY;
}

@NotNull
protected ItemStack getDefaultPickupItem() {
return new ItemStack(Items.ARROW);
Expand Down

0 comments on commit 32d8cef

Please sign in to comment.