Search in sources :

Example 1 with Value

use of me.wiefferink.areashop.tools.Value in project AreaShop by NLthijs48.

the class TeleportFeature method teleportPlayer.

/**
 * Teleport a player to the region or sign.
 * @param player            Player that should be teleported
 * @param toSign            true to teleport to the first sign of the region, false for teleporting to the region itself
 * @param checkRestrictions Set to true if teleport permissions should be checked, false otherwise, also toggles cross-world check
 * @return true if the teleport succeeded, otherwise false
 */
public boolean teleportPlayer(Player player, boolean toSign, boolean checkRestrictions) {
    // Check basics
    if (getRegion().getWorld() == null) {
        getRegion().message(player, "general-noWorld");
        return false;
    }
    if (getRegion().getRegion() == null) {
        getRegion().message(player, "general-noRegion");
        return false;
    }
    if (checkRestrictions) {
        // Check correct world
        if (!getRegion().getBooleanSetting("general.teleportCrossWorld") && !player.getWorld().equals(getRegion().getWorld())) {
            getRegion().message(player, "teleport-wrongWorld", player.getWorld().getName());
            return false;
        }
        boolean owner = player.getUniqueId().equals(getRegion().getOwner());
        boolean friend = getRegion().getFriendsFeature().getFriends().contains(player.getUniqueId());
        boolean available = getRegion().isAvailable();
        // Teleport to sign instead if they dont have permission for teleporting to region
        if ((!toSign && owner && !player.hasPermission("areashop.teleport") && player.hasPermission("areashop.teleportsign") || !toSign && !owner && !friend && !player.hasPermission("areashop.teleportall") && player.hasPermission("areashop.teleportsignall") || !toSign && !owner && friend && !player.hasPermission("areashop.teleportfriend") && player.hasPermission("areashop.teleportfriendsign") || !toSign && !owner && !friend && available && !player.hasPermission("areashop.teleportavailable") && player.hasPermission("areashop.teleportavailablesign"))) {
            getRegion().message(player, "teleport-changedToSign");
            toSign = true;
        }
        // Check permissions
        if (owner && !available && !player.hasPermission("areashop.teleport") && !toSign) {
            getRegion().message(player, "teleport-noPermission");
            return false;
        } else if (!owner && !available && !player.hasPermission("areashop.teleportall") && !toSign && !friend) {
            getRegion().message(player, "teleport-noPermissionOther");
            return false;
        } else if (!owner && !available && !player.hasPermission("areashop.teleportfriend") && !toSign && friend) {
            getRegion().message(player, "teleport-noPermissionFriend");
            return false;
        } else if (available && !player.hasPermission("areashop.teleportavailable") && !toSign) {
            getRegion().message(player, "teleport-noPermissionAvailable");
            return false;
        } else if (owner && !available && !player.hasPermission("areashop.teleportsign") && toSign) {
            getRegion().message(player, "teleport-noPermissionSign");
            return false;
        } else if (!owner && !available && !player.hasPermission("areashop.teleportsignall") && toSign && !friend) {
            getRegion().message(player, "teleport-noPermissionOtherSign");
            return false;
        } else if (!owner && !available && !player.hasPermission("areashop.teleportfriendsign") && toSign && friend) {
            getRegion().message(player, "teleport-noPermissionFriendSign");
            return false;
        } else if (available && !player.hasPermission("areashop.teleportavailablesign") && toSign) {
            getRegion().message(player, "teleport-noPermissionAvailableSign");
            return false;
        }
    }
    // Get the starting location
    Value<Boolean> toSignRef = new Value<>(toSign);
    Location startLocation = getStartLocation(player, toSignRef);
    toSign = toSignRef.get();
    boolean insideRegion;
    if (toSign) {
        insideRegion = getRegion().getBooleanSetting("general.teleportToSignIntoRegion");
    } else {
        insideRegion = getRegion().getBooleanSetting("general.teleportIntoRegion");
    }
    // Check locations starting from startLocation and then a cube that increases
    // radius around that (until no block in the region is found at all cube sides)
    Location safeLocation = startLocation;
    ProtectedRegion worldguardRegion = getRegion().getRegion();
    boolean blocksInRegion = worldguardRegion.contains(startLocation.getBlockX(), startLocation.getBlockY(), startLocation.getBlockZ());
    if (!blocksInRegion && insideRegion) {
        getRegion().message(player, "teleport-blocked");
        return false;
    }
    // Tries limit tracking
    int radius = 1;
    int checked = 1;
    int maxTries = plugin.getConfig().getInt("maximumTries");
    // Tracking of which sides to continue the search
    boolean done = isSafe(safeLocation) && ((blocksInRegion && insideRegion) || (!insideRegion));
    boolean northDone = false, eastDone = false, southDone = false, westDone = false, topDone = false, bottomDone = false;
    boolean continueThisDirection;
    while (((blocksInRegion && insideRegion) || (!insideRegion)) && !done) {
        blocksInRegion = false;
        // North side
        continueThisDirection = false;
        for (int x = -radius + 1; x <= radius && !done && !northDone; x++) {
            for (int y = -radius + 1; y < radius && !done; y++) {
                safeLocation = startLocation.clone().add(x, y, -radius);
                if (safeLocation.getBlockY() > 256 || safeLocation.getBlockY() < 0) {
                    continue;
                }
                if (worldguardRegion.contains(safeLocation.getBlockX(), safeLocation.getBlockY(), safeLocation.getBlockZ()) || !insideRegion) {
                    checked++;
                    done = isSafe(safeLocation) || checked > maxTries;
                    blocksInRegion = true;
                    continueThisDirection = true;
                }
            }
        }
        northDone = northDone || !continueThisDirection;
        // East side
        continueThisDirection = false;
        for (int z = -radius + 1; z <= radius && !done && !eastDone; z++) {
            for (int y = -radius + 1; y < radius && !done; y++) {
                safeLocation = startLocation.clone().add(radius, y, z);
                if (safeLocation.getBlockY() > 256 || safeLocation.getBlockY() < 0) {
                    continue;
                }
                if (worldguardRegion.contains(safeLocation.getBlockX(), safeLocation.getBlockY(), safeLocation.getBlockZ()) || !insideRegion) {
                    checked++;
                    done = isSafe(safeLocation) || checked > maxTries;
                    blocksInRegion = true;
                    continueThisDirection = true;
                }
            }
        }
        eastDone = eastDone || !continueThisDirection;
        // South side
        continueThisDirection = false;
        for (int x = radius - 1; x >= -radius && !done && !southDone; x--) {
            for (int y = -radius + 1; y < radius && !done; y++) {
                safeLocation = startLocation.clone().add(x, y, radius);
                if (safeLocation.getBlockY() > 256 || safeLocation.getBlockY() < 0) {
                    continue;
                }
                if (worldguardRegion.contains(safeLocation.getBlockX(), safeLocation.getBlockY(), safeLocation.getBlockZ()) || !insideRegion) {
                    checked++;
                    done = isSafe(safeLocation) || checked > maxTries;
                    blocksInRegion = true;
                    continueThisDirection = true;
                }
            }
        }
        southDone = southDone || !continueThisDirection;
        // West side
        continueThisDirection = false;
        for (int z = radius - 1; z >= -radius && !done && !westDone; z--) {
            for (int y = -radius + 1; y < radius && !done; y++) {
                safeLocation = startLocation.clone().add(-radius, y, z);
                if (safeLocation.getBlockY() > 256 || safeLocation.getBlockY() < 0) {
                    continue;
                }
                if (worldguardRegion.contains(safeLocation.getBlockX(), safeLocation.getBlockY(), safeLocation.getBlockZ()) || !insideRegion) {
                    checked++;
                    done = isSafe(safeLocation) || checked > maxTries;
                    blocksInRegion = true;
                    continueThisDirection = true;
                }
            }
        }
        westDone = westDone || !continueThisDirection;
        // Top side
        continueThisDirection = false;
        // Middle block of the top
        if ((startLocation.getBlockY() + radius) > 256) {
            topDone = true;
        }
        if (!done && !topDone) {
            safeLocation = startLocation.clone().add(0, radius, 0);
            if (worldguardRegion.contains(safeLocation.getBlockX(), safeLocation.getBlockY(), safeLocation.getBlockZ()) || !insideRegion) {
                checked++;
                done = isSafe(safeLocation) || checked > maxTries;
                blocksInRegion = true;
                continueThisDirection = true;
            }
        }
        for (int r = 1; r <= radius && !done && !topDone; r++) {
            // North
            for (int x = -r + 1; x <= r && !done; x++) {
                safeLocation = startLocation.clone().add(x, radius, -r);
                if (worldguardRegion.contains(safeLocation.getBlockX(), safeLocation.getBlockY(), safeLocation.getBlockZ()) || !insideRegion) {
                    checked++;
                    done = isSafe(safeLocation) || checked > maxTries;
                    blocksInRegion = true;
                    continueThisDirection = true;
                }
            }
            // East
            for (int z = -r + 1; z <= r && !done; z++) {
                safeLocation = startLocation.clone().add(r, radius, z);
                if (worldguardRegion.contains(safeLocation.getBlockX(), safeLocation.getBlockY(), safeLocation.getBlockZ()) || !insideRegion) {
                    checked++;
                    done = isSafe(safeLocation) || checked > maxTries;
                    blocksInRegion = true;
                    continueThisDirection = true;
                }
            }
            // South side
            for (int x = r - 1; x >= -r && !done; x--) {
                safeLocation = startLocation.clone().add(x, radius, r);
                if (worldguardRegion.contains(safeLocation.getBlockX(), safeLocation.getBlockY(), safeLocation.getBlockZ()) || !insideRegion) {
                    checked++;
                    done = isSafe(safeLocation) || checked > maxTries;
                    blocksInRegion = true;
                    continueThisDirection = true;
                }
            }
            // West side
            for (int z = r - 1; z >= -r && !done; z--) {
                safeLocation = startLocation.clone().add(-r, radius, z);
                if (worldguardRegion.contains(safeLocation.getBlockX(), safeLocation.getBlockY(), safeLocation.getBlockZ()) || !insideRegion) {
                    checked++;
                    done = isSafe(safeLocation) || checked > maxTries;
                    blocksInRegion = true;
                    continueThisDirection = true;
                }
            }
        }
        topDone = topDone || !continueThisDirection;
        // Bottom side
        continueThisDirection = false;
        // Middle block of the bottom
        if (startLocation.getBlockY() - radius < 0) {
            bottomDone = true;
        }
        if (!done && !bottomDone) {
            safeLocation = startLocation.clone().add(0, -radius, 0);
            if (worldguardRegion.contains(safeLocation.getBlockX(), safeLocation.getBlockY(), safeLocation.getBlockZ()) || !insideRegion) {
                checked++;
                done = isSafe(safeLocation) || checked > maxTries;
                blocksInRegion = true;
                continueThisDirection = true;
            }
        }
        for (int r = 1; r <= radius && !done && !bottomDone; r++) {
            // North
            for (int x = -r + 1; x <= r && !done; x++) {
                safeLocation = startLocation.clone().add(x, -radius, -r);
                if (worldguardRegion.contains(safeLocation.getBlockX(), safeLocation.getBlockY(), safeLocation.getBlockZ()) || !insideRegion) {
                    checked++;
                    done = isSafe(safeLocation) || checked > maxTries;
                    blocksInRegion = true;
                    continueThisDirection = true;
                }
            }
            // East
            for (int z = -r + 1; z <= r && !done; z++) {
                safeLocation = startLocation.clone().add(r, -radius, z);
                if (worldguardRegion.contains(safeLocation.getBlockX(), safeLocation.getBlockY(), safeLocation.getBlockZ()) || !insideRegion) {
                    checked++;
                    done = isSafe(safeLocation) || checked > maxTries;
                    blocksInRegion = true;
                    continueThisDirection = true;
                }
            }
            // South side
            for (int x = r - 1; x >= -r && !done; x--) {
                safeLocation = startLocation.clone().add(x, -radius, r);
                if (worldguardRegion.contains(safeLocation.getBlockX(), safeLocation.getBlockY(), safeLocation.getBlockZ()) || !insideRegion) {
                    checked++;
                    done = isSafe(safeLocation) || checked > maxTries;
                    blocksInRegion = true;
                    continueThisDirection = true;
                }
            }
            // West side
            for (int z = r - 1; z >= -r && !done; z--) {
                safeLocation = startLocation.clone().add(-r, -radius, z);
                if (worldguardRegion.contains(safeLocation.getBlockX(), safeLocation.getBlockY(), safeLocation.getBlockZ()) || !insideRegion) {
                    checked++;
                    done = isSafe(safeLocation) || checked > maxTries;
                    blocksInRegion = true;
                    continueThisDirection = true;
                }
            }
        }
        bottomDone = bottomDone || !continueThisDirection;
        // Increase cube radius
        radius++;
    }
    if (done && isSafe(safeLocation)) {
        if (toSign) {
            getRegion().message(player, "teleport-successSign");
            // Let the player look at the sign
            Vector playerVector = safeLocation.toVector();
            playerVector.setY(playerVector.getY() + player.getEyeHeight(true));
            Vector signVector = getRegion().getSignsFeature().getSigns().get(0).getLocation().toVector().add(new Vector(0.5, 0.5, 0.5));
            Vector direction = playerVector.clone().subtract(signVector).normalize();
            safeLocation.setYaw(180 - (float) Math.toDegrees(Math.atan2(direction.getX(), direction.getZ())));
            safeLocation.setPitch(90 - (float) Math.toDegrees(Math.acos(direction.getY())));
        } else {
            getRegion().message(player, "teleport-success");
        }
        player.teleport(safeLocation);
        AreaShop.debug("Found location: " + safeLocation.toString() + " Tries: " + (checked - 1));
        return true;
    } else {
        getRegion().message(player, "teleport-noSafe", checked - 1, maxTries);
        AreaShop.debug("No location found, checked " + (checked - 1) + " spots of max " + maxTries);
        return false;
    }
}
Also used : Value(me.wiefferink.areashop.tools.Value) ProtectedRegion(com.sk89q.worldguard.protection.regions.ProtectedRegion) Vector(org.bukkit.util.Vector) Location(org.bukkit.Location)

Aggregations

ProtectedRegion (com.sk89q.worldguard.protection.regions.ProtectedRegion)1 Value (me.wiefferink.areashop.tools.Value)1 Location (org.bukkit.Location)1 Vector (org.bukkit.util.Vector)1