Search in sources :

Example 6 with BuyRegion

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

the class ResellCommand method execute.

@Override
public void execute(CommandSender sender, String[] args) {
    if (!sender.hasPermission("areashop.resell") && !sender.hasPermission("areashop.resellall")) {
        plugin.message(sender, "resell-noPermissionOther");
        return;
    }
    if (args.length <= 1) {
        plugin.message(sender, "resell-help");
        return;
    }
    double price;
    try {
        price = Double.parseDouble(args[1]);
    } catch (NumberFormatException e) {
        plugin.message(sender, "resell-wrongPrice", args[1]);
        return;
    }
    if (price < 0) {
        plugin.message(sender, "resell-wrongPrice", args[1]);
        return;
    }
    BuyRegion buy;
    if (args.length <= 2) {
        if (sender instanceof Player) {
            // get the region by location
            List<BuyRegion> regions = Utils.getImportantBuyRegions(((Player) sender).getLocation());
            if (regions.isEmpty()) {
                plugin.message(sender, "cmd-noRegionsAtLocation");
                return;
            } else if (regions.size() > 1) {
                plugin.message(sender, "cmd-moreRegionsAtLocation");
                return;
            } else {
                buy = regions.get(0);
            }
        } else {
            plugin.message(sender, "cmd-automaticRegionOnlyByPlayer");
            return;
        }
    } else {
        buy = plugin.getFileManager().getBuy(args[2]);
        if (buy == null) {
            plugin.message(sender, "resell-notRegistered", args[2]);
            return;
        }
    }
    if (buy == null) {
        plugin.message(sender, "cmd-noRegionsAtLocation");
        return;
    }
    if (!buy.isSold()) {
        plugin.message(sender, "resell-notBought", buy);
        return;
    }
    if (sender.hasPermission("areashop.resellall")) {
        buy.enableReselling(price);
        buy.update();
        plugin.message(sender, "resell-success", buy);
    } else if (sender.hasPermission("areashop.resell") && sender instanceof Player) {
        if (buy.isOwner((Player) sender)) {
            buy.enableReselling(price);
            buy.update();
            plugin.message(sender, "resell-success", buy);
        } else {
            plugin.message(sender, "resell-noPermissionOther", buy);
        }
    } else {
        plugin.message(sender, "resell-noPermission", buy);
    }
}
Also used : Player(org.bukkit.entity.Player) BuyRegion(me.wiefferink.areashop.regions.BuyRegion)

Example 7 with BuyRegion

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

the class SetpriceCommand method execute.

@Override
public void execute(CommandSender sender, String[] args) {
    if (!sender.hasPermission("areashop.setprice") && (!sender.hasPermission("areashop.setprice.landlord") && sender instanceof Player)) {
        plugin.message(sender, "setprice-noPermission");
        return;
    }
    if (args.length < 2 || args[1] == null) {
        plugin.message(sender, "setprice-help");
        return;
    }
    GeneralRegion region;
    if (args.length < 3) {
        if (sender instanceof Player) {
            // get the region by location
            List<GeneralRegion> regions = Utils.getImportantRegions(((Player) sender).getLocation());
            if (regions.isEmpty()) {
                plugin.message(sender, "cmd-noRegionsAtLocation");
                return;
            } else if (regions.size() > 1) {
                plugin.message(sender, "cmd-moreRegionsAtLocation");
                return;
            } else {
                region = regions.get(0);
            }
        } else {
            plugin.message(sender, "cmd-automaticRegionOnlyByPlayer");
            return;
        }
    } else {
        region = plugin.getFileManager().getRegion(args[2]);
    }
    if (region == null) {
        plugin.message(sender, "setprice-notRegistered", args[2]);
        return;
    }
    if (!sender.hasPermission("areashop.setprice") && !(sender instanceof Player && region.isLandlord(((Player) sender).getUniqueId()))) {
        plugin.message(sender, "setprice-noLandlord", region);
        return;
    }
    if ("default".equalsIgnoreCase(args[1]) || "reset".equalsIgnoreCase(args[1])) {
        if (region instanceof RentRegion) {
            ((RentRegion) region).setPrice(null);
        } else if (region instanceof BuyRegion) {
            ((BuyRegion) region).setPrice(null);
        }
        region.update();
        plugin.message(sender, "setprice-successRemoved", region);
        return;
    }
    double price;
    try {
        price = Double.parseDouble(args[1]);
    } catch (NumberFormatException e) {
        plugin.message(sender, "setprice-wrongPrice", args[1], region);
        return;
    }
    if (region instanceof RentRegion) {
        ((RentRegion) region).setPrice(price);
        plugin.message(sender, "setprice-successRent", region);
    } else if (region instanceof BuyRegion) {
        ((BuyRegion) region).setPrice(price);
        plugin.message(sender, "setprice-successBuy", region);
    }
    region.update();
}
Also used : Player(org.bukkit.entity.Player) BuyRegion(me.wiefferink.areashop.regions.BuyRegion) GeneralRegion(me.wiefferink.areashop.regions.GeneralRegion) RentRegion(me.wiefferink.areashop.regions.RentRegion)

Example 8 with BuyRegion

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

the class StopresellCommand method execute.

@Override
public void execute(CommandSender sender, String[] args) {
    if (!sender.hasPermission("areashop.stopresell") && !sender.hasPermission("areashop.stopresellall")) {
        plugin.message(sender, "stopresell-noPermissionOther");
        return;
    }
    BuyRegion buy;
    if (args.length <= 1) {
        if (sender instanceof Player) {
            // get the region by location
            List<BuyRegion> regions = Utils.getImportantBuyRegions(((Player) sender).getLocation());
            if (regions.isEmpty()) {
                plugin.message(sender, "cmd-noRegionsAtLocation");
                return;
            } else if (regions.size() > 1) {
                plugin.message(sender, "cmd-moreRegionsAtLocation");
                return;
            } else {
                buy = regions.get(0);
            }
        } else {
            plugin.message(sender, "cmd-automaticRegionOnlyByPlayer");
            return;
        }
    } else {
        buy = plugin.getFileManager().getBuy(args[1]);
        if (buy == null) {
            plugin.message(sender, "stopresell-notRegistered", args[1]);
            return;
        }
    }
    if (buy == null) {
        plugin.message(sender, "cmd-noRegionsAtLocation");
        return;
    }
    if (!buy.isInResellingMode()) {
        plugin.message(sender, "stopresell-notResell", buy);
        return;
    }
    if (sender.hasPermission("areashop.stopresellall")) {
        buy.disableReselling();
        buy.update();
        plugin.message(sender, "stopresell-success", buy);
    } else if (sender.hasPermission("areashop.stopresell") && sender instanceof Player) {
        if (buy.isOwner((Player) sender)) {
            buy.disableReselling();
            buy.update();
            plugin.message(sender, "stopresell-success", buy);
        } else {
            plugin.message(sender, "stopresell-noPermissionOther", buy);
        }
    } else {
        plugin.message(sender, "stopresell-noPermission", buy);
    }
}
Also used : Player(org.bukkit.entity.Player) BuyRegion(me.wiefferink.areashop.regions.BuyRegion)

Example 9 with BuyRegion

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

the class DelCommand method execute.

@Override
public void execute(CommandSender sender, String[] args) {
    if (!sender.hasPermission("areashop.destroybuy") && !sender.hasPermission("areashop.destroybuy.landlord") && !sender.hasPermission("areashop.destroyrent") && !sender.hasPermission("areashop.destroyrent.landlord")) {
        plugin.message(sender, "del-noPermission");
        return;
    }
    if (args.length < 2) {
        // Only players can have a selection
        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 == null || regions.size() == 0) {
            plugin.message(player, "cmd-noRegionsFound");
            return;
        }
        // Start removing the regions that he has permission for
        ArrayList<String> namesSuccess = new ArrayList<>();
        TreeSet<GeneralRegion> namesFailed = new TreeSet<>();
        for (GeneralRegion region : regions) {
            boolean isLandlord = region.isLandlord(((Player) sender).getUniqueId());
            if (region instanceof RentRegion) {
                if (!sender.hasPermission("areashop.destroyrent") && !(isLandlord && sender.hasPermission("areashop.destroyrent.landlord"))) {
                    namesFailed.add(region);
                } else {
                    plugin.getFileManager().removeRent((RentRegion) region, true);
                    namesSuccess.add(region.getName());
                }
            } else if (region instanceof BuyRegion) {
                if (!sender.hasPermission("areashop.destroybuy") && !(isLandlord && sender.hasPermission("areashop.destroybuy.landlord"))) {
                    namesFailed.add(region);
                } else {
                    plugin.getFileManager().removeBuy((BuyRegion) region, true);
                    namesSuccess.add(region.getName());
                }
            }
        }
        // send messages
        if (namesSuccess.size() != 0) {
            plugin.message(sender, "del-success", Utils.createCommaSeparatedList(namesSuccess));
        }
        if (namesFailed.size() != 0) {
            plugin.message(sender, "del-failed", Utils.combinedMessage(namesFailed, "region"));
        }
    } else {
        GeneralRegion region = plugin.getFileManager().getRegion(args[1]);
        if (region == null) {
            plugin.message(sender, "cmd-notRegistered", args[1]);
            return;
        }
        boolean isLandlord = sender instanceof Player && region.isLandlord(((Player) sender).getUniqueId());
        if (region instanceof RentRegion) {
            // Remove the rent if the player has permission
            if (sender.hasPermission("areashop.destroyrent") || (isLandlord && sender.hasPermission("areashop.destroyrent.landlord"))) {
                plugin.getFileManager().removeRent((RentRegion) region, true);
                plugin.message(sender, "destroy-successRent", region);
            } else {
                plugin.message(sender, "destroy-noPermissionRent", region);
            }
        } else if (region instanceof BuyRegion) {
            // Remove the buy if the player has permission
            if (sender.hasPermission("areashop.destroybuy") || (isLandlord && sender.hasPermission("areashop.destroybuy.landlord"))) {
                plugin.getFileManager().removeBuy((BuyRegion) region, true);
                plugin.message(sender, "destroy-successBuy", region);
            } else {
                plugin.message(sender, "destroy-noPermissionBuy", region);
            }
        }
    }
}
Also used : Player(org.bukkit.entity.Player) Selection(com.sk89q.worldedit.bukkit.selections.Selection) TreeSet(java.util.TreeSet) BuyRegion(me.wiefferink.areashop.regions.BuyRegion) GeneralRegion(me.wiefferink.areashop.regions.GeneralRegion) ArrayList(java.util.ArrayList) RentRegion(me.wiefferink.areashop.regions.RentRegion)

Example 10 with BuyRegion

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

BuyRegion (me.wiefferink.areashop.regions.BuyRegion)20 RentRegion (me.wiefferink.areashop.regions.RentRegion)16 Player (org.bukkit.entity.Player)16 GeneralRegion (me.wiefferink.areashop.regions.GeneralRegion)15 ArrayList (java.util.ArrayList)8 ProtectedRegion (com.sk89q.worldguard.protection.regions.ProtectedRegion)5 OfflinePlayer (org.bukkit.OfflinePlayer)5 RegionGroup (me.wiefferink.areashop.regions.RegionGroup)4 Message (me.wiefferink.interactivemessenger.processing.Message)4 Selection (com.sk89q.worldedit.bukkit.selections.Selection)3 RegionManager (com.sk89q.worldguard.protection.managers.RegionManager)3 UUID (java.util.UUID)3 File (java.io.File)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 TreeSet (java.util.TreeSet)2 FileManager (me.wiefferink.areashop.managers.FileManager)2 World (org.bukkit.World)2 YamlConfiguration (org.bukkit.configuration.file.YamlConfiguration)2 EventHandler (org.bukkit.event.EventHandler)2