http-api: Use observables for world api

This commit is contained in:
Owain van Brakel
2019-07-19 05:00:39 +02:00
parent 60d734549f
commit 1cddf12a2c
3 changed files with 53 additions and 48 deletions

View File

@@ -61,7 +61,7 @@ public class WorldClient
if (!response.isSuccessful())
{
logger.debug("Error looking up worlds: {}", response);
return null;
return Observable.just(null);
}
InputStream in = response.body().byteStream();
@@ -69,7 +69,7 @@ public class WorldClient
}
catch (JsonParseException ex)
{
throw new IOException(ex);
return Observable.error(ex);
}
});
}

View File

@@ -25,6 +25,7 @@
package net.runelite.client.plugins.defaultworld;
import com.google.inject.Provides;
import io.reactivex.schedulers.Schedulers;
import java.io.IOException;
import javax.inject.Inject;
import javax.inject.Singleton;
@@ -32,6 +33,7 @@ import lombok.extern.slf4j.Slf4j;
import net.runelite.api.Client;
import net.runelite.api.GameState;
import net.runelite.api.events.GameStateChanged;
import net.runelite.client.callback.ClientThread;
import net.runelite.client.config.ConfigManager;
import net.runelite.client.eventbus.EventBus;
import net.runelite.client.events.SessionOpen;
@@ -60,6 +62,9 @@ public class DefaultWorldPlugin extends Plugin
@Inject
private EventBus eventBus;
@Inject
private ClientThread clientThread;
private final WorldClient worldClient = new WorldClient();
private int worldCache;
private boolean worldChangeRequired;
@@ -122,39 +127,39 @@ public class DefaultWorldPlugin extends Plugin
return;
}
try
{
final WorldResult worldResult = worldClient.lookupWorlds();
worldClient.lookupWorlds()
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.from(clientThread))
.subscribe(
(worldResult) ->
{
if (worldResult == null)
{
return;
}
if (worldResult == null)
{
return;
}
final World world = worldResult.findWorld(correctedWorld);
final World world = worldResult.findWorld(correctedWorld);
if (world != null)
{
final net.runelite.api.World rsWorld = client.createWorld();
rsWorld.setActivity(world.getActivity());
rsWorld.setAddress(world.getAddress());
rsWorld.setId(world.getId());
rsWorld.setPlayerCount(world.getPlayers());
rsWorld.setLocation(world.getLocation());
rsWorld.setTypes(WorldUtil.toWorldTypes(world.getTypes()));
if (world != null)
{
final net.runelite.api.World rsWorld = client.createWorld();
rsWorld.setActivity(world.getActivity());
rsWorld.setAddress(world.getAddress());
rsWorld.setId(world.getId());
rsWorld.setPlayerCount(world.getPlayers());
rsWorld.setLocation(world.getLocation());
rsWorld.setTypes(WorldUtil.toWorldTypes(world.getTypes()));
client.changeWorld(rsWorld);
log.debug("Applied new world {}", correctedWorld);
}
else
{
log.warn("World {} not found.", correctedWorld);
}
}
catch (IOException e)
{
log.warn("Error looking up world {}. Error: {}", correctedWorld, e);
}
client.changeWorld(rsWorld);
log.debug("Applied new world {}", correctedWorld);
}
else
{
log.warn("World {} not found.", correctedWorld);
}
},
(e) -> log.warn("Error looking up world {}. Error: {}", correctedWorld, e)
);
}
private void applyWorld()

View File

@@ -29,6 +29,7 @@ import com.google.common.base.Stopwatch;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ObjectArrays;
import com.google.inject.Provides;
import io.reactivex.schedulers.Schedulers;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.time.Duration;
@@ -502,22 +503,21 @@ public class WorldHopperPlugin extends Plugin
{
log.debug("Fetching worlds");
try
{
WorldResult worldResult = new WorldClient().lookupWorlds();
if (worldResult != null)
{
worldResult.getWorlds().sort(Comparator.comparingInt(World::getId));
this.worldResult = worldResult;
this.lastFetch = Instant.now();
updateList();
}
}
catch (IOException ex)
{
log.warn("Error looking up worlds", ex);
}
new WorldClient().lookupWorlds()
.subscribeOn(Schedulers.io())
.subscribe(
(worldResult) ->
{
if (worldResult != null)
{
worldResult.getWorlds().sort(Comparator.comparingInt(World::getId));
this.worldResult = worldResult;
this.lastFetch = Instant.now();
updateList();
}
},
(ex) -> log.warn("Error looking up worlds", ex)
);
}
/**