Search in sources :

Example 6 with RegionGroup

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

the class FileManager method removeRent.

/**
 * Remove a rent from the list.
 * @param rent          The region to remove
 * @param giveMoneyBack use true to give money back to the player if someone is currently renting this region, otherwise false
 * @return true if the rent has been removed, false otherwise
 */
public boolean removeRent(RentRegion rent, boolean giveMoneyBack) {
    boolean result = false;
    if (rent != null) {
        rent.setDeleted();
        if (rent.isRented()) {
            rent.unRent(giveMoneyBack, null);
        }
        // Handle schematics and run commands
        rent.handleSchematicEvent(RegionEvent.DELETED);
        rent.runEventCommands(RegionEvent.DELETED, true);
        // Delete the signs and the variable
        if (rent.getWorld() != null) {
            for (Location sign : rent.getSignsFeature().getSignLocations()) {
                sign.getBlock().setType(Material.AIR);
                AreaShop.debug("Removed sign at: " + sign.toString());
            }
        }
        RegionGroup[] groups = getGroups().toArray(new RegionGroup[getGroups().size()]);
        for (RegionGroup group : groups) {
            group.removeMember(rent);
        }
        rent.resetRegionFlags();
        regions.remove(rent.getLowerCaseName());
        File file = new File(plugin.getDataFolder() + File.separator + AreaShop.regionsFolder + File.separator + rent.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(rent));
        // Run commands
        rent.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 7 with RegionGroup

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

the class FindCommand method execute.

@Override
public void execute(CommandSender sender, String[] args) {
    if (!sender.hasPermission("areashop.find")) {
        plugin.message(sender, "find-noPermission");
        return;
    }
    if (!(sender instanceof Player)) {
        plugin.message(sender, "cmd-onlyByPlayer");
        return;
    }
    if (args.length <= 1 || args[1] == null || (!args[1].equalsIgnoreCase("buy") && !args[1].equalsIgnoreCase("rent"))) {
        plugin.message(sender, "find-help");
        return;
    }
    Player player = (Player) sender;
    double balance = 0.0;
    if (plugin.getEconomy() != null) {
        balance = plugin.getEconomy().getBalance(player);
    }
    double maxPrice = 0;
    boolean maxPriceSet = false;
    RegionGroup group = null;
    // Parse optional price argument
    if (args.length >= 3) {
        try {
            maxPrice = Double.parseDouble(args[2]);
            maxPriceSet = true;
        } catch (NumberFormatException e) {
            plugin.message(sender, "find-wrongMaxPrice", args[2]);
            return;
        }
    }
    // Parse optional group argument
    if (args.length >= 4) {
        group = plugin.getFileManager().getGroup(args[3]);
        if (group == null) {
            plugin.message(sender, "find-wrongGroup", args[3]);
            return;
        }
    }
    // Find buy regions
    if (args[1].equalsIgnoreCase("buy")) {
        List<BuyRegion> regions = plugin.getFileManager().getBuys();
        List<BuyRegion> results = new ArrayList<>();
        for (BuyRegion region : regions) {
            if (!region.isSold() && ((region.getPrice() <= balance && !maxPriceSet) || (region.getPrice() <= maxPrice && maxPriceSet)) && (group == null || group.isMember(region)) && (region.getBooleanSetting("general.findCrossWorld") || player.getWorld().equals(region.getWorld()))) {
                results.add(region);
            }
        }
        if (!results.isEmpty()) {
            // Draw a random one
            BuyRegion region = results.get(new Random().nextInt(results.size()));
            Message onlyInGroup = Message.empty();
            if (group != null) {
                onlyInGroup = Message.fromKey("find-onlyInGroup").replacements(args[3]);
            }
            // Teleport
            if (maxPriceSet) {
                plugin.message(player, "find-successMax", "buy", Utils.formatCurrency(maxPrice), onlyInGroup, region);
            } else {
                plugin.message(player, "find-success", "buy", Utils.formatCurrency(balance), onlyInGroup, region);
            }
            region.getTeleportFeature().teleportPlayer(player, region.getBooleanSetting("general.findTeleportToSign"), false);
        } else {
            Message onlyInGroup = Message.empty();
            if (group != null) {
                onlyInGroup = Message.fromKey("find-onlyInGroup").replacements(args[3]);
            }
            if (maxPriceSet) {
                plugin.message(player, "find-noneFoundMax", "buy", Utils.formatCurrency(maxPrice), onlyInGroup);
            } else {
                plugin.message(player, "find-noneFound", "buy", Utils.formatCurrency(balance), onlyInGroup);
            }
        }
    } else // Find rental regions
    {
        List<RentRegion> regions = plugin.getFileManager().getRents();
        List<RentRegion> results = new ArrayList<>();
        for (RentRegion region : regions) {
            if (!region.isRented() && ((region.getPrice() <= balance && !maxPriceSet) || (region.getPrice() <= maxPrice && maxPriceSet)) && (group == null || group.isMember(region)) && (region.getBooleanSetting("general.findCrossWorld") || player.getWorld().equals(region.getWorld()))) {
                results.add(region);
            }
        }
        if (!results.isEmpty()) {
            // Draw a random one
            RentRegion region = results.get(new Random().nextInt(results.size()));
            Message onlyInGroup = Message.empty();
            if (group != null) {
                onlyInGroup = Message.fromKey("find-onlyInGroup").replacements(args[3]);
            }
            // Teleport
            if (maxPriceSet) {
                plugin.message(player, "find-successMax", "rent", Utils.formatCurrency(maxPrice), onlyInGroup, region);
            } else {
                plugin.message(player, "find-success", "rent", Utils.formatCurrency(balance), onlyInGroup, region);
            }
            region.getTeleportFeature().teleportPlayer(player, region.getBooleanSetting("general.findTeleportToSign"), false);
        } else {
            Message onlyInGroup = Message.empty();
            if (group != null) {
                onlyInGroup = Message.fromKey("find-onlyInGroup").replacements(args[3]);
            }
            if (maxPriceSet) {
                plugin.message(player, "find-noneFoundMax", "rent", Utils.formatCurrency(maxPrice), onlyInGroup);
            } else {
                plugin.message(player, "find-noneFound", "rent", Utils.formatCurrency(balance), onlyInGroup);
            }
        }
    }
}
Also used : Player(org.bukkit.entity.Player) Random(java.util.Random) Message(me.wiefferink.interactivemessenger.processing.Message) BuyRegion(me.wiefferink.areashop.regions.BuyRegion) ArrayList(java.util.ArrayList) RentRegion(me.wiefferink.areashop.regions.RentRegion) RegionGroup(me.wiefferink.areashop.regions.RegionGroup)

Example 8 with RegionGroup

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

the class GroupdelCommand method execute.

@Override
public void execute(CommandSender sender, String[] args) {
    if (!sender.hasPermission("areashop.groupdel")) {
        plugin.message(sender, "groupdel-noPermission");
        return;
    }
    if (args.length < 2 || args[1] == null) {
        plugin.message(sender, "groupdel-help");
        return;
    }
    RegionGroup group = plugin.getFileManager().getGroup(args[1]);
    if (group == null) {
        plugin.message(sender, "groupdel-wrongGroup", args[1]);
        return;
    }
    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.removeMember(region)) {
                regionsSuccess.add(region);
            } else {
                regionsFailed.add(region);
            }
        }
        if (regionsSuccess.size() != 0) {
            plugin.message(player, "groupdel-weSuccess", group.getName(), Utils.combinedMessage(regionsSuccess, "region"));
        }
        if (regionsFailed.size() != 0) {
            plugin.message(player, "groupdel-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);
        group.saveRequired();
    } else {
        GeneralRegion region = plugin.getFileManager().getRegion(args[2]);
        if (region == null) {
            plugin.message(sender, "cmd-notRegistered", args[2]);
            return;
        }
        if (group.removeMember(region)) {
            region.update();
            plugin.message(sender, "groupdel-success", group.getName(), group.getMembers().size(), region);
        } else {
            plugin.message(sender, "groupdel-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 9 with RegionGroup

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

the class InfoCommand method execute.

@Override
public void execute(CommandSender sender, String[] args) {
    if (!sender.hasPermission("areashop.info")) {
        plugin.message(sender, "info-noPermission");
        return;
    }
    if (args.length > 1 && args[1] != null) {
        // Get filter group (only used by some commands)
        RegionGroup filterGroup = null;
        Set<String> groupFilters = new HashSet<>(Arrays.asList("all", "rented", "forrent", "sold", "forsale", "reselling"));
        if (groupFilters.contains(args[1].toLowerCase()) && args.length > 2) {
            if (!Utils.isNumeric(args[2])) {
                filterGroup = plugin.getFileManager().getGroup(args[2]);
                if (filterGroup == null) {
                    plugin.message(sender, "info-noFiltergroup", args[2]);
                    return;
                }
                // Pass page number to args[2] if available
                if (args.length > 3) {
                    args[2] = args[3];
                }
            }
        }
        // All regions
        if (args[1].equalsIgnoreCase("all")) {
            showSortedPagedList(sender, plugin.getFileManager().getRegions(), filterGroup, "info-allHeader", (args.length > 2 ? args[2] : null), "info all");
        } else // Rented regions
        if (args[1].equalsIgnoreCase("rented")) {
            List<RentRegion> regions = plugin.getFileManager().getRents();
            regions.removeIf(RentRegion::isAvailable);
            showSortedPagedList(sender, regions, filterGroup, "info-rentedHeader", (args.length > 2 ? args[2] : null), "info rented");
        } else // Forrent regions
        if (args[1].equalsIgnoreCase("forrent")) {
            List<RentRegion> regions = plugin.getFileManager().getRents();
            regions.removeIf(RentRegion::isRented);
            showSortedPagedList(sender, regions, filterGroup, "info-forrentHeader", (args.length > 2 ? args[2] : null), "info forrent");
        } else // Sold regions
        if (args[1].equalsIgnoreCase("sold")) {
            List<BuyRegion> regions = plugin.getFileManager().getBuys();
            regions.removeIf(BuyRegion::isAvailable);
            showSortedPagedList(sender, regions, filterGroup, "info-soldHeader", (args.length > 2 ? args[2] : null), "info sold");
        } else // Forsale regions
        if (args[1].equalsIgnoreCase("forsale")) {
            List<BuyRegion> regions = plugin.getFileManager().getBuys();
            regions.removeIf(BuyRegion::isSold);
            showSortedPagedList(sender, regions, filterGroup, "info-forsaleHeader", (args.length > 2 ? args[2] : null), "info forsale");
        } else // Reselling regions
        if (args[1].equalsIgnoreCase("reselling")) {
            List<BuyRegion> regions = plugin.getFileManager().getBuys();
            regions.removeIf(region -> !region.isInResellingMode());
            showSortedPagedList(sender, regions, filterGroup, "info-resellingHeader", (args.length > 2 ? args[2] : null), "info reselling");
        } else // List of regions without a group
        if (args[1].equalsIgnoreCase("nogroup")) {
            List<GeneralRegion> regions = plugin.getFileManager().getRegions();
            for (RegionGroup group : plugin.getFileManager().getGroups()) {
                regions.removeAll(group.getMemberRegions());
            }
            if (regions.isEmpty()) {
                plugin.message(sender, "info-nogroupNone");
            } else {
                showSortedPagedList(sender, regions, filterGroup, "info-nogroupHeader", (args.length > 2 ? args[2] : null), "info nogroup");
            }
        } else // Region info
        if (args[1].equalsIgnoreCase("region")) {
            if (args.length > 1) {
                RentRegion rent = null;
                BuyRegion buy = null;
                if (args.length > 2) {
                    rent = plugin.getFileManager().getRent(args[2]);
                    buy = plugin.getFileManager().getBuy(args[2]);
                } else {
                    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 {
                            if (regions.get(0) instanceof RentRegion) {
                                rent = (RentRegion) regions.get(0);
                            } else if (regions.get(0) instanceof BuyRegion) {
                                buy = (BuyRegion) regions.get(0);
                            }
                        }
                    } else {
                        plugin.message(sender, "cmd-automaticRegionOnlyByPlayer");
                        return;
                    }
                }
                if (rent == null && buy == null) {
                    plugin.message(sender, "info-regionNotExisting", args[2]);
                    return;
                }
                if (rent != null) {
                    plugin.message(sender, "info-regionHeaderRent", rent);
                    if (rent.isRented()) {
                        plugin.messageNoPrefix(sender, "info-regionRented", rent);
                        plugin.messageNoPrefix(sender, "info-regionExtending", rent);
                        // Money back
                        if (UnrentCommand.canUse(sender, rent)) {
                            plugin.messageNoPrefix(sender, "info-regionMoneyBackRentClick", rent);
                        } else {
                            plugin.messageNoPrefix(sender, "info-regionMoneyBackRent", rent);
                        }
                        // Friends
                        if (!rent.getFriendsFeature().getFriendNames().isEmpty()) {
                            String messagePart = "info-friend";
                            if (DelfriendCommand.canUse(sender, rent)) {
                                messagePart = "info-friendRemove";
                            }
                            plugin.messageNoPrefix(sender, "info-regionFriends", rent, Utils.combinedMessage(rent.getFriendsFeature().getFriendNames(), messagePart));
                        }
                    } else {
                        plugin.messageNoPrefix(sender, "info-regionCanBeRented", rent);
                    }
                    if (rent.getLandlordName() != null) {
                        plugin.messageNoPrefix(sender, "info-regionLandlord", rent);
                    }
                    // Maximum extends
                    if (rent.getMaxExtends() != -1) {
                        if (rent.getMaxExtends() == 0) {
                            plugin.messageNoPrefix(sender, "info-regionNoExtending", rent);
                        } else if (rent.isRented()) {
                            plugin.messageNoPrefix(sender, "info-regionExtendsLeft", rent);
                        } else {
                            plugin.messageNoPrefix(sender, "info-regionMaxExtends", rent);
                        }
                    }
                    // If maxExtends is zero it does not make sense to show this message
                    if (rent.getMaxRentTime() != -1 && rent.getMaxExtends() != 0) {
                        plugin.messageNoPrefix(sender, "info-regionMaxRentTime", rent);
                    }
                    if (rent.getInactiveTimeUntilUnrent() != -1) {
                        plugin.messageNoPrefix(sender, "info-regionInactiveUnrent", rent);
                    }
                    // Teleport
                    Message tp = Message.fromKey("info-prefix");
                    boolean foundSomething = false;
                    if (TeleportCommand.canUse(sender, rent)) {
                        foundSomething = true;
                        tp.append(Message.fromKey("info-regionTeleport").replacements(rent));
                    }
                    if (SetteleportCommand.canUse(sender, rent)) {
                        if (foundSomething) {
                            tp.append(", ");
                        }
                        foundSomething = true;
                        tp.append(Message.fromKey("info-setRegionTeleport").replacements(rent));
                    }
                    if (foundSomething) {
                        tp.append(".");
                        tp.send(sender);
                    }
                    // Signs
                    List<String> signLocations = new ArrayList<>();
                    for (Location location : rent.getSignsFeature().getSignLocations()) {
                        signLocations.add(Message.fromKey("info-regionSignLocation").replacements(location.getWorld().getName(), location.getBlockX(), location.getBlockY(), location.getBlockZ()).getPlain());
                    }
                    if (!signLocations.isEmpty()) {
                        plugin.messageNoPrefix(sender, "info-regionSigns", Utils.createCommaSeparatedList(signLocations));
                    }
                    // Groups
                    if (sender.hasPermission("areashop.groupinfo") && !rent.getGroupNames().isEmpty()) {
                        plugin.messageNoPrefix(sender, "info-regionGroups", Utils.createCommaSeparatedList(rent.getGroupNames()));
                    }
                    // Restoring
                    if (rent.isRestoreEnabled()) {
                        plugin.messageNoPrefix(sender, "info-regionRestoringRent", rent);
                    }
                    // Restrictions
                    if (!rent.isRented()) {
                        if (rent.restrictedToRegion()) {
                            plugin.messageNoPrefix(sender, "info-regionRestrictedRegionRent", rent);
                        } else if (rent.restrictedToWorld()) {
                            plugin.messageNoPrefix(sender, "info-regionRestrictedWorldRent", rent);
                        }
                    }
                    plugin.messageNoPrefix(sender, "info-regionFooterRent", rent);
                } else if (buy != null) {
                    plugin.message(sender, "info-regionHeaderBuy", buy);
                    if (buy.isSold()) {
                        if (buy.isInResellingMode()) {
                            plugin.messageNoPrefix(sender, "info-regionReselling", buy);
                            plugin.messageNoPrefix(sender, "info-regionReselPrice", buy);
                        } else {
                            plugin.messageNoPrefix(sender, "info-regionBought", buy);
                        }
                        // Money back
                        if (SellCommand.canUse(sender, buy)) {
                            plugin.messageNoPrefix(sender, "info-regionMoneyBackBuyClick", buy);
                        } else {
                            plugin.messageNoPrefix(sender, "info-regionMoneyBackBuy", buy);
                        }
                        // Friends
                        if (!buy.getFriendsFeature().getFriendNames().isEmpty()) {
                            String messagePart = "info-friend";
                            if (DelfriendCommand.canUse(sender, buy)) {
                                messagePart = "info-friendRemove";
                            }
                            plugin.messageNoPrefix(sender, "info-regionFriends", buy, Utils.combinedMessage(buy.getFriendsFeature().getFriendNames(), messagePart));
                        }
                    } else {
                        plugin.messageNoPrefix(sender, "info-regionCanBeBought", buy);
                    }
                    if (buy.getLandlord() != null) {
                        plugin.messageNoPrefix(sender, "info-regionLandlord", buy);
                    }
                    if (buy.getInactiveTimeUntilSell() != -1) {
                        plugin.messageNoPrefix(sender, "info-regionInactiveSell", buy);
                    }
                    // Teleport
                    Message tp = Message.fromKey("info-prefix");
                    boolean foundSomething = false;
                    if (TeleportCommand.canUse(sender, buy)) {
                        foundSomething = true;
                        tp.append(Message.fromKey("info-regionTeleport").replacements(buy));
                    }
                    if (SetteleportCommand.canUse(sender, buy)) {
                        if (foundSomething) {
                            tp.append(", ");
                        }
                        foundSomething = true;
                        tp.append(Message.fromKey("info-setRegionTeleport").replacements(buy));
                    }
                    if (foundSomething) {
                        tp.append(".");
                        tp.send(sender);
                    }
                    // Signs
                    List<String> signLocations = new ArrayList<>();
                    for (Location location : buy.getSignsFeature().getSignLocations()) {
                        signLocations.add(Message.fromKey("info-regionSignLocation").replacements(location.getWorld().getName(), location.getBlockX(), location.getBlockY(), location.getBlockZ()).getPlain());
                    }
                    if (!signLocations.isEmpty()) {
                        plugin.messageNoPrefix(sender, "info-regionSigns", Utils.createCommaSeparatedList(signLocations));
                    }
                    // Groups
                    if (sender.hasPermission("areashop.groupinfo") && !buy.getGroupNames().isEmpty()) {
                        plugin.messageNoPrefix(sender, "info-regionGroups", Utils.createCommaSeparatedList(buy.getGroupNames()));
                    }
                    // Restoring
                    if (buy.isRestoreEnabled()) {
                        plugin.messageNoPrefix(sender, "info-regionRestoringBuy", buy);
                    }
                    // Restrictions
                    if (!buy.isSold()) {
                        if (buy.restrictedToRegion()) {
                            plugin.messageNoPrefix(sender, "info-regionRestrictedRegionBuy", buy);
                        } else if (buy.restrictedToWorld()) {
                            plugin.messageNoPrefix(sender, "info-regionRestrictedWorldBuy", buy);
                        }
                    }
                    plugin.messageNoPrefix(sender, "info-regionFooterBuy", buy);
                }
            } else {
                plugin.message(sender, "info-regionHelp");
            }
        } else {
            plugin.message(sender, "info-help");
        }
    } else {
        plugin.message(sender, "info-help");
    }
}
Also used : GeneralRegion(me.wiefferink.areashop.regions.GeneralRegion) CommandSender(org.bukkit.command.CommandSender) Arrays(java.util.Arrays) Message(me.wiefferink.interactivemessenger.processing.Message) Set(java.util.Set) Player(org.bukkit.entity.Player) Utils(me.wiefferink.areashop.tools.Utils) ArrayList(java.util.ArrayList) RentRegion(me.wiefferink.areashop.regions.RentRegion) HashSet(java.util.HashSet) List(java.util.List) Location(org.bukkit.Location) RegionGroup(me.wiefferink.areashop.regions.RegionGroup) Nonnull(javax.annotation.Nonnull) BuyRegion(me.wiefferink.areashop.regions.BuyRegion) Nullable(javax.annotation.Nullable) Player(org.bukkit.entity.Player) Message(me.wiefferink.interactivemessenger.processing.Message) ArrayList(java.util.ArrayList) RentRegion(me.wiefferink.areashop.regions.RentRegion) RegionGroup(me.wiefferink.areashop.regions.RegionGroup) BuyRegion(me.wiefferink.areashop.regions.BuyRegion) GeneralRegion(me.wiefferink.areashop.regions.GeneralRegion) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) Location(org.bukkit.Location)

Example 10 with RegionGroup

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

the class StackCommand method execute.

@Override
public void execute(CommandSender sender, String[] args) {
    // Check permission
    if (!sender.hasPermission("areashop.stack")) {
        plugin.message(sender, "stack-noPermission");
        return;
    }
    // Only from ingame
    if (!(sender instanceof Player)) {
        plugin.message(sender, "cmd-onlyByPlayer");
        return;
    }
    final Player player = (Player) sender;
    // Specify enough arguments
    if (args.length < 5) {
        plugin.message(sender, "stack-help");
        return;
    }
    // Check amount
    int tempAmount = -1;
    try {
        tempAmount = Integer.parseInt(args[1]);
    } catch (NumberFormatException e) {
    // Incorrect number
    }
    if (tempAmount <= 0) {
        plugin.message(player, "stack-wrongAmount", args[1]);
        return;
    }
    // Check gap
    int gap;
    try {
        gap = Integer.parseInt(args[2]);
    } catch (NumberFormatException e) {
        plugin.message(player, "stack-wrongGap", args[2]);
        return;
    }
    // Check region type
    if (!"rent".equalsIgnoreCase(args[4]) && !"buy".equalsIgnoreCase(args[4])) {
        plugin.message(sender, "stack-help");
        return;
    }
    // Get WorldEdit selection
    final Selection selection = plugin.getWorldEdit().getSelection(player);
    if (selection == null) {
        plugin.message(player, "stack-noSelection");
        return;
    }
    // Get or create group
    RegionGroup group = null;
    if (args.length > 5) {
        group = plugin.getFileManager().getGroup(args[5]);
        if (group == null) {
            group = new RegionGroup(plugin, args[5]);
            plugin.getFileManager().addGroup(group);
        }
    }
    // Get facing of the player (must be clearly one of the four directions to make sure it is no mistake)
    BlockFace facing = Utils.yawToFacing(player.getLocation().getYaw());
    if (player.getLocation().getPitch() > 45) {
        facing = BlockFace.DOWN;
    } else if (player.getLocation().getPitch() < -45) {
        facing = BlockFace.UP;
    }
    if (!(facing == BlockFace.NORTH || facing == BlockFace.EAST || facing == BlockFace.SOUTH || facing == BlockFace.WEST || facing == BlockFace.UP || facing == BlockFace.DOWN)) {
        plugin.message(player, "stack-unclearDirection", facing.toString().toLowerCase().replace('_', '-'));
        return;
    }
    Vector shift = new BlockVector(0, 0, 0);
    if (facing == BlockFace.SOUTH) {
        shift = shift.setZ(-selection.getLength() - gap);
    } else if (facing == BlockFace.WEST) {
        shift = shift.setX(selection.getWidth() + gap);
    } else if (facing == BlockFace.NORTH) {
        shift = shift.setZ(selection.getLength() + gap);
    } else if (facing == BlockFace.EAST) {
        shift = shift.setX(-selection.getWidth() - gap);
    } else if (facing == BlockFace.DOWN) {
        shift = shift.setY(-selection.getHeight() - gap);
    } else if (facing == BlockFace.UP) {
        shift = shift.setY(selection.getHeight() + gap);
    }
    AreaShop.debug("  calculated shift vector: " + shift + ", with facing=" + facing);
    // Create regions and add them to AreaShop
    final String nameTemplate = args[3];
    final int regionsPerTick = plugin.getConfig().getInt("adding.regionsPerTick");
    final boolean rentRegions = "rent".equalsIgnoreCase(args[4]);
    final int amount = tempAmount;
    final RegionGroup finalGroup = group;
    final Vector finalShift = shift;
    String type;
    if (rentRegions) {
        type = "rent";
    } else {
        type = "buy";
    }
    Message groupsMessage = Message.empty();
    if (group != null) {
        groupsMessage = Message.fromKey("stack-addToGroup").replacements(group.getName());
    }
    plugin.message(player, "stack-accepted", amount, type, gap, nameTemplate, groupsMessage);
    plugin.message(player, "stack-addStart", amount, regionsPerTick * 20);
    new BukkitRunnable() {

        private int current = -1;

        private RegionManager manager = AreaShop.getInstance().getWorldGuard().getRegionManager(selection.getWorld());

        private int counter = 1;

        private int tooLow = 0;

        private int tooHigh = 0;

        @Override
        public void run() {
            for (int i = 0; i < regionsPerTick; i++) {
                current++;
                if (current < amount) {
                    // Create the region name
                    String counterName = counter + "";
                    int minimumLength = plugin.getConfig().getInt("stackRegionNumberLength");
                    while (counterName.length() < minimumLength) {
                        counterName = "0" + counterName;
                    }
                    String regionName;
                    if (nameTemplate.contains("#")) {
                        regionName = nameTemplate.replace("#", counterName);
                    } else {
                        regionName = nameTemplate + counterName;
                    }
                    while (manager.getRegion(regionName) != null || AreaShop.getInstance().getFileManager().getRegion(regionName) != null) {
                        counter++;
                        counterName = counter + "";
                        minimumLength = plugin.getConfig().getInt("stackRegionNumberLength");
                        while (counterName.length() < minimumLength) {
                            counterName = "0" + counterName;
                        }
                        if (nameTemplate.contains("#")) {
                            regionName = nameTemplate.replace("#", counterName);
                        } else {
                            regionName = nameTemplate + counterName;
                        }
                    }
                    // Add the region to WorldGuard (at startposition shifted by the number of this region times the blocks it should shift)
                    BlockVector minimum = new BlockVector(selection.getNativeMinimumPoint().add(finalShift.multiply(current)));
                    BlockVector maximum = new BlockVector(selection.getNativeMaximumPoint().add(finalShift.multiply(current)));
                    // Check for out of bounds
                    if (minimum.getBlockY() < 0) {
                        tooLow++;
                        continue;
                    } else if (maximum.getBlockY() > 256) {
                        tooHigh++;
                        continue;
                    }
                    ProtectedCuboidRegion region = new ProtectedCuboidRegion(regionName, minimum, maximum);
                    manager.addRegion(region);
                    // Add the region to AreaShop
                    if (rentRegions) {
                        RentRegion rent = new RentRegion(regionName, selection.getWorld());
                        if (finalGroup != null) {
                            finalGroup.addMember(rent);
                        }
                        rent.runEventCommands(GeneralRegion.RegionEvent.CREATED, true);
                        plugin.getFileManager().addRent(rent);
                        rent.handleSchematicEvent(GeneralRegion.RegionEvent.CREATED);
                        rent.runEventCommands(GeneralRegion.RegionEvent.CREATED, false);
                        rent.update();
                    } else {
                        BuyRegion buy = new BuyRegion(regionName, selection.getWorld());
                        if (finalGroup != null) {
                            finalGroup.addMember(buy);
                        }
                        buy.runEventCommands(GeneralRegion.RegionEvent.CREATED, true);
                        plugin.getFileManager().addBuy(buy);
                        buy.handleSchematicEvent(GeneralRegion.RegionEvent.CREATED);
                        buy.runEventCommands(GeneralRegion.RegionEvent.CREATED, false);
                        buy.update();
                    }
                }
            }
            if (current >= amount) {
                if (player.isOnline()) {
                    int added = amount - tooLow - tooHigh;
                    Message wrong = Message.empty();
                    if (tooHigh > 0) {
                        wrong.append(Message.fromKey("stack-tooHigh").replacements(tooHigh));
                    }
                    if (tooLow > 0) {
                        wrong.append(Message.fromKey("stack-tooLow").replacements(tooLow));
                    }
                    plugin.message(player, "stack-addComplete", added, wrong);
                }
                this.cancel();
            }
        }
    }.runTaskTimer(plugin, 1, 1);
}
Also used : Player(org.bukkit.entity.Player) Message(me.wiefferink.interactivemessenger.processing.Message) Selection(com.sk89q.worldedit.bukkit.selections.Selection) BlockFace(org.bukkit.block.BlockFace) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) RentRegion(me.wiefferink.areashop.regions.RentRegion) RegionGroup(me.wiefferink.areashop.regions.RegionGroup) BuyRegion(me.wiefferink.areashop.regions.BuyRegion) RegionManager(com.sk89q.worldguard.protection.managers.RegionManager) ProtectedCuboidRegion(com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion) BlockVector(com.sk89q.worldedit.BlockVector) Vector(com.sk89q.worldedit.Vector) BlockVector(com.sk89q.worldedit.BlockVector)

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