Using config.yml

Discussion in 'Plugin Development' started by SgtPunishment, Jun 10, 2013.

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

    SgtPunishment

    I've searched this forum high and low and I find different answers for the same question, also some unasked questions...

    1) My code currently loads up my 'vanilla' config.yml from my plugin's jar file using " this.saveDefaultConfig();" but I have no idea how to read values, or write values or have the values (like a boolean) effect stuff within the plugin... for example here is my config.yml as it stands now.

    Code:
    settings:
      health:
        regenwhenfull: false
        godapples: true
        goldenapples: true
        glisteringmelon: true
        enderpearldamage: true
      recipes:
        goldblock: true
      ghastgold:
        dropingots: true
    
    How do I use say for example "ghastgold / dropingots" to effect this chunk of code?

    Code:
        @EventHandler
        public void onGhastDeath(EntityDeathEvent event){
            if(event.getEntity() instanceof Ghast){
                final Iterator<ItemStack> iter = event.getDrops().iterator();
                while (iter.hasNext()){
                    if (iter.next().getType().equals(Material.GHAST_TEAR)){
                        iter.remove();
                        ItemStack ingot = new ItemStack(Material.GOLD_INGOT);
                        event.getEntity().getWorld().dropItem(event.getEntity().getLocation(), ingot);
                    }
                }
            } else {
                return;
            }
        }
    
     
  2. Offline

    Compressions

    SgtPunishment
    Code:
    if(getConfig().getBoolean("settings.ghastgold.dropingots")) {
    //code for if true
    } else if(!(getConfig().getBoolean("settings.ghastgold.dropingots"))) {
    //code for if false
    }
     
  3. Offline

    SgtPunishment

    now do I put that inside the void or outside the void?
     
  4. Offline

    Rhino390

    Inside. It works as a regular if statement, so the code would look like this:
    Code:java
    1.  
    2. @EventHandler
    3. public void onGhastDeath(EntityDeathEvent event) {
    4. if(event.getEntity() instanceof Ghast) {
    5. if(plugin.getConfig().getString("settings.ghastgold.dropingots").equalsIgnoreCase("true") {
    6. final Iterator<ItemStack> iter = event.getDrops().iterator();
    7. while(iter.hasNext()) {
    8. if(iter.getNext().getType() == Material.GHAST_TEAR) {
    9. iter.remove();
    10. ItemStack ingot = new ItemStack(Material.GOLD_INGOT, 1);
    11. event.getEntity().getWorld().dropItem(event.getEntity().getLocation(), ingot);
    12. }
    13. }
    14. }
    15. }
    16. }
    17.  
     
  5. Offline

    SgtPunishment

    Thank you Rhino390 and Compressions that helps me a lot! next up... editing the values on the fly!
     
  6. Offline

    Compressions

    Rhino390 Why are you getting a string when it is a boolean?
     
  7. Offline

    SgtPunishment

    lol, actually I just realised that I marked the thread as solved when only one of the things where answered lol.
     
  8. Offline

    Rhino390

    oh yeah, im an idiot. Make sure to throw a null check when getting the string from the config, then check if it equals true
     
Thread Status:
Not open for further replies.

Share This Page