Search in sources :

Example 1 with World

use of net.runelite.http.api.worlds.World in project runelite by runelite.

the class XpTrackerPlugin method onGameStateChanged.

@Subscribe
public void onGameStateChanged(GameStateChanged event) {
    if (event.getGameState() == GameState.LOGGED_IN) {
        // LOGGED_IN is triggered between region changes too.
        // Check that the username changed or the world type changed.
        World world = worlds.findWorld(client.getWorld());
        if (world == null) {
            log.warn("Logged into nonexistent world {}?", client.getWorld());
            return;
        }
        XpWorldType type = worldSetToType(world.getTypes());
        if (!Objects.equals(client.getUsername(), lastUsername) || lastWorldType != type) {
            // Reset
            log.debug("World change: {} -> {}, {} -> {}", lastUsername, client.getUsername(), lastWorldType, type);
            lastUsername = client.getUsername();
            lastWorldType = type;
            xpPanel.resetAllInfoBoxes();
        }
    } else if (event.getGameState() == GameState.LOGIN_SCREEN) {
        Player local = client.getLocalPlayer();
        String username = local != null ? local.getName() : null;
        if (username != null) {
            log.debug("Submitting xp track for {}", username);
            executor.submit(() -> {
                try {
                    xpClient.update(username);
                } catch (IOException ex) {
                    log.warn("error submitting xp track", ex);
                }
            });
        }
    }
}
Also used : Player(net.runelite.api.Player) IOException(java.io.IOException) World(net.runelite.http.api.worlds.World) Subscribe(com.google.common.eventbus.Subscribe)

Example 2 with World

use of net.runelite.http.api.worlds.World in project runelite by runelite.

the class WorldsService method listWorlds.

@RequestMapping
public WorldResult listWorlds() throws IOException {
    Request okrequest = new Request.Builder().url(url).build();
    byte[] b;
    try (Response okresponse = RuneLiteAPI.CLIENT.newCall(okrequest).execute()) {
        b = okresponse.body().bytes();
    }
    List<World> worlds = new ArrayList<>();
    ByteBuffer buf = ByteBuffer.wrap(b);
    int length = buf.getInt();
    buf.limit(length + 4);
    int num = buf.getShort() & 0xFFFF;
    for (int i = 0; i < num; ++i) {
        final World.WorldBuilder worldBuilder = World.builder().id(buf.getShort() & 0xFFFF).types(getTypes(buf.getInt())).address(readString(buf)).activity(readString(buf)).location(buf.get() & 0xFF).players(buf.getShort() & 0xFFFF);
        worlds.add(worldBuilder.build());
    }
    WorldResult result = new WorldResult();
    result.setWorlds(worlds);
    return result;
}
Also used : Response(okhttp3.Response) WorldResult(net.runelite.http.api.worlds.WorldResult) Request(okhttp3.Request) ArrayList(java.util.ArrayList) World(net.runelite.http.api.worlds.World) ByteBuffer(java.nio.ByteBuffer) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with World

use of net.runelite.http.api.worlds.World in project runelite by runelite.

the class WorldsServiceTest method testListWorlds.

@Test
public void testListWorlds() throws Exception {
    WorldsService worlds = new WorldsService();
    worlds.setUrl(server.url("/"));
    WorldResult worldResult = worlds.listWorlds();
    assertEquals(82, worldResult.getWorlds().size());
    World world = worldResult.findWorld(385);
    assertNotNull(world);
    assertTrue(world.getTypes().contains(WorldType.SKILL_TOTAL));
}
Also used : WorldResult(net.runelite.http.api.worlds.WorldResult) World(net.runelite.http.api.worlds.World) Test(org.junit.Test)

Example 4 with World

use of net.runelite.http.api.worlds.World in project runelite by runelite.

the class UpdateCheckService method checkUpdate.

private boolean checkUpdate() {
    World nextWorld = randomWorld();
    if (nextWorld == null) {
        return false;
    }
    InetAddress address;
    try {
        String url = nextWorld.getAddress();
        address = InetAddress.getByName(url);
    } catch (UnknownHostException ex) {
        logger.warn(null, ex);
        return false;
    }
    try (Socket socket = new Socket(address, PORT)) {
        ByteBuffer buffer = ByteBuffer.allocate(5);
        buffer.put(HANDSHAKE_TYPE);
        buffer.putInt(RuneLiteAPI.getRsVersion());
        InputStream is = socket.getInputStream();
        OutputStream os = socket.getOutputStream();
        os.write(buffer.array());
        int reply = is.read();
        if (reply == RESPONSE_OUTDATED) {
            return true;
        }
        if (reply != RESPONSE_OK) {
            logger.debug("Non-ok response for handshake: {}", reply);
        }
    } catch (IOException ex) {
        logger.warn(null, ex);
    }
    // no update
    return false;
}
Also used : UnknownHostException(java.net.UnknownHostException) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) World(net.runelite.http.api.worlds.World) InetAddress(java.net.InetAddress) ByteBuffer(java.nio.ByteBuffer) Socket(java.net.Socket)

Example 5 with World

use of net.runelite.http.api.worlds.World in project runelite by runelite.

the class UpdateCheckService method randomWorld.

private World randomWorld() {
    try {
        WorldResult worldResult = worldsService.listWorlds();
        List<World> worlds = worldResult.getWorlds();
        Random rand = new Random();
        return worlds.get(rand.nextInt(worlds.size()));
    } catch (IOException ex) {
        logger.warn(null, ex);
        return null;
    }
}
Also used : WorldResult(net.runelite.http.api.worlds.WorldResult) Random(java.util.Random) IOException(java.io.IOException) World(net.runelite.http.api.worlds.World)

Aggregations

World (net.runelite.http.api.worlds.World)6 IOException (java.io.IOException)4 WorldResult (net.runelite.http.api.worlds.WorldResult)4 ByteBuffer (java.nio.ByteBuffer)2 Subscribe (com.google.common.eventbus.Subscribe)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 InetAddress (java.net.InetAddress)1 Socket (java.net.Socket)1 UnknownHostException (java.net.UnknownHostException)1 ArrayList (java.util.ArrayList)1 Random (java.util.Random)1 Player (net.runelite.api.Player)1 Request (okhttp3.Request)1 Response (okhttp3.Response)1 Test (org.junit.Test)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1