Package de.Keyle.MyPet.api.skill
Class UpgradeComputer<T>
java.lang.Object
de.Keyle.MyPet.api.skill.UpgradeComputer<T>
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumIndicates whether the value change was caused by adding or removing modifiers.static interfaceListener interface notified whenever theUpgradeComputer's current value changes due to modifier addition or removal. -
Constructor Summary
ConstructorsConstructorDescriptionUpgradeComputer(T baseValue) Creates a new computer with the given base (default) value. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddCallback(UpgradeComputer.UpgradeCallback<T> callback) Registers a callback to be notified whenever the computed value changes.voidaddUpgrade(UpgradeModifier<T> upgrade) Appends a single modifier to the history, updates the current value by applying it, and notifies all callbacks withUpgradeComputer.CallbackReason.Add.voidaddUpgrades(Collection<UpgradeModifier<T>> upgrades) Appends multiple modifiers to the history in order, recomputes the current value, and notifies callbacks once withUpgradeComputer.CallbackReason.Add.getValue()Returns the current computed value after all applied modifiers.voidClears all modifiers and resets the current value to the base value.voidremoveCallback(UpgradeComputer.UpgradeCallback<T> callback) Removes a previously registered callback.voidremoveUpgrade(UpgradeModifier<T> upgrade) Removes the last occurrence of the given modifier from the history, then recomputes the current value from scratch (base + remaining modifiers).voidremoveUpgrades(Collection<UpgradeModifier<T>> upgrades) Removes the last occurrence of each modifier in the collection, then recomputes the current value from scratch.
-
Constructor Details
-
UpgradeComputer
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
Returns the current computed value after all applied modifiers.- Returns:
- the accumulated value across every applied upgrade
-
addUpgrade
Appends a single modifier to the history, updates the current value by applying it, and notifies all callbacks withUpgradeComputer.CallbackReason.Add.- Parameters:
upgrade- the modifier to add (ignored ifnull)
-
addUpgrades
Appends multiple modifiers to the history in order, recomputes the current value, and notifies callbacks once withUpgradeComputer.CallbackReason.Add.- Parameters:
upgrades- the modifiers to add (ignored ifnullor empty)
-
removeUpgrade
Removes the last occurrence of the given modifier from the history, then recomputes the current value from scratch (base + remaining modifiers). Notifies callbacks withUpgradeComputer.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 ifnullor not currently applied)
-
removeUpgrades
Removes the last occurrence of each modifier in the collection, then recomputes the current value from scratch. Notifies callbacks once withUpgradeComputer.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 ifnullor empty)
-
removeAllUpgrades
public void removeAllUpgrades()Clears all modifiers and resets the current value to the base value. Notifies all callbacks withUpgradeComputer.CallbackReason.Remove. -
addCallback
Registers a callback to be notified whenever the computed value changes.- Parameters:
callback- run whenever the value changes
-
removeCallback
Removes a previously registered callback.- Parameters:
callback- the callback to stop running
-