Getting ItemStack from config and give to player

Discussion in 'Spigot Plugin Development' started by DarkEyeDragon, Apr 17, 2017.

  1. Title expains most of it. I'm storing the itemstack (unmodified) in the default config.

    Code to GET the itemstack from config
    Code (Text):
    public void getKit(String kitName, Player p){
            ItemStack test = this.getConfig().getItemStack("kits.boots");
            kitName = kitName.replace("§6§l", "");
            if(this.getConfig().getItemStack("kits."+kitName) == null) return;
            p.getInventory().addItem(test);
            p.sendMessage(kitName);
        }
    Code to STORE the itemstack to config:
    Code (Text):
    if(args.length == 2){
                            for (ItemStack itemStack : p.getInventory().getContents()) {
                                if(itemStack != null){
                                    items.add(itemStack);
                                }
                            }
                            this.getConfig().set("kits."+args[1], items);
                            this.saveConfig();
                            items.clear();
                            s.sendMessage("§aKit succesfully created!");
                        }
    this creates this config file if i have like 10 diamond boots in my inv:

    [​IMG]

    The itemstack however ALWAYS returns null. so with this nullcheck in place it never triggers. When i remove it
    it just throws a NPE.

    How would i solve this issue?
     
  2. I think you save the Items to the Config, by storing them into a List and then save the list to the config. (here is the list called "items") right?

    If you store the List to a the Path Kits.Boots why should it return a single Itemstack if you want to retrieve data from this path?
    As you didn´t post your full code i just can assume things from the syntax you were using.
    But try to retrieve the whole List in the getter code and not a single Itemstack.
     
  3. I don't know if that's a good idea, you shouldn't be storing plain objects like that. I'd say try to do it like essentials does this: store only the things important to you. For instance instead of a bunch of metadata, just store for example the durability (if that matters to you), the item id (if you need help resolving items with a colon, tell me), and the amount. That's another approach but I'd use that instead of doing this. If you're planning on publishing your plugin, it might look frightening to some of your users.
     

  4. Yeah i'm storing them using a List
    Code (Text):
    List<ItemStack> items = new ArrayList<ItemStack>();
    I tried getting the list but it cannot be cast to ItemStack.

    Changed code:
    Code (Text):
    public void getKit(String kitName, Player p){
            kitName = kitName.replace("§6§l", "");
            //if(this.getConfig().getItemStack("kits."+kitName) == null) return;
            ItemStack test = (ItemStack) this.getConfig().getList("kits."+kitName);
            p.getInventory().addItem(test);
            p.sendMessage(kitName);
        }
    this gives me an ArrayList cannot be cast to ItemStack
     
  5. Why are you trying to cast a List to an itemstack? A list is, well .. A list! If you need one of the elements inside of that list, use the get(); method.
     

  6. Yeah thats what i was considering but I'm a bit confused on what data i need to store and mainly HOW to store it and convert it back to an itemstack. I want people to be able to make kits from the items they have in their inventory, enchantments, amounts, what item ofcourse. So i'm not really sure how to do this.
     
  7. I'd do it like this: Make a method inside of your JavaPlugin inherited class, then get everything you need and set a config path for it. For instance:
    Code (Text):

    for(ItemStack stack : player.getInventory().getContents) {
        myYamlConf.set("items."+stack.getType + ".enchantments", stack.getEnchantments);
    }
     
    Something like this, I wrote this on the spot, might be making some mistakes. Edit the path to whatever suits you.
     

  8. Thanks. I'll mess around with that. And how would i get them back to an usuable itemstack?
     
  9. I'd get all keys under "items.itemType<UniqueIdentifier>", strip off the identifier and then create a new ItemStack(); instance. Then pass all the data through the methods on that object. Just a note; the example I gave you is really rough and won't be an exact fit for you. :)
    That unique identifier is to make sure you can actually store multiple kinds of the same item in your kit.