Skip to content

Commit

Permalink
Move tooltip helper methods to the tooltip modifier hook
Browse files Browse the repository at this point in the history
Still in modifier deprecated, reason for the move is in the future, most people using them will not be extensions of modifier
  • Loading branch information
KnightMiner committed Jun 7, 2023
1 parent a863604 commit f131d9c
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import slimeknights.mantle.data.GenericLoaderRegistry.IHaveLoader;
import slimeknights.tconstruct.common.TinkerTags;
import slimeknights.tconstruct.library.modifiers.ModifierManager.ModifierRegistrationEvent;
import slimeknights.tconstruct.library.modifiers.hook.TooltipModifierHook;
import slimeknights.tconstruct.library.modifiers.hook.combat.MeleeDamageModifierHook;
import slimeknights.tconstruct.library.modifiers.hook.combat.MeleeHitModifierHook;
import slimeknights.tconstruct.library.modifiers.hook.interaction.InteractionSource;
Expand Down Expand Up @@ -849,48 +850,28 @@ public static float getMiningModifier(LivingEntity entity) {
return modifier;
}

/**
* Adds a flat bonus tooltip
* @param name Bonus name
* @param bonus Bonus amount
* @param tooltip Tooltip list
*/
/** @deprecated use {@link TooltipModifierHook#addFlatBoost(Modifier, Component, double, List)} */
@Deprecated
protected void addFlatBoost(Component name, double bonus, List<Component> tooltip) {
tooltip.add(applyStyle(new TextComponent(Util.BONUS_FORMAT.format(bonus) + " ").append(name)));
TooltipModifierHook.addFlatBoost(this, name, bonus, tooltip);
}

/**
* Adds a percent bonus tooltip
* @param name Bonus name
* @param bonus Bonus amount
* @param tooltip Tooltip list
*/
/** @deprecated use {@link TooltipModifierHook#addPercentBoost(Modifier, Component, double, List)} (Modifier, Component, double, List)} */
@Deprecated
protected void addPercentTooltip(Component name, double bonus, List<Component> tooltip) {
tooltip.add(applyStyle(new TextComponent(Util.PERCENT_BOOST_FORMAT.format(bonus) + " ").append(name)));
TooltipModifierHook.addPercentBoost(this, name, bonus, tooltip);
}

/**
* Adds a tooltip showing a bonus stat
* @param tool Tool instance
* @param stat Stat added
* @param condition Condition to show the tooltip
* @param amount Amount to show, before scaling by the tool's modifier
* @param tooltip Tooltip list
*/
/** @deprecated use {@link TooltipModifierHook#addStatBoost(IToolStackView, Modifier, FloatToolStat, TagKey, float, List)} */
@Deprecated
protected void addStatTooltip(IToolStackView tool, FloatToolStat stat, TagKey<Item> condition, float amount, List<Component> tooltip) {
if (tool.hasTag(condition)) {
addFlatBoost(new TranslatableComponent(getTranslationKey() + "." + stat.getName().getPath()), amount * tool.getMultiplier(stat), tooltip);
}
TooltipModifierHook.addStatBoost(tool, this, stat, condition, amount, tooltip);
}

/**
* Adds a tooltip showing the bonus damage and the type of damage
* @param tool Tool instance
* @param amount Damage amount
* @param tooltip Tooltip
*/
/** @deprecated use {@link TooltipModifierHook#addDamageBoost(IToolStackView, Modifier, float, List)} */
@Deprecated
protected void addDamageTooltip(IToolStackView tool, float amount, List<Component> tooltip) {
addStatTooltip(tool, ToolStats.ATTACK_DAMAGE, TinkerTags.Items.MELEE_OR_UNARMED, amount, tooltip);
TooltipModifierHook.addDamageBoost(tool, this, amount, tooltip);
}

/** Tries an expected module against the given module type, returning null if failing. Do not use if you extend another modifier with modules */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
package slimeknights.tconstruct.library.modifiers.hook;

import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.tags.TagKey;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.TooltipFlag;
import slimeknights.mantle.client.TooltipKey;
import slimeknights.tconstruct.common.TinkerTags;
import slimeknights.tconstruct.library.modifiers.Modifier;
import slimeknights.tconstruct.library.modifiers.ModifierEntry;
import slimeknights.tconstruct.library.tools.nbt.IToolStackView;
import slimeknights.tconstruct.library.tools.stat.FloatToolStat;
import slimeknights.tconstruct.library.tools.stat.ToolStats;
import slimeknights.tconstruct.library.utils.Util;

import javax.annotation.Nullable;
import java.util.Collection;
Expand Down Expand Up @@ -35,4 +44,54 @@ public void addTooltip(IToolStackView tool, ModifierEntry modifier, @Nullable Pl
}
}
}


/* Helpers */

/** Adds a flat bonus tooltip */
static void addFlatBoost(Modifier modifier, Component name, double bonus, List<Component> tooltip) {
tooltip.add(modifier.applyStyle(new TextComponent(Util.BONUS_FORMAT.format(bonus) + " ").append(name)));
}

/** Adds a percentage boost tooltip */
static void addPercentBoost(Modifier modifier, Component name, double bonus, List<Component> tooltip) {
tooltip.add(modifier.applyStyle(new TextComponent(Util.PERCENT_BOOST_FORMAT.format(bonus) + " ").append(name)));
}

/**
* Adds a tooltip showing a bonus stat
* @param tool Tool instance
* @param modifier Modifier for style
* @param stat Stat added
* @param condition Condition to show the tooltip
* @param amount Amount to show, before scaling by the tool's modifier
* @param tooltip Tooltip list
*/
static void addStatBoost(IToolStackView tool, Modifier modifier, FloatToolStat stat, TagKey<Item> condition, float amount, List<Component> tooltip) {
if (tool.hasTag(condition)) {
addFlatBoost(modifier, new TranslatableComponent(modifier.getTranslationKey() + "." + stat.getName().getPath()), amount * tool.getMultiplier(stat), tooltip);
}
}

/**
* Adds a tooltip showing the bonus damage and the type of damage
* @param tool Tool instance
* @param modifier Modifier for style
* @param amount Damage amount
* @param tooltip Tooltip
*/
static void addDamageBoost(IToolStackView tool, Modifier modifier, float amount, List<Component> tooltip) {
addStatBoost(tool, modifier, ToolStats.ATTACK_DAMAGE, TinkerTags.Items.MELEE_OR_UNARMED, amount, tooltip);
}

/**
* Adds a tooltip showing the bonus damage and the type of damage dded
* @param tool Tool instance
* @param modifier Modifier and level
* @param levelAmount Bonus per level
* @param tooltip Tooltip
*/
static void addDamageBoost(IToolStackView tool, ModifierEntry modifier, float levelAmount, List<Component> tooltip) {
addDamageBoost(tool, modifier.getModifier(), modifier.getEffectiveLevel(tool) * levelAmount, tooltip);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.minecraft.nbt.Tag;
import net.minecraft.network.chat.Component;
import slimeknights.tconstruct.library.modifiers.Modifier;
import slimeknights.tconstruct.library.modifiers.ModifierEntry;
import slimeknights.tconstruct.library.modifiers.ModifierId;
import slimeknights.tconstruct.library.recipe.modifiers.ModifierRecipeLookup;
import slimeknights.tconstruct.library.tools.nbt.IModDataView;
Expand Down Expand Up @@ -111,7 +112,7 @@ public float getEffectiveLevel(IToolContext tool, int level) {
* @param tool Tool instance
* @param level Modifier level
* @return Level, possibly reduced by an incomplete level
* @deprecated use {@link #getEffectiveLevel(IToolContext, int)}
* @deprecated use {@link #getEffectiveLevel(IToolContext, int)} or {@link ModifierEntry#getEffectiveLevel(IToolContext)}
*/
@Deprecated
public float getScaledLevel(IToolContext tool, int level) {
Expand All @@ -128,13 +129,8 @@ public static void setAmount(ModDataNBT persistentData, ModifierId modifier, int
persistentData.putInt(modifier, amount);
}

/**
* Adds a tooltip showing the bonus damage and the type of damage dded
* @param tool Tool instance
* @param level Current level
* @param levelAmount Bonus per level
* @param tooltip Tooltip
*/
/** @deprecated use {@link slimeknights.tconstruct.library.modifiers.hook.TooltipModifierHook#addDamageBoost(IToolStackView, ModifierEntry, float, List)} */
@Deprecated
protected void addDamageTooltip(IToolStackView tool, int level, float levelAmount, List<Component> tooltip) {
addDamageTooltip(tool, getScaledLevel(tool, level) * levelAmount, tooltip);
}
Expand Down

0 comments on commit f131d9c

Please sign in to comment.