Search in sources :

Example 11 with ProtectedRegion

use of com.sk89q.worldguard.protection.regions.ProtectedRegion in project LandLord by SpatiumPrinceps.

the class AddfriendAll method onAddfriend.

public void onAddfriend(Player player, String name) {
    if (name == null || name.isEmpty()) {
        player.sendMessage(lm.getString("Commands.AddfriendAll.noPlayer").replace("%player%", name == null ? "[]" : name));
        return;
    }
    plugin.getPlayerManager().getOfflinePlayer(name, lPlayer -> {
        if (lPlayer == null) {
            // Failure
            player.sendMessage(lm.getString("Commands.AddfriendAll.noPlayer").replace("%player%", name));
        } else {
            int count = 0;
            for (ProtectedRegion protectedRegion : plugin.getWgHandler().getRegions(player.getUniqueId())) {
                if (!protectedRegion.getMembers().getUniqueIds().contains(lPlayer.getUuid())) {
                    protectedRegion.getMembers().addPlayer(lPlayer.getUuid());
                    count++;
                }
            }
            player.sendMessage(lm.getString("Commands.AddfriendAll.success").replace("%player%", name).replace("%count%", count + ""));
            new BukkitRunnable() {

                @Override
                public void run() {
                    plugin.getMapManager().updateAll();
                }
            }.runTask(plugin);
        }
    });
}
Also used : ProtectedRegion(com.sk89q.worldguard.protection.regions.ProtectedRegion) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable)

Example 12 with ProtectedRegion

use of com.sk89q.worldguard.protection.regions.ProtectedRegion in project LandLord by SpatiumPrinceps.

the class UnfriendAll method onUnfriendall.

public void onUnfriendall(Player player, String name) {
    if (name == null || name.isEmpty()) {
        player.sendMessage(lm.getString("Commands.Addfriend.noPlayer").replace("%players%", Collections.singletonList("[]").toString()));
        return;
    }
    plugin.getPlayerManager().getOfflinePlayer(name, lPlayer -> {
        // Failure
        if (lPlayer == null) {
            player.sendMessage(lm.getString("Commands.UnfriendAll.noPlayer").replace("%players%", Collections.singletonList(name).toString()));
        } else {
            // Success
            int count = 0;
            for (ProtectedRegion protectedRegion : plugin.getWgHandler().getRegions(player.getUniqueId())) {
                if (protectedRegion.getMembers().contains(lPlayer.getUuid())) {
                    protectedRegion.getMembers().removePlayer(lPlayer.getUuid());
                    count++;
                }
            }
            if (count > 0) {
                player.sendMessage(lm.getString("Commands.UnfriendAll.success").replace("%count%", String.valueOf(count)).replace("%players%", name));
                new BukkitRunnable() {

                    @Override
                    public void run() {
                        plugin.getMapManager().updateAll();
                    }
                }.runTask(plugin);
            } else {
                player.sendMessage(lm.getString("Commands.UnfriendAll.noFriend").replace("%player%", name));
            }
        }
    });
}
Also used : ProtectedRegion(com.sk89q.worldguard.protection.regions.ProtectedRegion) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable)

Example 13 with ProtectedRegion

use of com.sk89q.worldguard.protection.regions.ProtectedRegion in project LandLord by SpatiumPrinceps.

the class ListLands method onListLands.

public void onListLands(Player sender, LPlayer target, int page) {
    List<ProtectedRegion> lands = new ArrayList<>(plugin.getWgHandler().getRegions(target.getUuid()));
    if (lands.size() > 0) {
        String mode = plugin.getConfig().getString("CommandSettings.ListLands.mode");
        if (mode.equals("gui")) {
            MultiPagedGUI landGui = new MultiPagedGUI(sender, 5, plugin.getLangManager().getRawString("Commands.ListLands.header").replace("%player%", target.getName()));
            lands.forEach(land -> landGui.addIcon(new Icon(new ItemStack(Material.GRASS)).setName(land.getId())));
            landGui.setIcon(52, new Icon(new ItemStack(Material.BEACON)).setName(lm.getRawString("Commands.ListLands.manageAll")).addClickAction((p, ic2) -> {
                ManageGUIAll manageGUIAll = new ManageGUIAll(sender, landGui, plugin.getWgHandler().getRegionsAsOL(target.getUuid()));
                manageGUIAll.display();
            }));
            landGui.display();
        } else {
            // Chat based system
            List<String> formatted = new ArrayList<>();
            String segment = lm.getRawString("Commands.ListLands.chat.segment");
            lands.forEach(land -> {
                OwnedLand ol = plugin.getLand(land);
                formatted.add(segment.replace("%landname%", ol.getName()).replace("%members%", ol.printMembers()));
            });
            String prev = lm.getRawString("Commands.ListLands.chat.previous");
            String next = lm.getRawString("Commands.ListLands.chat.next");
            MultiPagedMessage message = new MultiPagedMessage("/land list", plugin.getLangManager().getRawString("Commands.ListLands.header").replace("%player%", target.getName()), plugin.getConfig().getInt("CommandSettings.ListLands.landsPerPage"), formatted, prev, next, page);
            sender.spigot().sendMessage(message.create());
        }
    } else {
        sender.sendMessage(plugin.getLangManager().getString("Commands.ListLands.noLands"));
    }
}
Also used : MultiPagedGUI(biz.princeps.lib.gui.MultiPagedGUI) LandlordCommand(biz.princeps.landlord.commands.LandlordCommand) ManageGUIAll(biz.princeps.landlord.guis.ManageGUIAll) ProtectedRegion(com.sk89q.worldguard.protection.regions.ProtectedRegion) LPlayer(biz.princeps.landlord.persistent.LPlayer) OwnedLand(biz.princeps.landlord.util.OwnedLand) Player(org.bukkit.entity.Player) OfflinePlayer(org.bukkit.OfflinePlayer) ItemStack(org.bukkit.inventory.ItemStack) ArrayList(java.util.ArrayList) List(java.util.List) MultiPagedMessage(biz.princeps.lib.chat.MultiPagedMessage) Icon(biz.princeps.lib.gui.simple.Icon) Material(org.bukkit.Material) ManageGUIAll(biz.princeps.landlord.guis.ManageGUIAll) MultiPagedGUI(biz.princeps.lib.gui.MultiPagedGUI) OwnedLand(biz.princeps.landlord.util.OwnedLand) ProtectedRegion(com.sk89q.worldguard.protection.regions.ProtectedRegion) ArrayList(java.util.ArrayList) Icon(biz.princeps.lib.gui.simple.Icon) MultiPagedMessage(biz.princeps.lib.chat.MultiPagedMessage) ItemStack(org.bukkit.inventory.ItemStack)

Example 14 with ProtectedRegion

use of com.sk89q.worldguard.protection.regions.ProtectedRegion in project AreaShop by NLthijs48.

the class ImportJob method execute.

/**
 * Execute the job.
 */
private void execute() {
    // Check for RegionForSale data
    File regionForSaleFolder = new File(plugin.getDataFolder().getParentFile().getAbsolutePath(), "RegionForSale");
    if (!regionForSaleFolder.exists()) {
        message("import-noPluginFolder", regionForSaleFolder.getName());
        return;
    }
    File worldsFolder = new File(regionForSaleFolder.getAbsolutePath(), "worlds");
    if (!worldsFolder.exists()) {
        message("import-noWorldsFolder");
        return;
    }
    File[] worldFolders = worldsFolder.listFiles();
    if (worldFolders == null) {
        message("import-noWorldsFolder");
        return;
    }
    // Import data for each world
    message("import-start");
    // Group with settings for all imported regions
    RegionGroup regionForSaleGroup = new RegionGroup(plugin, "RegionForSale");
    plugin.getFileManager().addGroup(regionForSaleGroup);
    // Import /RegionForSale/config.yml settings
    File regionForSaleConfigFile = new File(regionForSaleFolder.getAbsolutePath(), "config.yml");
    YamlConfiguration regionForSaleConfig = loadConfiguration(regionForSaleConfigFile);
    if (regionForSaleConfig == null) {
        messageNoPrefix("import-loadConfigFailed", regionForSaleConfigFile.getAbsolutePath());
        regionForSaleConfig = new YamlConfiguration();
    } else {
        importRegionSettings(regionForSaleConfig, regionForSaleGroup.getSettings(), null);
        regionForSaleGroup.setSetting("priority", 0);
    }
    // Import /RegionForSale/general.yml settings
    File regionForSaleGeneralFile = new File(regionForSaleFolder.getAbsolutePath(), "config.yml");
    YamlConfiguration regionForSaleGeneral = loadConfiguration(regionForSaleConfigFile);
    if (regionForSaleGeneral == null) {
        messageNoPrefix("import-loadConfigFailed", regionForSaleGeneralFile.getAbsolutePath());
    } else {
        // Collection interval of RegionForSale maps to rent duration
        String duration = "1 day";
        if (regionForSaleGeneral.isLong("interval.collect_money")) {
            duration = minutesToString(regionForSaleGeneral.getLong("interval.collect_money"));
        }
        regionForSaleGroup.setSetting("rent.duration", duration);
        // Global economy account has an effect close to landlord in AreaShop
        if (regionForSaleGeneral.isString("global_econ_account")) {
            regionForSaleGroup.setSetting("general.landlordName", regionForSaleGeneral.getString("global_econ_account"));
        }
    }
    regionForSaleGroup.saveRequired();
    // //////// Handle defaults of RegionForSale
    // Set autoExtend, to keep the same behavior as RegionForSale had
    regionForSaleGroup.setSetting("rent.autoExtend", true);
    // Import regions from each world
    for (File worldFolder : worldFolders) {
        // Skip files
        if (!worldFolder.isDirectory()) {
            continue;
        }
        messageNoPrefix("import-doWorld", worldFolder.getName());
        // Get the Bukkit world
        World world = Bukkit.getWorld(worldFolder.getName());
        if (world == null) {
            messageNoPrefix("import-noBukkitWorld");
            continue;
        }
        // Get the WorldGuard RegionManager
        RegionManager regionManager = plugin.getWorldGuard().getRegionManager(world);
        if (regionManager == null) {
            messageNoPrefix("import-noRegionManger");
            continue;
        }
        // Load the /worlds/<world>/regions.yml file
        File regionsFile = new File(worldFolder.getAbsolutePath(), "regions.yml");
        YamlConfiguration regions = loadConfiguration(regionsFile);
        if (regions == null) {
            messageNoPrefix("import-loadRegionsFailed", regionsFile.getAbsolutePath());
            continue;
        }
        // Load /worlds/<world>/config.yml file
        File worldConfigFile = new File(worldFolder.getAbsolutePath(), "config.yml");
        YamlConfiguration worldConfig = loadConfiguration(worldConfigFile);
        if (worldConfig == null) {
            messageNoPrefix("import-loadWorldConfigFailed", worldConfigFile.getAbsolutePath());
            // Simply skip importing the settings, since this is not really fatal
            worldConfig = new YamlConfiguration();
        } else {
            // RegionGroup with all world settings
            RegionGroup worldGroup = new RegionGroup(plugin, "RegionForSale-" + worldFolder.getName());
            importRegionSettings(worldConfig, worldGroup.getSettings(), null);
            worldGroup.setSetting("priority", 1);
            worldGroup.addWorld(worldFolder.getName());
            plugin.getFileManager().addGroup(regionForSaleGroup);
            worldGroup.saveRequired();
        }
        // Create groups to hold settings of /worlds/<world>/parent-regions.yml
        File parentRegionsFile = new File(worldFolder.getAbsolutePath(), "parent-regions.yml");
        YamlConfiguration parentRegions = loadConfiguration(parentRegionsFile);
        if (parentRegions == null) {
            messageNoPrefix("import-loadParentRegionsFailed", parentRegionsFile.getAbsolutePath());
        // Non-fatal, so just continue
        } else {
            for (String parentRegionName : parentRegions.getKeys(false)) {
                // Get WorldGuard region
                ProtectedRegion worldGuardRegion = regionManager.getRegion(parentRegionName);
                if (worldGuardRegion == null) {
                    messageNoPrefix("import-noWorldGuardRegionParent", parentRegionName);
                    continue;
                }
                // Get settings section
                ConfigurationSection parentRegionSection = parentRegions.getConfigurationSection(parentRegionName);
                if (parentRegionSection == null) {
                    messageNoPrefix("import-improperParentRegion", parentRegionName);
                    continue;
                }
                // Skip if it does not have any settings
                if (parentRegionSection.getKeys(false).isEmpty()) {
                    continue;
                }
                // Import parent region settings into a RegionGroup
                RegionGroup parentRegionGroup = new RegionGroup(plugin, "RegionForSale-" + worldFolder.getName() + "-" + parentRegionName);
                importRegionSettings(parentRegionSection, parentRegionGroup.getSettings(), null);
                parentRegionGroup.setSetting("priority", 2 + parentRegionSection.getLong("info.priority", 0));
                parentRegionGroup.saveRequired();
            // TODO add all regions that are contained in this parent region
            // Utils.getWorldEditRegionsInSelection()
            }
        }
        // Read and import regions
        for (String regionKey : regions.getKeys(false)) {
            GeneralRegion existingRegion = plugin.getFileManager().getRegion(regionKey);
            if (existingRegion != null) {
                if (world.getName().equalsIgnoreCase(existingRegion.getWorldName())) {
                    messageNoPrefix("import-alreadyAdded", regionKey);
                } else {
                    messageNoPrefix("import-alreadyAddedOtherWorld", regionKey, existingRegion.getWorldName(), world.getName());
                }
                continue;
            }
            ConfigurationSection regionSection = regions.getConfigurationSection(regionKey);
            if (regionSection == null) {
                messageNoPrefix("import-invalidRegionSection", regionKey);
                continue;
            }
            // Get WorldGuard region
            ProtectedRegion worldGuardRegion = regionManager.getRegion(regionKey);
            if (worldGuardRegion == null) {
                messageNoPrefix("import-noWorldGuardRegion", regionKey);
                continue;
            }
            String owner = regionSection.getString("info.owner", null);
            boolean isBought = regionSection.getBoolean("info.is-bought");
            // TODO: should also take into config settings of parent regions
            boolean rentable = regionSection.getBoolean("economic-settings.rentable", worldConfig.getBoolean("economic-settings.rentable", regionForSaleConfig.getBoolean("economic-settings.rentable")));
            boolean buyable = regionSection.getBoolean("economic-settings.buyable", worldConfig.getBoolean("economic-settings.buyable", regionForSaleConfig.getBoolean("economic-settings.buyable")));
            // Can be bought and rented, import as buy
            if (buyable && rentable) {
                messageNoPrefix("import-buyAndRent", regionKey);
            }
            // Cannot be bought or rented, skip
            if (!buyable && !rentable && owner == null) {
                messageNoPrefix("import-noBuyAndNoRent", regionKey);
                continue;
            }
            // Create region
            GeneralRegion region;
            if (rentable || (owner != null && !isBought)) {
                region = new RentRegion(regionKey, world);
                plugin.getFileManager().addRent((RentRegion) region);
            } else {
                region = new BuyRegion(regionKey, world);
                plugin.getFileManager().addBuy((BuyRegion) region);
            }
            // Import settings
            importRegionSettings(regionSection, region.getConfig(), region);
            region.getConfig().set("general.importedFrom", "RegionForSale");
            // Get existing owners and members
            List<UUID> existing = new ArrayList<>();
            if (owner != null) {
                @SuppressWarnings("deprecation") OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(owner);
                if (offlinePlayer != null) {
                    existing.add(offlinePlayer.getUniqueId());
                }
            }
            for (UUID uuid : plugin.getWorldGuardHandler().getOwners(worldGuardRegion).asUniqueIdList()) {
                if (!existing.contains(uuid)) {
                    existing.add(uuid);
                }
            }
            for (UUID uuid : plugin.getWorldGuardHandler().getMembers(worldGuardRegion).asUniqueIdList()) {
                if (!existing.contains(uuid)) {
                    existing.add(uuid);
                }
            }
            // First owner (or if none, the first member) will be the renter/buyer
            if (!existing.isEmpty()) {
                region.setOwner(existing.remove(0));
            }
            // Add others as friends
            for (UUID friend : existing) {
                region.getFriendsFeature().addFriend(friend, null);
            }
            region.saveRequired();
            messageNoPrefix("import-imported", regionKey);
        }
    }
    // Update all regions
    plugin.getFileManager().updateAllRegions(sender);
    // Write all imported regions and settings to disk
    plugin.getFileManager().saveRequiredFiles();
}
Also used : ArrayList(java.util.ArrayList) RentRegion(me.wiefferink.areashop.regions.RentRegion) YamlConfiguration(org.bukkit.configuration.file.YamlConfiguration) World(org.bukkit.World) RegionGroup(me.wiefferink.areashop.regions.RegionGroup) BuyRegion(me.wiefferink.areashop.regions.BuyRegion) ProtectedRegion(com.sk89q.worldguard.protection.regions.ProtectedRegion) GeneralRegion(me.wiefferink.areashop.regions.GeneralRegion) RegionManager(com.sk89q.worldguard.protection.managers.RegionManager) OfflinePlayer(org.bukkit.OfflinePlayer) UUID(java.util.UUID) File(java.io.File) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Example 15 with ProtectedRegion

use of com.sk89q.worldguard.protection.regions.ProtectedRegion in project AreaShop by NLthijs48.

the class Utils method getImportantWorldEditRegions.

/**
 * Get a list of regions around a location.
 * - Returns highest priority, child instead of parent regions
 * @param location The location to check for regions
 * @return empty list if no regions found, 1 member if 1 region is a priority, more if regions with the same priority
 */
public static List<ProtectedRegion> getImportantWorldEditRegions(Location location) {
    List<ProtectedRegion> result = new ArrayList<>();
    Set<ProtectedRegion> regions = AreaShop.getInstance().getWorldGuardHandler().getApplicableRegionsSet(location);
    if (regions != null) {
        boolean first = true;
        for (ProtectedRegion pr : regions) {
            if (first) {
                result.add(pr);
                first = false;
            } else {
                if (pr.getPriority() > result.get(0).getPriority()) {
                    result.clear();
                    result.add(pr);
                } else if (pr.getParent() != null && pr.getParent().equals(result.get(0))) {
                    result.clear();
                    result.add(pr);
                } else {
                    result.add(pr);
                }
            }
        }
    }
    return result;
}
Also used : ProtectedRegion(com.sk89q.worldguard.protection.regions.ProtectedRegion) ArrayList(java.util.ArrayList)

Aggregations

ProtectedRegion (com.sk89q.worldguard.protection.regions.ProtectedRegion)29 ArrayList (java.util.ArrayList)7 World (org.bukkit.World)7 RegionManager (com.sk89q.worldguard.protection.managers.RegionManager)6 OwnedLand (biz.princeps.landlord.util.OwnedLand)5 BuyRegion (me.wiefferink.areashop.regions.BuyRegion)5 GeneralRegion (me.wiefferink.areashop.regions.GeneralRegion)5 RentRegion (me.wiefferink.areashop.regions.RentRegion)5 Vector (com.sk89q.worldedit.Vector)4 HashSet (java.util.HashSet)4 Location (org.bukkit.Location)4 Player (org.bukkit.entity.Player)4 LandlordCommand (biz.princeps.landlord.commands.LandlordCommand)3 BlockVector (com.sk89q.worldedit.BlockVector)3 Flag (com.sk89q.worldguard.protection.flags.Flag)3 StateFlag (com.sk89q.worldguard.protection.flags.StateFlag)3 List (java.util.List)3 Landlord (biz.princeps.landlord.Landlord)2 ManageGUIAll (biz.princeps.landlord.guis.ManageGUIAll)2 EditSession (com.sk89q.worldedit.EditSession)2