Game State
Understand how to access player data, inventory, equipment, and other game information.
Game State refers to all the information about the current state of the game - your player's stats, location, inventory, equipment, nearby NPCs, and more.
The Client Interface
The Client interface is your window into the game. You inject it using @Inject, and it provides access to everything:
- **Player Data**: Name, combat level, stats, location
- **Inventory**: Items in your inventory
- **Equipment**: What you're wearing
- **NPCs**: Non-player characters around you
- **World**: Map data, regions, etc.
Accessing Player Data
The most common thing you'll do is access the local player:
Player localPlayer = client.getLocalPlayer();
if (localPlayer != null) {
String name = localPlayer.getName();
int combatLevel = localPlayer.getCombatLevel();
}
**Always check for null!** The player might not be logged in.
Item Containers
Items are stored in containers. Common containers:
- Inventory (ID: 93)
- Equipment (ID: 94)
- Bank (ID: 95)
ItemContainer inventory = client.getItemContainer(InventoryID.INVENTORY);
if (inventory != null) {
Item[] items = inventory.getItems();
}
Best Practices
- Always null-check before accessing game state
- Cache frequently accessed data
- Don't access game state from background threads
- Be aware that game state can change at any time
Code Examples
Example 1
1// Get player information
2@Inject
3private Client client;
4
5@Subscribe
6public void onGameTick(GameTick event) {
7 Player localPlayer = client.getLocalPlayer();
8 if (localPlayer != null) {
9 String name = localPlayer.getName();
10 int combatLevel = localPlayer.getCombatLevel();
11 WorldPoint location = localPlayer.getWorldLocation();
12
13 System.out.println(name + " is level " + combatLevel);
14 System.out.println("Location: " + location.getX() + ", " + location.getY());
15 }
16}Example 2
1// Access inventory
2ItemContainer inventory = client.getItemContainer(InventoryID.INVENTORY);
3if (inventory != null) {
4 Item[] items = inventory.getItems();
5 for (Item item : items) {
6 if (item.getId() != -1) {
7 System.out.println("Item ID: " + item.getId() + ", Quantity: " + item.getQuantity());
8 }
9 }
10}