Queue System....

Discussion in 'Spigot Plugin Development' started by TedTheTechie, Jan 24, 2015.

Thread Status:
Not open for further replies.
  1. Hello, I've been a little stumped on doing this.

    I'm trying to make a simple queue system, to where I can add players into it, and when they disconnect, they are removed from queue. I'm also trying to make this into a simple queue API that I can use in the future. Another thing I am unable to figure out, is how would I be able to move players up in the queue, to add special players 1st in queue.
     
  2. You would use a queues algorithm.

    Are queue will look like this (With items in between the rear and front):
    --------------------------------------
    rear front
    --------------------------------------

    So with that we know we will need the following variables
    1) A list (That way you don't have to worry about resizing)
    2) A int variable to store the rear index
    3) A int variable to store the front index

    From here, it's just some simple java
    Note that I'm storing the players through there names but if you want the queue to be persistent you will have to store it using UUID's to a file.

    Code (Text):
    List<String> queue = new ArrayList<String>(); //You must use a list as sets don't maintain order (Lists do)
    int rear = -1; //We initialize these to -1 so when we increase them they become 0
    int front = -1;

    public void addPlayer(Player p){
        rear++;
        queue.add(p.getName());
    }

    public Player getNext(){ //When using getNext() you should double check to make sure that there are still players in the queue.
         front++;
         return Bukkit.getPlayer(queue.get(front));
    }
    This is pseudo code, so it may or may not work. I'm on my phone as well so there may be some typos.

    This is just a simple implementation, you can adapt it to increase priority of certain players.
     
  3. @ArticleZero

    How would I go about adding players in the front of the queue so they're immediately ahead of others?

    Also, would the edits I have made work:


    Code (Text):
    List<String> queue = new ArrayList<String>();
    int rear = -1;
    int front = -1;

    public void addPlayer(Player p){
        rear++;
        queue.add(p.getName());
    }

    public void removePlayer(Player player) {
       queue.remove(player.getName());
       rear--; // not sure if I need to subtract one from rear when removing player.
    }

    public Player getNext(){
         front++;
       Player p = Bukkit.getPlayer(queue.get(front));
        removePlayer(p);
        return p;
    }
     
  4. It is more easier with the usage of ArrayLists as thought.
    Code (Text):

    List<String> queue = new ArrayList<>();

    public boolean add(String name) {
        if (queue.contains(name))
            return false;
        queue.add(name);
        return true;
    }

    public boolean insert(String name, int pos) {
        if (pos > queue.size())
            pos = queue.size();
        queue.add(pos, name);
    }

    public String getFront(boolean removeFromQueue) {
        String name = queue.get(0);
        if (removeFromQueue)
            queue.remove(0);
        return name;
    }
     
     
    • Like Like x 1
  5. @Janmm14 For placing a user in the 1st slot, would I put pos as 0 or 1? Cause of how arraylists work :p
     
  6. pos 0, as lists work in that case like arrays in java, starting at index 0
     
  7. You should use one of the Queues that are provided in java's standard library (see more here).
     
    #7 hamzaxx, Jun 1, 2015
    Last edited: Jun 2, 2015
    • Like Like x 1
    • Winner Winner x 1
  8. Yeah use a queue or priority queue from java.util. Also I would back it with a linked list because your adding and removing is going to be a lot cheaper.
     
    • Like Like x 1
  9. Instead of reinventing the wheel, why not use a stable, viable and documented alternative, such as ArrayDeque or LinkedList ?
     
    • Creative Creative x 1
  10. That was already suggested...
     
  11. Hey, I know that this thread is ancient, but I'm trying to use this in a plugin I'm making.

    How would I get a players' position in queue with this. I just want to add them to the end of the line, but as the line moves forward, I want the number to change so they know when they are first.

    any ideas?
     
  12. Ffs, you could've freaking googled it. Something like "java get position of object in list". The anwer is so easy I'm not giving it
     
Thread Status:
Not open for further replies.