Create automatic Arrays

Discussion in 'Spigot Plugin Development' started by cadox8, Jul 9, 2015.

  1. The thing I want to do is that when I create a arena, set the limit of the players in the arena up to 2. I have a command to create infinite Arenas, so I want an Arrays for each arena to limit the players. Is It posibble?
  2. Use an ArrayList. They're automatically sized. If you need to then use an array (for whatever reason), then you can easily use the toArray() operator.
  3. can you explain it with code, please?
  4. Code (Text):
    ArrayList<E> arryList = new ArrayList<>(Collection<? extends E> c);
    Code (Text):
    Player[] players = new Player[2];
    Would become:
    Code (Text):
    ArrayList<Player> players = new ArrayList<>();
    If you need to add a player to the list, just use:
    Code (Text):
    You should note though that you should not store the players directly. If the players log off the server then the server will not properly clean up everything.

    For a safe way of storing the players, store an ArrayList of the UUIDs of the players instead, storing offline players would probably work too.

    Also, check out the documentation for ArrayList here:
    You can find handy methods such as contains, remove, removeAll, addAll, clear, isEmtpy and more.
    • Like Like x 1
  5. Yeah, thanks, but the thing I'm trying create is an automatic ArrayList.
    I run a command and It create a file with the location of a place.
    Then the plugin, must create an ArrayList with the name of the arena to prevent join more than 2 players.
    Next, if the arena is full (checking the Array), stop joining players until one player left the arena
  6. Just let players join, put them in arraylist/hashmap check if size == 2, prevent joining
  7. joehot200


    What, so you want a HashMap of the arena, which then returns presumably an instance of an Arena class?
  8. Mmmm no exactly, I want a code that create Arrays of players joining in Arrays of Arenas (created by code automatic. An Array for each arena)
  9. Do the arenas have ids? Presuming they do, here's an example:
    HashMap<Integer, ArrayList<UUID>> players = HashMap<Integer, ArrayList<UUID>>();

    When you create an arena, add an empty list to the hashmap:
    players.put(arenaId, new ArrayList<UUID>());

    When a player joins:

    When a player leaves:

    Check how many players are in an arena:
    int n = players.get(arenaId).size();
  10. I put your code, but now, I have this problem:
    [21:21:29] [Server thread/INFO]: cadox8 issued server command: /next
    [21:21:29] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'next' in plugin MineRoulette v1.1.3
        at org.bukkit.command.PluginCommand.execute( ~[craftbukkit.jar:git-Spigot-b73910a-726656b]
        at org.bukkit.command.SimpleCommandMap.dispatch( ~[craftbukkit.jar:git-Spigot-b73910a-726656b]
        at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchCommand( ~[craftbukkit.jar:git-Spigot-b73910a-726656b]
        at net.minecraft.server.v1_8_R1.PlayerConnection.handleCommand( [craftbukkit.jar:git-Spigot-b73910a-726656b]
        at net.minecraft.server.v1_8_R1.PlayerConnection.a( [craftbukkit.jar:git-Spigot-b73910a-726656b]
        at net.minecraft.server.v1_8_R1.PacketPlayInChat.a( [craftbukkit.jar:git-Spigot-b73910a-726656b]
        at net.minecraft.server.v1_8_R1.PacketPlayInChat.a( [craftbukkit.jar:git-Spigot-b73910a-726656b]
        at [craftbukkit.jar:git-Spigot-b73910a-726656b]
        at java.util.concurrent.Executors$ Source) [?:1.8.0_45]
        at Source) [?:1.8.0_45]
        at net.minecraft.server.v1_8_R1.MinecraftServer.z( [craftbukkit.jar:git-Spigot-b73910a-726656b]
        at net.minecraft.server.v1_8_R1.DedicatedServer.z( [craftbukkit.jar:git-Spigot-b73910a-726656b]
        at net.minecraft.server.v1_8_R1.MinecraftServer.y( [craftbukkit.jar:git-Spigot-b73910a-726656b]
        at [craftbukkit.jar:git-Spigot-b73910a-726656b]
        at Source) [?:1.8.0_45]
    Caused by: java.lang.NullPointerException
        at ~[?:?]
        at ~[?:?]
        at org.bukkit.command.PluginCommand.execute( ~[craftbukkit.jar:git-Spigot-b73910a-726656b]
        ... 14 more

    My Code:
        public static void joinPlayer(Player p){

         (line 32)   int n = Rooms.players.get(Rooms.getTPRoom()).size();
            File file = new File("plugins//MineRoulette//Rooms//Room_" + Rooms.getTPRoom() + ".yml");

            if(n == 2){
            } else {

        public static int countRooms(){
            int d = Main.cfg.getInt("Plugin.CountRooms");
            int a = d + 1;
            return a;

        public static int getTPRoom(){
            Random r = new Random();
            int t = r.nextInt(Rooms.countRooms());
            return t;

    Some Help, please???