Solved How would I make a queue?

Discussion in 'Plugin Development' started by Ragnarok_, Mar 25, 2017.

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


    I'm making a duel plugin where players can duel other players. When you right click an item, it opens a menu for you, and if you click on for say a carrot, it will add you in the queue until someone else joins in it. How would I make this happen so it match makes and stuff with the opponent also wanting to duel? I don't want to make possibly 100 arraylist's or something like that. Ideas, information, and help is appreciated!
  2. Offline


    1. Create a single player field. This represents an opponent.
    2. When a player clicks the carrot, check if that field is null. If it is, set it equal to the player.
    3. If it is not null, set up the duel. If you need to store both players together, I would recommend creating a new object type for holding both players.
    jobisingh likes this.
  3. Offline


    This is probably obvious but I'll point out anyway to make sure to set the value back to null when the second player has joined
  4. Offline


    Could you link me a page or source for me to learn about that? I may know what you're talking about, but just not with the correct term. How would I create a field and an object? Sorry, I'm a beginner in Java, but I'm willing to learn, if you could show me a page or something that'd be great. Sorry for my lack of knowledge of Java, I'm a beginner.
  5. Offline



    public class Test implements Listener {
        private Player playerStorage;
        public void onJoin(PlayerJoinEvent e)
            if (this.playerStorage != null)
                e.getPlayer().sendMessage(this.playerStorage.getName()+" connected just before you.");
            this.playerStorage = e.getPlayer();
    Last edited: Mar 25, 2017
  6. Offline


    Hm.. I'm trying to understand this, tell me if I get it, I don't have a clue. So maybe the PlayerJoinEvent, is when the click the carrot, and what's the 'if(this.player != null'? What is this exactly doing?
  7. Offline


    @Ragnarok_ 'this.' is for access a variable in the class.

    A variable, it's a storage. Here we create a variable Player, and by default, it's null;

    != null, we check if the variable has a value or not yet.

    In this case, the variable Player will return null if it hasn't a value yet, or if the player stored in the value is offline.

    I edited the variable name in my post. That could cause confusion
    Last edited: Mar 25, 2017
  8. Offline


    Okay.. correct my if I'm wrong, but why would you create a field?
    Why not a HashMap or something along those lines like:
     HashMap<String, String> queued = new hash<> 
    If someone wants to dual, just do
     queued.put(player.getname(), null); 
    and if someone else wants to duel you'd just look through it and check if the second value is null, then add the player to it, or remove it and boom, duel.
    Ragnarok_ likes this.
  9. Offline


  10. Offline


    Oh okay,
    But what if more people join the queue? Then, would I just remove them both from the hash once they are matched?
  11. Offline


    Yes you could, or you could keep the two people in the map together so that you can determine whether or not they've already been matched with an opponent so that nobody can duel them whilst they are already in one.

    and if more people want to join, just do queued.put again.. the hashmap can store multiple values
    Ragnarok_ likes this.
  12. Offline


    Yea, that's true, but I could just make another hashmap or arraylist called like "InMatch" and just test if their in it or not. You kinda have got the same concept as me, if they want to duel a specific person. I know how to make it do they can /duel <player name>, but do you happen to know how they would accept that duel? I have a feeling I could use like a runnable and after 20 seconds, if they haven't did whatever they needed to do to accept that duel, cancel the task with a message. But, do you know how they can accept or cancel that duel, or lead me in the right spot? You we're very helpful in the previous questions. Sorry if this is off topic.

    Edit: I tried testing is queue.isEmpty() { Blah blah blah and that works, but what about if the queue has one person in it, how am I supposed to match those people up? I looked and saw a queue.get() option and I put in 0, because that's the first one and I'm guessing it was that since you said HashMaps can hold multiple values. For the else I did if(!queue.isEmpty() {
    and I don't really know what to do.
    Last edited: Mar 25, 2017
  13. Offline


    You're on the right track. For the timeout a runnable would work great :p.
    As for the duel, I'll give you a short example:
    HashMap<StringStringmatchedUsers = new HashMap<>();
    // So first off, the user must initiate a duel with /duel <playername>
            // Then it would add them to a hashmap like this, and check if anyone else is pending for a duel.
    if(matchedUsers.containsKey(someuser.getname) || matchedUsers.containsValue(someuser.getname)) { // they're trying to duel again?
    sendmessage("You cant duel again, silly!");
    sendmessage("Please wait while we try and pair you up!");
    // This will loop through the hashmap and find a value that is null, meaning that the user does not have a duel buddy yet
    Optional<StringfindUser matchedUsers.keySet().stream().filter(key -> matchedUsers.get(key) == null).findAny();
            if ( !
    findUser.isPresent() ) return; // Couldnt find anyone
    String keyUser findUser.get(); // grab the user that doesn't have a buddy
    matchedUsers.put(keyUsersomeuser.getname); // put em together
    sendmessage("You have been paired up!");
    sendothermessage("You have been paired up!");
    // teleport
            // duel
            // etc
            // after duel
    matchedUsers.remove(keyUser or someuser);
    //teleport back, bla bla bla
    Now, this code is by no means working BUT, hopefully you get what I'm going at. I don't know if this example would work fully, I just typed it up real quick.
    Ragnarok_ likes this.
  14. @Ragnarok_
    You could create a class that stores the player UUID in a treeset then fetch the two first UUID in the set. You then ask them if they want to duel, like with /duel, and if so, you would make them duel. Otherwise, you remove their names and add them to the end of the list again, until they're in a match. It's kind of like a queue.
  15. Offline


    THANK YOU SO MUCH! You've been such a good help and a great example. Sorry, I had a concert to take care of, but now that I researched HashMaps a bit more and by you're example, I've got it solved! Thank you!
    N00BHUN73R likes this.
Thread Status:
Not open for further replies.

Share This Page