Solved get online owners of a region with WorldGuardAPI

Discussion in 'Spigot Plugin Development' started by AL3X707, Jul 14, 2021.

  1. Hi, I've been working on a project, and it's been going very good so far. Now, I want to add a command, so you can attack someones region, but only if the owners of this region are online. I tried some things, which didn't work, so I'm asking here for some help.
     
  2. you'll probably need to use for loop with region member list and check if players are online.
     
    • Like Like x 1
  3. Made this method that will check if any of the owners of the region are online, and if any of them are, it will return true.

    Code (Text):
        public boolean isOwnerOnline(ProtectedRegion region) {
            Optional<UUID> online = region.getMembers().getUniqueIds().stream()
                    .filter(u -> Bukkit.getPlayer(u) != null)
                    .filter(u -> Bukkit.getPlayer(u).isOnline())
                    .findAny();
           
            return online.isPresent();
        }
    If you don't understanjd it or got any questions, please lmk :)
     
    • Like Like x 1
  4. My bad, I forgot to say that I also need the amount of online owners. I've come to a solution now using an Iterator.
    Code (Text):
    Iterator<UUID> ownersIterator = regions.getOwners().getUniqueIds().iterator();
                                        int ownersOnline = 0;
                                        while(ownersIterator.hasNext()) {
                                            UUID ownersUUID = ownersIterator.next();
                                            if(Bukkit.getOnlinePlayers().contains(Bukkit.getPlayer(ownersUUID))) {
                                                p.sendMessage(Bukkit.getPlayer(ownersUUID).getName() + " is online");
                                                ownersOnline++;
                                            } else {
                                                try {
                                                    p.sendMessage(Bukkit.getPlayer(ownersUUID).getName() + "is offline");
                                                } catch (Exception e) {
                                                    p.sendMessage("unknown is offline");
                                                }
                                            }                
                                        }
                                        p.sendMessage("online owners: " + ownersOnline);
    or to make it a bit shorter:
    Code (Text):
    public static int getOnlineOwnersSize(ProtectedRegion region) {
            Iterator<UUID> ownersIterator = region.getOwners().getUniqueIds().iterator();
            int ownersOnline = 0;
            while(ownersIterator.hasNext()) {
                UUID ownersUUID = ownersIterator.next();
                if(Bukkit.getOnlinePlayers().contains(Bukkit.getPlayer(ownersUUID))) {
                    ownersOnline++;
                }
            }
            return ownersOnline;
        }
     
    #4 AL3X707, Jul 14, 2021
    Last edited: Jul 14, 2021