Solved Bukkit.getOnlinePlayers complex issues

Discussion in 'Plugin Development' started by Telmovieira, Jul 28, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    Telmovieira

    Hello everone!

    Today i bring you this:
    Code:
    @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event) {
            Player player1 = event.getPlayer();
           if (Bukkit.getOnlinePlayers().size() == 1) {
                me.Telmovieira.Skywars.SpawnUtils
                        .addToNumber(Position.ONE, player1);
            }
            if (Bukkit.getOnlinePlayers().size() == 2) {
                me.Telmovieira.Skywars.SpawnUtils
                        .addToNumber(Position.TWO, player1);
            }
            if (Bukkit.getOnlinePlayers().size() == 3) {
                me.Telmovieira.Skywars.SpawnUtils.addToNumber(Position.THREE,
                        player1);
            }
            if (Bukkit.getOnlinePlayers().size() == 4) {
                me.Telmovieira.Skywars.SpawnUtils.addToNumber(Position.FOUR,
                        player1);
            }
            if (Bukkit.getOnlinePlayers().size() == 5) {
                me.Telmovieira.Skywars.SpawnUtils.addToNumber(Position.FIVE,
                        player1);
            }
            if (Bukkit.getOnlinePlayers().size() == 6) {
                me.Telmovieira.Skywars.SpawnUtils
                        .addToNumber(Position.SIX, player1);
            }
            if (Bukkit.getOnlinePlayers().size() == 7) {
                me.Telmovieira.Skywars.SpawnUtils.addToNumber(Position.SEVEN,
                        player1);
            }
            if (Bukkit.getOnlinePlayers().size() == 8) {
                me.Telmovieira.Skywars.SpawnUtils.addToNumber(Position.EIGHT,
                        player1);
            }
            if (Bukkit.getOnlinePlayers().size() == 9) {
                player1.kickPlayer("This server is full! Try again later.");
            }
    
        }
    HOW IT IS SUPPOSED TO WORKS:

    When a player joins he gets ut in one of this positions. The positions are made using a highly modified Teams API that i created that is proved that works. This positions are later used to teleport the players to diffrent sky islands on the map. When there are 8 players on the server it is full.

    HOW IT ACTUALLY WORKS:

    The first player is placed in the right place. The others go to the no second position for some dumb reason...

    Every help is very much apriciated!

    Thanks
     
  2. Offline

    Ruptur

    @Telmovieira
    Using enums like that isnt very efficient. I sugguest that you use an int to specify the player positions.
    That way it make things alot easier and clearer.

    Taking my suggestion, you can then avoid any errors.
    Ana example of what i mean is this:
    Code:
    int playersOnline = getOnlinePlayers();
    if (playersOnline > 8) {
        kick recently joined player
    } else {
        int position = playersOnline;
        addPlayer(position, event.getPlayer());
    }
    
     
  3. Offline

    _Error

    try using else if(){}?

    EDIT: @Ruptur we posted at the same time, And I was going to note that too but thought it was not to necessary as it's not his problem.
     
  4. Offline

    Telmovieira

    @_Error
    Already tryed.... It does not work.

    @Ruptur
    I would preffer a solution that uses the logic already implemented

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 11, 2016
  5. Offline

    _Error

    Did you add debug messages?
     
  6. Offline

    CraftCreeper6

    @Telmovieira
    Okay, stop with this immediately and use a switch statement.

    switch(Bukkit.getOnlinePlayers()...)
    {
    case 1....
    break...
    }

    Much cleaner and better, actually work.

    EDIT: May I also see your Teams class?
     
  7. Offline

    Telmovieira

    @CraftCreeper6 @_Error

    The switch statment puts the first 2 players in the first and second positions, but the rest go on the eighth...

    Code:
    switch (Bukkit.getOnlinePlayers().size()) {
                case 1:
                    me.Telmovieira.Skywars.SpawnUtils.addToNumber(Position.ONE,player1);
                    break;
                case 2:
                    me.Telmovieira.Skywars.SpawnUtils.addToNumber(Position.TWO,player1);
                    break;
                case 3:
                    me.Telmovieira.Skywars.SpawnUtils.addToNumber(Position.THREE,player1);
                    break;
                case 4:
                    me.Telmovieira.Skywars.SpawnUtils.addToNumber(Position.FOUR,player1);
                    break;
                case 5:
                    me.Telmovieira.Skywars.SpawnUtils.addToNumber(Position.FIVE,player1);
                    break;
                case 6:
                    me.Telmovieira.Skywars.SpawnUtils.addToNumber(Position.SIX,player1);
                    break;
                case 7:
                    me.Telmovieira.Skywars.SpawnUtils.addToNumber(Position.SEVEN,player1);
                    break;
                case 8:
                    me.Telmovieira.Skywars.SpawnUtils.addToNumber(Position.EIGHT,player1);
                    break;
                case 9:
                    player1.kickPlayer("The server is full");
                    break;
                }

    here is my teams class(or spawn utils as i called it. But it's what i use on teams):

    Code:
    package me.Telmovieira.Skywars;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.entity.Player;
    
    public class SpawnUtils {
    
        private static List<String> one = new ArrayList<String>();
        private static List<String> two = new ArrayList<String>();
        private static List<String> three = new ArrayList<String>();
        private static List<String> four = new ArrayList<String>();
        private static List<String> five = new ArrayList<String>();
        private static List<String> six = new ArrayList<String>();
        private static List<String> seven = new ArrayList<String>();
        private static List<String> eight = new ArrayList<String>();
    
        public static void addToNumber(Position num, Player player) {
            switch (num) {
            case ONE:
                one.add(player.getName());
                break;
            case TWO:
                two.add(player.getName());
                break;
            case EIGHT:
                eight.add(player.getName());
                break;
            case FIVE:
                five.add(player.getName());
                break;
            case FOUR:
                four.add(player.getName());
                break;
            case SEVEN:
                seven.add(player.getName());
                break;
            case SIX:
                six.add(player.getName());
                break;
            case THREE:
                three.add(player.getName());
                break;
            }
    
        }
    
        public static boolean isInPosition(Player player) {
            return one.contains(player.getName()) || two.contains(player.getName())
                    || three.contains(player.getName())
                    || four.contains(player.getName())
                    || five.contains(player.getName())
                    || six.contains(player.getName())
                    || seven.contains(player.getName())
                    || eight.contains(player.getName());
        }
    
        public static void clearGroups() {
            one.clear();
            two.clear();
        }
    
        public static List<String> getOne() {
            return one;
        }
    
        public static List<String> getTwo() {
            return two;
        }
       
        public static List<String> getThree() {
            return three;
        }
       
        public static List<String> getFour() {
            return four;
        }
       
        public static List<String> getFive() {
            return five;
        }
       
        public static List<String> getSix() {
            return six;
        }
       
        public static List<String> getSeven() {
            return seven;
        }
       
        public static List<String> getEight() {
            return eight;
        }
    
        public static Position getNumberType(Player player) {
            if (!isInPosition(player))
                return null;
            return (one.contains(player.getName()) ? Position.ONE
                    : two.contains(player.getName()) ? Position.TWO : one.contains(player.getName()) ? Position.THREE : one.contains(player.getName()) ? Position.FOUR : one.contains(player.getName()) ? Position.FIVE : one.contains(player.getName()) ? Position.SIX : one.contains(player.getName()) ? Position.SEVEN : Position.EIGHT);
        }
    }
    
    I used the following debbug command:

    Code:
    if (commandLabel.equalsIgnoreCase("position")) {
                if (player.isOp()) {
                    if (SpawnUtils.getNumberType(player) == Position.ONE) {
                        player.sendMessage(ChatColor.BLUE
                                + "You are on the first position.");
                    }
                    if (SpawnUtils.getNumberType(player) == Position.TWO) {
                        player.sendMessage(ChatColor.BLUE
                                + "You are on the second position.");
                    }
                    if (SpawnUtils.getNumberType(player) == Position.THREE) {
                        player.sendMessage(ChatColor.BLUE
                                + "You are on the third position.");
                    }
                    if (SpawnUtils.getNumberType(player) == Position.FOUR) {
                        player.sendMessage(ChatColor.BLUE
                                + "You are on the fourth position.");
                    }
                    if (SpawnUtils.getNumberType(player) == Position.FIVE) {
                        player.sendMessage(ChatColor.BLUE
                                + "You are on the fifth position.");
                    }
                    if (SpawnUtils.getNumberType(player) == Position.SIX) {
                        player.sendMessage(ChatColor.BLUE
                                + "You are on the sixth position.");
                    }
                    if (SpawnUtils.getNumberType(player) == Position.SEVEN) {
                        player.sendMessage(ChatColor.BLUE
                                + "You are on the seventh position.");
                    }
                    if (SpawnUtils.getNumberType(player) == Position.EIGHT) {
                        player.sendMessage(ChatColor.BLUE
                                + "You are on the eighth position.");
                    }
                }
            }
     
  8. Offline

    CraftCreeper6

    @Telmovieira
    Use a Hashmap, position as key and player name as value.
     
  9. Offline

    teej107

    @Telmovieira Add a method in the Position class/enum to get a "Position" object from an int and then you won't need all those if statements/switch case.
     
  10. Offline

    Telmovieira

    @CraftCreeper6
    And then how whould i use said Hashmaps?

    Do i replace them with the lists? This way i would have to change the whole class...

    Isn't there a way that i can do this with what i already have, not altering the SpawnUtils class?
     
  11. Offline

    CraftCreeper6

    @Telmovieira
    The Java Docs has a great explanation on how to use them.
     
  12. Offline

    Telmovieira

    I got it... there were some problems in the SpawnUtils class after all...
     
Thread Status:
Not open for further replies.

Share This Page