Error with itemstack from configuration file

Discussion in 'Spigot Plugin Development' started by Kaamil3q, May 29, 2017.

  1. Hi there guys,
    i'm trying to create kit's plugin with simple config.
    Error is in line 46, can someone fix it? Thnx.
    Code (Text):

    ItemStack slot1 = new ItemStack(Material.getMaterial("kits.item_slot_1"));
     
    Code (Text):

    package me.kamiiq.blacksquad.kits;

    import java.io.File;
    import java.io.IOException;

    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;

    import net.md_5.bungee.api.ChatColor;

    public class Main extends JavaPlugin{
        final static File configFile = new File("plugins/BQ-Kits", "config.yml");
        static FileConfiguration cfg = YamlConfiguration.loadConfiguration(configFile);
       
        public void onEnable(){
            try {
                loadConfig();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            }
       
        public void onDisable(){
           
        }
        public static void loadConfig() throws IOException {
            if (!(configFile.exists())) {
               cfg.addDefault("kits.item_slot_1", "DIAMOND, 1");
               cfg.options().copyDefaults(true);
               cfg.save(configFile);
                }
            }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            if (sender instanceof Player){
                if(label.equalsIgnoreCase("kit")){
                   Player p = (Player) sender;
                   ItemStack slot1 = new ItemStack(Material.getMaterial("kits.item_slot_1"));
                   p.getInventory().addItem(slot1);
                }
            }
            else{
                    Bukkit.broadcastMessage(ChatColor.RED + ("Console's can't use kit command(s)!"));
            }
            return false;
        }
    }
     
     
  2. ScarabCoder

    ScarabCoder Retired Resource Staff
    Retired

    You're trying to get the material for the string "kits.item_slot_1". You're probably trying to get it from a config, so try getting the string config.getString("kits.item_slot_1") instead.
     
    • Agree Agree x 2
  3. If you are indeed attempting to get this from the config file, you need to actually access the config file
    Code (Text):
    ItemStack slot1 = new ItemStack(Material.getMaterial(getConfig.getString("kits.item_slot_1")));
     
    • Agree Agree x 2
  4. Awesome! Ty very much!

    Working code (maybe someone want to see it):
    Code (Text):

    package me.kamiiq.blacksquad.kits;

    import java.io.File;
    import java.io.IOException;

    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;

    import net.md_5.bungee.api.ChatColor;

    public class Main extends JavaPlugin{
        final static File configFile = new File("plugins/BQ-Kits", "config.yml");
        static FileConfiguration config = YamlConfiguration.loadConfiguration(configFile);
       
        public void onEnable(){
            try {
                loadConfig();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            }
       
        public void onDisable(){
           
        }
        public static void loadConfig() throws IOException {
            if (!(configFile.exists())) {
               config.addDefault("kits.item_slot_1", "DIAMOND, 1");
               config.options().copyDefaults(true);
               config.save(configFile);
                }
            }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            if (sender instanceof Player){
                if(label.equalsIgnoreCase("kit")){
                   Player p = (Player) sender;
                   ItemStack slot1 = new ItemStack(Material.getMaterial(config.getString("kits.item_slot_1")));
                   p.getInventory().addItem(slot1);
                }
            }
            else{
                    Bukkit.broadcastMessage(ChatColor.RED + ("Only player's can use kit command!"));
            }
            return false;
        }
    }
     
     
    • Friendly Friendly x 1
  5. btw, please set the thread as SOLVED, and except if you're making a bungeecord plugin (what isn't in this case) import the ChatColor from bukkit, not bungeecord :)
     
  6. Em... i can't mark that thread as solved because it isn't... I added some features to my plugin, restart server - BOOM error, paste code which i pasted here and again error - what's going on? That's code work before i don't restart server... Again line 46.
    Code (Text):

    ItemStack slot1 = new ItemStack(Material.getMaterial(cfg.getString("kits.item_slot_1")));
     
     
  7. Try

    Material#matchMaterial();
     
  8. If you want to keep complete kits a good way is to serialize them here I give you an example of how to do it, I hope you serve, greetings. https://pastebin.com/CZtLP2N5
     
    • Like Like x 1
  9. ScarabCoder

    ScarabCoder Retired Resource Staff
    Retired

    You should post the error, otherwise we don't know why it's happening.
    Try Material.valueOf(), as well as making sure kits.item_slot_1 exists in the config. Finally, make sure that if it does exist, that it's a proper name
     
  10. Okay, very nice - it works again.

    But now... how to add amount? i tried
    Code (Text):

    String amount1 = config.getString("kits.amount_slot_1")
    (...)
    ItemStack slot1 = new ItemStack (Material.getMaterial(config.getString("kits.item_slot_1")), amount1);
     
    Code:
    Code (Text):

    package me.kamiiq.blacksquad.kits; //package

    import java.io.File;
    import java.io.IOException;

    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.ChatColor

    public class Main extends JavaPlugin{
        final static File configFile = new File("plugins/BQ-Kits", "config.yml");
        static FileConfiguration config = YamlConfiguration.loadConfiguration(configFile);
       
        public void onEnable(){
            try {
                loadConfig();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            }
       
        public void onDisable(){
           
        }
        public static void loadConfig() throws IOException {
            if (!(configFile.exists())) {
               
                config.addDefault("kits.item_slot_1", "DIAMOND");
                config.options().copyDefaults(true);
                config.save(configFile);
                }
            }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            Player p = (Player) sender;
            if (sender instanceof Player){
                if(label.equalsIgnoreCase("kit")){
                    if(args.length == 1){
                        if(args[0].equalsIgnoreCase("start"))  {
                          ItemStack slot1 = new ItemStack (Material.getMaterial(config.getString("kits.item_slot_1")), 1);
                          p.getInventory().addItem(slot1);
                        }
                      else{
                          p.sendMessage(ChatColor.DARK_RED + ("You are using bad argument"));
                      }
                    }
                    else{
                        p.sendMessage(ChatColor.DARK_RED + ("You must use a argument!"));
                    }
                }
           else{
                Bukkit.broadcastMessage(ChatColor.RED + ("Only player's can use kit command!"));
                  }
            }
            return false;
        }
    }
     
     
  11. With the JavaPlugin class you have the methods saveDefaultConifg (), getConfig () you do not need to do all that ..
     
  12. try this
    Code (Text):
            String type = getConfig().getString("Slot-1");
           
            Material material = Material.valueOf(type);
           
            ItemStack item = new ItemStack(material);
           
            for(Player all : Bukkit.getOnlinePlayers()) {
                all.getInventory().addItem(item);
                all.updateInventory();
            }