Kit PVP Plugin: Get item from config

Discussion in 'Spigot Plugin Development' started by CodingDuck, May 29, 2015.

  1. Hello,

    I try to write a Kit PVP Plugin. So I could write it with items i would define and I already did it, but now I want to get an item (in string form) from the config and I want to set is to be seen in my virtual inventory. This is what I tried:

    Code (Text):
    package me.codingduck.main;

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

    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.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin{
     
        public static void loadConfig() throws IOException {

            final File FILE = new File("plugins/Kits", "config.yml");
            final FileConfiguration cfg = YamlConfiguration.loadConfiguration(FILE);

            cfg.options().copyDefaults(true);
            cfg.save(FILE);

            String item1 = cfg.getString("item1");
    }
     
        private Inventory inv;
        private int lines = 1;
     
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
         
            if(label.equalsIgnoreCase("kits"))
            {
                Player p = (Player) sender;
                if(p.hasPermission("kits.default")){
                    inv = p.getServer().createInventory(null, lines  * 9, "§b" + p.getName() + "§e " + "Inventory");
                 
                    ItemStack item = new ItemStack(Material.getMaterial(item1));
                    ItemMeta meta = item.getItemMeta();
                    meta.setDisplayName("§aFOOD");
                    item.setItemMeta(meta);
                 
                    inv.setItem(13, item);
                 
                    p.openInventory(inv);
                }
            }
         
            return true;
        }
     
    }
           
    Can anyone help me?
     
    • Like Like x 1
  2. I see no problems there, whats the error?

    From a resource of mine:
    Code (Text):
        String mat = getConfig().getString("material");
        int matam = getConfig().getInt("material-stack-amount");
       
        public void openSoupInventory(Player p) {
            for(int b : allowed) {
                if(slotam != b) {
                    slotamr = 36;
                } else {
                    slotamr = slotam;
                }
            }
            Inventory inv = Bukkit.getServer().createInventory(null, slotamr, ChatColor.translateAlternateColorCodes('&', invtitle));
            for(int i = 0; i < slotam; i++) {
                if(Material.matchMaterial(mat) != null) {
                    inv.addItem(new ItemStack(Material.getMaterial(mat), matam));
                } else {
                    inv.addItem(new ItemStack(Material.MUSHROOM_SOUP, matam));
                }
            }
            p.openInventory(inv);
        }
     
  3. The problem is that it says item1 cannot be resolved to a variable, btw I don't know why.
     
  4. that is a problem with your code, not a bukkit api function.
    you are creating and defining the string variable "item1" inside your static loadConfig() function, so how are you suppose to access it from another place?

    create your string openly in the class, than define it in your loadConfig() and then call it from your command.
     
  5. Thanks alot! I didn't meant that the API is wrong, but that I don't know what to do
     
  6. Hi, now I get an Error that my plugin.yml is not correct. I can't find the mistake:

    Code (Text):
    name: KitPVP
    main: me.codingduck.main.Main
    author: CodingDuck
    version: 1.0
    commands:
      kits:
        description: '§4Choose your kit, brave warrior!'
        usage: /<command>
    permissions:
      kits.default:
        description: '&4 Gives you the permissions to choose use your kit'
    My Java Class:

    Code (Text):
    package me.codingduck.main;

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

    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.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin{
       
        static String item1;
        static String item2;
        static String item3;
        static String item4;
        static String item5;
        static String item6;
        static String item7;
        static String item8;
        static String item9;
       
        public void onEnable(){
           
        }
       
        public void onDisable(){
           
        }
       
        public static void loadConfig() throws IOException {

            final File FILE = new File("plugins/Kits", "config.yml");
            final FileConfiguration cfg = YamlConfiguration.loadConfiguration(FILE);

            cfg.options().copyDefaults(true);
            cfg.save(FILE);

            item1 = cfg.getString("item_slot_1");
            item2 = cfg.getString("item_slot_2");
            item3 = cfg.getString("item_slot_3");
            item4 = cfg.getString("item_slot_4");
            item5 = cfg.getString("item_slot_5");
            item6 = cfg.getString("item_slot_6");
            item7 = cfg.getString("item_slot_7");
            item8 = cfg.getString("item_slot_8");
            item9 = cfg.getString("item_slot_9");
    }
       
        private Inventory inv;
        private int lines = 1;
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
           
            if(label.equalsIgnoreCase("kits"))
            {
                Player p = (Player) sender;
                if(p.hasPermission("kits.default")){
                    inv = p.getServer().createInventory(null, lines  * 9, "§eKit Selector");
                   
                    ItemStack itemstack1 = new ItemStack(Material.getMaterial(item1));
                    ItemStack itemstack2 = new ItemStack(Material.getMaterial(item2));
                    ItemStack itemstack3 = new ItemStack(Material.getMaterial(item3));
                    ItemStack itemstack4 = new ItemStack(Material.getMaterial(item4));
                    ItemStack itemstack5 = new ItemStack(Material.getMaterial(item5));
                    ItemStack itemstack6 = new ItemStack(Material.getMaterial(item6));
                    ItemStack itemstack7 = new ItemStack(Material.getMaterial(item7));
                    ItemStack itemstack8 = new ItemStack(Material.getMaterial(item8));
                    ItemStack itemstack9 = new ItemStack(Material.getMaterial(item9));
                   
                   
                    inv.setItem(0, itemstack1);
                    inv.setItem(1, itemstack2);
                    inv.setItem(2, itemstack3);
                    inv.setItem(3, itemstack4);
                    inv.setItem(4, itemstack5);
                    inv.setItem(5, itemstack6);
                    inv.setItem(6, itemstack7);
                    inv.setItem(7, itemstack8);
                    inv.setItem(8, itemstack9);
                   
                    p.openInventory(inv);
                }
            }
           
            return true;
        }
       
    }
     
  7. And can I make a loop which adds the ItemStacks for me automatically?
     
  8. JustThiemo

    JustThiemo Previously TrinityDEV

    @CodingDuck
    I dont think plugin.yml supports color tags, and '', just use
    description: MyDescriptionYay , etc.

    Its possible to loop through the items and set them in inventory, but its bit a messy way, if you dont serialize it.

    Code (Text):
    //for saving your inv to config
    public void SaveItems(Player p, String name){
      getConfig().set("KitItems." + name,     p.getInventory().getContents());
      this.saveConfig();
    }

    //getting your inv from config
    public void LoadItems(Player p, String name, Inventory inv){
    //get stuff
      List listitems = (List) this.getConfig().get("KitItems." + name);
      ItemStack[] items = (ItemStack[]) listarmor.toArray(new ItemStack[0]);

    //set items and armor
              inv.setContents(items);
     
  9. [​IMG]

    Pretty sure that an ItemStack[] is not a string
     
    • Funny Funny x 1
  10. JustThiemo

    JustThiemo Previously TrinityDEV

    Pretty sure this thread is +1 1 year old.
     
    • Optimistic Optimistic x 2