Search in sources :

Example 1 with IslandNewEvent

use of com.wasteofplastic.askyblock.events.IslandNewEvent in project askyblock by tastybento.

the class IslandCmd method newIsland.

/**
 * Makes an island using schematic. No permission checks are made. They have to be decided
 * before this method is called.
 * @param player
 * @param schematic
 */
public void newIsland(final Player player, final Schematic schematic) {
    // long time = System.nanoTime();
    final UUID playerUUID = player.getUniqueId();
    boolean firstTime = false;
    if (!plugin.getPlayers().hasIsland(playerUUID)) {
        firstTime = true;
    }
    // plugin.getLogger().info("DEBUG: finding island location");
    Location next = getNextIsland(player.getUniqueId());
    // plugin.getLogger().info("DEBUG: found " + next);
    // Set the player's parameters to this island
    plugin.getPlayers().setHasIsland(playerUUID, true);
    // Clear any old home locations (they should be clear, but just in case)
    plugin.getPlayers().clearHomeLocations(playerUUID);
    // Set the player's island location to this new spot
    plugin.getPlayers().setIslandLocation(playerUUID, next);
    // Teleport to the new home
    if (schematic.isPlayerSpawn()) {
        // Set home and teleport
        plugin.getPlayers().setHomeLocation(playerUUID, schematic.getPlayerSpawn(next), 1);
        // Save it for later reference
        plugin.getPlayers().setHomeLocation(playerUUID, schematic.getPlayerSpawn(next), -1);
    }
    // Sets a flag to temporarily disable cleanstone generation
    plugin.setNewIsland(true);
    // Create island based on schematic
    if (schematic != null) {
        // Paste the starting island. If it is a HELL biome, then we start in the Nether
        if (Settings.createNether && schematic.isInNether() && Settings.newNether && ASkyBlock.getNetherWorld() != null) {
            // Paste the overworld if it exists
            if (!schematic.getPartnerName().isEmpty() && schematics.containsKey(schematic.getPartnerName())) {
                // A partner schematic is available
                pastePartner(schematics.get(schematic.getPartnerName()), next, player);
            }
            // Switch home location to the Nether
            next = next.toVector().toLocation(ASkyBlock.getNetherWorld());
            // Set the player's island location to this new spot
            plugin.getPlayers().setIslandLocation(playerUUID, next);
            schematic.pasteSchematic(next, player, true, firstTime ? PasteReason.NEW_ISLAND : PasteReason.RESET);
        } else {
            // Over world start
            // plugin.getLogger().info("DEBUG: pasting");
            // long timer = System.nanoTime();
            // Paste the island and teleport the player home
            schematic.pasteSchematic(next, player, true, firstTime ? PasteReason.NEW_ISLAND : PasteReason.RESET);
            // plugin.getLogger().info("DEBUG: pasted overworld");
            if (Settings.createNether && Settings.newNether && ASkyBlock.getNetherWorld() != null) {
                // Paste the other world schematic
                final Location netherLoc = next.toVector().toLocation(ASkyBlock.getNetherWorld());
                if (schematic.getPartnerName().isEmpty()) {
                    // This will paste the over world schematic again
                    // plugin.getLogger().info("DEBUG: pasting nether");
                    pastePartner(schematic, netherLoc, player);
                // plugin.getLogger().info("DEBUG: pasted nether");
                } else {
                    if (schematics.containsKey(schematic.getPartnerName())) {
                        // plugin.getLogger().info("DEBUG: pasting partner");
                        // A partner schematic is available
                        pastePartner(schematics.get(schematic.getPartnerName()), netherLoc, player);
                    } else {
                        plugin.getLogger().severe("Partner schematic heading '" + schematic.getPartnerName() + "' does not exist");
                    }
                }
            }
        }
        // Record the rating of this schematic - not used for anything right now
        plugin.getPlayers().setStartIslandRating(playerUUID, schematic.getRating());
    }
    // Clear the cleanstone flag so events can happen again
    plugin.setNewIsland(false);
    // Add to the grid
    Island myIsland = plugin.getGrid().addIsland(next.getBlockX(), next.getBlockZ(), playerUUID);
    myIsland.setLevelHandicap(schematic.getLevelHandicap());
    // Save the player so that if the server is reset weird things won't happen
    plugin.getPlayers().save(playerUUID);
    // Start the reset cooldown
    if (!firstTime) {
        setResetWaitTime(player);
    }
    // Set the custom protection range if appropriate
    // Dynamic island range sizes with permissions
    int range = Settings.islandProtectionRange;
    for (PermissionAttachmentInfo perms : player.getEffectivePermissions()) {
        if (perms.getPermission().startsWith(Settings.PERMPREFIX + "island.range.")) {
            if (perms.getPermission().contains(Settings.PERMPREFIX + "island.range.*")) {
                range = Settings.islandProtectionRange;
                break;
            } else {
                String[] spl = perms.getPermission().split(Settings.PERMPREFIX + "island.range.");
                if (spl.length > 1) {
                    if (!NumberUtils.isDigits(spl[1])) {
                        plugin.getLogger().severe("Player " + player.getName() + " has permission: " + perms.getPermission() + " <-- the last part MUST be a number! Ignoring...");
                    } else {
                        range = Math.max(range, Integer.valueOf(spl[1]));
                    }
                }
            }
        }
    }
    // Do some sanity checking
    if (range % 2 != 0) {
        range--;
        plugin.getLogger().warning("Protection range must be even, using " + range + " for " + player.getName());
    }
    if (range > Settings.islandDistance) {
        plugin.getLogger().warning("Player has " + Settings.PERMPREFIX + "island.range." + range);
        range = Settings.islandDistance;
        plugin.getLogger().warning("Island protection range must be " + Settings.islandDistance + " or less. Setting to: " + range);
    }
    myIsland.setProtectionSize(range);
    // Save grid just in case there's a crash
    plugin.getGrid().saveGrid();
    // Done - fire event
    final IslandNewEvent event = new IslandNewEvent(player, schematic, myIsland);
    plugin.getServer().getPluginManager().callEvent(event);
// plugin.getLogger().info("DEBUG: Done! " + (System.nanoTime()- time) * 0.000001);
}
Also used : PermissionAttachmentInfo(org.bukkit.permissions.PermissionAttachmentInfo) UUID(java.util.UUID) IslandNewEvent(com.wasteofplastic.askyblock.events.IslandNewEvent) Island(com.wasteofplastic.askyblock.Island) Location(org.bukkit.Location)

Aggregations

Island (com.wasteofplastic.askyblock.Island)1 IslandNewEvent (com.wasteofplastic.askyblock.events.IslandNewEvent)1 UUID (java.util.UUID)1 Location (org.bukkit.Location)1 PermissionAttachmentInfo (org.bukkit.permissions.PermissionAttachmentInfo)1