worldlocation: null check closestArea
This commit is contained in:
@@ -9,11 +9,11 @@
|
|||||||
|
|
||||||
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.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 +193,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 +224,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 +235,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,7 +244,7 @@ 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()
|
public static Map<WorldArea, String> getLOCATION_MAP()
|
||||||
{
|
{
|
||||||
Map<WorldArea, String> hashMap = new HashMap<>();
|
Map<WorldArea, String> hashMap = new HashMap<>();
|
||||||
Arrays.stream(values()).forEach(worldLocation ->
|
Arrays.stream(values()).forEach(worldLocation ->
|
||||||
@@ -238,53 +254,68 @@ public enum WorldLocation
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user