Search in sources :

Example 1 with RegionGroup

use of me.wiefferink.areashop.regions.RegionGroup in project AreaShop by NLthijs48.

the class FileManager method loadGroupsFile.

/**
 * Load the groups.yml file from disk
 * @return true if succeeded, otherwise false
 */
public boolean loadGroupsFile() {
    boolean result = true;
    File groupFile = new File(groupsPath);
    if (groupFile.exists() && groupFile.isFile()) {
        try (InputStreamReader reader = new InputStreamReader(new FileInputStream(groupFile), Charsets.UTF_8)) {
            groupsConfig = YamlConfiguration.loadConfiguration(reader);
        } catch (IOException e) {
            AreaShop.warn("Could not load groups.yml file: " + groupFile.getAbsolutePath());
        }
    }
    if (groupsConfig == null) {
        groupsConfig = new YamlConfiguration();
    }
    for (String groupName : groupsConfig.getKeys(false)) {
        RegionGroup group = new RegionGroup(plugin, groupName);
        groups.put(groupName, group);
    }
    return result;
}
Also used : InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) YamlConfiguration(org.bukkit.configuration.file.YamlConfiguration) File(java.io.File) FileInputStream(java.io.FileInputStream) RegionGroup(me.wiefferink.areashop.regions.RegionGroup)

Example 2 with RegionGroup

use of me.wiefferink.areashop.regions.RegionGroup in project AreaShop by NLthijs48.

the class FileManager method removeBuy.

/**
 * Remove a buy from the list.
 * @param buy           The BuyRegion to remove
 * @param giveMoneyBack true if money should be given back to the player, otherwise false
 * @return true if the buy has been removed, false otherwise
 */
public boolean removeBuy(BuyRegion buy, boolean giveMoneyBack) {
    boolean result = false;
    if (buy != null) {
        buy.setDeleted();
        if (buy.isSold()) {
            buy.sell(giveMoneyBack, null);
        }
        // Handle schematics and run commands
        buy.handleSchematicEvent(RegionEvent.DELETED);
        buy.runEventCommands(RegionEvent.DELETED, true);
        // Delete the sign and the variable
        if (buy.getWorld() != null) {
            for (Location sign : buy.getSignsFeature().getSignLocations()) {
                sign.getBlock().setType(Material.AIR);
            }
        }
        regions.remove(buy.getLowerCaseName());
        buy.resetRegionFlags();
        // Removing from groups
        for (RegionGroup group : getGroups()) {
            group.removeMember(buy);
        }
        // Deleting the file
        File file = new File(plugin.getDataFolder() + File.separator + AreaShop.regionsFolder + File.separator + buy.getLowerCaseName() + ".yml");
        boolean deleted;
        if (file.exists()) {
            try {
                deleted = file.delete();
            } catch (Exception e) {
                deleted = false;
            }
            if (!deleted) {
                AreaShop.warn("File could not be deleted: " + file.toString());
            }
        }
        result = true;
        // Broadcast event
        Bukkit.getPluginManager().callEvent(new DeletedRegionEvent(buy));
        // Run commands
        buy.runEventCommands(RegionEvent.DELETED, false);
    }
    return result;
}
Also used : DeletedRegionEvent(me.wiefferink.areashop.events.notify.DeletedRegionEvent) File(java.io.File) RegionGroup(me.wiefferink.areashop.regions.RegionGroup) IOException(java.io.IOException) Location(org.bukkit.Location)

Example 3 with RegionGroup

use of me.wiefferink.areashop.regions.RegionGroup in project AreaShop by NLthijs48.

the class GroupaddCommand method execute.

@Override
public void execute(CommandSender sender, String[] args) {
    if (!sender.hasPermission("areashop.groupadd")) {
        plugin.message(sender, "groupadd-noPermission");
        return;
    }
    if (args.length < 2 || args[1] == null) {
        plugin.message(sender, "groupadd-help");
        return;
    }
    RegionGroup group = plugin.getFileManager().getGroup(args[1]);
    if (group == null) {
        group = new RegionGroup(plugin, args[1]);
        plugin.getFileManager().addGroup(group);
    }
    if (args.length == 2) {
        if (!(sender instanceof Player)) {
            plugin.message(sender, "cmd-weOnlyByPlayer");
            return;
        }
        Player player = (Player) sender;
        Selection selection = plugin.getWorldEdit().getSelection(player);
        if (selection == null) {
            plugin.message(player, "cmd-noSelection");
            return;
        }
        List<GeneralRegion> regions = Utils.getRegionsInSelection(selection);
        if (regions.size() == 0) {
            plugin.message(player, "cmd-noRegionsFound");
            return;
        }
        TreeSet<GeneralRegion> regionsSuccess = new TreeSet<>();
        TreeSet<GeneralRegion> regionsFailed = new TreeSet<>();
        for (GeneralRegion region : regions) {
            if (group.addMember(region)) {
                regionsSuccess.add(region);
            } else {
                regionsFailed.add(region);
            }
        }
        if (regionsSuccess.size() != 0) {
            plugin.message(player, "groupadd-weSuccess", group.getName(), Utils.combinedMessage(regionsSuccess, "region"));
        }
        if (regionsFailed.size() != 0) {
            plugin.message(player, "groupadd-weFailed", group.getName(), Utils.combinedMessage(regionsFailed, "region"));
        }
        // Update all regions, this does it in a task, updating them without lag
        plugin.getFileManager().updateRegions(new ArrayList<>(regionsSuccess), player);
    } else {
        GeneralRegion region = plugin.getFileManager().getRegion(args[2]);
        if (region == null) {
            plugin.message(sender, "cmd-notRegistered", args[2]);
            return;
        }
        if (group.addMember(region)) {
            region.update();
            plugin.message(sender, "groupadd-success", group.getName(), group.getMembers().size(), region);
        } else {
            plugin.message(sender, "groupadd-failed", group.getName(), region);
        }
    }
}
Also used : Player(org.bukkit.entity.Player) Selection(com.sk89q.worldedit.bukkit.selections.Selection) TreeSet(java.util.TreeSet) GeneralRegion(me.wiefferink.areashop.regions.GeneralRegion) RegionGroup(me.wiefferink.areashop.regions.RegionGroup)

Example 4 with RegionGroup

use of me.wiefferink.areashop.regions.RegionGroup in project AreaShop by NLthijs48.

the class GroupinfoCommand method execute.

@Override
public void execute(CommandSender sender, String[] args) {
    if (!sender.hasPermission("areashop.groupinfo")) {
        plugin.message(sender, "groupinfo-noPermission");
        return;
    }
    if (args.length < 2 || args[1] == null) {
        plugin.message(sender, "groupinfo-help");
        return;
    }
    RegionGroup group = plugin.getFileManager().getGroup(args[1]);
    if (group == null) {
        plugin.message(sender, "groupinfo-noGroup", args[1]);
        return;
    }
    Set<String> members = group.getMembers();
    if (members.size() == 0) {
        plugin.message(sender, "groupinfo-noMembers", group.getName());
    } else {
        plugin.message(sender, "groupinfo-members", group.getName(), Utils.createCommaSeparatedList(members));
    }
}
Also used : RegionGroup(me.wiefferink.areashop.regions.RegionGroup)

Example 5 with RegionGroup

use of me.wiefferink.areashop.regions.RegionGroup 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)

Aggregations

RegionGroup (me.wiefferink.areashop.regions.RegionGroup)10 Player (org.bukkit.entity.Player)5 File (java.io.File)4 BuyRegion (me.wiefferink.areashop.regions.BuyRegion)4 GeneralRegion (me.wiefferink.areashop.regions.GeneralRegion)4 RentRegion (me.wiefferink.areashop.regions.RentRegion)4 Selection (com.sk89q.worldedit.bukkit.selections.Selection)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 Message (me.wiefferink.interactivemessenger.processing.Message)3 Location (org.bukkit.Location)3 RegionManager (com.sk89q.worldguard.protection.managers.RegionManager)2 TreeSet (java.util.TreeSet)2 DeletedRegionEvent (me.wiefferink.areashop.events.notify.DeletedRegionEvent)2 YamlConfiguration (org.bukkit.configuration.file.YamlConfiguration)2 BlockVector (com.sk89q.worldedit.BlockVector)1 Vector (com.sk89q.worldedit.Vector)1 ProtectedCuboidRegion (com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion)1 ProtectedRegion (com.sk89q.worldguard.protection.regions.ProtectedRegion)1 FileInputStream (java.io.FileInputStream)1