Search in sources :

Example 1 with BuyRegion

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

the class FileManager method loadRegionFilesNow.

private void loadRegionFilesNow() {
    File file = new File(regionsPath);
    File[] regionFiles = file.listFiles();
    if (regionFiles == null) {
        plugin.setReady(true);
        return;
    }
    List<String> noRegionType = new ArrayList<>();
    List<String> noNamePaths = new ArrayList<>();
    List<GeneralRegion> noWorld = new ArrayList<>();
    List<GeneralRegion> noRegion = new ArrayList<>();
    List<GeneralRegion> incorrectDuration = new ArrayList<>();
    for (File regionFile : regionFiles) {
        if (regionFile.exists() && regionFile.isFile()) {
            // Load the region file from disk in UTF8 mode
            YamlConfiguration config;
            try (InputStreamReader reader = new InputStreamReader(new FileInputStream(regionFile), Charsets.UTF_8)) {
                config = YamlConfiguration.loadConfiguration(reader);
                if (config.getKeys(false).size() == 0) {
                    AreaShop.warn("Region file '" + regionFile.getName() + "' is empty, check for errors in the log.");
                }
            } catch (IOException e) {
                AreaShop.warn("Something went wrong reading region file: " + regionFile.getAbsolutePath());
                continue;
            }
            // Construct the correct type of region
            String type = config.getString("general.type");
            GeneralRegion region;
            if (RegionType.RENT.getValue().equals(type)) {
                region = new RentRegion(config);
            } else if (RegionType.BUY.getValue().equals(type)) {
                region = new BuyRegion(config);
            } else {
                noNamePaths.add(regionFile.getPath());
                continue;
            }
            // Check consistency
            boolean added = false;
            if (region.getName() == null) {
                noNamePaths.add(regionFile.getPath());
            } else if (region.getWorld() == null) {
                noWorld.add(region);
            } else if (region.getRegion() == null) {
                noRegion.add(region);
            } else if (region instanceof RentRegion && !Utils.checkTimeFormat(((RentRegion) region).getDurationString())) {
                incorrectDuration.add(region);
            } else {
                added = true;
                if (region instanceof RentRegion) {
                    addRentNoSave((RentRegion) region);
                } else if (region instanceof BuyRegion) {
                    addBuyNoSave((BuyRegion) region);
                }
            }
            if (!added) {
                region.destroy();
            }
        }
    }
    // All files are loaded, print problems to the console
    if (!noRegionType.isEmpty()) {
        AreaShop.warn("The following region files do no have a region type: " + Utils.createCommaSeparatedList(noRegionType));
    }
    if (!noNamePaths.isEmpty()) {
        AreaShop.warn("The following region files do no have a name in their file: " + Utils.createCommaSeparatedList(noNamePaths));
    }
    if (!noRegion.isEmpty()) {
        List<String> noRegionNames = new ArrayList<>();
        for (GeneralRegion region : noRegion) {
            noRegionNames.add(region.getName());
        }
        AreaShop.warn("AreaShop regions that are missing their WorldGuard region: " + Utils.createCommaSeparatedList(noRegionNames));
        AreaShop.warn("Remove these regions from AreaShop with '/as del' or recreate their regions in WorldGuard.");
    }
    boolean noWorldRegions = !noWorld.isEmpty();
    while (!noWorld.isEmpty()) {
        List<GeneralRegion> toDisplay = new ArrayList<>();
        String missingWorld = noWorld.get(0).getWorldName();
        toDisplay.add(noWorld.get(0));
        for (int i = 1; i < noWorld.size(); i++) {
            if (noWorld.get(i).getWorldName().equalsIgnoreCase(missingWorld)) {
                toDisplay.add(noWorld.get(i));
            }
        }
        List<String> noWorldNames = new ArrayList<>();
        for (GeneralRegion region : toDisplay) {
            noWorldNames.add(region.getName());
        }
        AreaShop.warn("World " + missingWorld + " is not loaded, the following AreaShop regions are not functional now: " + Utils.createCommaSeparatedList(noWorldNames));
        noWorld.removeAll(toDisplay);
    }
    if (noWorldRegions) {
        AreaShop.warn("Remove these regions from AreaShop with '/as del' or load the world(s) on the server again.");
    }
    if (!incorrectDuration.isEmpty()) {
        List<String> incorrectDurationNames = new ArrayList<>();
        for (GeneralRegion region : incorrectDuration) {
            incorrectDurationNames.add(region.getName());
        }
        AreaShop.warn("The following regions have an incorrect time format as duration: " + Utils.createCommaSeparatedList(incorrectDurationNames));
    }
    plugin.setReady(true);
}
Also used : InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) RentRegion(me.wiefferink.areashop.regions.RentRegion) IOException(java.io.IOException) YamlConfiguration(org.bukkit.configuration.file.YamlConfiguration) FileInputStream(java.io.FileInputStream) BuyRegion(me.wiefferink.areashop.regions.BuyRegion) GeneralRegion(me.wiefferink.areashop.regions.GeneralRegion) File(java.io.File)

Example 2 with BuyRegion

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

the class Utils method getImportantRegions.

/**
 * Get the most important AreaShop regions.
 * - Returns highest priority, child instead of parent regions.
 * @param location The location to check for regions
 * @param type     The type of regions to look for, null for all
 * @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<GeneralRegion> getImportantRegions(Location location, GeneralRegion.RegionType type) {
    List<GeneralRegion> result = new ArrayList<>();
    Set<ProtectedRegion> regions = AreaShop.getInstance().getWorldGuardHandler().getApplicableRegionsSet(location);
    if (regions != null) {
        List<GeneralRegion> candidates = new ArrayList<>();
        for (ProtectedRegion pr : regions) {
            GeneralRegion region = AreaShop.getInstance().getFileManager().getRegion(pr.getId());
            if (region != null && ((type == GeneralRegion.RegionType.RENT && region instanceof RentRegion) || (type == GeneralRegion.RegionType.BUY && region instanceof BuyRegion) || type == null)) {
                candidates.add(region);
            }
        }
        boolean first = true;
        for (GeneralRegion region : candidates) {
            if (region == null) {
                AreaShop.debug("skipped null region");
                continue;
            }
            if (first) {
                result.add(region);
                first = false;
            } else {
                if (region.getRegion().getPriority() > result.get(0).getRegion().getPriority()) {
                    result.clear();
                    result.add(region);
                } else if (region.getRegion().getParent() != null && region.getRegion().getParent().equals(result.get(0).getRegion())) {
                    result.clear();
                    result.add(region);
                } else {
                    result.add(region);
                }
            }
        }
    }
    return new ArrayList<>(result);
}
Also used : BuyRegion(me.wiefferink.areashop.regions.BuyRegion) GeneralRegion(me.wiefferink.areashop.regions.GeneralRegion) ArrayList(java.util.ArrayList) ProtectedRegion(com.sk89q.worldguard.protection.regions.ProtectedRegion) RentRegion(me.wiefferink.areashop.regions.RentRegion)

Example 3 with BuyRegion

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

the class SetownerCommand method execute.

@Override
public void execute(CommandSender sender, String[] args) {
    if (!sender.hasPermission("areashop.setownerrent") && !sender.hasPermission("areashop.setownerbuy")) {
        plugin.message(sender, "setowner-noPermission");
        return;
    }
    GeneralRegion region;
    if (args.length < 2) {
        plugin.message(sender, "setowner-help");
        return;
    }
    if (args.length == 2) {
        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, "setowner-notRegistered");
        return;
    }
    if (region instanceof RentRegion && !sender.hasPermission("areashop.setownerrent")) {
        plugin.message(sender, "setowner-noPermissionRent", region);
        return;
    }
    if (region instanceof BuyRegion && !sender.hasPermission("areashop.setownerbuy")) {
        plugin.message(sender, "setowner-noPermissionBuy", region);
        return;
    }
    UUID uuid = null;
    @SuppressWarnings("deprecation") OfflinePlayer player = Bukkit.getOfflinePlayer(args[1]);
    if (player != null) {
        uuid = player.getUniqueId();
    }
    if (uuid == null) {
        plugin.message(sender, "setowner-noPlayer", args[1], region);
        return;
    }
    if (region instanceof RentRegion) {
        RentRegion rent = (RentRegion) region;
        if (rent.isRenter(uuid)) {
            // extend
            rent.setRentedUntil(rent.getRentedUntil() + rent.getDuration());
            rent.setRenter(uuid);
            plugin.message(sender, "setowner-succesRentExtend", region);
        } else {
            // change
            if (!rent.isRented()) {
                rent.setRentedUntil(Calendar.getInstance().getTimeInMillis() + rent.getDuration());
            }
            rent.setRenter(uuid);
            plugin.message(sender, "setowner-succesRent", region);
        }
    }
    if (region instanceof BuyRegion) {
        BuyRegion buy = (BuyRegion) region;
        buy.setBuyer(uuid);
        plugin.message(sender, "setowner-succesBuy", region);
    }
    region.getFriendsFeature().deleteFriend(region.getOwner(), null);
    region.update();
    region.saveRequired();
}
Also used : Player(org.bukkit.entity.Player) OfflinePlayer(org.bukkit.OfflinePlayer) BuyRegion(me.wiefferink.areashop.regions.BuyRegion) GeneralRegion(me.wiefferink.areashop.regions.GeneralRegion) OfflinePlayer(org.bukkit.OfflinePlayer) RentRegion(me.wiefferink.areashop.regions.RentRegion) UUID(java.util.UUID)

Example 4 with BuyRegion

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

the class InfoCommand method showSortedPagedList.

/**
 * Display a page of a list of regions.
 * @param sender      The CommandSender to send the messages to
 * @param regions     The regions to display
 * @param filterGroup The group to filter the regions by
 * @param keyHeader   The header to print above the page
 * @param pageInput   The page number, if any
 * @param baseCommand The command to execute for next/previous page (/areashop will be added)
 */
private void showSortedPagedList(@Nonnull CommandSender sender, @Nonnull List<? extends GeneralRegion> regions, @Nullable RegionGroup filterGroup, @Nonnull String keyHeader, @Nullable String pageInput, @Nonnull String baseCommand) {
    int maximumItems = 20;
    int itemsPerPage = maximumItems - 2;
    int page = 1;
    if (pageInput != null && Utils.isNumeric(pageInput)) {
        try {
            page = Integer.parseInt(pageInput);
        } catch (NumberFormatException e) {
            plugin.message(sender, "info-wrongPage", pageInput);
            return;
        }
    }
    if (filterGroup != null) {
        regions.removeIf(generalRegion -> !filterGroup.isMember(generalRegion));
    }
    if (regions.isEmpty()) {
        plugin.message(sender, "info-noRegions");
    } else {
        // First sort by type, then by name
        regions.sort((one, two) -> {
            int typeCompare = getTypeOrder(two).compareTo(getTypeOrder(one));
            if (typeCompare != 0) {
                return typeCompare;
            } else {
                return one.getName().compareTo(two.getName());
            }
        });
        // Header
        Message limitedToGroup = Message.empty();
        if (filterGroup != null) {
            limitedToGroup = Message.fromKey("info-limitedToGroup").replacements(filterGroup.getName());
        }
        plugin.message(sender, keyHeader, limitedToGroup);
        // Page entries
        // Clip page to correct boundaries, not much need to tell the user
        int totalPages = (int) Math.ceil(regions.size() / (double) itemsPerPage);
        if (regions.size() == itemsPerPage + 1) {
            // 19 total items is mapped to 1 page of 19
            itemsPerPage++;
            totalPages = 1;
        }
        page = Math.max(1, Math.min(totalPages, page));
        // header
        int linesPrinted = 1;
        for (int i = (page - 1) * itemsPerPage; i < page * itemsPerPage && i < regions.size(); i++) {
            String state;
            GeneralRegion region = regions.get(i);
            if (region.getType() == GeneralRegion.RegionType.RENT) {
                if (region.getOwner() == null) {
                    state = "Forrent";
                } else {
                    state = "Rented";
                }
            } else {
                if (region.getOwner() == null) {
                    state = "Forsale";
                } else if (!((BuyRegion) region).isInResellingMode()) {
                    state = "Sold";
                } else {
                    state = "Reselling";
                }
            }
            plugin.messageNoPrefix(sender, "info-entry" + state, region);
            linesPrinted++;
        }
        Message footer = Message.empty();
        // Previous button
        if (page > 1) {
            footer.append(Message.fromKey("info-pagePrevious").replacements(baseCommand + " " + (page - 1)));
        } else {
            footer.append(Message.fromKey("info-pageNoPrevious"));
        }
        // Page status
        if (totalPages > 1) {
            String pageString = "" + page;
            for (int i = pageString.length(); i < (totalPages + "").length(); i++) {
                pageString = "0" + pageString;
            }
            footer.append(Message.fromKey("info-pageStatus").replacements(page, totalPages));
            if (page < totalPages) {
                footer.append(Message.fromKey("info-pageNext").replacements(baseCommand + " " + (page + 1)));
            } else {
                footer.append(Message.fromKey("info-pageNoNext"));
            }
            // Fill up space if the page is not full (aligns header nicely)
            for (int i = linesPrinted; i < maximumItems - 1; i++) {
                sender.sendMessage(" ");
            }
            footer.send(sender);
        }
    }
}
Also used : Message(me.wiefferink.interactivemessenger.processing.Message) BuyRegion(me.wiefferink.areashop.regions.BuyRegion) GeneralRegion(me.wiefferink.areashop.regions.GeneralRegion)

Example 5 with BuyRegion

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

the class AddfriendCommand method execute.

@SuppressWarnings("deprecation")
@Override
public void execute(CommandSender sender, String[] args) {
    if (!sender.hasPermission("areashop.addfriend") && !sender.hasPermission("areashop.addfriendall")) {
        plugin.message(sender, "addfriend-noPermission");
        return;
    }
    if (args.length < 2) {
        plugin.message(sender, "addfriend-help");
        return;
    }
    GeneralRegion region;
    if (args.length <= 2) {
        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, "cmd-notRegistered", args[2]);
            return;
        }
    }
    if (sender.hasPermission("areashop.addfriendall")) {
        if ((region instanceof RentRegion && !((RentRegion) region).isRented()) || (region instanceof BuyRegion && !((BuyRegion) region).isSold())) {
            plugin.message(sender, "addfriend-noOwner", region);
            return;
        }
        OfflinePlayer friend = Bukkit.getOfflinePlayer(args[1]);
        if (friend.getLastPlayed() == 0 && !friend.isOnline() && !plugin.getConfig().getBoolean("addFriendNotExistingPlayers")) {
            plugin.message(sender, "addfriend-notVisited", args[1], region);
            return;
        }
        if (region.getFriendsFeature().getFriends().contains(friend.getUniqueId())) {
            plugin.message(sender, "addfriend-alreadyAdded", friend.getName(), region);
            return;
        }
        if (region.isOwner(friend.getUniqueId())) {
            plugin.message(sender, "addfriend-self", friend.getName(), region);
            return;
        }
        if (region.getFriendsFeature().addFriend(friend.getUniqueId(), sender)) {
            region.update();
            plugin.message(sender, "addfriend-successOther", friend.getName(), region);
        }
    } else {
        if (sender.hasPermission("areashop.addfriend") && sender instanceof Player) {
            if (region.isOwner((Player) sender)) {
                OfflinePlayer friend = Bukkit.getOfflinePlayer(args[1]);
                if (friend.getLastPlayed() == 0 && !friend.isOnline() && !plugin.getConfig().getBoolean("addFriendNotExistingPlayers")) {
                    plugin.message(sender, "addfriend-notVisited", args[1], region);
                    return;
                }
                if (region.getFriendsFeature().getFriends().contains(friend.getUniqueId())) {
                    plugin.message(sender, "addfriend-alreadyAdded", friend.getName(), region);
                    return;
                }
                if (region.isOwner(friend.getUniqueId())) {
                    plugin.message(sender, "addfriend-self", friend.getName(), region);
                    return;
                }
                if (region.getFriendsFeature().addFriend(friend.getUniqueId(), sender)) {
                    region.update();
                    plugin.message(sender, "addfriend-success", friend.getName(), region);
                }
            } else {
                plugin.message(sender, "addfriend-noPermissionOther", region);
            }
        } else {
            plugin.message(sender, "addfriend-noPermission", region);
        }
    }
}
Also used : Player(org.bukkit.entity.Player) OfflinePlayer(org.bukkit.OfflinePlayer) BuyRegion(me.wiefferink.areashop.regions.BuyRegion) GeneralRegion(me.wiefferink.areashop.regions.GeneralRegion) OfflinePlayer(org.bukkit.OfflinePlayer) RentRegion(me.wiefferink.areashop.regions.RentRegion)

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