worldlocation: null check closestArea (#1909)

worldlocation: null check closestArea
This commit is contained in:
Owain van Brakel
2019-11-07 09:35:12 +01:00
committed by GitHub
3 changed files with 46 additions and 24 deletions

View File

@@ -9,11 +9,10 @@
package net.runelite.client.game; package net.runelite.client.game;
import com.google.common.collect.ImmutableMap;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import lombok.Getter; import lombok.Getter;
import net.runelite.api.coords.WorldArea; import net.runelite.api.coords.WorldArea;
@@ -193,6 +192,20 @@ public enum WorldLocation
private final WorldArea worldArea; private final WorldArea worldArea;
@Getter @Getter
private final Location location; private final Location location;
@Getter
private static final Map<WorldArea, String> LOCATION_MAP;
static
{
ImmutableMap.Builder<WorldArea, String> builder = ImmutableMap.builder();
for (WorldLocation value : values())
{
builder.put(value.getWorldArea(), value.getName());
}
LOCATION_MAP = builder.build();
}
/** /**
* Creates a location used to get the name of a location by a WorldPoint * Creates a location used to get the name of a location by a WorldPoint
@@ -210,6 +223,7 @@ public enum WorldLocation
/** /**
* Returns all locations that aren't in the wild * Returns all locations that aren't in the wild
*
* @return - A Collection of non-wilderness WorldLocations * @return - A Collection of non-wilderness WorldLocations
*/ */
public static Collection<WorldLocation> getNonWildernessLocations() public static Collection<WorldLocation> getNonWildernessLocations()
@@ -220,6 +234,7 @@ public enum WorldLocation
/** /**
* Returns only the WorldLocations that are in the wilderness * Returns only the WorldLocations that are in the wilderness
*
* @return - A Collection of WorldLocations in the wilderness * @return - A Collection of WorldLocations in the wilderness
*/ */
public static Collection<WorldLocation> getWildernessLocations() public static Collection<WorldLocation> getWildernessLocations()
@@ -228,63 +243,70 @@ public enum WorldLocation
PvPUtil.getWildernessLevelFrom(loc.worldArea.toWorldPoint()) > 0).collect(Collectors.toList()); PvPUtil.getWildernessLevelFrom(loc.worldArea.toWorldPoint()) > 0).collect(Collectors.toList());
} }
public static Map<WorldArea, String> getLocationMap()
{
Map<WorldArea, String> hashMap = new HashMap<>();
Arrays.stream(values()).forEach(worldLocation ->
hashMap.put(worldLocation.getWorldArea(), worldLocation.getName()));
return hashMap;
}
/** /**
* Returns the WorldLocation that a WorldPoint is in, or the closest WorldLocation to the point * Returns the WorldLocation that a WorldPoint is in, or the closest WorldLocation to the point
*
* @param worldPoint - the WorldPoint to find the WorldLocation of * @param worldPoint - the WorldPoint to find the WorldLocation of
* @return - Containing location or closest location if it isn't in any * @return - Containing location or closest location if it isn't in any
*/ */
public static String location(WorldPoint worldPoint) public static String location(WorldPoint worldPoint)
{ {
final Map<WorldArea, String> locationMap = getLocationMap();
int dist = 10000; int dist = 10000;
String s = ""; String s = "";
WorldArea closestArea = null; WorldArea closestArea = null;
for (Map.Entry<WorldArea, String> entry : locationMap.entrySet())
for (Map.Entry<WorldArea, String> entry : LOCATION_MAP.entrySet())
{ {
WorldArea worldArea = entry.getKey(); final WorldArea worldArea = entry.getKey();
if (worldArea.toWorldPointList().contains(worldPoint)) if (worldArea.toWorldPointList().contains(worldPoint))
{ {
s = entry.getValue(); s = entry.getValue();
return s; return s;
} }
int distTo = worldArea.distanceTo(worldPoint);
final int distTo = worldArea.distanceTo(worldPoint);
if (distTo < dist) if (distTo < dist)
{ {
dist = distTo; dist = distTo;
closestArea = worldArea; closestArea = worldArea;
} }
} }
if (worldPoint.getY() > (Objects.requireNonNull(closestArea).toWorldPoint().getY() + closestArea.getHeight()))
if (closestArea == null)
{
return s;
}
if (worldPoint.getY() > closestArea.toWorldPoint().getY() + closestArea.getHeight())
{ {
s = s + "N"; s = s + "N";
} }
if (worldPoint.getY() < closestArea.toWorldPoint().getY()) if (worldPoint.getY() < closestArea.toWorldPoint().getY())
{ {
s = s + "S"; s = s + "S";
} }
if (worldPoint.getX() < closestArea.toWorldPoint().getX()) if (worldPoint.getX() < closestArea.toWorldPoint().getX())
{ {
s = s + "W"; s = s + "W";
} }
if (worldPoint.getX() > (closestArea.toWorldPoint().getX() + closestArea.getWidth())) if (worldPoint.getX() > (closestArea.toWorldPoint().getX() + closestArea.getWidth()))
{ {
s = s + "E"; s = s + "E";
} }
s = s + " of "; s = s + " of ";
s = s + locationMap.get(closestArea); s = s + LOCATION_MAP.get(closestArea);
if (s.startsWith(" of ")) if (s.startsWith(" of "))
{ {
s = s.substring(3); s = s.substring(3);
} }
return s; return s;
} }

View File

@@ -93,7 +93,7 @@ public class PlayerScouter extends Plugin
{ {
private static final HiscoreClient HISCORE_CLIENT = new HiscoreClient(); private static final HiscoreClient HISCORE_CLIENT = new HiscoreClient();
private static final DiscordClient DISCORD_CLIENT = new DiscordClient(); private static final DiscordClient DISCORD_CLIENT = new DiscordClient();
private static final Map<WorldArea, String> WILD_LOCS = WorldLocation.getLocationMap(); private static final Map<WorldArea, String> WILD_LOCS = WorldLocation.getLOCATION_MAP();
private static final SimpleDateFormat SDF = new SimpleDateFormat("MMM dd h:mm a z"); private static final SimpleDateFormat SDF = new SimpleDateFormat("MMM dd h:mm a z");
private static final String ICON_URL = "https://www.osrsbox.com/osrsbox-db/items-icons/"; // Add item id + ".png" private static final String ICON_URL = "https://www.osrsbox.com/osrsbox-db/items-icons/"; // Add item id + ".png"
@Inject @Inject

View File

@@ -12,18 +12,17 @@ package net.runelite.client.plugins.wildernesslocations;
import com.google.inject.Provides; import com.google.inject.Provides;
import java.awt.Color; import java.awt.Color;
import java.util.Map;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
import lombok.AccessLevel; import lombok.AccessLevel;
import lombok.Getter; import lombok.Getter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import net.runelite.api.Client; import net.runelite.api.Client;
import net.runelite.api.Player;
import net.runelite.api.ScriptID; import net.runelite.api.ScriptID;
import net.runelite.api.VarClientStr; import net.runelite.api.VarClientStr;
import net.runelite.api.Varbits; import net.runelite.api.Varbits;
import net.runelite.api.WorldType; import net.runelite.api.WorldType;
import net.runelite.api.coords.WorldArea;
import net.runelite.api.coords.WorldPoint; import net.runelite.api.coords.WorldPoint;
import net.runelite.api.events.ConfigChanged; import net.runelite.api.events.ConfigChanged;
import net.runelite.api.events.GameTick; import net.runelite.api.events.GameTick;
@@ -33,13 +32,13 @@ import net.runelite.client.callback.ClientThread;
import net.runelite.client.config.ConfigManager; import net.runelite.client.config.ConfigManager;
import net.runelite.client.config.Keybind; import net.runelite.client.config.Keybind;
import net.runelite.client.eventbus.EventBus; import net.runelite.client.eventbus.EventBus;
import net.runelite.client.game.WorldLocation;
import net.runelite.client.input.KeyManager; import net.runelite.client.input.KeyManager;
import net.runelite.client.plugins.Plugin; import net.runelite.client.plugins.Plugin;
import net.runelite.client.plugins.PluginDescriptor; import net.runelite.client.plugins.PluginDescriptor;
import net.runelite.client.plugins.PluginType; import net.runelite.client.plugins.PluginType;
import net.runelite.client.ui.overlay.OverlayManager; import net.runelite.client.ui.overlay.OverlayManager;
import net.runelite.client.util.HotkeyListener; import net.runelite.client.util.HotkeyListener;
import net.runelite.client.game.WorldLocation;
@Slf4j @Slf4j
@PluginDescriptor( @PluginDescriptor(
@@ -86,7 +85,6 @@ public class WildernessLocationsPlugin extends Plugin
private String oldChat = ""; private String oldChat = "";
private int currentCooldown = 0; private int currentCooldown = 0;
private WorldPoint worldPoint = null; private WorldPoint worldPoint = null;
private static final Map<WorldArea, String> wildLocs = WorldLocation.getLocationMap();
private final HotkeyListener hotkeyListener = new HotkeyListener(() -> this.keybind) private final HotkeyListener hotkeyListener = new HotkeyListener(() -> this.keybind)
{ {
@@ -173,11 +171,13 @@ public class WildernessLocationsPlugin extends Plugin
{ {
currentCooldown--; currentCooldown--;
} }
renderLocation = (client.getVar(Varbits.IN_WILDERNESS) == 1
|| (this.pvpWorld && WorldType.isAllPvpWorld(client.getWorldType()))); renderLocation = (client.getVar(Varbits.IN_WILDERNESS) == 1 || (this.pvpWorld && WorldType.isAllPvpWorld(client.getWorldType())));
if (renderLocation) if (renderLocation)
{ {
if (client.getLocalPlayer().getWorldLocation() != worldPoint) final Player player = client.getLocalPlayer();
if (player != null && player.getWorldLocation() != worldPoint)
{ {
locationString = WorldLocation.location(client.getLocalPlayer().getWorldLocation()); locationString = WorldLocation.location(client.getLocalPlayer().getWorldLocation());
worldPoint = client.getLocalPlayer().getWorldLocation(); worldPoint = client.getLocalPlayer().getWorldLocation();