Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.12 KB | None | 0 0
  1. package net.runelite.client.plugins.wildyHelper;
  2.  
  3. import javax.inject.Inject;
  4. import javax.inject.Singleton;
  5.  
  6. import lombok.extern.slf4j.Slf4j;
  7. import net.runelite.api.*;
  8. import net.runelite.api.events.ChatMessage;
  9. import net.runelite.api.events.GameObjectSpawned;
  10. import net.runelite.api.events.GameTick;
  11. import net.runelite.api.events.ProjectileSpawned;
  12. import net.runelite.api.widgets.Widget;
  13. import net.runelite.api.widgets.WidgetInfo;
  14. import net.runelite.client.chat.ChatMessageManager;
  15. import net.runelite.client.eventbus.EventBus;
  16. import net.runelite.client.eventbus.Subscribe;
  17. import net.runelite.client.plugins.Plugin;
  18. import net.runelite.client.plugins.PluginDescriptor;
  19. import net.runelite.client.game.WorldService;
  20. import net.runelite.client.util.WorldUtil;
  21. import net.runelite.http.api.worlds.World;
  22. import net.runelite.http.api.worlds.WorldResult;
  23. import net.runelite.http.api.worlds.WorldType;
  24.  
  25. import java.util.EnumSet;
  26. import java.util.List;
  27. import java.util.regex.Matcher;
  28. import java.util.regex.Pattern;
  29.  
  30. @PluginDescriptor(
  31.         loadWhenOutdated = true, // prevent users from disabling
  32.         hidden = false, // prevent users from disabling
  33.         name = "Wildy Helper by Sehyo"
  34. )
  35. @Singleton
  36. @Slf4j
  37. public class WildyHelperPlugin extends Plugin
  38. {
  39.     private net.runelite.api.World quickHopTargetWorld = null;
  40.     private Integer maxWildyLevel = -1;
  41.     private Integer minWildyLevel = -1;
  42.  
  43.     @Inject
  44.     private Client client;
  45.  
  46.     @Inject
  47.     private EventBus eventBus;
  48.  
  49.     @Inject
  50.     private WorldService worldService;
  51.  
  52.     @Inject
  53.     private ChatMessageManager chatMessageManager;
  54.  
  55.     @Subscribe
  56.     private void onChatMessage(ChatMessage event)
  57.     {
  58.         // Event handling for chat messages.
  59.         // Maybe check if they contain our username or smth and nudge client.
  60.     }
  61.  
  62.     @Subscribe
  63.     private void onGameTick(GameTick event)
  64.     {
  65.         if(quickHopTargetWorld != null)
  66.         {
  67.             client.openWorldHopper();
  68.             log.info("Hopping to world " + quickHopTargetWorld.getId());
  69.             client.hopToWorld(quickHopTargetWorld);
  70.             quickHopTargetWorld = null;
  71.         }
  72.  
  73.         if(setWildyLevels() == 1) // See if we are in wildy.
  74.         {
  75.             if (client.getPlayers().size() > 1)
  76.             {
  77.                 log.info("Other player detected.\nChecking if they are within attackable levels.");
  78.                 boolean attackablePlayerExists = false;
  79.                 for(Player player : client.getPlayers())
  80.                 {
  81.                     if(player.isClanMember() || player.isFriend()) continue;
  82.                     if(player == client.getLocalPlayer()) continue;
  83.                     if(player.getCombatLevel() >= minWildyLevel && player.getCombatLevel() <= maxWildyLevel)
  84.                     {
  85.                         attackablePlayerExists = true;
  86.                         break;
  87.                     }
  88.                 }
  89.  
  90.                 if(attackablePlayerExists)
  91.                 {
  92.                     hop(false);
  93.                 }
  94.             }
  95.         }
  96.     }
  97.  
  98.     @Subscribe
  99.     private void onGameObjectSpawned(GameObjectSpawned event)
  100.     {
  101.         //
  102.     }
  103.  
  104.     @Subscribe
  105.     private void onProjectileSpawned(ProjectileSpawned event)
  106.     {
  107.         //
  108.     }
  109.  
  110.     // Sets minWildyLevel and maxWildyLevel vars.
  111.     // Also returns 0 if we are not in wildy, or 1 if we are.
  112.     private int setWildyLevels()
  113.     {
  114.         final Widget wildernessLevelWidget = client.getWidget(WidgetInfo.PVP_WILDERNESS_LEVEL);
  115.         if(wildernessLevelWidget == null) return 0; // Level 0 means not in wildy.
  116.  
  117.         int lowerLevel, higherLevel;
  118.  
  119.         final String wildernessLevelText = wildernessLevelWidget.getText();
  120.         log.info("Wildy text:\n" + wildernessLevelText);
  121.  
  122.  
  123.         String regex = "<br>(.*?)(.*)";
  124.         Pattern pattern = Pattern.compile(regex);
  125.  
  126.         final Matcher matcher = pattern.matcher(wildernessLevelText);
  127.  
  128.         if(!matcher.find()) return 0;
  129.         log.info("group is: " + matcher.group(2));
  130.  
  131.         String[] levels = matcher.group(2).split("-");
  132.         minWildyLevel = Integer.parseInt(levels[0]);
  133.         maxWildyLevel = Integer.parseInt(levels[1]);
  134.  
  135.         return 1;
  136.     }
  137.  
  138.     // Mostly copy pasted from the WorldHopper plugin, thx.
  139.     private void hop(int worldId)
  140.     {
  141.         WorldResult worldResult = worldService.getWorlds();
  142.         // Don't try to hop if the world doesn't exist
  143.         World world = worldResult.findWorld(worldId);
  144.         if (world == null)
  145.         {
  146.             return;
  147.         }
  148.  
  149.         final net.runelite.api.World rsWorld = client.createWorld();
  150.         rsWorld.setActivity(world.getActivity());
  151.         rsWorld.setAddress(world.getAddress());
  152.         rsWorld.setId(world.getId());
  153.         rsWorld.setPlayerCount(world.getPlayers());
  154.         rsWorld.setLocation(world.getLocation());
  155.         rsWorld.setTypes(WorldUtil.toWorldTypes(world.getTypes()));
  156.  
  157.         if (client.getGameState() == GameState.LOGIN_SCREEN)
  158.         {
  159.             // on the login screen we can just change the world by ourselves
  160.             client.changeWorld(rsWorld);
  161.             return;
  162.         }
  163.  
  164.         log.info("Hopping to world " + world.getId());
  165.  
  166.         quickHopTargetWorld = rsWorld;
  167.     }
  168.  
  169.  
  170.     // Mostly copy pasted from the WorldHopper plugin, thx.
  171.     private void hop(boolean previous)
  172.     {
  173.         WorldResult worldResult = worldService.getWorlds();
  174.         if(worldResult == null || client.getGameState() != GameState.LOGGED_IN)
  175.             return;
  176.  
  177.         World currentWorld = worldResult.findWorld(client.getWorld());
  178.  
  179.         if(currentWorld == null) return;
  180.  
  181.         EnumSet<WorldType> currentWorldTypes = currentWorld.getTypes().clone();
  182.  
  183.         currentWorldTypes.remove(WorldType.PVP);
  184.         currentWorldTypes.remove(WorldType.HIGH_RISK);
  185.         currentWorldTypes.remove(WorldType.BOUNTY);
  186.         currentWorldTypes.remove(WorldType.SKILL_TOTAL);
  187.         currentWorldTypes.remove(WorldType.LAST_MAN_STANDING);
  188.  
  189.         List<World> worlds = worldResult.getWorlds();
  190.  
  191.         int worldIdx = worlds.indexOf(currentWorld);
  192.         int totalLevel = client.getTotalLevel();
  193.  
  194.         World world;
  195.         int iters = 0;
  196.         do
  197.         {
  198.             if(previous)
  199.             {
  200.                 worldIdx--;
  201.                 if (worldIdx < 0) worldIdx = worlds.size() - 1;
  202.             }
  203.             else
  204.             {
  205.                 worldIdx++;
  206.                 if (worldIdx >= worlds.size())
  207.                     worldIdx = 0;
  208.             }
  209.  
  210.             log.info("Did " + iters + " iterations searching for world.");
  211.             log.info("Worlds size is: " + worlds.size());
  212.  
  213.             world = worlds.get(worldIdx);
  214.             EnumSet<WorldType> types = world.getTypes().clone();
  215.             types.remove(WorldType.BOUNTY);
  216.             types.remove(WorldType.LAST_MAN_STANDING);
  217.  
  218.             if(types.contains(WorldType.SKILL_TOTAL))
  219.             {
  220.                 try {
  221.                     int totalRequirement = Integer.parseInt(world.getActivity().substring(0, world.getActivity().indexOf(" ")));
  222.  
  223.                     if (totalLevel >= totalRequirement) {
  224.                         types.remove(WorldType.SKILL_TOTAL);
  225.                     }
  226.                 } catch (NumberFormatException ex) {
  227.                     log.warn("Failed to parse total level requirement for target world", ex);
  228.                 }
  229.             }
  230.  
  231.             // Break out if we've found a good world to hop to
  232.             if (currentWorldTypes.equals(types))
  233.             {
  234.                 log.info("Breaking out because world types are equals.");
  235.                 log.info("Found world id: " + world.getId());
  236.                 log.info("Current world id: " + currentWorld.getId());
  237.                 break;
  238.             }
  239.             if(world == currentWorld)
  240.             {
  241.                 log.info("Breaking out because worlds match.");
  242.             }
  243.         } while(world != currentWorld);
  244.  
  245.         if (world == currentWorld)
  246.             log.info("Couldn't find world to hop to!");
  247.         else
  248.             hop(world.getId());
  249.     }
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement