Search in sources :

Example 1 with GeneralRegion

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

the class FileManager method checkRegionAdd.

/**
 * Check if a player can add a certain region as rent or buy region.
 * @param sender The player/console that wants to add a region
 * @param region The WorldGuard region to add
 * @param type   The type the region should have in AreaShop
 * @return The result if a player would want to add this region
 */
public AddResult checkRegionAdd(CommandSender sender, ProtectedRegion region, RegionType type) {
    Player player = null;
    if (sender instanceof Player) {
        player = (Player) sender;
    }
    // Determine if the player is an owner or member of the region
    boolean isMember = player != null && plugin.getWorldGuardHandler().containsMember(region, player.getUniqueId());
    boolean isOwner = player != null && plugin.getWorldGuardHandler().containsOwner(region, player.getUniqueId());
    AreaShop.debug("checkRegionAdd: isOwner=" + isOwner + ", isMember=" + isMember);
    String typeString;
    if (type == RegionType.RENT) {
        typeString = "rent";
    } else {
        typeString = "buy";
    }
    AreaShop.debug("  permissions: .create=" + sender.hasPermission("areashop.create" + typeString) + ", .create.owner=" + sender.hasPermission("areashop.create" + typeString + ".owner") + ", .create.member=" + sender.hasPermission("areashop.create" + typeString + ".member"));
    if (!(sender.hasPermission("areashop.create" + typeString) || (sender.hasPermission("areashop.create" + typeString + ".owner") && isOwner) || (sender.hasPermission("areashop.create" + typeString + ".member") && isMember))) {
        return AddResult.NOPERMISSION;
    }
    GeneralRegion asRegion = plugin.getFileManager().getRegion(region.getId());
    if (asRegion != null) {
        return AddResult.ALREADYADDED;
    } else if (plugin.getFileManager().isBlacklisted(region.getId())) {
        return AddResult.BLACKLISTED;
    } else {
        return AddResult.SUCCESS;
    }
}
Also used : Player(org.bukkit.entity.Player) OfflinePlayer(org.bukkit.OfflinePlayer) GeneralRegion(me.wiefferink.areashop.regions.GeneralRegion)

Example 2 with GeneralRegion

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

the class FileManager method postUpdateFiles.

/**
 * Checks for old file formats and converts them to the latest format.
 * This is to be triggered after the load of the region files.
 */
private void postUpdateFiles() {
    Integer fileStatus = versions.get(AreaShop.versionFiles);
    // If the the files are already the current version
    if (fileStatus != null && fileStatus == AreaShop.versionFilesCurrent) {
        return;
    }
    // Add 'general.lastActive' to rented/bought regions (initialize at current time)
    if (fileStatus == null || fileStatus < 3) {
        for (GeneralRegion region : getRegions()) {
            region.updateLastActiveTime();
        }
        // Update versions file to 3
        versions.put(AreaShop.versionFiles, 3);
        saveVersions();
        if (getRegions().size() > 0) {
            AreaShop.info("  Added last active time to regions (v2 to v3)");
        }
    }
}
Also used : GeneralRegion(me.wiefferink.areashop.regions.GeneralRegion)

Example 3 with GeneralRegion

use of me.wiefferink.areashop.regions.GeneralRegion 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 4 with GeneralRegion

use of me.wiefferink.areashop.regions.GeneralRegion 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 5 with GeneralRegion

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

Aggregations

GeneralRegion (me.wiefferink.areashop.regions.GeneralRegion)25 Player (org.bukkit.entity.Player)18 BuyRegion (me.wiefferink.areashop.regions.BuyRegion)15 RentRegion (me.wiefferink.areashop.regions.RentRegion)14 ArrayList (java.util.ArrayList)8 OfflinePlayer (org.bukkit.OfflinePlayer)7 ProtectedRegion (com.sk89q.worldguard.protection.regions.ProtectedRegion)5 Selection (com.sk89q.worldedit.bukkit.selections.Selection)4 TreeSet (java.util.TreeSet)4 RegionGroup (me.wiefferink.areashop.regions.RegionGroup)4 HashSet (java.util.HashSet)3 Set (java.util.Set)3 UUID (java.util.UUID)3 EventHandler (org.bukkit.event.EventHandler)3 CuboidSelection (com.sk89q.worldedit.bukkit.selections.CuboidSelection)2 RegionManager (com.sk89q.worldguard.protection.managers.RegionManager)2 File (java.io.File)2 RegionSign (me.wiefferink.areashop.features.signs.RegionSign)2 FileManager (me.wiefferink.areashop.managers.FileManager)2 Message (me.wiefferink.interactivemessenger.processing.Message)2