New to programming, need a little help.

Discussion in 'Plugin Development' started by HeadGam3z, Jan 29, 2014.

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

    HeadGam3z

    Hey there. So like the title says, I'm new to programming. But I always thought programming was so cool! So I finally decided to give it a try, and I like it so far; but I'm having a little problem.

    I'm trying to make a plugin for my friend's server, and it was going "OK", until I tried to make a reload command for the plugin. I'll post the code to the plugin and the error below:

    Code
    Code:java
    1. package yolo.HeadGam3z.PBB;
    2.  
    3. import java.io.File;
    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.configuration.file.FileConfiguration;
    10. import org.bukkit.configuration.file.YamlConfiguration;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13.  
    14. public class MainClass extends JavaPlugin{
    15.  
    16. FileConfiguration config;
    17. File cfile;
    18.  
    19. public void onEnabled(){
    20. config = getConfig();
    21. config.options().copyDefaults(true);
    22. saveConfig();
    23. cfile = new File(getDataFolder(), "config.yml");
    24. getLogger().info("PBB has been successfully enabled!");
    25. }
    26.  
    27. public void onDisabled(){
    28. getLogger().info("PBB has been successfully disabled!");
    29. }
    30.  
    31. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    32. Player trolol = (Player)sender;
    33. if (cmd.getName().equalsIgnoreCase("pbb")){
    34. if (((Player) sender).hasPermission("pavilion.pbb")){
    35. Bukkit.broadcastMessage(config.getString("pavilion") + ChatColor.GREEN + " I see a lot of Members who haven't got their FREE Builder rank yet! > " + ChatColor.AQUA + "<Edit by Moderator: Redacted bit url>
    36. }
    37. }
    38. if (cmd.getName().equalsIgnoreCase("preload")){
    39. if (((Player) sender).hasPermission("pavilion.reload")){
    40. config = YamlConfiguration.loadConfiguration(cfile);
    41. trolol.sendMessage("PavilionPlugin configuration reloaded!");
    42. }
    43. }
    44. return false;
    45. }
    46. }
    47.  


    Error
    Code:
    org.bukkit.command.CommandException: Unhandled exception executing command 'pbb' in plugin PBB v1.1
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[bukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:196) ~[bukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchCommand(CraftServer.java:542) ~[bukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.PlayerConnection.handleCommand(PlayerConnection.java:932) [bukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java:814) [bukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.PacketPlayInChat.a(PacketPlayInChat.java:28) [bukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.PacketPlayInChat.handle(PacketPlayInChat.java:47) [bukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146) [bukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [bukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:655) [bukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:250) [bukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:545) [bukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:457) [bukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [bukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    Caused by: java.lang.NullPointerException
        at yolo.HeadGam3z.PBB.MainClass.onCommand(MainClass.java:35) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[bukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        ... 13 more
    Here's my plugin.yml and config.yml if you need it:

    Plugin.yml
    Code:
    name: PBB
    author: HeadGam3z
    version: 1.1
    description: Broadcasts an integrated message
    main: yolo.HeadGam3z.PBB.MainClass
    commands:
      pbb:
        description: Broadcasts an integrated message.
      preload:
        description: Reloads plugin.
    permissions:
      pavilion.pbb:
        description: Nu, no desc.
      pavilion.reload:
        description: Reloads PBB.
    
    Config.yml
    Code:
    pavilion: &8[&bThe Pavilion&8]




    Any help would be awesome! Thanks guys, and bare with me as I'm new to this; haha.
     
    Last edited by a moderator: Feb 14, 2017
  2. Offline

    Mmarz11

    HeadGam3z Change line 19 to public void onEnable() and line 27 to public void onDisable(). onEnabled() and onDisabled() are not methods of JavaPlugin and are never called which results in your config never getting set.
     
  3. Offline

    HeadGam3z

    On like 19, it is public void onEnabled(); and on line 27 it is public void onDisabled(). Am I misunderstanding something? But thanks for the help! :)
     
  4. Offline

    Mmarz11


    Remove the d on the end.
     
  5. Offline

    HeadGam3z

    Ohhhh! Haha, thanks; it worked! But when I try to add color in the config.yml file - like "&2test", - it outputs "null" when I try to run the /pbb command.

    Any idea how to allow color in the config.yml file?
     
  6. Offline

    Mmarz11


    I've never tried but another thread suggested:

    ChatColor.translateAlternateColorCodes('&', getConfig().getString("path.to.string"))

    Replace "path.to.string" with the correct path to the string in your config.
     
  7. Offline

    itzrobotix

    Using & to add colour isn't a built in method. You need to check it yourself. Check it the string in the config.yml contains &1 for example then .replace("&1", ChatColor.DARK_BLUE);
    I think that is how you do it.
     
  8. Offline

    HeadGam3z

    How would I add
    Code:java
    1. ChatColor.translateAlternateColorCodes('&', getConfig().getString("pavilion"))

    to
    Code:java
    1. Bukkit.broadcastMessage(config.getString("pavilion"));


    Maybe this?
    Code:java
    1. Bukkit.broadcastMessage(config.getString("pavilion") + ChatColor.translateAlternateColorCodes('&', getConfig().getString("pavilion"))
     
  9. Offline

    Arcoz

    PHP:
    Bukkit.broadcastMessage(config.getString("pavilion").replaceAll("&""§");
     
  10. Offline

    mattibijnens

    I am also new but for color codes:
    Code:java
    1. player.sendMessage(ChatColor.GREEN + "Hope this helps!");
     
  11. Offline

    Arcoz

  12. Offline

    HeadGam3z

    Arcoz
    I added that to my plugin, and it came back with this error when I used the /pbb command after /preload
    Code:
    [12:34:08] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'pbb' in plugin PBB v1.1
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[bukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:196) ~[bukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchCommand(CraftServer.java:542) ~[bukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.PlayerConnection.handleCommand(PlayerConnection.java:932) [bukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java:814) [bukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.PacketPlayInChat.a(PacketPlayInChat.java:28) [bukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.PacketPlayInChat.handle(PacketPlayInChat.java:47) [bukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146) [bukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [bukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:655) [bukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:250) [bukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:545) [bukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:457) [bukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [bukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    Caused by: java.lang.NullPointerException
        at yolo.HeadGam3z.PBB.MainClass.onCommand(MainClass.java:35) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[bukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        ... 13 more
    
     
  13. Offline

    Arcoz

    Could you show me your current code?
    Also your prereload isn't doing anything but getting a new "string".
     
  14. Offline

    HeadGam3z

    Arcoz
    Code:java
    1. package yolo.HeadGam3z.PBB;
    2.  
    3. import java.io.File;
    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.configuration.file.FileConfiguration;
    10. import org.bukkit.configuration.file.YamlConfiguration;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13.  
    14. public class MainClass extends JavaPlugin{
    15.  
    16. FileConfiguration config;
    17. File cfile;
    18.  
    19. public void onEnable(){
    20. config = getConfig();
    21. config.options().copyDefaults(true);
    22. saveConfig();
    23. cfile = new File(getDataFolder(), "config.yml");
    24. getLogger().info("PBB has been successfully enabled!");
    25. }
    26.  
    27. public void onDisable(){
    28. getLogger().info("PBB has been successfully disabled!");
    29. }
    30.  
    31. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    32. Player trolol = (Player)sender;
    33. if (cmd.getName().equalsIgnoreCase("pbb")){
    34. if (((Player) sender).hasPermission("pavilion.pbb")){
    35. Bukkit.broadcastMessage(config.getString("pavilion").replaceAll("&", "§") + ChatColor.GREEN + " I see a lot of Members who haven't got their FREE Builder rank yet! > " + ChatColor.AQUA + "<Edit by Moderator: Redacted bit url>
    36. }
    37. }
    38. if (cmd.getName().equalsIgnoreCase("preload")){
    39. if (((Player) sender).hasPermission("pavilion.reload")){
    40. config = YamlConfiguration.loadConfiguration(cfile);
    41. trolol.sendMessage("PavilionPlugin configuration reloaded!");
    42. }
    43. }
    44. return false;
    45. }
    46. }
     
    Last edited by a moderator: Feb 14, 2017
  15. Offline

    Arcoz

    HeadGam3z
    As said, your preload command doesn't do anything.

    Instead of using config = config = YamlConfiguration.loadConfiguration(cfile);
    Use:
    reloadConfig();
    Since this is just your normal config.yml
     
  16. Offline

    HeadGam3z

    Arcoz
    So I can take the cfile = new File(getDataFolder(), "config.yml"); out of my onEnable() method?
     
  17. Offline

    afistofirony

    HeadGam3z Maybe try removing the config field and just using getConfig() instead? You may be running into concurrency issues (i.e. your config field is not updated, but the JavaPlugin field for the configuration is):

    Code:
    Bukkit.broadcastMessage(getConfig().getString("pavilion") ...);
     
  18. Offline

    Arcoz

    HeadGam3z as afistofirony said.
    Try using getConfig() instead of config.

    And yes, you can remove that from your onEnable() method
    I don't know why you would have
    PHP:
    cfile = new File(getDataFolder(), "config.yml"); 
    In the first place. Since you're not using your
    Code:
    cfile
    string anywhere in the whole plugin
     
  19. Offline

    HeadGam3z

    afistofirony
    Arcoz
    Here's my current code:
    Code:java
    1. package yolo.HeadGam3z.PBB;
    2.  
    3. import java.io.File;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.configuration.file.FileConfiguration;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public class MainClass extends JavaPlugin{
    13.  
    14. FileConfiguration config;
    15. File cfile;
    16.  
    17. public void onEnable(){
    18. getConfig().options().copyDefaults(true);
    19. saveConfig();
    20. getLogger().info("PBB has been successfully enabled!");
    21. }
    22.  
    23. public void onDisable(){
    24. getLogger().info("PBB has been successfully disabled!");
    25. }
    26.  
    27. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    28. Player trolol = (Player)sender;
    29. if (cmd.getName().equalsIgnoreCase("pbb")){
    30. if (((Player) sender).hasPermission("pavilion.pbb")){
    31. Bukkit.broadcastMessage(getConfig().getString("pavilion"));
    32. }
    33. }
    34. if (cmd.getName().equalsIgnoreCase("preload")){
    35. if (((Player) sender).hasPermission("pavilion.reload")){
    36. reloadConfig();
    37. trolol.sendMessage("PavilionPlugin configuration reloaded!");
    38. }
    39. }
    40. return false;
    41. }
    42. }

    I'm not getting any errors anymore, but it's not saving my config.yml changes after I use /preload.
    (take your time replying, as I have to leave now and won't be back for 2-3 hours.)
     
  20. Offline

    Arcoz

    HeadGam3z


    Code:
    package yolo.HeadGam3z.PBB;
     
     
    import java.util.logging.Logger;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class MainClass extends JavaPlugin{
          public final Logger logger = Logger.getLogger("Minecraft");
     
          public void onEnable(){
            getConfig().options().copyDefaults(true);
            saveDefaultConfig();
            logger.info("PBB has been successfully enabled!");
        }
     
        public void onDisable(){
            logger.info("PBB has been successfully disabled!");
            saveDefaultConfig();
        }
     
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("pbb")){
                if (sender.hasPermission("pavilion.pbb")){
                    Bukkit.broadcastMessage(getConfig().getString("pavilion").replaceAll("&", "§") + ChatColor.GREEN + " I see a lot of Members who haven't got their FREE Builder rank yet! > " + ChatColor.AQUA + "<Edit by Moderator: Redacted bit url>
                    return true;
                }
            }
            if (cmd.getName().equalsIgnoreCase("preload")){
                if (sender.hasPermission("pavilion.reload")){
                    reloadConfig();
                    sender.sendMessage("PavilionPlugin configuration reloaded!");
                    return true;
                }
            }
            return false;
        }
    }
    I've fixed it for you... You dont always need to use ((Player ) sender, since this just removes console support.
    Also make sure your config file looks like this
    [​IMG]
    Your string is ofc optional so inside of the ' ' you can have anything

    Now you will receive this message ingame:
    [​IMG]
     
    Last edited by a moderator: Feb 14, 2017
    HeadGam3z likes this.
  21. Offline

    HeadGam3z

    afistofirony Arcoz
    Thanks for all of your help guys. Just one more thing though... lol I tried editing it a bit so I can configure the message without re-coding the plugin every time I'd like to change the message; and I came up with this:
    PHP:
    package yolo.HeadGam3z.PavilionPlugin;
     
     
    import java.util.logging.Logger;
     
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class 
    MainClass extends JavaPlugin{
          public final 
    Logger logger Logger.getLogger("Minecraft");
     
          public 
    void onEnable(){
            
    getConfig().options().copyDefaults(true);
            
    saveDefaultConfig();
            
    logger.info("PavilionPlugin has been successfully enabled!");
        }
     
        public 
    void onDisable(){
            
    logger.info("PavilionPlugin has been successfully disabled!");
            
    saveDefaultConfig();
        }
     
        public 
    boolean onCommand(CommandSender senderCommand cmdString labelString[] args) {
            if (
    cmd.getName().equalsIgnoreCase("pbb")){
                if (
    sender.hasPermission("pavilion.builder.broadcast")){
                    
    Bukkit.broadcastMessage(getConfig().getString("prefix").replaceAll("&""§") + " " getConfig().getString("broadcasted_message").replaceAll("&""§") + " " getConfig().getString("link_to_application").replaceAll("&""§"));
                    return 
    true;
                }
            }
            if (
    cmd.getName().equalsIgnoreCase("preload")){
                if (
    sender.hasPermission("pavilion.reload")){
                    
    reloadConfig();
                    
    sender.sendMessage("PavilionPlugin sucessfully configuration reloaded!");
                    return 
    true;
                }
            }
            return 
    false;
        }
    }
    And got this back:
    PHP:
    Cannot load plugins\PavilionPlugin\config.yml
    org
    .bukkit.configuration.InvalidConfigurationException: while parsing a block mapping
    in 
    "<string>"line 1column 1:
        
    prefix'&8[&bThe Pavilion&8]'
        
    ^
    expected <block end>, but found Scalar
    in 
    "<string>"line 2column 58:
        ...  
    see a lot of Members who haven't got their FREE Builder rank yet!'
                                            
    ^
    Now, I understand the word "haven't" would mess up the configuration file, but is there anyway I could use words with apostrophes?
     
  22. Offline

    Technes

    HeadGam3z

    You can either use different quotes on either end of a string, i.e.

    Code:JAVA
    1. "This is a string with an apostrophe in it, which works because it uses different quotes on either end! There's an apostrophe here."


    Or alternatively, you can escape it using a backslash, used as follows:

    Code:JAVA
    1. 'Well, there\'s something fishy going on here. But it works.'
     
  23. Offline

    L33m4n123

    Never tried it but try to mask them with a \ so basicly haven\'t


    AFAIK in yaml-files ALL strings have to be marked with double quotes since it looks for a char if you use single quotes so you prolly have to even escape it in a String with ""

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

    HeadGam3z

    Tried that, got this:
    Code:
    Cannot load configuration from stream
    org.bukkit.configuration.InvalidConfigurationException: while parsing a block mapping
    in "<string>", line 1, column 1:
        prefix: '&8[&bThe Pavilion&8]'
        ^
    expected <block end>, but found Scalar
    in "<string>", line 2, column 59:
        ... see a lot of Members who haven\'t got their FREE Builder rank yet!'
                                            ^
     
  25. Offline

    Garris0n

    No, for a few reasons.
    1. There's no regex in there, so you can just use .replace() instead.
    2. That will replace all ampersands, while you only want to replace them when a number is behind them. Amusingly enough that would require regex which would in turn require .replaceAll(), but both of these are moot points because...
    3. You should use ChatColor.translateAlternateColorCodes() instead. Always use the API when you can.
     
  26. Offline

    HeadGam3z

    And with that being said, would I use it like this:
    Code:java
    1. Bukkit.broadcastMessage(getConfig().getString("prefix").ChatColor.translateAlternateColorCodes()
     
  27. Offline

    Garris0n

    ChatColor.translateAlternateColorCodes(character to translate, string) returns the translated string.
     
  28. Offline

    HeadGam3z

    Garris0n
    Could I use more than one string at a time, or would I have to use ChatColor.translateAlternateColorCodes() again?
     
  29. Offline

    Garris0n

    I'm not sure what you're asking, but you could concatenate them...
     
  30. Offline

    L33m4n123


    You cannot mark strings in your config.yml with ' ' yet you do it here

    prefix: '&8[&bThe Pavilion&8]'

    You have to surround Strings with " "
     
Thread Status:
Not open for further replies.

Share This Page