Search in sources :

Example 11 with GeneralRegion

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

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

the class GroupaddCommand method execute.

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

Example 13 with GeneralRegion

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

Example 14 with GeneralRegion

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

the class PlayerLoginLogoutListener method onPlayerLogin.

/**
 * Called when a sign is changed.
 * @param event The event
 */
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerLogin(PlayerLoginEvent event) {
    if (event.getResult() != Result.ALLOWED) {
        return;
    }
    final Player player = event.getPlayer();
    // Schedule task to check for notifications, prevents a lag spike at login
    Do.syncTimerLater(25, 25, () -> {
        // Delay until all regions are loaded
        if (!plugin.isReady()) {
            return true;
        }
        if (!player.isOnline()) {
            return false;
        }
        // Notify for rents that almost run out
        for (RentRegion region : plugin.getFileManager().getRents()) {
            if (region.isRenter(player)) {
                String warningSetting = region.getStringSetting("rent.warningOnLoginTime");
                if (warningSetting == null || warningSetting.isEmpty()) {
                    continue;
                }
                long warningTime = Utils.durationStringToLong(warningSetting);
                if (region.getTimeLeft() < warningTime) {
                    // Send the warning message later to let it appear after general MOTD messages
                    AreaShop.getInstance().message(player, "rent-expireWarning", region);
                }
            }
        }
        // Notify admins for plugin updates
        AreaShop.getInstance().notifyUpdate(player);
        return false;
    });
    // Check if the player has regions that use an old name of him and update them
    Do.syncTimerLater(22, 10, () -> {
        if (!plugin.isReady()) {
            return true;
        }
        List<GeneralRegion> regions = new ArrayList<>();
        for (GeneralRegion region : plugin.getFileManager().getRegions()) {
            if (region.isOwner(player)) {
                regions.add(region);
            }
        }
        Do.forAll(plugin.getConfig().getInt("nameupdate.regionsPerTick"), regions, region -> {
            if (region instanceof BuyRegion) {
                if (!player.getName().equals(region.getStringSetting("buy.buyerName"))) {
                    region.setSetting("buy.buyerName", player.getName());
                    region.update();
                }
            } else if (region instanceof RentRegion) {
                if (!player.getName().equals(region.getStringSetting("rent.renterName"))) {
                    region.setSetting("rent.renterName", player.getName());
                    region.update();
                }
            }
        });
        return false;
    });
}
Also used : Player(org.bukkit.entity.Player) BuyRegion(me.wiefferink.areashop.regions.BuyRegion) GeneralRegion(me.wiefferink.areashop.regions.GeneralRegion) ArrayList(java.util.ArrayList) RentRegion(me.wiefferink.areashop.regions.RentRegion) EventHandler(org.bukkit.event.EventHandler)

Example 15 with GeneralRegion

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

the class SignLinkerManager method onPlayerInteract.

/**
 * On player interactions.
 * @param event The PlayerInteractEvent
 */
@EventHandler(priority = EventPriority.HIGH)
public void onPlayerInteract(PlayerInteractEvent event) {
    if (isInSignLinkMode(event.getPlayer())) {
        event.setCancelled(true);
        Player player = event.getPlayer();
        SignLinker linker = signLinkers.get(event.getPlayer().getUniqueId());
        if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
            // Get the region
            BlockIterator blockIterator = new BlockIterator(player, 100);
            while (blockIterator.hasNext()) {
                Block next = blockIterator.next();
                List<GeneralRegion> regions = Utils.getRegions(next.getLocation());
                if (regions.size() == 1) {
                    linker.setRegion(regions.get(0));
                    return;
                } else if (regions.size() > 1) {
                    Set<String> names = new HashSet<>();
                    for (GeneralRegion region : regions) {
                        names.add(region.getName());
                    }
                    plugin.message(player, "linksigns-multipleRegions", Utils.createCommaSeparatedList(names));
                    plugin.message(player, "linksigns-multipleRegionsAdvice");
                    return;
                }
            }
            // No regions found within the maximum range
            plugin.message(player, "linksigns-noRegions");
        } else if (event.getAction() == Action.LEFT_CLICK_AIR || event.getAction() == Action.LEFT_CLICK_BLOCK) {
            Block block = null;
            BlockIterator blockIterator = new BlockIterator(player, 100);
            while (blockIterator.hasNext() && block == null) {
                Block next = blockIterator.next();
                if (next.getType() != Material.AIR) {
                    block = next;
                }
            }
            if (block == null || !(block.getType() == Material.WALL_SIGN || block.getType() == Material.SIGN_POST)) {
                plugin.message(player, "linksigns-noSign");
                return;
            }
            RegionSign regionSign = SignsFeature.getSignByLocation(block.getLocation());
            if (regionSign != null) {
                plugin.message(player, "linksigns-alreadyRegistered", regionSign.getRegion());
                return;
            }
            Sign sign = (Sign) block.getState().getData();
            linker.setSign(block.getLocation(), block.getType(), sign.getFacing());
        }
    }
}
Also used : BlockIterator(org.bukkit.util.BlockIterator) Player(org.bukkit.entity.Player) HashSet(java.util.HashSet) Set(java.util.Set) GeneralRegion(me.wiefferink.areashop.regions.GeneralRegion) Block(org.bukkit.block.Block) Sign(org.bukkit.material.Sign) RegionSign(me.wiefferink.areashop.features.signs.RegionSign) RegionSign(me.wiefferink.areashop.features.signs.RegionSign) EventHandler(org.bukkit.event.EventHandler)

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