Solved Why isn't it working??

Discussion in 'Plugin Development' started by Condolent, Jul 31, 2015.

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

    Condolent

    Did something like this on another plugin like a year ago, and now when I recently started playing around in bukkit again I tried making a automated messaging-system.

    This is the code for it:
    Code:
        public void autoBroadcast() {
            final List<String> messages = getConfig().getStringList("auto_msg");
         
    Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
             
                public void run() {Bukkit.broadcastMessage(Arrays.asList(messages).get(new Random().nextInt(messages.length)));
                }
             
            }, 0, getConfig().getInt("auto_interval") * 20);
         
        }
    But it's not working and it's giving me an error on the codes, no idea why (I'm pretty sure this should work if I haven't made some mistake by accident :S)

    Type safety: A generic array of List<String> is created for a varargs parameter is the error I get under Arrays.asList(messages)

    length cannot be resolved or is not a field is what I get on the .length in the end
     
  2. Offline

    shades161

    try .size, that returns an integer of how big the arraylist is, or how many objects are in it.
     
  3. Offline

    Condolent

    The method broadcastMessage(String) in the type Bukkit is not applicable for the arguments (int) is what I get now on BroadcastMessage :/
     
  4. Offline

    shades161

    Oh, I was thinking, it may be more lines of code but, add all of the messages to an arraylist, then choose a random integer that is within the size of that array list (random nextInt() will generate a number between 0 and the max number you selected, so be careful of that). And then get the message from the arraylist using that integer with listName.get(Int) and then, since that is a string, broadcast that message.
     
  5. Offline

    Condolent

    @shades161
    Code:
    Bukkit.broadcastMessage(Arrays.asList(messages)).get(new Random().nextInt(messages.size()));
    this what you mean??

    The method broadcastMessage(String) in the type Bukkit is not applicable for the arguments (List<List<String>>)
    is the error now :/

    I feel like the Arrays.asList shouldn't be there, and that being the cause of the error, but I don't know how to do it without a array-list

    <Edited by bwfcwalshy: Merged posts, please use the edit button rather than triple posting.>
     
    Last edited by a moderator: Jul 31, 2015
  6. You are right. Directly use messages.get(random index)

    Or even better: Shuffle the list and then display every string so it does not select the same message over and over because of the random. When all messages are displayed you can optionally shuffle the list again:
    Code:
    List<String> messages = ...;
    
    //start scheduler
    int i = messages.size();
    public void run() {
    
    if (i >= messages.size()) {
    i = 0;
    //OPTIONAL: Collections.shuffle(messages);
    }
    //broadcast messages.get(i)
    i++;
    }
     
    Last edited: Jul 31, 2015
  7. Offline

    Condolent

    Code:
        public void autoBroadcast() {
            final List<String> messages = getConfig().getStringList("auto_msg");
         
            Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
             
                int i = messages.size();
                public void run() {
                 
                    Bukkit.broadcastMessage(messages.get(i));
                }
             
            }, 0, getConfig().getInt("auto_interval") * 20);
         
        }
    And I get the stacktrace error:
    Code:
    [17:10:46 WARN]: [Condolent] Task #14 for Condolent v1.0 generated an exception
    java.lang.IndexOutOfBoundsException: Index: 3, Size: 3
            at java.util.ArrayList.rangeCheck(Unknown Source) ~[?:1.8.0_45]
            at java.util.ArrayList.get(Unknown Source) ~[?:1.8.0_45]
            at me.condolent.ServerMain$1.run(ServerMain.java:125) ~[?:?]
            at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftTask.run(CraftTask.java
    :71) ~[lel.jar:git-Spigot-6d16e64-bf4818b]
            at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftScheduler.mainThreadHea
    rtbeat(CraftScheduler.java:350) [lel.jar:git-Spigot-6d16e64-bf4818b]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:7
    22) [lel.jar:git-Spigot-6d16e64-bf4818b]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:3
    74) [lel.jar:git-Spigot-6d16e64-bf4818b]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:6
    53) [lel.jar:git-Spigot-6d16e64-bf4818b]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java
    :556) [lel.jar:git-Spigot-6d16e64-bf4818b]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_45]
     
  8. You forgot the most important part:
    And it's recommend that you actually THINK BY YOURSELF how to solve this problem. A good recommendation is to increment i after displaying the message
     
  9. Offline

    Condolent

    How is this not working? I don't get what's wrong ._.
    Code:
        public void autoBroadcast() {
            final List<String> messages = getConfig().getStringList("auto_msg");
          
            Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
              
                int i = messages.size();
                public void run() {
                  
                    if(i >= messages.size()) {
                        i = 0;
                        Collections.shuffle(messages);
                    }
                  
                    Bukkit.broadcastMessage(messages.get(new Random().nextInt(i)));
                }
              
            }, 0, getConfig().getInt("auto_interval") * 20);
          
        }
    Just stacktrace errors now, saying bound must be positive
     
  10. remove the random part and after broadcasting it increment i
     
  11. Offline

    Condolent

    @FisheyLP but without the random-part it just gives me the same line over and over again until I reload the server, then it swaps
     
  12. Offline

    PDKnight

    I've repaired your code (I tested it, it should work :)):
    Code:java
    1. public void autoBroadcast() {
    2. final List<String> messages = getConfig().getStringList("auto_msg");
    3. Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    4. public void run() {
    5. Bukkit.broadcastMessage(messages.get(new Random().nextInt(messages.size())));
    6. }
    7. }, 0, getConfig().getInt("auto_interval") * 20);
    8. }
     
  13. Thats because you NEVER INCREMENT THE INDEX LIKE I SAID 3-4 TIMES /.- argh
     
  14. Offline

    Condolent

    @FisheyLP lol missed that hahaha.

    For anyone wanting the whole code:
    Code:
        public void autoBroadcast() {
            final List<String> messages = getConfig().getStringList("auto_msg");
           
            Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
               
                int i = messages.size();
                public void run() {
                   
                    if(i >= messages.size()) {
                        i = 0;
                        Collections.shuffle(messages);
                    }
                   
                    Bukkit.broadcastMessage(ChatColor.GREEN + "§l[TIP] §r" + messages.get(i));
                    i++;
                }
               
            }, 0, getConfig().getInt("auto_interval") * 20);
           
        }
     
  15. Offline

    PDKnight

    @Condolent Try to use repaired code I posted, it's faster anyway (and compact) ;)
     
Thread Status:
Not open for further replies.

Share This Page