Class DynamicDescriptionFormatter

java.lang.Object
johnsmith.enchantmentcore.api.tooltip.DynamicDescriptionFormatter

public class DynamicDescriptionFormatter extends Object
Parses and formats dynamic enchantment descriptions using registered ValueModifiers and StyleModifiers.

Evaluates nested tag syntax in three sequential passes:

  1. Pointer interpolation (<val:key> and <array:key>) via DynamicDescriptionParser.
  2. Inside-out mathematical value transformation (e.g., <percent:<val:key>>, <fixed:2:<div:20:<val:ticks>>>).
  3. Linear stack-based rich text style application (e.g., <rgb=#FF5555:content>, <obf:text>, <sga:runes>).
  • Constructor Details

    • DynamicDescriptionFormatter

      public DynamicDescriptionFormatter()
  • Method Details

    • registerValueModifier

      public static void registerValueModifier(String name, ValueModifier modifier)
      Registers a global ValueModifier available to all dynamic descriptions. Invalidates the cached regex pattern so it is rebuilt on the next parse call.
      Parameters:
      name - The command identifier used in description tags (e.g., "percent").
      modifier - The ValueModifier implementation.
    • registerStyleModifier

      public static void registerStyleModifier(String name, StyleModifier modifier)
      Registers a global StyleModifier available to all dynamic descriptions.
      Parameters:
      name - The style command identifier used in description tags (e.g., "rgb").
      modifier - The StyleModifier implementation.
    • parseAndFormat

      public static net.minecraft.network.chat.MutableComponent parseAndFormat(String rawDescription, int currentLevel, int maxLevel, Map<String, net.minecraft.world.item.enchantment.LevelBasedValue> args, net.minecraft.network.chat.Style baseStyle, Map<String, StyleModifier> contextStyles)
      Evaluates a raw description string and constructs a formatted rich text MutableComponent.

      Executes pointer resolution, nested mathematical value modifications, and rich style tag applications sequentially.

      Parameters:
      rawDescription - The unformatted translation string containing dynamic tag syntax.
      currentLevel - The active enchantment level for <val:key> pointer evaluation.
      maxLevel - The maximum enchantment level for <array:key> bracket generation.
      args - The map of dynamic LevelBasedValue pointers extracted from the enchantment.
      baseStyle - The foundational Style applied to unstyled root text segments.
      contextStyles - An optional map of context-specific StyleModifiers that override global style modifiers.
      Returns:
      The fully evaluated and styled MutableComponent.
    • wrapRich

      public static List<net.minecraft.network.chat.Component> wrapRich(net.minecraft.network.chat.Component root, int maxChars, int nestingDepth)
      Wraps a styled rich text Component into multiple lines constrained by a maximum character count, preserving individual token styles across line breaks.
      Parameters:
      root - The source rich text Component to wrap.
      maxChars - The maximum character length permitted per line.
      nestingDepth - The number of leading spaces prepended to each wrapped line.
      Returns:
      A list of wrapped line Components.