Config and internal error help

Discussion in 'Plugin Development' started by Plaze_Demon, Aug 21, 2014.

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

    Plaze_Demon

    So basically I have a config file which allows a user to change the item and amount that they want to exchange for something else upon typing /exchange. In my onEnable I have set the default values of the configuration file, it loads the file and the values but weirdly and all bunched up, and then when I type /exchange it throws an internal error.

    Code . . .:
    Code:
    package me.bukkit.Plaze_Demon;
     
    import java.io.File;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
     
     
     
    public class ItemExchange extends JavaPlugin {
       
        @Override
        public void onEnable() {
            getLogger().info("Item Exchange is working");
            File file = new File(getDataFolder() + File.separator + "config.yml");
           
            if (!file.exists()) {
                this.getLogger().info("Creating config.yml");
               
                this.getConfig().addDefault("itemtotake", "COAL");
                this.getConfig().addDefault("amounttotake", "64");
                this.getConfig().addDefault("itemtogive", "DIAMOND");
                this.getConfig().addDefault("amounttogive", "3");
                this.getConfig().options().copyDefaults(true);
                this.saveConfig();
            }
        }
       
        @Override
        public void onDisable(){
           
           
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String Label, String[] args){
           
           
            if (cmd.getName().equalsIgnoreCase("itemexchange") && sender instanceof Player) {
               
                Player player = (Player) sender;
               
                player.sendMessage(ChatColor.AQUA + "Developed by Plaze_Demon. Responsive");
               
                    }
           
               
            if (cmd.getName().equalsIgnoreCase("exchange")){
               
                String itemtotake = this.getConfig().getString("itemtotake");
                int amounttotake = this.getConfig().getInt("amounttotake");
                int amounttogive = this.getConfig().getInt("amounttogive");
                Player player = (Player) sender;
                PlayerInventory inventory = player.getInventory();
                ItemStack take = new ItemStack(Material.getMaterial("itemtotake"), amounttotake);
                ItemStack give = new ItemStack(Material.getMaterial("itemtogive"), amounttogive);
             
                if (player.hasPermission("itemexchange.exchange")){
                   
                    if(inventory.contains(take)){
                        inventory.removeItem(take);
                        inventory.addItem(give);
                        player.sendMessage(ChatColor.AQUA + "Exchanged!");
                       
                        }
                    else player.sendMessage(ChatColor.RED + "You do not have enough" + itemtotake );
           
                }
                else player.sendMessage(ChatColor.DARK_RED + "You do not have permission!");
                return true;
            }
           
               
                return false;
           
        }
       
       
       
       
       
       
     
    }
    
    And this is how it loads the config file upon first running . . . :
    Code:
    itemtotake: COALamounttotake: '64'itemtogive: DIAMONDamounttogive: '3'
    
     
  2. Offline

    stoneminer02

    First of all you set all the stuff everytime so it shouldn't be able to be edited(I haven't used the setDefault thingy before).
    And the secound is that you check if the file exists, if it doesn't, use getConfig().set("path.underpath", "coal");
     
  3. Offline

    fireblast709

    Plaze_Demon a stacktrace would be nice. Some things I can spot:
    • You don't check if the sender instanceof Player before casting (second cast, the first is ok)
    • getString(path) returns null as default (in the case it's not there), could lead to a NullPointerException
    • Material.getMaterial(String) would return null if the String isn't a valid Material. Your input there most likely leads to your error. (ItemStacks don't like null Materials)
    • according to the docs, contains checks for the exact ItemStack, so if you have more of that ItemStack, it should return false.
     
  4. Offline

    Plaze_Demon

    Thanks fireblast709, I'll add the player sender check, just a question, how could getString(Path) return null if I've already set the default values which I made sure are correct in the onEnable. And also, Material.getMaterial(String) is a valid material, I think COAL is the right way to right it? And also about the exact item stack thing, I'm not exactly sure how to use < and > in the situation of using a int from the config (would it be if (inventory.contains(<amounttotake);?

    Sorry if I'm not typing proper english, it my second language

    stoneminer02 i used getConfig().set("path.underpath", "coal"); instead of my original way, by underpath do you mean the sub path, eg itemtotake.amount or do you mean i should actually write "underpath" after the string?

    bump

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

    fireblast709

    Plaze_Demon I cannot really call
    Code:
    Material.getMaterial("itemtotake")
    a valid Material ;3. As for the contains part, I would recommend this function (read the documentation to know why)
     
  6. Offline

    Plaze_Demon

    fireblast709 ok thanks for the contains part. As for Material.getMaterial("itemtotake", amounttotake") would it not work even though I have set itemtotake as a String leading to the material in the config.yml. E.g. itemtotake: COAL, is what it is as default
     
  7. Offline

    stoneminer02

    With underpath I meant that it will go like this:
    Code:yaml
    1.  
    2. path:
    3. underpath:
    4. anotherunderpath: '&6Hai'

    How to do this:
    Code:java
    1. path.underpath.anotherunderpath
     
  8. Offline

    fireblast709

    Plaze_Demon Have you even read the documentation of Material.getMaterial(String)? It has absolutely nothing to do with the configuration API.
     
Thread Status:
Not open for further replies.

Share This Page