Class UpgradeComputer<T>

java.lang.Object
de.Keyle.MyPet.api.skill.UpgradeComputer<T>
Type Parameters:
T - the value type (typically Number, Boolean, or an enum)

public class UpgradeComputer<T> extends Object
Tracks the cumulative value of a single skill property (e.g. damage, chance, cooldown) as upgrade modifiers are applied and removed. The computer maintains an ordered history of all applied UpgradeModifiers and recomputes the current value from the baseValue whenever modifiers change, making skilltree resets fully reversible.

Registered UpgradeComputer.UpgradeCallbacks are notified after every value change, allowing skills to react immediately (e.g. update an attribute on the pet entity).

See Also:
  • Constructor Details

    • UpgradeComputer

      public UpgradeComputer(T baseValue)
      Creates a new computer with the given base (default) value. The current value starts equal to the base value and is recalculated as modifiers are added or removed.
      Parameters:
      baseValue - the initial value before any upgrades
  • Method Details

    • getValue

      public T getValue()
      Returns the current computed value after all applied modifiers.
      Returns:
      the accumulated value across every applied upgrade
    • addUpgrade

      public void addUpgrade(UpgradeModifier<T> upgrade)
      Appends a single modifier to the history, updates the current value by applying it, and notifies all callbacks with UpgradeComputer.CallbackReason.Add.
      Parameters:
      upgrade - the modifier to add (ignored if null)
    • addUpgrades

      public void addUpgrades(Collection<UpgradeModifier<T>> upgrades)
      Appends multiple modifiers to the history in order, recomputes the current value, and notifies callbacks once with UpgradeComputer.CallbackReason.Add.
      Parameters:
      upgrades - the modifiers to add (ignored if null or empty)
    • removeUpgrade

      public void removeUpgrade(UpgradeModifier<T> upgrade)
      Removes the last occurrence of the given modifier from the history, then recomputes the current value from scratch (base + remaining modifiers). Notifies callbacks with UpgradeComputer.CallbackReason.Remove. If the modifier was never applied (for example a level-down after a reset), nothing changes and no callbacks run.
      Parameters:
      upgrade - the modifier to remove (ignored if null or not currently applied)
    • removeUpgrades

      public void removeUpgrades(Collection<UpgradeModifier<T>> upgrades)
      Removes the last occurrence of each modifier in the collection, then recomputes the current value from scratch. Notifies callbacks once with UpgradeComputer.CallbackReason.Remove. Modifiers that are not currently applied are skipped; if none of them were applied, nothing changes and no callbacks run.
      Parameters:
      upgrades - the modifiers to remove (ignored if null or empty)
    • removeAllUpgrades

      public void removeAllUpgrades()
      Clears all modifiers and resets the current value to the base value. Notifies all callbacks with UpgradeComputer.CallbackReason.Remove.
    • addCallback

      public void addCallback(UpgradeComputer.UpgradeCallback<T> callback)
      Registers a callback to be notified whenever the computed value changes.
      Parameters:
      callback - run whenever the value changes
    • removeCallback

      public void removeCallback(UpgradeComputer.UpgradeCallback<T> callback)
      Removes a previously registered callback.
      Parameters:
      callback - the callback to stop running