Iterating through messages

Discussion in 'Plugin Development' started by Chibbey, Jan 17, 2014.

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

    Chibbey

    Hello, so I am trying to make a broadcasting plugin where It broadcasts messages listed in the config.
    And I think iterating means looping through messages so goes through the list and says them. So I have a list named messages only thing is. It doesn't broadcast it. heres the code
    Code:java
    1. package me.chibbey.broadcaster;
    2.  
    3. import java.util.List;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class Broadcaster extends JavaPlugin implements Listener {
    14.  
    15. @SuppressWarnings("unchecked")
    16. List<String> messages = (List<String>) getConfig().getList("messages");
    17.  
    18. public void onEnable() {
    19. getConfig().options().copyDefaults(true);
    20. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    21. public void run() {
    22. Bukkit.getServer().broadcastMessage(ChatColor.GRAY + "[" + ChatColor.AQUA + "FrostBiteMC" + ChatColor.GRAY + "] " + ChatColor.GOLD + messages.iterator());
    23. }
    24. }, 20 * getConfig().getInt("serverstart"), 20 * getConfig().getInt("time"));
    25. }
    26.  
    27. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    28.  
    29. Player p = (Player) sender;
    30.  
    31. if (cmd.getName().equalsIgnoreCase("addmessage")) {
    32. if (!p.hasPermission("broadcaster.add")) {
    33. p.sendMessage(ChatColor.RED + "You do not have permission!");
    34. return true;
    35. }
    36. if (p.hasPermission("broadcaster.add")) {
    37. if (args.length == 0) {
    38. p.sendMessage(ChatColor.RED + "Please specify a message!");
    39. return true;
    40. }
    41. if (args.length == 1) {
    42. messages.add(args[0]);
    43. saveConfig();
    44. reloadConfig();
    45. return true;
    46. }
    47. }
    48. }
    49. return true;
    50. }
    51. }
    52.  

    The config.
    Code:
    messages:
        - 123
        - 321
        - 213
    #Time - Measured in seconds!
     
    #When Server starts
    serverstart: 5
     
    #Between messages
    time: 10
    So it doesn't broadcast the messages. I was also wondering if the time will work. With the getConfig().getInt() and then it gets the times in the config. Please help me! heres what the console says.
    Code:
    2014-01-17 21:47:50 [SEVERE] Could not load 'plugins\Broadcaster.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.IllegalArgumentException: File cannot be null
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:182)
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:306)
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:230)
        at org.bukkit.craftbukkit.v1_6_R3.CraftServer.loadPlugins(CraftServer.java:245)
        at net.minecraft.server.v1_6_R3.DedicatedServer.init(DedicatedServer.java:118)
        at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:409)
        at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)
    Caused by: java.lang.IllegalArgumentException: File cannot be null
        at org.apache.commons.lang.Validate.notNull(Validate.java:192)
        at org.bukkit.configuration.file.YamlConfiguration.loadConfiguration(YamlConfiguration.java:170)
        at org.bukkit.plugin.java.JavaPlugin.reloadConfig(JavaPlugin.java:117)
        at org.bukkit.plugin.java.JavaPlugin.getConfig(JavaPlugin.java:111)
        at me.chibbey.broadcaster.Broadcaster.<init>(Broadcaster.java:16)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:178)
        ... 6 more
    And yea please help!
     
  2. Offline

    xTrollxDudex

    Chibbey
    You need to instantiate your field after you create the file in your onEnable. You should actually be using saveDefaultConfig() by the way.
     
  3. Offline

    Chibbey

    xTrollxDudex
    Sorry I don't really know what you mean by instantiate the field. I don't really know all the fancy words. Even though its probably not the fancy. Do you just mean change it to saveDefaultConfig() ?
     
  4. Offline

    jacklin213

    yes change it :D
     
  5. Offline

    Sagacious_Zed Bukkit Docs

    Instantiate, to create a realized instance.
    as defined by wikipedia http://en.wikipedia.org/wiki/Instance_(computer_science)

    However, this statement
    Code:
    List<String> messages = (List<String>) getConfig().getList("messages");/code]
    is really an assignment, because of the assignment operator (=)
    
    You should assign the the value in onEnable, but leave the declaration in the body.
     
  6. Offline

    xTrollxDudex

    Chibbey
    Yes, as well as doing this:
    PHP:
    List<Stringmessages;

    public 
    void onEnable() {
        
    saveDefaultConfig();
        
    messages getConfig().getStringList("messages");
        
    // Other stuff
    }
    // Other stuff
     
  7. Offline

    Chibbey

    xTrollxDudex
    Ok, sorry for late reply :( . Im trying now! also why do you do all your code in PHP? :p

    Ok... Its working now kinda but its now broadcasting a message like
    Code:java
    1. [FrostBiteMC] ArrayList.<RandomStuff>

    so its messing up on the iterator part :( . My code is now like this.
    Code:java
    1. package me.chibbey.broadcaster;
    2.  
    3. import java.util.List;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class Broadcaster extends JavaPlugin implements Listener {
    14.  
    15. List<String> messages;
    16.  
    17. public void onEnable() {
    18. saveDefaultConfig();
    19. messages = getConfig().getStringList("messages");
    20. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    21. public void run() {
    22. Bukkit.getServer().broadcastMessage(ChatColor.GRAY + "[" + ChatColor.AQUA + "FrostBiteMC" + ChatColor.GRAY + "] " + ChatColor.GOLD + messages.iterator());
    23. }
    24. }, 20 * getConfig().getInt("serverstart"), 20 * getConfig().getInt("time"));
    25. }
    26.  
    27. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    28.  
    29. Player p = (Player) sender;
    30.  
    31. if (cmd.getName().equalsIgnoreCase("addmessage")) {
    32. if (!p.hasPermission("broadcaster.add")) {
    33. p.sendMessage(ChatColor.RED + "You do not have permission!");
    34. return true;
    35. }
    36. if (p.hasPermission("broadcaster.add")) {
    37. if (args.length == 0) {
    38. p.sendMessage(ChatColor.RED + "Please specify a message!");
    39. return true;
    40. }
    41. if (args.length == 1) {
    42. messages.add(args[0]);
    43. saveConfig();
    44. reloadConfig();
    45. return true;
    46. }
    47. }
    48. }
    49. return true;
    50. }
    51. }

    Help please. :D

    Also the addmessage isn't working if you could figure this out.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  8. Offline

    xTrollxDudex

    Chibbey
    You need to re-assign the ArrayList again after you add the message.

    I use PHP code because it looks nicer than the dark background on syntax tags.

    You should do this inside your runnable:
    PHP:
    int random = new Random().nextInt(messages.size 1);
    Bukkit.getServer().broadcastMessage(/* Prefix and colors */ messages.get(random));
     
  9. Offline

    Chibbey

    xTrollxDudex
    Ok so now its messaging the 123 but not the 321 and the 213
     
Thread Status:
Not open for further replies.

Share This Page