Interaction
The Kraken API interacts with the RuneLite game client using reflection to call a method within the client called doAction. The doAction method is a "choke point" in the client for a vast majority of menu action interactions: clicking NPC's, game objects, widgets, ground items, interfaces (again widgets), and more.
Kraken still utilizes some network packets to directly communicate with Jagex's servers to process certain actions like movement, spoofed mouse clicks, and some niche dialogue operations. By leveraging doAction for the heavy lifting for ~85% of interactions and the network packets for the remaining ~15%, the Kraken API takes significantly less time and effort to update when the game obfuscation changes.
Both the doAction hooks and the packet definitions come from hooks.json, which HooksLoader reads once, and everything is prepared when the Context class is built by Guice. There is nothing to initialize in your plugin:
@Inject
private Context ctx;Interaction Manager
A vast majority of actions happen via the InteractionManager (com.kraken.api.core.interaction). Typically, as a script developer, you will not need to interact with this class directly as the interactions themselves are abstracted by the Query and Service system defined in the API docs.
The InteractionManager exposes a set of overloaded interact() methods that accept RuneLite objects (NPC, Player, TileObject, Widget, container items, and widget-on-target combinations). Each call is:
- Resolved by
MenuActionResolverRegistry, which picks the resolver for that entity type. The resolver looks the action name up on the entity (an NPC's composition, an object's definition, a widget's actions) and produces aResolvedMenuAction: the menu opcode, identifier, and parametersdoActionexpects. - Dispatched by
InteractionDispatcher, which queues anEVENT_MOUSE_CLICKpacket at the entity's clickbox so the server sees a click where the action happened, then hands the resolved action toDoActionInvoker. - Invoked by
DoActionInvoker, a reflective call into the client's obfuscateddoActionusing the class and method names inhooks.json.
The client then validates, constructs, and queues the packet exactly as it would for a real click. interact(...) returns false when nothing was sent (no matching action, entity gone, hooks missing), so retrying on a false result is safe.
Example: Attacking an NPC
When you call ctx.npcs().withName("Goblin").interact("Attack"), the following happens internally:
- The
NpcQueryfinds the "Goblin" NPC. - The
interact("Attack")method on theNpcEntityis called and delegates to theInteractionManager. - The NPC resolver finds "Attack" in the NPC's composition to determine its action index and builds the
ResolvedMenuAction(anNPC_*_OPTIONopcode with the NPC's index). InteractionDispatcherqueues a spoofed mouse click on the NPC's clickbox andDoActionInvokercallsdoActionwith the resolved parameters.- The client builds the
OPNPCpacket and queues it to be sent to the server.
Packet System Overview
The packet system covers the actions that do not go through doAction. It consists of several key components working together:
- Packet Definitions (
PacketDefinition): These define the structure of each packet type, including the packet's name, the data fields it contains, the methods used to write that data, and the associatedPacketType. They are loaded from thepacketssection ofhooks.json. - Packet Types (
PacketType): An enumeration of the client packet kinds the API knows the shape of (OPNPC,OPLOC,IF_BUTTON,MOVE_GAMECLICK, and so on). Only the five with definitions inhooks.jsonare built and sent by the API:EVENT_MOUSE_CLICK,MOVE_GAMECLICK,RESUME_COUNTDIALOG,RESUME_OBJDIALOGandRESUME_STRINGDIALOG. Everything else is handled bydoAction. - Packet Definition Factory (
PacketFactory): A factory class that creates and cachesPacketDefinitioninstances for the supported packet types. - Packet Client (
PacketClient): The core component responsible for constructing and sending packets. It uses thereflectionHooksfromhooks.json(packet writer, buffer node,addNode, Isaac cipher) to access the client's internals so that packets are formatted correctly and queued for transmission. - Entity Packet Helpers (
com.kraken.api.core.packet.entity):MousePackets,MovementPacketsandWidgetPacketswrap the packet client for the specific cases above. These classes are further abstracted by the Query and Service system.
How Packets are Sent
The process of sending a packet involves the following steps:
- Identify the Action: The user (or a high-level API) determines the desired action (e.g., "walk to this tile").
- Determine Packet Type: The system identifies the appropriate
PacketTypefor the action (e.g.,MOVE_GAMECLICK). - Retrieve Definition: The
PacketFactoryprovides thePacketDefinitionfor the packet type. - Prepare Data: The necessary data (e.g., the target coordinates) is collected.
- Send Packet: The
PacketClientis invoked with thePacketDefinitionand the data.- It uses reflection to create a
PacketBufferNode. - It writes the data into the packet's buffer using the methods specified in the definition.
- It queues the packet to the client's
PacketWriterto be sent to the server.
- It uses reflection to create a
Key Parts
PacketClient
The PacketClient is a singleton that handles the low-level details of packet construction and transmission. It:
- Locates the necessary internal client methods and fields from the mappings
HooksLoaderprovides. - Provides the
sendPacket(PacketDefinition def, Object... objects)method, which is the entry point for sending any packet. - Handles the obfuscation and reflection required to interact with the RuneLite client's internals.
Entity Packet Helpers
To make the packet layer easier to use, the API provides helper classes for specific cases. For example, MovementPackets provides methods like:
queueMovement(WorldPoint location): Finds the correct x and y coordinates and sends the packet to move your player.queueResumeObj(int itemId): Sends a packet to select an object from a list (i.e. selecting an item to buy from the GE)
MousePackets.queueClickPacket(x, y) sends the spoofed click that precedes every doAction call.
Learning More
The client development section explains what doAction, packet buffers and Isaac ciphers are, and how the mappings in hooks.json are found in the obfuscated client. See the packets guide for a table of every packet type and its parameters.