Queuing System...

Discussion in 'Plugin Development' started by spiroulis, Mar 20, 2014.

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

    spiroulis

    Hey so i've been trying to figure out how to make a queuing system but i cant find a way...If anyone knows how please let me know.
     
  2. Offline

    Darq

    Much specific question.. so much detail contained to help us help you easier... not.

    You want a queue?
    Code:
     Deque deck = new ArrayDeque();
    deck.pop() will take off the last element inserted.
    deck.push(anObject) will push that object onto the "top" of the queue.

    Deque is a special kind of queue that allows inserting/removing from both ends, so it nether FIFO (first in first out) or LIFO (last in first out), but both. (more information here)

    As for a "queuing system".. if that doesn't already answer your question, can you elaborate?
     
  3. Offline

    iBieel

    Code:java
    1. //Setting an ArrayList that will serve as a queue
    2. List<String> queue = new ArrayList<String>();
    3.  
    4. //Adding new people in the queue
    5. queue.add(player.getName());
    6.  
    7. //Removing people from the queue
    8. queue.remove(player.getName());
    9.  
    10. //Getting the people on the queue
    11. queue.get(0);
    12. queue.get(1);
    13. queue.get(2);
    14. ...
    15.  
    16. //If the person is in the queue
    17. if(queue.contains(player.getName())){
    18.  
    19. }
     
  4. Offline

    rfsantos1996

    Create a list with what you need to work, for example, I'll make a queue for setting the blocks to Dirt
    Code:java
    1. LinkedArrayList<Block> queue = new LinkedArrayList();

    Ok, now that I have the queue, i'll keep setting all blocks to dirt with a limit per run. (using a SyncRepeatingTask)
    Code:java
    1. private void run() { // all this inside a repeating task, so it'll loop everytime, if you set timesperRun outside of this, it wont loop, it'll stop after a few repeats
    2. int timesPerRun = 100;
    3. if(queue.size() < timesPerRun) { // if the queue size is lower than the runs that we'll do
    4. timesPerRun = queue.size(); // make it more efficient by just making what we need
    5. }
    6.  
    7. while(timesPerRun > 0) { // i'm NOT sure, but I think you can do '&& queue.size() > 0' here
    8. Iterable<Block> iterable = queue.iterable();
    9. if(iterable.hasPrevious()) { // I'll use previous so older values are always removed
    10. Block block = iterable.previous();
    11. block.setType(Material.DIRT);
    12. queue.remove(block);
    13. }
    14. }
    15. }


    I THINK thats how you create a queue... I'm not sure lol - I made this queue to run a task with a limitation on each run because you'll need depending on whats the queue for. But you can make queues with a ordered list (list that doesn't lose its order)
     
  5. Offline

    spiroulis

    rfsantos1996 iBieel Darq sorry guys i should have been more specific. I meant that i want to make a system that when a player enables it, the system searches for someone else that enabled it too so they can have a match together :)
     
    MRPS likes this.
  6. Offline

    rfsantos1996

    You should use Darq or iBieel, mine is more useful for tasks that require queueing because its a heavy performance...
     
  7. Offline

    MRPS

    Code:java
    1. import java.util.HashMap;
    2.  
    3. import org.bukkit.entity.Player;
    4. import org.bukkit.plugin.java.JavaPlugin;
    5.  
    6.  
    7. public class PlayerQueueSystem {
    8.  
    9. private JavaPlugin plugin;
    10. private int queueSize;
    11. public PlayerQueueSystem(JavaPlugin plugin, int queueSize){
    12. this.plugin = plugin;
    13. this.queueSize = queueSize;
    14. }
    15.  
    16. private final HashMap<String,Integer> player_index = new HashMap<String,Integer>();
    17. private String[] player_queue = new String[queueSize];
    18.  
    19. /**
    20.   *
    21.   * @return Next available spot in the queue.
    22.   */
    23. private int getNextSpot(){
    24. int freeIndex = 0;
    25. int i;
    26. for(i = 0; i <= queueSize; i++){
    27. if(player_queue[i] == "" | player_queue[i] == null){
    28. freeIndex = i;
    29. continue; // breakpoint
    30. }
    31. }
    32.  
    33. return freeIndex;
    34. }
    35. /**
    36.   *
    37.   * @return True if there is a spot in the queue, false if there is no space in the queue.
    38.   */
    39. private boolean hasNextSpot(){
    40. if(getNextSpot() > queueSize) return false;
    41. else return true;
    42. }
    43.  
    44. /**
    45.   * Adds a player (by name) to the queue and flags them for selection.
    46.   * @param p Player to add to the queue
    47.   *
    48.   * @return If the player was added to the queue, return true else return false.
    49.   */
    50. public boolean queue_add(String pName){
    51. if(hasNextSpot()){
    52. int nextSpot = getNextSpot();
    53. player_queue[nextSpot] = pName;
    54. player_index.put(pName, nextSpot);
    55. return true;
    56. }
    57. else return false;
    58. }
    59.  
    60. /**
    61.   * Removes a player (by name) from the queue and unflags them for selection.
    62.   * @param p Player to remove from the queue
    63.   *
    64.   * @return If the player was removed from the queue, return true else return false.
    65.   */
    66. public boolean queue_rem(String pName){
    67. if(hasNextSpot()){
    68. player_queue[player_index.get(pName)] = "";
    69. player_index.put(pName, -1);
    70. return true;
    71. }
    72. else return false;
    73. }
    74.  
    75. /**
    76.   * This has to be called in an onPlayerJoin() or other similar method; creates the defaults for the player
    77.   * @param p
    78.   */
    79. public void queue_default(Player p){
    80. player_index.put(p.getName(), -1); // -1 value prevents selection
    81. }
    82.  
    83. /**
    84.   * This is called when you want to select some people from the queue
    85.   * @return Array of player names that have been selected from the queue
    86.   */
    87. public String[] queue_select(int amountToSelect){
    88. String[] selected = new String[amountToSelect];
    89.  
    90. int i;
    91. int index = 0;
    92. for(i = 0; i <= queueSize; i++){
    93. if(index > amountToSelect) continue;
    94.  
    95. if(player_queue[i] != null && player_queue[i] != ""){
    96. // Player is in the queue
    97. if(player_index.get(player_queue[i]) > 0){ // Check if they are flagged for selection
    98. // Add them to the selected players
    99. selected[index] = player_queue[index];
    100.  
    101. // Remove them from the queue
    102. queue_rem(player_queue[i]);
    103.  
    104. // Increment up the selection index
    105. index++;
    106. }
    107. }
    108. }
    109.  
    110. return selected; // Returns a string array of the selected players so that the plugin can do something with them
    111. }
    112. }
    113. [/i][/i][/i][/i][/i][/i]


    This should work for your needs, bud. Good luck!
     
Thread Status:
Not open for further replies.

Share This Page