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);
    So:
    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):
    players.add(youPlayer);
    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:
    http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
    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.
    Explain:
    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

    Supporter

    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:
    players.get(arenaId).add(player.getUniqueId());

    When a player leaves:
    players.get(arenaId).remove(player.getUniqueId());

    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:
    HTML:
    [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(PluginCommand.java:46) ~[craftbukkit.jar:git-Spigot-b73910a-726656b]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[craftbukkit.jar:git-Spigot-b73910a-726656b]
        at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchCommand(CraftServer.java:646) ~[craftbukkit.jar:git-Spigot-b73910a-726656b]
        at net.minecraft.server.v1_8_R1.PlayerConnection.handleCommand(PlayerConnection.java:1115) [craftbukkit.jar:git-Spigot-b73910a-726656b]
        at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java:950) [craftbukkit.jar:git-Spigot-b73910a-726656b]
        at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java:26) [craftbukkit.jar:git-Spigot-b73910a-726656b]
        at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java:53) [craftbukkit.jar:git-Spigot-b73910a-726656b]
        at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [craftbukkit.jar:git-Spigot-b73910a-726656b]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_45]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_45]
        at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:696) [craftbukkit.jar:git-Spigot-b73910a-726656b]
        at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:316) [craftbukkit.jar:git-Spigot-b73910a-726656b]
        at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:634) [craftbukkit.jar:git-Spigot-b73910a-726656b]
        at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:537) [craftbukkit.jar:git-Spigot-b73910a-726656b]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_45]
    Caused by: java.lang.NullPointerException
        at com.gmail.cadox8.mr.room.RoomTP.joinPlayer(RoomTP.java:32) ~[?:?]
        at com.gmail.cadox8.mr.cmd.Next.onCommand(Next.java:28) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit.jar:git-Spigot-b73910a-726656b]
        ... 14 more

    My Code:
    PHP:
        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){
                return;
            } else {

    PHP:
        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???