Skip to content

Commit

Permalink
Fixed tooltip rounding and added attack and damage calculators for di…
Browse files Browse the repository at this point in the history
…fferent tools
  • Loading branch information
anthonymendez committed Aug 13, 2024
1 parent 7a0686f commit 4c7edee
Showing 1 changed file with 61 additions and 6 deletions.
67 changes: 61 additions & 6 deletions common/src/main/java/com/anthonymendez/items/SimpleToolUtils.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package com.anthonymendez.items;

import com.mojang.datafixers.util.Pair;
import java.text.DecimalFormat;
import java.util.List;
import net.minecraft.component.type.AttributeModifiersComponent;
import net.minecraft.entity.attribute.EntityAttributeModifier;
import net.minecraft.item.Item;
import net.minecraft.item.MiningToolItem;
import net.minecraft.item.SwordItem;
import net.minecraft.item.*;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.minecraft.util.Identifier;

public class SimpleToolUtils {

private static final DecimalFormat TOOLTIP_FORMAT = new DecimalFormat("#0.#");

/**
* Retrieves the attack damage and speed from a {@link MiningToolItem} in a {@link Pair} whereby
* the order is:
Expand Down Expand Up @@ -43,7 +44,61 @@ public static Pair<Float, Float> getAttackDamageAndSpeedFromSwordItem(
SwordItem swordItem, float baseAttackDamage, float baseAttackSpeed) {
List<AttributeModifiersComponent.Entry> modifiers =
SwordItem.createAttributeModifiers(
swordItem.getMaterial(), (int)baseAttackDamage, baseAttackSpeed)
swordItem.getMaterial(), (int) baseAttackDamage, baseAttackSpeed)
.modifiers();
return getAttackDamageAndSpeedFromAttributeModifiersComponentEntryList(
modifiers, baseAttackDamage, baseAttackSpeed);
}

/**
* Retrieves the attack damage and speed from a {@link ShovelItem} in a {@link Pair} whereby the
* order is:
*
* <p>* {@code Pair.of(attackDamage, attackSpeed)}
*
* <p>Base attack damage and speed need to be provided as it's dependent off the weapon.
*/
public static Pair<Float, Float> getAttackDamageAndSpeedFromShovelItem(
ShovelItem shovelItem, float baseAttackDamage, float baseAttackSpeed) {
List<AttributeModifiersComponent.Entry> modifiers =
ShovelItem.createAttributeModifiers(
shovelItem.getMaterial(), (int) baseAttackDamage, baseAttackSpeed)
.modifiers();
return getAttackDamageAndSpeedFromAttributeModifiersComponentEntryList(
modifiers, baseAttackDamage, baseAttackSpeed);
}

/**
* Retrieves the attack damage and speed from a {@link HoeItem} in a {@link Pair} whereby the
* order is:
*
* <p>* {@code Pair.of(attackDamage, attackSpeed)}
*
* <p>Base attack damage and speed need to be provided as it's dependent off the weapon.
*/
public static Pair<Float, Float> getAttackDamageAndSpeedFromHoeItem(
HoeItem hoeItem, float baseAttackDamage, float baseAttackSpeed) {
List<AttributeModifiersComponent.Entry> modifiers =
HoeItem.createAttributeModifiers(
hoeItem.getMaterial(), (int) baseAttackDamage, baseAttackSpeed)
.modifiers();
return getAttackDamageAndSpeedFromAttributeModifiersComponentEntryList(
modifiers, baseAttackDamage, baseAttackSpeed);
}

/**
* Retrieves the attack damage and speed from a {@link AxeItem} in a {@link Pair} whereby the
* order is:
*
* <p>* {@code Pair.of(attackDamage, attackSpeed)}
*
* <p>Base attack damage and speed need to be provided as it's dependent off the weapon.
*/
public static Pair<Float, Float> getAttackDamageAndSpeedFromAxeItem(
AxeItem axeItem, float baseAttackDamage, float baseAttackSpeed) {
List<AttributeModifiersComponent.Entry> modifiers =
AxeItem.createAttributeModifiers(
axeItem.getMaterial(), (int) baseAttackDamage, baseAttackSpeed)
.modifiers();
return getAttackDamageAndSpeedFromAttributeModifiersComponentEntryList(
modifiers, baseAttackDamage, baseAttackSpeed);
Expand Down Expand Up @@ -109,10 +164,10 @@ public static void AppendDefaultMinecraftMiningItemTooltip(
tooltip.add(Text.of(""));
tooltip.add(Text.translatable("When in Main Hand:").formatted(Formatting.GRAY));
tooltip.add(
Text.translatable(String.format(" %.1f Attack Damage", attackDamage))
Text.translatable(String.format(" %s Attack Damage", TOOLTIP_FORMAT.format(attackDamage)))
.formatted(Formatting.DARK_GREEN));
tooltip.add(
Text.translatable(String.format(" %.1f Attack Speed", attackSpeed))
Text.translatable(String.format(" %s Attack Speed", TOOLTIP_FORMAT.format(attackSpeed)))
.formatted(Formatting.DARK_GREEN));
}
}

0 comments on commit 4c7edee

Please sign in to comment.