Class Magic

java.lang.Object
privbot.hooks.Magic

public final class Magic extends Object
Magic spell casting through the game client.

Spells are defined in the shared spell table. For WIDGET_TARGET spells, the spell widget is selected first, then the target entity action is sent. For self-cast spells (CC_OP / no target), only a single event is needed.

  • Method Details

    • autocastSpellWidgetId

      public Integer autocastSpellWidgetId()
      Current autocast selection, or null if the profile cannot observe it.
    • isAutocasting

      public boolean isAutocasting(String name)
      Whether the exact named spell is selected for autocast. The client sets its selection on the click, before the server answers, so where the server's autocast flag can be read it must also read on (1); otherwise a spell the account cannot cast would read as selected. An unreadable flag answers false.
    • autocast

      public boolean autocast(String name)
      Selects the named spell for autocast. The click is a toggle, so the selection is read again right before it goes out. True means already selected or sent; confirm with isAutocasting(java.lang.String) afterwards. An unknown state sends nothing.
    • autocastWidget

      public Magic.AutocastClick autocastWidget(int widgetId, String label)
      Turns autocast back on with the spell whose spellbook widget is widgetId (one the caller saw the server accept), for a spell PrivBot does not know by name. The magic tab is opened first (a readable open book is never clicked again), then the spell's Autocast click goes out.

      Only while the server's autocast flag reads 2 (off) or 0 (not sent this login): read before the tab is touched and again right before the click. Never while it reads 1 (on) or cannot be read: the server treats the click as a toggle on its own state, so a click on an autocast that is already on could turn it off. On a server where the flag cannot be read or the click cannot be sent, nothing is sent. The client's own selection is not read (it is set on the click even for a spell the server refuses, and not when the login restores autocast); callers confirm the outcome by the flag reading 1.

      Only on a spell the open book draws: once the tab reads open, the click goes out only while the client reports the widget as drawn for widgetId. A widget from another spellbook (a spell learned on another account, or before a book change) is not drawn: an Autocast on it is a click no real client makes, which the server refuses anyway. That answer is Magic.AutocastClick.NOT_IN_OPEN_BOOK, apart from every other refusal (Magic.AutocastClick.NOT_SENT, an unanswerable visibility included), so a caller can tell "this spell is not in the book" from "the click did not go out".

      Parameters:
      label - the menu target text for the click (the spell's name when known; may be blank)
      Returns:
      what happened; only Magic.AutocastClick.SENT dispatched (queued) the click
    • cast

      public boolean cast(String name)
      Casts the spell whose in-game label matches name (display name, enum constant, or colour-stripped menu target). If it is not in the spell table, clicks the live spellbook widget by that exact name.
      Returns:
      true if a click was dispatched
    • castOnNpc

      public boolean castOnNpc(String name, PrivNPC target)
      Casts a spell by in-game name on an NPC.
      Parameters:
      name - the spell's name ("Ice Barrage")
      target - the NPC
      Returns:
      dispatch acceptance; false for a null target or an unknown spell
    • spellbookTab

      public Magic.SpellbookTab spellbookTab()
      The magic tab's state, read in one GameThread.ensure(java.util.function.Supplier<T>, T) (EDT-ordered on the EDT-marshal servers, so it sees a tab click queued before it). UNKNOWN whenever anything is unreadable, and always under the legacy switch.
    • spellbookTabInBatch

      public Magic.SpellbookTab spellbookTabInBatch()
      spellbookTab() for a caller that already owns the client-thread batch.
    • openSpellbook

      public Magic.SpellbookTab openSpellbook()
      Opens the book without waiting: one TAB_SELECT on a readable closed book, nothing otherwise, and never a sleep. Returns the state read before the click.
    • castOnPlayer

      public boolean castOnPlayer(String name, PrivPlayer target)
      Casts name on target. True when the target click went out or was queued behind the select (Magic.CastResult.sent()). On Elorin this can now be false where it used to be true: a held or refused target click is no longer reported as a cast. castOnPlayerOutcome(java.lang.String, privbot.wrappers.PrivPlayer) says why.
    • castOnPlayerOutcome

      public Magic.CastResult castOnPlayerOutcome(String name, PrivPlayer target)
      Casts name on target and reports what happened to the target click.

      On Elorin the target is resolved BEFORE the spell is selected, so a cast that could not be aimed sends nothing. Once the select is confirmed the target click always goes out, queued behind it. A worker waits at most CAST_DISPATCH_WAIT_MS for the outcome; on the EDT, the client thread or a timeout the answer is Magic.CastOutcome.POSTED_UNCONFIRMED.

      On other servers the answer is POSTED_UNCONFIRMED once the clicks are queued.

    • castOnObject

      public boolean castOnObject(String name, PrivObject target)
      Casts a spell by in-game name on a scene object.
      Parameters:
      name - the spell's name
      target - the object
      Returns:
      dispatch acceptance; false for a null target
    • castOnInventoryItem

      public boolean castOnInventoryItem(String name, int itemId, int slot)
      Casts name (e.g. High Level Alchemy) on the inventory item itemId in slot: selects the spell, then clicks the item, the way the client's own spell-on-item click is sent (Elorin). Returns false with nothing sent on a server where PrivBot cannot send that click, including the servers that use RuneLite menu actions, which alch through the item-interaction helper.