[Solved] Get number from config and convert to material type

Discussion in 'Plugin Development' started by devilquak, Aug 17, 2012.

  1. Offline

    devilquak

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    I'm trying to let a listener take a value from a config file, an integer, then change it to the corresponding material name that Bukkit will recognize and put it into my code. I've tried everything I can think of, which is probably very little, so I need some help here, if you all don't mind.

    The thing I need to put the material in is this:
    PHP:
    event.getPlayer().getItemInHand().getType() == Material.<Material type to insert>
    It's for a right-click event that detects what the player is holding in his/her hand. It won't let me put a string in for the material type, and anything else I try doesn't seem to work.

    If it's too hard to convert an ID into a material, I'll settle with just inputting the material into the config, but I can't even figure out how to do that. I've never used configs before, so I know I'm probably making a stupid error somewhere. Can anyone spare a moment to give me a few pointers here?
  2. Offline

    Timr

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Take a look at the two different getMaterial() methods here.
    devilquak likes this.
  3. Offline

    devilquak

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Actually I might have just solved my own problem... let me see.
  4. Offline

    Digi

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    I suggest you use Material.matchMaterial() as it accepts both IDs and item names because people shouldn't be forced to input only IDs :p
    devilquak likes this.
  5. Offline

    devilquak

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    I figured the first part out on my own, right after I posted this. I had been trying to use Material.<string> when I only needed the string.

    But this is interesting. If I get you correctly,
    PHP:
    String item getConfig().getString("<ConfigItem>");
    Material configMaterial Material.matchMaterial(item);
    will match up whatever is in the config, be it an ID or a material, to the correct material in-game?

    This post has been edited 1 time. It was last edited by devilquak Aug 18, 2012.
  6. Offline

    Timr

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Yes.
  7. Offline

    Digi

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Yes, but ofc it will return null if it couldn't match either.
  8. Offline

    devilquak

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Okay, great, thank you so much.

    I just have one more quick question. Is there an easy way to convert the item that is in the config to a word that is pleasant to look at? Like if I put in 57, for a diamond block, it would output DIAMOND_BLOCK if i tried to put it into a message, when I'd like it to say something more pleasant, like "diamond block". Do I have to define every block/item myself or is there another way to do this?
  9. Offline

    Digi

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Why not use the result of the matchMaterial() ? Printing a Material object will print its name.
  10. Offline

    devilquak

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Sorry to keep bothering you, but the matchMaterial method isn't working. I'm using this:
    PHP:
    String configItem getConfig().getString("Item");
    Material item Material.matchMaterial(configItem);
    The config has this, among other things: Item: 39

    I'm sure the config isn't the problem or anything, I'm using other things from there that work fine elsewhere. The error I get in the console is this:
    Code:
    Caused by: java.lang.IllegalArgumentException: File cannot be null
        at org.apache.commons.lang.Validate.notNull(Validate.java:203)
        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)
    
    It won't work when I put in a material name either, like instead of 39 if I put BROWN_MUSHROOM, I get the same error.
  11. Offline

    Timr

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    The config is the problem. Read your stack trace.
  12. Offline

    devilquak

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Yeah, I just noticed. I thought it was looking for the wrong thing in my code, but you're right, it's the config.

    If only I could tell what it meant. :( I know how to decipher stack traces, but I can't figure out how to fix mine. It says it can't validate my config file and that it can't be null. It's all in the correct format and the other parts of my plugin that use the config as well work fine, even after I edit it and reload the plugin.
    PHP:
    Caused byjava.lang.IllegalArgumentExceptionFile cannot be null
        at org
    .apache.commons.lang.Validate.notNull(Validate.java:203)
        
    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 com.github.devilquak.ItemConsumeListener.onPlayerInteract(ItemConsumeListener.java:24)
        
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        
    at java.lang.reflect.Method.invoke(Method.java:597)
        
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:330)
    I know I'm just making another stupid mistake, which is the embarrassing part... :'(
  13. Offline

    Timr

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
  14. Offline

    devilquak

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    http://dev.bukkit.org/paste/htxql1djwuye9ofk/
    There's more code in this class, but I took out the parts that weren't relevant to the problem I'm having. It will work perfectly if I just put a material straight into the plugin instead of trying to get it from the config.

    Thank you so much for your help.
  15. Offline

    Timr

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    I would suggest a couple of things. First of all, I would use this for handling configs, I've never had a problem with it:

    http://dev.bukkit.org/paste/6001/

    Here's how you instance it:
    Code:
    Config config;
     
    public void onEnable() {
        config = new Config(this);
    }
    Next, add a Constructor to your Listener (if you don't already have one).

    Finally, I don't see anything wrong in that paste. The error in the stack trace is "file cannot be null", so somewhere when you call a file, you're not doing it properly.
  16. Offline

    devilquak

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    I've added that to my main class, thanks so much for giving it to me. However, "private static Configuration config;" gives an error, "the field config cannot be declared static; static fields can only be declared static or top level types". How do I go about fixing this?
  17. Offline

    Timr

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Source? ;)
  18. Offline

    devilquak

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
  19. Offline

    Timr

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    1) Put the config class in a different file
    2) Modify the config class with your various options:
    The Config acts almost like an enum (I suppose), but has a different concept. I'll give you what I use in my SignEdit plugin:

    http://dev.bukkit.org/paste/6019/

    Basically, above the constructor you define all your variables. Then in getOpts(), you set the values for them. You can see I have mainly booleans defined, some actions and some strings. Once you define them there, you can access them like this:

    Code:
    String logName = config.logName;
    devilquak likes this.
  20. Offline

    devilquak

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Man, I feel stupid. I should have known to do that. Anyway, I did everything, and there are no errors now, but when I do anything in Minecraft, I get this stack trace that I can't fix for the life of me...
    Code:
    Caused by: java.lang.NullPointerException
        at com.github.devilquak.ItemConsumeListener.onPlayerInteract(ItemConsumeListener.java:24)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:330)
    I'm so sorry I keep asking you for help, but I'm clueless at this point and have been stuck on this whole config thing for the past two days.
  21. Offline

    Timr

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    What's on line 24 of ItemConsumeListener.java?
  22. Offline

    devilquak

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    So sorry to keep doing this to you.

    http://dev.bukkit.org/paste/fgux47bwi1ok6ds0/

    It's the string defining the item from my config file, "String drugConfigItem = config.drugItem;"

    I don't know if this is the problem, but do I have to add a constructor? It worked perfectly well without one before I tried to add in these config features.
  23. Offline

    Timr

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Alright, let's try a few things

    1) Stop extending DrugDealer
    2) Add a Constructor to ItemConsumeListener then call things from the config with plugin.config.drugItem
    Code:
    public DrugDealer plugin;
     
    public ItemConsumeListener(DrugDealer plugin) {
        this.plugin = plugin;
    }
    3) in your main class, do this:
    Define your ICL var:
    Code:
    public ItemConsumeListener icl;
    Then in your onEnable():
    Code:
    icl = new ItemConsumeListener(this);
    getServer().getPluginManager().registerEvents(icl, this);
    
    That /should/ fix the problems. Let's see.
    devilquak likes this.
  24. Offline

    devilquak

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Holy wow, oh my god, nerdgasm, thank you so, so, so much. This worked perfectly, it all works correctly now, thank you so much, you're amazing. Thanks for taking so much time to help me, I would never have gotten this working by myself. I've still got one more thing to figure out before I'm done, but I won't ask you to do anything more for me.

    Again, thank you so so much.
  25. Offline

    Timr

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    If you have any more questions feel free to PM me or make a new thread. I'm always happy to help.
    devilquak likes this.
  26. Offline

    devilquak

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)

    Actually, regarding the config that you helped me fix, I can't figure out how to reload it correctly through normal API. this.reloadConfig() doesn't work, and neither does plugin.reloadConfig(), or really anything else I try. How do I do it with the config handler you supplied me with?
  27. Offline

    Digi

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    reloadConfig() works just fine, you need to use getConfig() again afterwards, if you're storing it you need to update that reference after you reload.

    This post has been edited 1 time. It was last edited by Digi Aug 20, 2012.
  28. Offline

    Timr

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Just use the Config.reload() method. It's a function within the class
  29. Offline

    devilquak

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Ahhh, I've been trying Config.reload(), but the C is actually lowercase.

    One more thing to bother you with if you're not intolerably tired of me yet...

    That config problem I had earlier, the integer I was trying to get, I'm using in my plugin to be consumed with a right-click. However, when the item is a block, if it's the last block in a stack it places the block as well as continuing the plugin's functions, so someone can keep placing it and destroying it and essentially go on forever.

    I'm just trying to cancel the place event if the item from the config is a block, however I'm having trouble with it, and the docs don't really help that much. The isBlock from the JD doesn't really help me that much, so if I just knew a way to do it, I'm pretty sure I could take it from there.
  30. Offline

    Timr

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Code..? :)

Share This Page