Selaa lähdekoodia

added optionals for preventing NPE's when in overworld

mastercake10 5 vuotta sitten
vanhempi
commit
9382c8b0e2

+ 1 - 1
pom.xml

@@ -4,7 +4,7 @@
 	<modelVersion>4.0.0</modelVersion>
 	<groupId>xyz.spaceio</groupId>
 	<artifactId>CustomOreGen</artifactId>
-	<version>1.3.7-SNAPSHOT</version>
+	<version>1.3.8-SNAPSHOT</version>
 	<repositories>
 		<!-- Spigot repository -->
 		<repository>

+ 4 - 3
src/xyz/spaceio/customoregen/CustomOreGen.java

@@ -9,6 +9,7 @@ import java.util.Arrays;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
+import java.util.Optional;
 import java.util.Set;
 import java.util.UUID;
 import java.util.stream.Collectors;
@@ -151,11 +152,11 @@ public class CustomOreGen extends JavaPlugin {
 		if (skyblockAPI.getIslandOwner(loc) == null) {
 			return null;
 		}
-		UUID uuid = skyblockAPI.getIslandOwner(loc);
-		if (uuid == null) {
+		Optional<UUID> uuid = skyblockAPI.getIslandOwner(loc);
+		if (!uuid.isPresent()) {
 			return null;
 		}
-		OfflinePlayer p = Bukkit.getOfflinePlayer(uuid);
+		OfflinePlayer p = Bukkit.getOfflinePlayer(uuid.get());
 
 		return p;
 	}

+ 7 - 2
src/xyz/spaceio/hooks/HookASkyBlock.java

@@ -1,5 +1,6 @@
 package xyz.spaceio.hooks;
 
+import java.util.Optional;
 import java.util.UUID;
 
 import org.bukkit.Location;
@@ -20,8 +21,12 @@ public class HookASkyBlock implements SkyblockAPIHook {
 	}
 
 	@Override
-	public UUID getIslandOwner(Location loc) {
-		return api.getIslandAt(loc).getOwner();
+	public Optional<UUID> getIslandOwner(Location loc) {
+		Optional<UUID> optional = Optional.empty();
+		if(api.getIslandAt(loc) != null) {
+			optional = Optional.of(api.getIslandAt(loc).getOwner());
+		}
+		return optional;
 	}
 
 	@Override

+ 7 - 2
src/xyz/spaceio/hooks/HookAcidIsland.java

@@ -1,5 +1,6 @@
 package xyz.spaceio.hooks;
 
+import java.util.Optional;
 import java.util.UUID;
 
 import org.bukkit.Location;
@@ -20,8 +21,12 @@ public class HookAcidIsland implements SkyblockAPIHook {
 	}
 
 	@Override
-	public UUID getIslandOwner(Location loc) {
-		return api.getIslandAt(loc).getOwner();
+	public Optional<UUID> getIslandOwner(Location loc) {
+		Optional<UUID> optional = Optional.empty();
+		if(api.getIslandAt(loc) != null) {
+			optional = Optional.of(api.getIslandAt(loc).getOwner());
+		}
+		return optional;
 	}
 
 	@Override

+ 4 - 3
src/xyz/spaceio/hooks/HookBentoBox.java

@@ -40,13 +40,14 @@ public class HookBentoBox implements SkyblockAPIHook{
 	}
 
 	@Override
-	public UUID getIslandOwner(Location loc) {
+	public Optional<UUID> getIslandOwner(Location loc) {
 		Optional<Island> optIsland = api.getIslands().getIslandAt(loc);
+		Optional<UUID> optional = Optional.empty();
 		
 		if(optIsland.isPresent()) {
-			return api.getIslands().getIslandAt(loc).get().getOwner();
+			optional = Optional.of(api.getIslands().getIslandAt(loc).get().getOwner());
 		}
-		return null;
+		return optional;
 	}
 
 	@Override

+ 14 - 12
src/xyz/spaceio/hooks/HookSkyblockEarth.java

@@ -3,6 +3,7 @@ package xyz.spaceio.hooks;
 import java.io.File;
 import java.lang.reflect.Field;
 import java.util.Arrays;
+import java.util.Optional;
 import java.util.UUID;
 
 import org.bukkit.Bukkit;
@@ -10,6 +11,7 @@ import org.bukkit.Location;
 import org.bukkit.configuration.file.FileConfiguration;
 
 import me.goodandevil.skyblock.config.FileManager.Config;
+import me.goodandevil.skyblock.island.Island;
 import me.goodandevil.skyblock.island.IslandEnvironment;
 import me.goodandevil.skyblock.island.IslandManager;
 import me.goodandevil.skyblock.island.IslandWorld;
@@ -42,19 +44,19 @@ public class HookSkyblockEarth implements SkyblockAPIHook {
 	}
 
 	@Override
-	public UUID getIslandOwner(Location loc) {
-		UUID[] owner = new UUID[1];
+	public Optional<UUID> getIslandOwner(Location loc) {
+		Optional<UUID> optional = api.getIslands().values().stream().filter(i -> isIslandAtLocation(i, loc)).findFirst().map(i -> i.getOwnerUUID());
 		
-		api.getIslands().forEach((k, v) -> {
-			Arrays.asList(IslandWorld.values()).forEach(world -> {
-				if (LocationUtil.isLocationAtLocationRadius(loc, v.getLocation(world, IslandEnvironment.Island), v.getRadius())) {
-					owner[0] = k;
-				}
-			});
-
-		});
-		
-		return owner[0];
+		return optional;
+	}
+	
+	private boolean isIslandAtLocation(Island island, Location loc) {
+		for(IslandWorld iw : IslandWorld.values()) {
+			if (LocationUtil.isLocationAtLocationRadius(loc, island.getLocation(iw, IslandEnvironment.Island), island.getRadius())) {
+				return true;
+			}
+		}
+		return false;
 	}
 
 	@Override

+ 6 - 4
src/xyz/spaceio/hooks/HookuSkyBlock.java

@@ -1,5 +1,6 @@
 package xyz.spaceio.hooks;
 
+import java.util.Optional;
 import java.util.UUID;
 
 import org.bukkit.Bukkit;
@@ -21,13 +22,14 @@ public class HookuSkyBlock implements SkyblockAPIHook {
 	}
 
 	@Override
-	public UUID getIslandOwner(Location loc) {
+	public Optional<UUID> getIslandOwner(Location loc) {
+		Optional<UUID> optional = Optional.empty();
+		
 		String player = api.getIslandInfo(loc).getLeader();
 		if ((Bukkit.getPlayer(player) != null) && (Bukkit.getPlayer(player).getUniqueId() != null)) {
-			return Bukkit.getOfflinePlayer(player).getUniqueId();
-		} else {
-			return null;
+			optional =  Optional.of(Bukkit.getOfflinePlayer(player).getUniqueId());
 		}
+		return optional;
 	}
 
 	@Override

+ 2 - 1
src/xyz/spaceio/hooks/SkyblockAPIHook.java

@@ -1,5 +1,6 @@
 package xyz.spaceio.hooks;
 
+import java.util.Optional;
 import java.util.UUID;
 
 import org.bukkit.Location;
@@ -21,7 +22,7 @@ public interface SkyblockAPIHook {
 	 * @param loc location to check for island
 	 * @return island owner UUID
 	 */
-	public UUID getIslandOwner(Location loc);
+	public Optional<UUID> getIslandOwner(Location loc);
 	
 	/**
 	 * Obtains the names of the skyblock worlds