Made code easier to understand

This commit is contained in:
Tastybento 2018-02-10 14:32:13 -08:00
parent 2b286ca58a
commit d32a6f8ff1
2 changed files with 62 additions and 88 deletions

View File

@ -71,7 +71,7 @@ public class SafeSpotTeleport {
// Start checking // Start checking
checking = true; checking = true;
// Start a recurring task until done or cancelled // Start a recurring task until done or cancelled
task = plugin.getServer().getScheduler().runTaskTimer(plugin, () -> { task = plugin.getServer().getScheduler().runTaskTimer(plugin, () -> {
List<ChunkSnapshot> chunkSnapshot = new ArrayList<>(); List<ChunkSnapshot> chunkSnapshot = new ArrayList<>();
@ -120,7 +120,7 @@ public class SafeSpotTeleport {
List<Pair<Integer, Integer>> result = new ArrayList<>(); List<Pair<Integer, Integer>> result = new ArrayList<>();
// Get island if available // Get island if available
Optional<Island> island = plugin.getIslands().getIslandAt(location); Optional<Island> island = plugin.getIslands().getIslandAt(location);
int maxRadius = island.map(x -> x.getProtectionRange()).orElse(plugin.getSettings().getIslandProtectionRange()); int maxRadius = island.map(Island::getProtectionRange).orElse(plugin.getSettings().getIslandProtectionRange());
int x = location.getBlockX(); int x = location.getBlockX();
int z = location.getBlockZ(); int z = location.getBlockZ();
// Create ever increasing squares around the target location // Create ever increasing squares around the target location
@ -130,7 +130,7 @@ public class SafeSpotTeleport {
for (int j = z - radius; j <= z + radius; j++) { for (int j = z - radius; j <= z + radius; j++) {
Pair<Integer, Integer> blockCoord = new Pair<>(i,j); Pair<Integer, Integer> blockCoord = new Pair<>(i,j);
Pair<Integer, Integer> chunkCoord = new Pair<>((int)i/16, (int)j/16); Pair<Integer, Integer> chunkCoord = new Pair<>(i/16, j/16);
if (!result.contains(chunkCoord)) { if (!result.contains(chunkCoord)) {
// Add the chunk coord // Add the chunk coord
if (!island.isPresent()) { if (!island.isPresent()) {
@ -142,7 +142,7 @@ public class SafeSpotTeleport {
if (is.inIslandSpace(blockCoord)) { if (is.inIslandSpace(blockCoord)) {
result.add(chunkCoord); result.add(chunkCoord);
} }
}); });
} }
} }
} }
@ -176,7 +176,6 @@ public class SafeSpotTeleport {
* @return true if a safe spot was found * @return true if a safe spot was found
*/ */
private boolean scanChunk(ChunkSnapshot chunk) { private boolean scanChunk(ChunkSnapshot chunk) {
World world = location.getWorld();
// Max height // Max height
int maxHeight = location.getWorld().getMaxHeight() - 20; int maxHeight = location.getWorld().getMaxHeight() - 20;
// Run through the chunk // Run through the chunk
@ -185,25 +184,9 @@ public class SafeSpotTeleport {
// Work down from the entry point up // Work down from the entry point up
for (int y = Math.min(chunk.getHighestBlockYAt(x, z), maxHeight); y >= 0; y--) { for (int y = Math.min(chunk.getHighestBlockYAt(x, z), maxHeight); y >= 0; y--) {
if (checkBlock(chunk, x,y,z, maxHeight)) { if (checkBlock(chunk, x,y,z, maxHeight)) {
Vector newSpot = new Vector(chunk.getX() * 16 + x + 0.5D, y + 1, chunk.getZ() * 16 + z + 0.5D); return true;
// Check for portal
if (portal) {
if (chunk.getBlockType(x, y, z).equals(Material.PORTAL)) {
// Teleport as soon as we find a portal
teleportEntity(newSpot.toLocation(world));
return true;
} else if (bestSpot == null ) {
// Stash the best spot
bestSpot = newSpot.toLocation(world);
}
} else {
// Regular search - teleport as soon as we find something
teleportEntity(newSpot.toLocation(world));
return true;
}
} }
} // end y
}
} //end z } //end z
} // end x } // end x
return false; return false;
@ -270,56 +253,72 @@ public class SafeSpotTeleport {
* @param y * @param y
* @param z * @param z
* @param worldHeight * @param worldHeight
* @return * @return true if this is a safe spot, false if this is a portal scan
*/ */
private boolean checkBlock(ChunkSnapshot chunk, int x, int y, int z, int worldHeight) { private boolean checkBlock(ChunkSnapshot chunk, int x, int y, int z, int worldHeight) {
World world = location.getWorld();
Bukkit.getLogger().info("checking " + x + " " + y + " "+ z); Bukkit.getLogger().info("checking " + x + " " + y + " "+ z);
Material type = chunk.getBlockType(x, y, z); Material type = chunk.getBlockType(x, y, z);
if (!type.equals(Material.AIR)) { // AIR if (!type.equals(Material.AIR)) { // AIR
Material space1 = chunk.getBlockType(x, Math.min(y + 1, worldHeight), z); Material space1 = chunk.getBlockType(x, Math.min(y + 1, worldHeight), z);
Material space2 = chunk.getBlockType(x, Math.min(y + 2, worldHeight), z); Material space2 = chunk.getBlockType(x, Math.min(y + 2, worldHeight), z);
if ((space1.equals(Material.AIR) && space2.equals(Material.AIR)) if ((space1.equals(Material.AIR) && space2.equals(Material.AIR))
|| (space1.equals(Material.PORTAL) && space2.equals(Material.PORTAL))) { || (space1.equals(Material.PORTAL) && space2.equals(Material.PORTAL))
// Now there is a chance that this is a safe spot && (!type.toString().contains("FENCE")
// Check for safe ground && !type.toString().contains("DOOR")
if (!type.toString().contains("FENCE") && !type.toString().contains("GATE")
&& !type.toString().contains("DOOR") && !type.toString().contains("PLATE"))) {
&& !type.toString().contains("GATE") switch (type) {
&& !type.toString().contains("PLATE")) { // Unsafe
switch (type) { case ANVIL:
// Unsafe case BARRIER:
case ANVIL: case BOAT:
case BARRIER: case CACTUS:
case BOAT: case DOUBLE_PLANT:
case CACTUS: case ENDER_PORTAL:
case DOUBLE_PLANT: case FIRE:
case ENDER_PORTAL: case FLOWER_POT:
case FIRE: case LADDER:
case FLOWER_POT: case LAVA:
case LADDER: case LEVER:
case LAVA: case LONG_GRASS:
case LEVER: case PISTON_EXTENSION:
case LONG_GRASS: case PISTON_MOVING_PIECE:
case PISTON_EXTENSION: case PORTAL:
case PISTON_MOVING_PIECE: case SIGN_POST:
case PORTAL: case SKULL:
case SIGN_POST: case STANDING_BANNER:
case SKULL: case STATIONARY_LAVA:
case STANDING_BANNER: case STATIONARY_WATER:
case STATIONARY_LAVA: case STONE_BUTTON:
case STATIONARY_WATER: case TORCH:
case STONE_BUTTON: case TRIPWIRE:
case TORCH: case WATER:
case TRIPWIRE: case WEB:
case WATER: case WOOD_BUTTON:
case WEB: //Block is dangerous
case WOOD_BUTTON: break;
//Block is dangerous default:
break; // Safe
default: Vector newSpot = new Vector(chunk.getX() * 16 + x + 0.5D, y + 1, chunk.getZ() * 16 + z + 0.5D);
// Safe // Check for portal
if (portal) {
if (chunk.getBlockType(x, y, z).equals(Material.PORTAL)) {
// Teleport as soon as we find a portal
teleportEntity(newSpot.toLocation(world));
return true;
} else if (bestSpot == null) {
// Stash the best spot
bestSpot = newSpot.toLocation(world);
return false;
}
} else {
// Regular search - teleport as soon as we find something
teleportEntity(newSpot.toLocation(world));
return true; return true;
} }
return true;
} }
} }
} }

View File

@ -11,7 +11,6 @@ import java.util.List;
import org.apache.commons.lang.math.NumberUtils; import org.apache.commons.lang.math.NumberUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
@ -176,30 +175,6 @@ public class Util {
return fin; return fin;
} }
/**
* Checks if player has this type of item in either hand
* @param player
* @param type
* @return true if they are holding an item of type type
*/
@SuppressWarnings("deprecation")
public static boolean playerIsHolding(Player player, Material type) {
if (plugin.getServer().getVersion().contains("(MC: 1.7")
|| plugin.getServer().getVersion().contains("(MC: 1.8")) {
if (player.getItemInHand() != null && player.getItemInHand().getType().equals(type)) {
return true;
}
return false;
}
if (player.getInventory().getItemInMainHand() != null && player.getInventory().getItemInMainHand().getType().equals(type)) {
return true;
}
if (player.getInventory().getItemInMainHand() != null && player.getInventory().getItemInOffHand().getType().equals(type)) {
return true;
}
return false;
}
/** /**
* Determines if a location is in the island world or not or * Determines if a location is in the island world or not or
* in the new nether if it is activated * in the new nether if it is activated