Time Display

BeginnerDisplay

Displays the current in-game time. Shows how to access game time and format it for display.

10 min read

What It Does

Displays the current in-game time. Shows how to access game time and format it for display.

Source Code

java
1package com.example;
2
3import net.runelite.api.Client;
4import net.runelite.client.ui.overlay.Overlay;
5import net.runelite.client.ui.overlay.OverlayManager;
6import net.runelite.client.plugins.Plugin;
7import net.runelite.client.plugins.PluginDescriptor;
8import javax.inject.Inject;
9import java.awt.*;
10
11@PluginDescriptor(
12    name = "Time Display",
13    description = "Shows game time"
14)
15public class TimeDisplayPlugin extends Plugin {
16    @Inject
17    private Client client;
18
19    @Inject
20    private OverlayManager overlayManager;
21
22    private final Overlay overlay = new Overlay() {
23        @Override
24        public Dimension render(Graphics2D graphics) {
25            int gameTime = client.getGameCycle();
26            int hours = (gameTime / 100) % 24;
27            int minutes = gameTime % 100;
28            
29            String timeString = String.format("%02d:%02d", hours, minutes);
30            graphics.setColor(Color.WHITE);
31            graphics.drawString("Time: " + timeString, 10, 10);
32            
33            return new Dimension(120, 20);
34        }
35    };
36
37    {
38        overlay.setLayer(OverlayLayer.ABOVE_SCENE);
39        overlay.setPosition(OverlayPosition.TOP_LEFT);
40    }
41
42    @Override
43    protected void startUp() {
44        overlayManager.add(overlay);
45    }
46
47    @Override
48    protected void shutDown() {
49        overlayManager.remove(overlay);
50    }
51}

Code Annotations

Line 25

Get game cycle (time)

Line 26

Calculate hours

Line 27

Calculate minutes

Line 29

Format time string

API Classes Used

Key Concepts

  • Get game cycle (time)
  • Calculate hours
  • Calculate minutes
  • Format time string

Next Steps

  • Learn about game time API
  • Add real-world time
  • Add time-based alerts