Skip to content

Library Guide

Mike Johnston edited this page Jul 30, 2022 · 5 revisions

Introduction

The library distinguishes between the Dota client playing a match and spectating a match. The available data is significantly different in these two cases, so different objects are available to you. For example, when the client is playing a match, data is only available for the player and their hero (i.e. no data is available on other players or heroes). However, when the client is spectating a match, data is available for all players and heroes in the match.

Add the dependency

Using your build system (e.g. Gradle, Maven), add the library's published JAR as a dependency.

Note: Replace the x.x.x part with the latest version number: Maven Central

Gradle example

repositories {
    mavenCentral()
}

dependencies {
    implementation "com.github.mrbean355:dota2-gsi:x.x.x"
}

Maven example

<dependency>
    <groupId>com.github.mrbean355</groupId>
    <artifactId>dota2-gsi</artifactId>
    <version>x.x.x</version>
</dependency>

Sample code

Note: Make sure you have set up Dota 2 first!

Kotlin example

fun main() {
    // Create a server which listens on the port configured during Dota setup:
    GameStateServer(44444)
        // Register one or more listeners: 
        .setPlayingListener { gameState ->
            // Do something with the received gameState object:
            val clockTime = gameState.map?.clockTime
            println("The clock time is $clockTime seconds.")
        }
        // Start the server, blocking the thread so the program doesn't immediately exit:
        .start() // Alternatively startAsync() will not block the thread.
}

Java example

public class GsiDemo {

    public static void main(String[] args) {
        // Create a server which listens on the port configured during Dota setup:
        GameStateServer.create(44444)
                // Register one or more listeners: 
                .setPlayingListener(gameState -> {
                    // Do something with the received gameState object:
                    if (gameState.getMap() != null) {
                        int clockTime = gameState.getMap().getClockTime();
                        System.out.println("The clock time is " + clockTime + " seconds.");
                    }
                })
                // Start the server, blocking the thread so the program doesn't immediately exit:
                .start(); // Alternatively startAsync() will not block the thread.
    }
}

Full documentation

There are many game objects (e.g. Hero) available with their own properties! Explore the complete public API with documentation online.

Snapshots

Snapshots of the next library version are also published to Maven Central. You can use them in your project as well, but are likely to be unstable. Include the snapshot repository and dependency:

repositories {
    maven {
        url 'https://oss.sonatype.org/content/repositories/snapshots/'
    }
}

dependencies {
    // Check this Maven Central page for the latest snapshot version:
    // https://oss.sonatype.org/content/repositories/snapshots/com/github/mrbean355/dota2-gsi/
    implementation "com.github.mrbean355:dota2-gsi:x.x.x-SNAPSHOT"
}

// This is needed to tell Gradle to always check for new uploads of the snapshot.
// Without this, Gradle will only check once per day.
configurations.configureEach {
    resolutionStrategy {
        cacheChangingModulesFor(0, "seconds")
    }
}