Class MagicService


  • public class MagicService
    extends java.lang.Object
    • Constructor Summary

      Constructors 
      Constructor Description
      MagicService()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean canCast​(CastableSpell spell)
      Determines whether the specified spell can be cast by the player.
      boolean cast​(CastableSpell spell)
      Casts the specified spell, if it is valid and the necessary conditions are met.
      boolean castOn​(CastableSpell spell, net.runelite.api.GameObject target)
      Casts a spell on a specified target object.
      boolean castOn​(CastableSpell spell, net.runelite.api.NPC target)
      Attempts to cast the given spell on a specified NPC target.
      boolean castOn​(CastableSpell spell, net.runelite.api.widgets.Widget target)
      Attempts to cast the given spell on a specified widget target.
      boolean hasRequiredRunes​(CastableSpell spell)
      Checks if the player has the required runes to cast a given spell.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • MagicService

        public MagicService()
    • Method Detail

      • canCast

        public boolean canCast​(CastableSpell spell)
        Determines whether the specified spell can be cast by the player.

        This method verifies various conditions required to cast a spell, including:

        • Whether the client packets are properly loaded
        • Whether the spell is valid and belongs to the player's current spellbook
        • Whether the player's boosted Magic level meets or exceeds the spell's required level
        • Whether the player possesses the necessary runes to cast the spell
        • For specific CastableSpell requiring prayer, whether the player has sufficient Prayer points
        If any of these conditions fail, the method logs a warning and returns @false.
        Parameters:
        spell - The @CastableSpell instance representing the spell to check.
        • Must not be @null.
        • The spell must belong to the player's active spellbook to be castable.
        Returns:
        @true if all conditions for casting the spell are met, @false otherwise.

        Returns @false for invalid CastableSpell, mismatched spellbooks, insufficient Magic level, missing runes, or insufficient Prayer points for certain CastableSpell.

      • cast

        public boolean cast​(CastableSpell spell)
        Casts the specified spell, if it is valid and the necessary conditions are met.

        This method validates whether the spell can be cast, determines the appropriate action (especially for teleport spell variants), and enqueues the required packets to perform the spell cast.

        Note that this method handles teleport CastableSpell with multiple variants (e.g., Varrock Teleport vs. Grand Exchange Teleport) and calculates the correct action based on the player's configuration.

        Parameters:
        spell - The @CastableSpell instance representing the spell to be cast. Must not be null.
        • For teleport CastableSpell with variants, the variant action will be determined dynamically.
        • Ensure the correct spell is passed to avoid unintended behavior.
        Returns:
        @true if the spell was successfully cast, @false otherwise.

        Returns @false if the spell is invalid, cannot be cast, or if required conditions (e.g., runes) are not met.

      • castOn

        public boolean castOn​(CastableSpell spell,
                              net.runelite.api.widgets.Widget target)
        Attempts to cast the given spell on a specified widget target.

        This method verifies if the spell can be cast by invoking the canCast method. If the spell is valid and the required conditions are met (e.g., the spell exists in the active spellbook, the player has the necessary items, levels, etc.), it retrieves the spell's widget representation and performs the "use on" action to cast the spell on the target.

        Parameters:
        spell - The @CastableSpell instance representing the spell to cast.
        • Must not be @null.
        • Should belong to the player's current spellbook and satisfy all requirements for casting.
        target - The @Widget instance representing the target of the spell.
        • Must not be @null.
        • The widget should correspond to a valid in-game target for the selected spell.
        Returns:
        @true if the spell was successfully cast on the target, @false otherwise.

        Returns @false in cases where the spell is invalid, conditions necessary for casting are not met, or the target widget is not valid or accessible.

      • castOn

        public boolean castOn​(CastableSpell spell,
                              net.runelite.api.NPC target)
        Attempts to cast the given spell on a specified NPC target.

        This method checks whether the spell can be cast by invoking the canCast method. If the spell is valid and all necessary conditions for casting (e.g., current spellbook, required runes, etc.) are satisfied, it retrieves the spell's corresponding widget and performs the "use-on" action to cast the spell on the NPC target.

        Parameters:
        spell - The @CastableSpell instance representing the spell to cast.
        • Must not be @null.
        • The spell should exist in the player's current spellbook.
        • The spell must meet all prerequisites for casting, including level and resource requirements.
        target - The @NPC instance representing the target of the spell.
        • Must not be @null.
        • The NPC must be a valid target for the selected spell.
        Returns:
        @true if the spell was successfully cast on the NPC target, @false otherwise.

        Returns @false if the spell is invalid, the conditions for casting are not met, the spell's widget cannot be retrieved, or the NPC is not a valid target.

      • castOn

        public boolean castOn​(CastableSpell spell,
                              net.runelite.api.GameObject target)
        Casts a spell on a specified target object.

        This method checks if the specified spell can be cast, retrieves the widget associated with the spell, and then uses it on the provided target object.

        Parameters:
        spell - The @CastableSpell object representing the spell to be cast.
        target - The @GameObject on which the spell will be cast.
        Returns:
        true if the spell was successfully cast on the target, false otherwise.
      • hasRequiredRunes

        public boolean hasRequiredRunes​(CastableSpell spell)
        Checks if the player has the required runes to cast a given spell.

        This method verifies the rune requirements for the provided spell by considering:

        • Runes available in the base rune pouch.
        • Runes available in the player's inventory, accounting for combination runes that can act as substitutes for their base elemental runes.
        If the rune requirements are met and the spell is deemed castable by the client, the method will return true.
        Parameters:
        spell - The CastableSpell representing the spell to check. Contains information about the rune requirements and castability.
        Returns:
        true if the player has the necessary runes and the spell is castable; false otherwise.