From df1e51dbc6d1892058f807cd00c855f2022bdbe6 Mon Sep 17 00:00:00 2001 From: Tomas Slusny Date: Mon, 5 Mar 2018 13:51:56 +0100 Subject: [PATCH] Add default world plugin Add default world plugin where user will be able to set his default world. In case invalid world is set, the client default/selected one will be used instead. Fixes #475 Closes #749 Signed-off-by: Tomas Slusny --- .../defaultworld/DefaultWorldConfig.java | 47 ++++++ .../defaultworld/DefaultWorldPlugin.java | 159 ++++++++++++++++++ 2 files changed, 206 insertions(+) create mode 100644 runelite-client/src/main/java/net/runelite/client/plugins/defaultworld/DefaultWorldConfig.java create mode 100644 runelite-client/src/main/java/net/runelite/client/plugins/defaultworld/DefaultWorldPlugin.java diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/defaultworld/DefaultWorldConfig.java b/runelite-client/src/main/java/net/runelite/client/plugins/defaultworld/DefaultWorldConfig.java new file mode 100644 index 0000000000..5e6b787001 --- /dev/null +++ b/runelite-client/src/main/java/net/runelite/client/plugins/defaultworld/DefaultWorldConfig.java @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2018, Tomas Slusny + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.client.plugins.defaultworld; + +import net.runelite.client.config.Config; +import net.runelite.client.config.ConfigGroup; +import net.runelite.client.config.ConfigItem; + +@ConfigGroup( + keyName = "defaultworld", + name = "Default World", + description = "Sets the default world and remembers it" +) +public interface DefaultWorldConfig extends Config +{ + @ConfigItem( + keyName = "defaultWorld", + name = "Default world", + description = "World to use as default one" + ) + default int getWorld() + { + return 0; + } +} \ No newline at end of file diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/defaultworld/DefaultWorldPlugin.java b/runelite-client/src/main/java/net/runelite/client/plugins/defaultworld/DefaultWorldPlugin.java new file mode 100644 index 0000000000..5eaab7d195 --- /dev/null +++ b/runelite-client/src/main/java/net/runelite/client/plugins/defaultworld/DefaultWorldPlugin.java @@ -0,0 +1,159 @@ +/* + * Copyright (c) 2018, Tomas Slusny + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.client.plugins.defaultworld; + +import com.google.common.eventbus.Subscribe; +import com.google.inject.Provides; +import java.io.IOException; +import java.util.EnumSet; +import javax.inject.Inject; +import lombok.extern.slf4j.Slf4j; +import net.runelite.api.Client; +import net.runelite.api.GameState; +import net.runelite.api.WorldType; +import net.runelite.api.events.GameStateChanged; +import net.runelite.api.events.SessionOpen; +import net.runelite.client.config.ConfigManager; +import net.runelite.client.plugins.Plugin; +import net.runelite.client.plugins.PluginDescriptor; +import net.runelite.http.api.worlds.World; +import net.runelite.http.api.worlds.WorldClient; +import net.runelite.http.api.worlds.WorldResult; + +@PluginDescriptor(name = "Default World") +@Slf4j +public class DefaultWorldPlugin extends Plugin +{ + @Inject + private Client client; + + @Inject + private DefaultWorldConfig config; + + private final WorldClient worldClient = new WorldClient(); + private int worldCache; + private boolean worldChangeRequired; + + @Override + protected void startUp() throws Exception + { + worldChangeRequired = true; + applyWorld(); + } + + @Override + protected void shutDown() throws Exception + { + worldChangeRequired = true; + changeWorld(worldCache); + } + + @Provides + DefaultWorldConfig getConfig(ConfigManager configManager) + { + return configManager.getConfig(DefaultWorldConfig.class); + } + + @Subscribe + public void onSessionOpen(SessionOpen event) + { + worldChangeRequired = true; + applyWorld(); + } + + @Subscribe + public void onGameStateChange(GameStateChanged event) + { + applyWorld(); + } + + private void changeWorld(int newWorld) + { + if (!worldChangeRequired || client.getGameState() != GameState.LOGIN_SCREEN || client.getWorld() == newWorld) + { + return; + } + + worldChangeRequired = false; + + // Old School RuneScape worlds start for 301 so don't even bother trying to find lower id ones + if (client.getWorld() <= 300) + { + return; + } + + try + { + final WorldResult worldResult = worldClient.lookupWorlds(); + final World world = worldResult.findWorld(newWorld); + + 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(toWorldTypes(world.getTypes())); + + client.changeWorld(rsWorld); + log.debug("Applied new world {}", newWorld); + } + else + { + log.warn("World {} not found.", newWorld); + } + } + catch (IOException e) + { + log.warn("Error looking up world {}. Error: {}", newWorld, e); + } + } + + private static EnumSet toWorldTypes(final EnumSet apiTypes) + { + final EnumSet types = EnumSet.noneOf(WorldType.class); + + for (net.runelite.http.api.worlds.WorldType apiType : apiTypes) + { + types.add(WorldType.valueOf(apiType.name())); + } + + return types; + } + + private void applyWorld() + { + if (worldCache == 0) + { + worldCache = client.getWorld(); + log.debug("Stored old world {}", worldCache); + } + + final int newWorld = config.getWorld(); + changeWorld(newWorld); + } +}