Hello World Plugin

BeginnerDisplay

A simple plugin that displays 'Hello World' in the chat when you log in. Perfect for learning the basics of plugin development.

5 min read

What It Does

A simple plugin that displays 'Hello World' in the chat when you log in. Perfect for learning the basics of plugin development.

Source Code

java
1package com.example;
2
3import net.runelite.api.Client;
4import net.runelite.api.GameState;
5import net.runelite.api.events.GameStateChanged;
6import net.runelite.client.callback.ClientThread;
7import net.runelite.client.eventbus.Subscribe;
8import net.runelite.client.plugins.Plugin;
9import net.runelite.client.plugins.PluginDescriptor;
10import javax.inject.Inject;
11
12@PluginDescriptor(
13    name = "Hello World",
14    description = "Displays Hello World message"
15)
16public class HelloWorldPlugin extends Plugin {
17    @Inject
18    private Client client;
19
20    @Inject
21    private ClientThread clientThread;
22
23    private boolean greeted = false;
24
25    @Subscribe
26    public void onGameStateChanged(GameStateChanged event) {
27        if (event.getGameState() == GameState.LOGGED_OUT) {
28            // Reset flag when logging out
29            greeted = false;
30        } else if (event.getGameState() == GameState.LOGGED_IN && !greeted) {
31            // Use ClientThread to ensure we're on the game thread
32            clientThread.invokeLater(() -> {
33                if (client.getLocalPlayer() != null) {
34                    client.addChatMessage(
35                        net.runelite.api.ChatMessageType.GAMEMESSAGE,
36                        "",
37                        "Hello World from RuneLite Plugin!",
38                        null
39                    );
40                    greeted = true;
41                }
42            });
43        }
44    }
45}

Code Annotations

Line 1

Package declaration - groups related classes together

Line 11

@PluginDescriptor annotation tells RuneLite about your plugin

Line 15

All plugins must extend the Plugin class

Line 18

@Inject annotation - RuneLite provides the Client instance

Line 23

@Subscribe annotation - listens for GameTick events

Variations

With Config

Add a config option to toggle the message

java
1// Add config field
2@ConfigItem(
3    keyName = "showMessage",
4    name = "Show Hello World Message",
5    description = "Toggle the hello world message"
6)
7default boolean showMessage() {
8    return true;
9}
10
11// Use in onGameTick
12if (config.showMessage() && !hasLoggedIn && client.getLocalPlayer() != null) {
13    // ... rest of code
14}

API Classes Used

ClientClientThread
GameStateChanged

Key Concepts

  • Package declaration - groups related classes together
  • @PluginDescriptor annotation tells RuneLite about your plugin
  • All plugins must extend the Plugin class
  • @Inject annotation - RuneLite provides the Client instance
  • @Subscribe annotation - listens for GameTick events

Next Steps

  • Learn about the Client API
  • Explore GameTick events
  • Try adding configuration options