defaultworld: always changeWorld on client thread

This commit is contained in:
Adam
2022-02-05 21:56:50 -05:00
parent 43e8a57cad
commit 5d99ec56f2

View File

@@ -29,7 +29,8 @@ import javax.inject.Inject;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import net.runelite.api.Client; import net.runelite.api.Client;
import net.runelite.api.GameState; import net.runelite.api.GameState;
import net.runelite.api.events.GameStateChanged; import net.runelite.api.events.WorldChanged;
import net.runelite.client.callback.ClientThread;
import net.runelite.client.config.ConfigManager; import net.runelite.client.config.ConfigManager;
import net.runelite.client.eventbus.Subscribe; import net.runelite.client.eventbus.Subscribe;
import net.runelite.client.events.SessionOpen; import net.runelite.client.events.SessionOpen;
@@ -51,27 +52,33 @@ public class DefaultWorldPlugin extends Plugin
@Inject @Inject
private Client client; private Client client;
@Inject
private ClientThread clientThread;
@Inject @Inject
private DefaultWorldConfig config; private DefaultWorldConfig config;
@Inject @Inject
private WorldService worldService; private WorldService worldService;
private int worldCache;
private boolean worldChangeRequired;
@Override @Override
protected void startUp() throws Exception protected void startUp()
{ {
worldChangeRequired = true; clientThread.invokeLater(() ->
applyWorld(); {
if (client.getGameState().getState() < GameState.LOGIN_SCREEN.getState())
{
return false;
}
applyWorld();
return true;
});
} }
@Override @Override
protected void shutDown() throws Exception protected void shutDown()
{ {
worldChangeRequired = true;
changeWorld(worldCache);
} }
@Provides @Provides
@@ -83,29 +90,25 @@ public class DefaultWorldPlugin extends Plugin
@Subscribe @Subscribe
public void onSessionOpen(SessionOpen event) public void onSessionOpen(SessionOpen event)
{ {
worldChangeRequired = true; clientThread.invokeLater(this::applyWorld);
applyWorld();
} }
@Subscribe @Subscribe
public void onGameStateChanged(GameStateChanged event) public void onWorldChanged(WorldChanged worldChanged)
{ {
if (event.getGameState() == GameState.LOGGED_IN) int world = client.getWorld();
{ config.lastWorld(world);
config.lastWorld(client.getWorld()); log.debug("Saving last world {}", world);
}
applyWorld();
} }
private void changeWorld(int newWorld) private void applyWorld()
{ {
if (!worldChangeRequired || client.getGameState() != GameState.LOGIN_SCREEN) if (client.getGameState() != GameState.LOGIN_SCREEN)
{ {
return; return;
} }
worldChangeRequired = false; final int newWorld = config.useLastWorld() ? config.lastWorld() : config.getWorld();
int correctedWorld = newWorld < 300 ? newWorld + 300 : newWorld; int correctedWorld = newWorld < 300 ? newWorld + 300 : newWorld;
// Old School RuneScape worlds start on 301 so don't even bother trying to find lower id ones // Old School RuneScape worlds start on 301 so don't even bother trying to find lower id ones
@@ -116,7 +119,6 @@ public class DefaultWorldPlugin extends Plugin
} }
final WorldResult worldResult = worldService.getWorlds(); final WorldResult worldResult = worldService.getWorlds();
if (worldResult == null) if (worldResult == null)
{ {
log.warn("Failed to lookup worlds."); log.warn("Failed to lookup worlds.");
@@ -124,35 +126,21 @@ public class DefaultWorldPlugin extends Plugin
} }
final World world = worldResult.findWorld(correctedWorld); final World world = worldResult.findWorld(correctedWorld);
if (world == null)
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); log.warn("World {} not found.", correctedWorld);
} return;
}
private void applyWorld()
{
if (worldCache == 0)
{
worldCache = client.getWorld();
log.debug("Stored old world {}", worldCache);
} }
final int newWorld = !config.useLastWorld() ? config.getWorld() : config.lastWorld(); final net.runelite.api.World rsWorld = client.createWorld();
changeWorld(newWorld); 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);
} }
} }