Search in sources :

Example 6 with SettingsFlag

use of com.wasteofplastic.askyblock.Island.SettingsFlag in project askyblock by tastybento.

the class SettingsPanel method onInventoryClick.

/**
 * Handle clicks to the Settings panel
 * @param event
 */
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onInventoryClick(InventoryClickEvent event) {
    // The player that clicked the item
    Player player = (Player) event.getWhoClicked();
    // The inventory that was clicked in
    Inventory inventory = event.getInventory();
    if (inventory.getName() == null) {
        return;
    }
    int slot = event.getRawSlot();
    // Check this is the right panel
    if (!inventory.getName().equals(plugin.myLocale(player.getUniqueId()).igsTitle)) {
        return;
    }
    // Stop removal of items
    event.setCancelled(true);
    if (event.getSlotType() == SlotType.OUTSIDE) {
        player.closeInventory();
        inventory.clear();
        return;
    }
    if (event.getClick().equals(ClickType.SHIFT_RIGHT)) {
        player.closeInventory();
        inventory.clear();
        player.updateInventory();
        return;
    }
    // Check world
    if (!player.getLocation().getWorld().equals(ASkyBlock.getIslandWorld()) && !player.getLocation().getWorld().equals(ASkyBlock.getNetherWorld())) {
        return;
    }
    // 1.7.x server
    if (!hasArmorStand && slot > lookup.size()) {
        return;
    }
    // 1.8.x server
    if (slot > (lookup.size() + 1)) {
        return;
    }
    // Get the flag
    SettingsFlag flag = null;
    if (lookup.containsKey(event.getCurrentItem().getType())) {
        // All other items
        flag = lookup.get(event.getCurrentItem().getType());
    } else if (hasArmorStand && event.getCurrentItem().getType() == Material.ARMOR_STAND) {
        // Special handling to avoid errors on 1.7.x servers
        flag = SettingsFlag.ARMOR_STAND;
    }
    // If flag is null, do nothing
    if (flag == null) {
        return;
    }
    // Players can only do something if they own the island or are op
    Island island = plugin.getGrid().getIslandAt(player.getLocation());
    if (island != null && (player.isOp() || (island.getOwner() != null && island.getOwner().equals(player.getUniqueId())))) {
        // Check perms
        if (player.isOp() || player.hasPermission(Settings.PERMPREFIX + "settings." + flag.toString())) {
            // plugin.getLogger().info("DEBUG: Player has perm " + flag.toString());
            if (flag.equals(SettingsFlag.PVP) || flag.equals(SettingsFlag.NETHER_PVP)) {
                // PVP always results in an inventory closure
                player.closeInventory();
                inventory.clear();
                // PVP activation
                if (!island.getIgsFlag(flag)) {
                    // plugin.getLogger().info("DEBUG: attempt to activate PVP");
                    if (pvpCoolDown.containsKey(player.getUniqueId())) {
                        // plugin.getLogger().info("DEBUG: player is in the cooldown list");
                        long setTime = pvpCoolDown.get(player.getUniqueId());
                        // plugin.getLogger().info("DEBUG: set time is " + setTime);
                        long secondsLeft = Settings.pvpRestartCooldown - (System.currentTimeMillis() - setTime) / 1000;
                        // plugin.getLogger().info("DEBUG: seconds left = " + secondsLeft);
                        if (secondsLeft > 0) {
                            Util.sendMessage(player, ChatColor.RED + "You must wait " + secondsLeft + " seconds until you can do that again!");
                            return;
                        }
                        // Tidy up
                        pvpCoolDown.remove(player.getUniqueId());
                    }
                    // Warn players on the island
                    for (Player p : plugin.getServer().getOnlinePlayers()) {
                        if (island.onIsland(p.getLocation())) {
                            if (flag.equals(SettingsFlag.NETHER_PVP)) {
                                Util.sendMessage(p, ChatColor.RED + "" + ChatColor.BOLD + plugin.myLocale(p.getUniqueId()).igs.get(SettingsFlag.NETHER_PVP) + " " + plugin.myLocale(p.getUniqueId()).igsAllowed);
                            } else {
                                Util.sendMessage(p, ChatColor.RED + "" + ChatColor.BOLD + plugin.myLocale(p.getUniqueId()).igs.get(SettingsFlag.PVP) + " " + plugin.myLocale(p.getUniqueId()).igsAllowed);
                            }
                            if (plugin.getServer().getVersion().contains("(MC: 1.8") || plugin.getServer().getVersion().contains("(MC: 1.7")) {
                                player.getWorld().playSound(player.getLocation(), Sound.valueOf("ARROW_HIT"), 1F, 1F);
                            } else {
                                player.getWorld().playSound(player.getLocation(), Sound.ENTITY_ARROW_HIT, 1F, 1F);
                            }
                        }
                    }
                    // Toggle the flag
                    island.toggleIgs(flag);
                    // Update warp signs
                    final List<UUID> members = island.getMembers();
                    // Run one tick later because text gets updated at the end of tick
                    plugin.getServer().getScheduler().runTask(plugin, new Runnable() {

                        @Override
                        public void run() {
                            for (UUID playerUUID : members) {
                                plugin.getWarpPanel().updateWarp(playerUUID);
                            }
                        }
                    });
                    return;
                } else {
                    // PVP deactivation
                    // Store this deactivation time
                    pvpCoolDown.put(player.getUniqueId(), System.currentTimeMillis());
                    // Immediately toggle the setting
                    island.toggleIgs(flag);
                    // Update warp signs
                    final List<UUID> members = island.getMembers();
                    // Run one tick later because text gets updated at the end of tick
                    plugin.getServer().getScheduler().runTask(plugin, new Runnable() {

                        @Override
                        public void run() {
                            for (UUID playerUUID : members) {
                                plugin.getWarpPanel().updateWarp(playerUUID);
                            }
                        }
                    });
                    // Warn players of change
                    for (Player p : plugin.getServer().getOnlinePlayers()) {
                        if (island.onIsland(p.getLocation())) {
                            // Deactivate PVP
                            if (flag.equals(SettingsFlag.NETHER_PVP)) {
                                Util.sendMessage(p, ChatColor.GREEN + plugin.myLocale(p.getUniqueId()).igs.get(SettingsFlag.NETHER_PVP) + " " + plugin.myLocale(p.getUniqueId()).igsDisallowed);
                            } else {
                                Util.sendMessage(p, ChatColor.GREEN + plugin.myLocale(p.getUniqueId()).igs.get(SettingsFlag.PVP) + " " + plugin.myLocale(p.getUniqueId()).igsDisallowed);
                            }
                            if (plugin.getServer().getVersion().contains("(MC: 1.8") || plugin.getServer().getVersion().contains("(MC: 1.7")) {
                                p.getWorld().playSound(p.getLocation(), Sound.valueOf("FIREWORK_TWINKLE"), 1F, 1F);
                            } else {
                                p.getWorld().playSound(p.getLocation(), Sound.ENTITY_FIREWORK_TWINKLE, 1F, 1F);
                            }
                        }
                    }
                }
            } else {
                island.toggleIgs(flag);
            }
        }
        // player.closeInventory();
        inventory.clear();
        player.openInventory(islandGuardPanel(player));
    }
}
Also used : SettingsFlag(com.wasteofplastic.askyblock.Island.SettingsFlag) Player(org.bukkit.entity.Player) UUID(java.util.UUID) Island(com.wasteofplastic.askyblock.Island) Inventory(org.bukkit.inventory.Inventory) EventHandler(org.bukkit.event.EventHandler)

Example 7 with SettingsFlag

use of com.wasteofplastic.askyblock.Island.SettingsFlag in project askyblock by tastybento.

the class GridManager method convert.

/**
 * Converts from the old version where islands were stored in an island
 * folder.
 * Did not work for large installations.
 */
private void convert() {
    // Read spawn file if it exists
    final File spawnFile = new File(plugin.getDataFolder(), "spawn.yml");
    if (spawnFile.exists()) {
        YamlConfiguration spawn = new YamlConfiguration();
        try {
            spawn.load(spawnFile);
            int range = spawn.getInt("spawn.range");
            // plugin.getLogger().info("DEBUG:" + range + " " +
            // spawn.getString("spawn.bedrock",""));
            Location spawnLoc = Util.getLocationString(spawn.getString("spawn.bedrock", ""));
            if (spawnLoc != null && onGrid(spawnLoc)) {
                Island newIsland = addIsland(spawnLoc.getBlockX(), spawnLoc.getBlockZ());
                setSpawn(newIsland);
                newIsland.setProtectionSize(range);
            } else {
                plugin.getLogger().severe("Spawn could not be imported! Location " + spawnLoc);
                plugin.getLogger().severe("Go to the spawn island and set it manually");
            }
        } catch (Exception e) {
            plugin.getLogger().severe("Spawn could not be imported! File could not load.");
        }
    }
    // Go through player folder
    final File playerFolder = new File(plugin.getDataFolder() + File.separator + "players");
    final File quarantineFolder = new File(plugin.getDataFolder() + File.separator + "quarantine");
    YamlConfiguration playerFile = new YamlConfiguration();
    int noisland = 0;
    int inTeam = 0;
    int count = 0;
    if (playerFolder.exists() && playerFolder.listFiles().length > 0) {
        plugin.getLogger().warning("Reading player folder...");
        if (playerFolder.listFiles().length > 5000) {
            plugin.getLogger().warning("This could take some time with a large number of islands...");
        }
        for (File f : playerFolder.listFiles()) {
            // Need to remove the .yml suffix
            String fileName = f.getName();
            if (fileName.endsWith(".yml")) {
                try {
                    playerFile.load(f);
                    boolean hasIsland = playerFile.getBoolean("hasIsland", false);
                    if (hasIsland) {
                        String islandLocation = playerFile.getString("islandLocation");
                        if (islandLocation.isEmpty()) {
                            plugin.getLogger().severe("Problem with " + fileName);
                            plugin.getLogger().severe("Owner :" + playerFile.getString("playerName", "Unknown"));
                            plugin.getLogger().severe("Player file says they have an island, but there is no location.");
                            // Move to quarantine
                            if (!quarantineFolder.exists()) {
                                quarantineFolder.mkdir();
                            }
                            // Move the file
                            plugin.getLogger().severe("Moving " + f.getName() + " to " + quarantineFolder.getName());
                            File rename = new File(quarantineFolder, f.getName());
                            f.renameTo(rename);
                        } else {
                            // Location exists
                            Location islandLoc = Util.getLocationString(islandLocation);
                            if (islandLoc != null) {
                                // Check to see if this island is already loaded
                                Island island = getIslandAt(islandLoc);
                                if (island != null) {
                                    // PlayerIsland exists, compare creation dates
                                    plugin.getLogger().severe("Problem with " + fileName);
                                    plugin.getLogger().severe("Owner :" + playerFile.getString("playerName", "Unknown"));
                                    plugin.getLogger().severe("This island location already exists and is already imported");
                                    if (island.getUpdatedDate() > f.lastModified()) {
                                        plugin.getLogger().severe("Previous file is more recent so keeping it.");
                                        // Move to quarantine
                                        if (!quarantineFolder.exists()) {
                                            quarantineFolder.mkdir();
                                        }
                                        plugin.getLogger().severe("Moving " + (playerFile.getString("playerName", "Unknown")) + "'s file (" + f.getName() + ") to " + quarantineFolder.getName());
                                        File rename = new File(quarantineFolder, f.getName());
                                        f.renameTo(rename);
                                    } else {
                                        // New file is more recent
                                        plugin.getLogger().severe(playerFile.getString("playerName", "Unknown") + "'s file is more recent");
                                        File oldFile = new File(playerFolder, island.getOwner().toString() + ".yml");
                                        File rename = new File(quarantineFolder, oldFile.getName());
                                        // Move to quarantine
                                        if (!quarantineFolder.exists()) {
                                            quarantineFolder.mkdir();
                                        }
                                        plugin.getLogger().severe("Moving previous file (" + oldFile.getName() + ") to " + quarantineFolder.getName());
                                        oldFile.renameTo(rename);
                                        deleteIsland(islandLoc);
                                        island = null;
                                    }
                                }
                                if (island == null) {
                                    if (!onGrid(islandLoc)) {
                                        plugin.getLogger().severe("Problem with " + fileName);
                                        plugin.getLogger().severe("Owner :" + playerFile.getString("playerName", "Unknown"));
                                        plugin.getLogger().severe("Island is not on grid lines! " + islandLoc);
                                    }
                                    String ownerString = fileName.substring(0, fileName.length() - 4);
                                    // Add the island
                                    UUID owner = UUID.fromString(ownerString);
                                    Island newIsland = addIsland(islandLoc.getBlockX(), islandLoc.getBlockZ(), owner);
                                    ownershipMap.put(owner, newIsland);
                                    // Grab when this was last updated
                                    newIsland.setUpdatedDate(f.lastModified());
                                    if ((count) % 1000 == 0) {
                                        plugin.getLogger().info("Converted " + count + " islands");
                                    }
                                    count++;
                                    // plugin.getLogger().info("Converted island at "
                                    // + islandLoc);
                                    // Top ten
                                    int islandLevel = playerFile.getInt("islandLevel", 0);
                                    String teamLeaderUUID = playerFile.getString("teamLeader", "");
                                    if (islandLevel > 0) {
                                        if (!playerFile.getBoolean("hasTeam")) {
                                            plugin.getTopTen().topTenAddEntry(owner, islandLevel);
                                        } else if (!teamLeaderUUID.isEmpty()) {
                                            if (teamLeaderUUID.equals(ownerString)) {
                                                plugin.getTopTen().topTenAddEntry(owner, islandLevel);
                                            }
                                        }
                                    }
                                    // Check if there is an island info string and see if it jibes
                                    String islandInfo = playerFile.getString("islandInfo", "");
                                    if (!islandInfo.isEmpty()) {
                                        String[] split = islandInfo.split(":");
                                        try {
                                            // int protectionRange = Integer.parseInt(split[3]);
                                            // int islandDistance = Integer.parseInt(split[4]);
                                            newIsland.setLocked(false);
                                            if (split.length > 6) {
                                                // Get locked status
                                                if (split[6].equalsIgnoreCase("true")) {
                                                    newIsland.setLocked(true);
                                                }
                                            }
                                            // Check if deletable
                                            newIsland.setPurgeProtected(false);
                                            if (split.length > 7) {
                                                if (split[7].equalsIgnoreCase("true")) {
                                                    newIsland.setPurgeProtected(true);
                                                }
                                            }
                                            if (!split[5].equals("null")) {
                                                if (split[5].equals("spawn")) {
                                                    newIsland.setSpawn(true);
                                                    // Try to get the spawn point
                                                    if (split.length > 8) {
                                                        // plugin.getLogger().info("DEBUG: " + serial.substring(serial.indexOf(":SP:") + 4));
                                                        Location spawnPoint = Util.getLocationString(islandInfo.substring(islandInfo.indexOf(":SP:") + 4));
                                                        newIsland.setSpawnPoint(spawnPoint);
                                                    }
                                                }
                                            }
                                            // Check if protection options there
                                            if (!newIsland.isSpawn()) {
                                                // plugin.getLogger().info("DEBUG: NOT SPAWN owner is " + owner + " location " + center);
                                                if (split.length > 8 && split[8].length() == 29) {
                                                    // Parse the 8th string into island guard protection settings
                                                    int index = 0;
                                                    // Run through the enum and set
                                                    for (SettingsFlag flag : SettingsFlag.values()) {
                                                        if (index < split[8].length()) {
                                                            newIsland.setIgsFlag(flag, split[8].charAt(index++) == '1');
                                                        }
                                                    }
                                                }
                                            }
                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        }
                                    }
                                }
                            } else {
                                plugin.getLogger().severe("Problem with " + fileName);
                                plugin.getLogger().severe("Owner :" + playerFile.getString("playerName", "Unknown"));
                                plugin.getLogger().severe("The world for this file does not exist!");
                            }
                        }
                    } else {
                        noisland++;
                        if (playerFile.getBoolean("hasTeam", false)) {
                            inTeam++;
                        }
                    }
                } catch (Exception e) {
                    plugin.getLogger().severe("Problem with " + fileName);
                // e.printStackTrace();
                }
            }
        }
        plugin.getLogger().info("Converted " + count + " islands from player's folder");
        plugin.getLogger().info(noisland + " have no island, of which " + inTeam + " are in a team.");
        plugin.getLogger().info((noisland - inTeam) + " are in the system, but have no island or team");
    }
    int count2 = 0;
    // Check island folder
    final File islandFolder = new File(plugin.getDataFolder() + File.separator + "islands");
    if (islandFolder.exists() && islandFolder.listFiles().length > 0) {
        plugin.getLogger().warning("Reading island folder...");
        if (islandFolder.listFiles().length > 5000) {
            plugin.getLogger().warning("This could take some time with a large number of islands...");
        }
        for (File f : islandFolder.listFiles()) {
            // Need to remove the .yml suffix
            String fileName = f.getName();
            int comma = fileName.indexOf(",");
            if (fileName.endsWith(".yml") && comma != -1) {
                try {
                    // Parse to an island value
                    int x = Integer.parseInt(fileName.substring(0, comma));
                    int z = Integer.parseInt(fileName.substring(comma + 1, fileName.indexOf(".")));
                    if (!onGrid(x, z)) {
                        plugin.getLogger().severe("Island is not on grid lines! " + x + "," + z + " skipping...");
                    } else {
                        // Note that this is the CENTER of the island
                        if (getIslandAt(x, z) == null) {
                            addIsland(x, z);
                            if (count2 % 1000 == 0) {
                                plugin.getLogger().info("Converted " + count + " islands");
                            }
                            count2++;
                        // plugin.getLogger().info("Added island from island folder: "
                        // + x + "," +z);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        plugin.getLogger().info("Converted " + count2 + " islands from island folder");
        plugin.getLogger().info("Total " + (count + count2) + " islands converted.");
    }
    // Now save the islandGrid
    saveGrid();
}
Also used : SettingsFlag(com.wasteofplastic.askyblock.Island.SettingsFlag) YamlConfiguration(org.bukkit.configuration.file.YamlConfiguration) UUID(java.util.UUID) File(java.io.File) IOException(java.io.IOException) Location(org.bukkit.Location)

Aggregations

SettingsFlag (com.wasteofplastic.askyblock.Island.SettingsFlag)7 UUID (java.util.UUID)5 ArrayList (java.util.ArrayList)4 Island (com.wasteofplastic.askyblock.Island)3 File (java.io.File)3 IOException (java.io.IOException)3 TreeMap (java.util.TreeMap)3 YamlConfiguration (org.bukkit.configuration.file.YamlConfiguration)3 Player (org.bukkit.entity.Player)3 SetBiome (com.wasteofplastic.askyblock.panels.SetBiome)2 HashMap (java.util.HashMap)2 Location (org.bukkit.Location)2 Material (org.bukkit.Material)2 Biome (org.bukkit.block.Biome)2 EntityType (org.bukkit.entity.EntityType)2 Inventory (org.bukkit.inventory.Inventory)2 HashMultiset (com.google.common.collect.HashMultiset)1 Multiset (com.google.common.collect.Multiset)1 ASLocale (com.wasteofplastic.askyblock.ASLocale)1 ASkyBlock (com.wasteofplastic.askyblock.ASkyBlock)1