Solved Config.yml help

Discussion in 'Plugin Help/Development/Requests' started by redtsch, May 26, 2016.

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

    redtsch

    Hey Bukkit.

    I'm starting to work on a config.yml for my latest project (i plan on posting this on these forums =P). I figured it would be good to start before i get to deep into coding the actual plugin itself.

    So, right now I've made that start of my config.yml:
    config.yml:
    Code:
    # -------------------------------------------------------------------------------- #
    # This is the config.yml file for EzEquipment.                    #
    # -------------------------------------------------------------------------------- #
    
    EzEquipment-Configuration:
      cmdInvalidSender: §4[Ez]: §cYou must be a player to use this command§4.
    and here is my command:
    CmdEz:
    Code:
    public class CmdEz implements CommandExecutor {
      
        EzCore core = EzCore.getInstance();
      
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (!(sender instanceof Player)) {
                sender.sendMessage(core.getConfig().getString("EzEquipment-Configuration.cmdInvalidSender"));
                return true;
            }
            return false;
        }
    }
    
    lastly here is my trace, there is a nullPointerException that i can figure out at CmdEz line: 16.
    trace:
    Code:
    >ez
    [11:13:11 WARN]: Unexpected exception while parsing console command "ez"
    org.bukkit.command.CommandException: Unhandled exception executing command 'ez' in plugin EzEquipment v1.0.0a
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[bukkit.jar:git-Bukkit-f326992]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) ~[bukkit.jar:git-Bukkit-f326992]
        at org.bukkit.craftbukkit.v1_9_R1.CraftServer.dispatchCommand(CraftServer.java:624) ~[bukkit.jar:git-Bukkit-f326992]
        at org.bukkit.craftbukkit.v1_9_R1.CraftServer.dispatchServerCommand(CraftServer.java:610) [bukkit.jar:git-Bukkit-f326992]
        at net.minecraft.server.v1_9_R1.DedicatedServer.aL(DedicatedServer.java:398) [bukkit.jar:git-Bukkit-f326992]
        at net.minecraft.server.v1_9_R1.DedicatedServer.D(DedicatedServer.java:362) [bukkit.jar:git-Bukkit-f326992]
        at net.minecraft.server.v1_9_R1.MinecraftServer.C(MinecraftServer.java:635) [bukkit.jar:git-Bukkit-f326992]
        at net.minecraft.server.v1_9_R1.MinecraftServer.run(MinecraftServer.java:539) [bukkit.jar:git-Bukkit-f326992]
        at java.lang.Thread.run(Thread.java:745) [?:1.8.0_60]
    Caused by: java.lang.NullPointerException
        at com.redtsch.EzEpuipment.main.EzCmd.CmdEz.onCommand(CmdEz.java:16) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[bukkit.jar:git-Bukkit-f326992]
        ... 8 more
    >
    
    All help is appreciated, please let me know if you need more info or if i was vague =)
     
  2. Offline

    I Al Istannen

    @redtsch
    In the onCommand, before the sendMessage, print out (just use System.out):
    1. core
    2. core.getConfig().getString(...
    Then see what is null there, maybe fix it and reply :p
     
    redtsch likes this.
  3. Offline

    redtsch

    @I Al Istannen
    Wait what?
    System.out.print(core.getConfig().getString(...); what do I out in place of the ... ?
    Also, I do believe that core is null. I'll have to check but that was my problem last project I was working on. I'll check next time I'm at my laptop.
     
  4. Offline

    I Al Istannen

    @redtsch
    The rest of what you have there. I was too lazy to type it.
    System.out.println(core.getConfig().getString("EzEquipment-Configuration.cmdInvalidSender"));

    EDIT: If you wonder why I said this. It is quite simple. There is a null pointer exception, meaning something was null, that shouldn't. It can't be sender, it can't be getConfig(). What is left is core or the String returned. Hence the two checking messages.
     
  5. Offline

    redtsch

    @I Al Istannen
    Ah ok, I thought you told me to change it! xD
    Thanks for the help, i'll see soon if it works =)

    EDIT:
    Right, so it did not work. In my onEnable() method I put core = this; but I get the same error: NullPointerException at CmdEz, line: 16.
    In other words: sender.sendMessage(core.getConfig().getString("EzEquipment-Configuration.cmdInvalidSender")); <this line
     
    Last edited: May 26, 2016
    I Al Istannen likes this.
  6. Online

    timtower Moderator Moderator

    @redtsch And that is why we use constructors.
    Main class?
     
  7. Offline

    redtsch

    @timtower
    Mind explaining what you mean?
    EzCore:
    Code:
    public class EzCore extends JavaPlugin {
    
        Logger logger = Logger.getLogger("Minecraft");
        PluginDescriptionFile pdf = getDescription();
        String versionId = pdf.getVersion(), plName = pdf.getName();
    
        private static EzCore ezcore;
    
        public static EzCore getInstance() {
            return ezcore;
        }
    
        public void onEnable() {
            ezcore = this;
           
            logger.info("[" + plName + "]" + " Enabled " + plName + " v" + versionId);
           
            registerEvents();
            registerCommands();
        }
       
        public void registerEvents() {
            PluginManager pm = Bukkit.getServer().getPluginManager();
        }
    
        public void registerCommands() {
            getCommand("ez").setExecutor(new CmdEz());
        }
    
        public void onDisable() {
            logger.info("[" + plName + "]" + " Disabled " + plName + " v" + versionId);
        }
    }
    
     
  8. Offline

    I Al Istannen

    @redtsch
    Did you try printing both things out? If yes, did an error occur or what was printed out? Were there any other errors in the console?

    He means, you should pass the instance of the Main class through the constructor in the other classes. It is "cleaner" this way. But honestly, a Singelton for your Main instance is fine.

    I heard you should set instance to null at the end of the onDisable, but I don't really understand why. Only reason I coudl imagine would be a new classloader letting the old instance floating around.
     
  9. Offline

    redtsch

    @I Al Istannen
    Sorry I blanked, both things?
    In my main class, I am using a constructor called getInstance() if you didn't see =P
    huh...
     
  10. Offline

    I Al Istannen

    @redtsch
    That is a method, not a constructor. You would need to add a constructor to the CmdEz class.

    Both ;)
     
  11. Offline

    redtsch

    @I Al Istannen
    Oops! yes it is XD

    EDIT:
    so would you mind explaining how to go about writing said constructor?
     
  12. Offline

    I Al Istannen

    @redtsch
    You don't need one. A singelton is just fine for the main class. Could you now please answer, what the two things printed out? Was there an error?
     
  13. Offline

    redtsch

    @I Al Istannen
    The only thing that printed out was a error.
    Code:
    >ez
    [14:21:21 ERROR]: Cannot load configuration from stream
    org.bukkit.configuration.InvalidConfigurationException: mapping values are not allowed here
    in 'string', line 6, column 25:
          cmdInvalidSender: [Ez]: You must be a player to use th ...
                                ^
    
        at org.bukkit.configuration.file.YamlConfiguration.loadFromString(YamlConfiguration.java:56) ~[bukkit.jar:git-Bukkit-f326992]
        at org.bukkit.configuration.file.FileConfiguration.load(FileConfiguration.java:184) ~[bukkit.jar:git-Bukkit-f326992]
        at org.bukkit.configuration.file.YamlConfiguration.loadConfiguration(YamlConfiguration.java:239) [bukkit.jar:git-Bukkit-f326992]
        at org.bukkit.plugin.java.JavaPlugin.reloadConfig(JavaPlugin.java:195) [bukkit.jar:git-Bukkit-f326992]
        at org.bukkit.plugin.java.JavaPlugin.getConfig(JavaPlugin.java:163) [bukkit.jar:git-Bukkit-f326992]
        at com.redtsch.EzEpuipment.main.EzCmd.CmdEz.onCommand(CmdEz.java:16) [EzEpuipment.jar:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) [bukkit.jar:git-Bukkit-f326992]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) [bukkit.jar:git-Bukkit-f326992]
        at org.bukkit.craftbukkit.v1_9_R1.CraftServer.dispatchCommand(CraftServer.java:624) [bukkit.jar:git-Bukkit-f326992]
        at org.bukkit.craftbukkit.v1_9_R1.CraftServer.dispatchServerCommand(CraftServer.java:610) [bukkit.jar:git-Bukkit-f326992]
        at net.minecraft.server.v1_9_R1.DedicatedServer.aL(DedicatedServer.java:398) [bukkit.jar:git-Bukkit-f326992]
        at net.minecraft.server.v1_9_R1.DedicatedServer.D(DedicatedServer.java:362) [bukkit.jar:git-Bukkit-f326992]
        at net.minecraft.server.v1_9_R1.MinecraftServer.C(MinecraftServer.java:635) [bukkit.jar:git-Bukkit-f326992]
        at net.minecraft.server.v1_9_R1.MinecraftServer.run(MinecraftServer.java:539) [bukkit.jar:git-Bukkit-f326992]
        at java.lang.Thread.run(Thread.java:745) [?:1.8.0_60]
    Caused by: org.yaml.snakeyaml.scanner.ScannerException: mapping values are not allowed here
    in 'string', line 6, column 25:
          cmdInvalidSender: [Ez]: You must be a player to use th ...
                                ^
    
        at org.yaml.snakeyaml.scanner.ScannerImpl.fetchValue(ScannerImpl.java:871) ~[bukkit.jar:git-Bukkit-f326992]
        at org.yaml.snakeyaml.scanner.ScannerImpl.fetchMoreTokens(ScannerImpl.java:360) ~[bukkit.jar:git-Bukkit-f326992]
        at org.yaml.snakeyaml.scanner.ScannerImpl.checkToken(ScannerImpl.java:226) ~[bukkit.jar:git-Bukkit-f326992]
        at org.yaml.snakeyaml.parser.ParserImpl$ParseBlockMappingKey.produce(ParserImpl.java:558) ~[bukkit.jar:git-Bukkit-f326992]
        at org.yaml.snakeyaml.parser.ParserImpl.peekEvent(ParserImpl.java:158) ~[bukkit.jar:git-Bukkit-f326992]
        at org.yaml.snakeyaml.parser.ParserImpl.checkEvent(ParserImpl.java:143) ~[bukkit.jar:git-Bukkit-f326992]
        at org.yaml.snakeyaml.composer.Composer.composeMappingNode(Composer.java:224) ~[bukkit.jar:git-Bukkit-f326992]
        at org.yaml.snakeyaml.composer.Composer.composeNode(Composer.java:155) ~[bukkit.jar:git-Bukkit-f326992]
        at org.yaml.snakeyaml.composer.Composer.composeMappingNode(Composer.java:229) ~[bukkit.jar:git-Bukkit-f326992]
        at org.yaml.snakeyaml.composer.Composer.composeNode(Composer.java:155) ~[bukkit.jar:git-Bukkit-f326992]
        at org.yaml.snakeyaml.composer.Composer.composeDocument(Composer.java:122) ~[bukkit.jar:git-Bukkit-f326992]
        at org.yaml.snakeyaml.composer.Composer.getSingleNode(Composer.java:105) ~[bukkit.jar:git-Bukkit-f326992]
        at org.yaml.snakeyaml.constructor.BaseConstructor.getSingleData(BaseConstructor.java:120) ~[bukkit.jar:git-Bukkit-f326992]
        at org.yaml.snakeyaml.Yaml.loadFromReader(Yaml.java:450) ~[bukkit.jar:git-Bukkit-f326992]
        at org.yaml.snakeyaml.Yaml.load(Yaml.java:369) ~[bukkit.jar:git-Bukkit-f326992]
        at org.bukkit.configuration.file.YamlConfiguration.loadFromString(YamlConfiguration.java:54) ~[bukkit.jar:git-Bukkit-f326992]
        ... 14 more
    [14:21:21 WARN]: Unexpected exception while parsing console command "ez"
    org.bukkit.command.CommandException: Unhandled exception executing command 'ez' in plugin EzEquipment v1.0.0a
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[bukkit.jar:git-Bukkit-f326992]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) ~[bukkit.jar:git-Bukkit-f326992]
        at org.bukkit.craftbukkit.v1_9_R1.CraftServer.dispatchCommand(CraftServer.java:624) ~[bukkit.jar:git-Bukkit-f326992]
        at org.bukkit.craftbukkit.v1_9_R1.CraftServer.dispatchServerCommand(CraftServer.java:610) [bukkit.jar:git-Bukkit-f326992]
        at net.minecraft.server.v1_9_R1.DedicatedServer.aL(DedicatedServer.java:398) [bukkit.jar:git-Bukkit-f326992]
        at net.minecraft.server.v1_9_R1.DedicatedServer.D(DedicatedServer.java:362) [bukkit.jar:git-Bukkit-f326992]
        at net.minecraft.server.v1_9_R1.MinecraftServer.C(MinecraftServer.java:635) [bukkit.jar:git-Bukkit-f326992]
        at net.minecraft.server.v1_9_R1.MinecraftServer.run(MinecraftServer.java:539) [bukkit.jar:git-Bukkit-f326992]
        at java.lang.Thread.run(Thread.java:745) [?:1.8.0_60]
    Caused by: java.lang.NullPointerException
        at org.bukkit.craftbukkit.v1_9_R1.command.ColouredConsoleSender.sendMessage(ColouredConsoleSender.java:55) ~[bukkit.jar:git-Bukkit-f326992]
        at com.redtsch.EzEpuipment.main.EzCmd.CmdEz.onCommand(CmdEz.java:16) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[bukkit.jar:git-Bukkit-f326992]
        ... 8 more
    >
    
    EDIT:
    LOL i removed the ':' and it works now, so you can't use :'s?
     
  14. Offline

    I Al Istannen

    @redtsch
    Nice.
    In your config.yml, change this:
    Code:
      cmdInvalidSender: §4[Ez]: §cYou must be a player to use this command§4.
    to this:
    Code:
      cmdInvalidSender: "§4[Ez]: §cYou must be a player to use this command§4."
    " are nice!
    And I would prefer using the "&" sign for color and then calling ChatColor.translateAlternateColorCodes('&', text) to color it. Much nicer.
     
  15. Offline

    redtsch

    @I Al Istannen
    yeah, thanks for the tip. this is still in 'preliminary' phases so not quite there yet =D

    EDIT:
    Ah, now it works completely. just needed to add quotes!
    thanks all =)
     
  16. Offline

    I Al Istannen

    @redtsch
    You can use ":", you just need to enclose the whole String in single ' or double " quotes. This is due to the way YAML parses data.
    But yea, know that stage :p

    EDIT:
    Also DON'T STEAL mincerafts logger. There is JavaPlugin#getLogger for that. Just call "getLogger()" in your main class. If you watched the BcBroz, STOP it NOW.

    And you don't need the onEnable/onDisable messages, Bukkit does this for you, even with version numbers. No need to do it yourself.
     
  17. Offline

    redtsch

    @I Al Istannen
    ok, thanks. so just 'getLogger'?
    and i put those messages there for the mean-time, i plan on replacing them with something else =P
     
  18. Offline

    I Al Istannen

    @redtsch
    Just try it :) But yes, replacing getLogger(minecraft) with getLogger() could work. Alternatively, you could remove the logger variable and just call getLogger() if you want it. If you want to do this from different classes, you would need an instance of the main class though.
     
  19. Offline

    redtsch

Thread Status:
Not open for further replies.

Share This Page