1.12.2 Mini-Game Queue System List

Discussion in 'Spigot Plugin Development' started by greg5423, Apr 17, 2020.

  1. I am making a mini-game plugin and I am making a queue system where players wait until there are enough players to start the game. I did some research, and people are recommending to use a list of players as the queue (when it gets full, start the game) which I could have figured myself. What I don't understand is how to make a permanent list. For example, when a player joins the queue, I add them to the list, but then the program is over and the list is forgotten. How do I get a list that I can access when the next player joins the queue? Do I need a program that runs forever? Any input/suggestions are appreciated.
  2. You could just get a list of the current players on the server?

    What are you trying to make a list of? You could just store the players in the config file. When a player joins, add them to the config, when a player leaves, take them away.
  3. You can use something called static for your ArrayList to call it in when you need it outside the class. This is the case if you're not worried about restarts on the server and queue's being restarted again in server restarts.
  4. When a player joins the queue I want to add them to the list, and then be able to use that list when another player joins the queue.
  5. I don't think that would solve the problem. I want to be able to access the list after the program has ended not outside of the class. But I will probably just use a YAML file to store the queue like superblaze27 said as I don't think it is possible to do what I want.
  6. why would you need to know the queue list after the server has restarted/closed (which probably means that the minigame server's instance has ended meaning the queue should be cleared at that point right?) an arraylist should work fine for that purpose.

    I mean if you want it to be permanent for whatever reason then yes the only way is to either use a database or a yml file.
  7. I don't mean I want to use them after the server stops. When a player joins the queue it runs a script that adds them to the list, but then (to my knowledge) the script is over and that list is gone. I might be misunderstanding how it works or maybe I don't have it setup right, but I thought that once the code has finished the variables and objects are deleted.
  8. Yes, you are correct. If you are storing a variable that is created after the program starts, it is stored in memory and deleted after a restart.
  9. I have a question, is the minigame using different worlds for creating a new map or the server itself is the minigame with its own map and no multiple arenas? When it's for only 1 server, you can create a static ArrayList as it means that it will be garbage collected only when the application is stopped/restarted. It is a "permanent" variable for that time being until the server would restart.
  10. So if I create an object in the onEnable method, should the values in the object last until the server stops?
  11. If you create it inside the onEnable, it will only be accessed in there leading it to be limited and can't be used outside of the void, just like any other variables that are created inside a void. Logically, you always need to create new ArrayList when you want one. To keep it from being renewed, you'd have to create the variable outside.
    Code (Text):
    private ArrayList<String> players = new ArrayList<>();

    public void addPlayer(Player player)
    String name = player.getName();
    When you want the list to be accessed for the other classes for a reason, you make the variable into a static.
  12. Usually you dont make it static... you pass it to the constructor. Static should not be your first go on this.
    And if you are really eager on using static than make it a singleton
  13. I have several objects being created into a hashmap (which I initialized outside) in the on enable method. But when I reference those objects elsewhere the values I set in the constructors are gone.

    Nevermind I fixed it.
    #13 greg5423, Apr 18, 2020
    Last edited: Apr 18, 2020