IteamStack returning Null

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

  1. So i have a plugin that saves a kit when you type /kits create <name> it saves the contents of your inventory to an
    ItemStack in the config. However whenever i try to obtain that kit it returns a EventException

    Code (Text):
    package me.darkeyedragon.customkits;

    import java.util.ArrayList;

    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockPlaceEvent;
    import org.bukkit.event.inventory.InventoryCloseEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;




    public class Main extends JavaPlugin implements Listener{
     
        ArrayList<ItemStack[]> items = new ArrayList<ItemStack[]>();
     
        public void onEnable(){
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            getLogger().info("Starting " + this.getConfig().getName() + " v"+ this.getDescription().getVersion());
        }
        public void onDisable(){
         
        }
     
        CreateItems inv = new CreateItems();
        static String prefix = "§6§o[SkullKit]§0-§7§o";
     
        public boolean onCommand(CommandSender s, Command cmd, String label, String[] args) {
            if(cmd.getName().equalsIgnoreCase("kits")){
                Player p = (Player)s;
                if(args.length > 0){
                    if(args[0].equalsIgnoreCase("create")){
                        if(args.length < 2){
                            s.sendMessage("§bUsage: /kits create <name>");
                            s.sendMessage("§7This will copy your current inventory and save it as a kit!");
                        }
                        if(args.length == 2){
                            items.add(p.getInventory().getContents());
                            this.getConfig().set("kits."+args[1], items);
                            //kits.add(args[1]);
                            //kits.add("kits."+ args[1] + p.getInventory().getContents());
                            this.saveConfig();
                            s.sendMessage("§aKit succesfully created!");
                        }
                    }
                    else if(args[0].equalsIgnoreCase("give")){
                        if(args.length < 2){
                            s.sendMessage("§bUsage: /kits give <name>");
                            s.sendMessage("§7This will give you a chest linked to the kit.");
                        }
                        if(args.length == 2){
                            p.getInventory().addItem(inv.createItem("§6§l"+args[1], Material.CHEST));
                            //s.sendMessage(this.getConfig().getList("kits."+args[1]).toString());
                         
                         
                        }
                    }
                    else{
                        s.sendMessage(prefix+ "Invalid parameter.");
                    }
                }
            }
            return false;  
        }
     
        @SuppressWarnings("deprecation")
        @EventHandler
        public void onBlockPlace(BlockPlaceEvent e){
            Player p = e.getPlayer();
            if(e.getBlock().getType().getId() == 54){
                e.setCancelled(true);
                if(p.getInventory().getItemInMainHand().getItemMeta().getDisplayName().contains("§6§l")){
                    getKit(p.getInventory().getItemInMainHand().getItemMeta().getDisplayName(), p);
                    p.getInventory().removeItem(p.getInventory().getItemInMainHand());
                }else{
                    p.sendMessage("Nei");
                }
            }
        }
        public void getKit(String kitName, Player p){
            kitName = kitName.replace("§6§l", "");
            p.getInventory().addItem(this.getConfig().getItemStack("kits."+kitName));
        }
     
        @EventHandler
        public void onPlayerInventoryCloseEvent(InventoryCloseEvent e){
            Player p = (Player) e.getPlayer();      
            for (ItemStack itemStack : p.getInventory().getContents()) {
                p.getWorld().dropItemNaturally(p.getLocation(), itemStack);
            }
        }
    }
     
    Note: the OnPlayerInventoryCloseEvent is just to test how itemstacks work. It seems to give the same error here too altough it works if i completly fill up my inventory.

    Is there a better way to store items instead? including enchantments etc. Or how can i fix this issue.
    Errorcode:

    Caused by: java.lang.IllegalArgumentException: Item cannot be null
    at org.apache.commons.lang.Validate.noNullElements(Validate.java:364) ~[spigot.jar:git-Spigot-6de2fbc-80dd971]
    at org.bukkit.craftbukkit.v1_11_R1.inventory.CraftInventory.addItem(CraftInventory.java:274) ~[spigot.jar:git-Spigot-6de2fbc-80dd971]
    at me.darkeyedragon.customkits.Main.getKit(Main.java:89) ~[?:?]
    at me.darkeyedragon.customkits.Main.onBlockPlace(Main.java:80) ~[?:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_121]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_121]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_121]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_121]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot.jar:git-Spigot-6de2fbc-80dd971]
    ... 18 more
     
  2. Can I see your config file?
     
  3. Sure thing:

    kits:
    meow:
    - - ==: org.bukkit.inventory.ItemStack
    type: CHEST
    meta:
    ==: ItemMeta
    meta-type: TILE_ENTITY
    display-name: §6§lmeow
    blockMaterial: CHEST
    - null
    - ==: org.bukkit.inventory.ItemStack
    type: BEDROCK
    - ==: org.bukkit.inventory.ItemStack
    type: SPONGE
    - ==: org.bukkit.inventory.ItemStack
    type: WOOD
    damage: 3
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    - null
    dirt:
    - - ==: org.bukkit.inventory.ItemStack
    type: DIRT
    damage: 1
    amount: 64
    - ==: org.bukkit.inventory.ItemStack
    type: DIRT
    damage: 1
    amount: 64
    - ==: org.bukkit.inventory.ItemStack
    type: DIRT
    damage: 1
    amount: 64
    - ==: org.bukkit.inventory.ItemStack
    type: DIRT
    damage: 1
    amount: 13
    - ==: org.bukkit.inventory.ItemStack
    type: DIRT
    damage: 1
    amount: 4
    - ==: org.bukkit.inventory.ItemStack
    type: DIRT
    damage: 1
    amount: 4
    - ==: org.bukkit.inventory.ItemStack
    type: DIRT
    damage: 1
    amount: 4
    - ==: org.bukkit.inventory.ItemStack
    type: DIRT
    damage: 1
    amount: 4
    - ==: org.bukkit.inventory.ItemStack
    type: DIRT
    damage: 1
    amount: 4
    - ==: org.bukkit.inventory.ItemStack
    type: DIRT
    amount: 64
    - ==: org.bukkit.inventory.ItemStack
    type: DIRT
    amount: 64
    - ==: org.bukkit.inventory.ItemStack
    type: DIRT
    amount: 64
    - ==: org.bukkit.inventory.ItemStack
    type: DIRT
    amount: 16
    - ==: org.bukkit.inventory.ItemStack
    type: DIRT
    amount: 4
    - ==: org.bukkit.inventory.ItemStack
    type: DIRT
    amount: 4
    - ==: org.bukkit.inventory.ItemStack
    type: DIRT
    amount: 4
    - ==: org.bukkit.inventory.ItemStack
    type: DIRT
    amount: 4
    - ==: org.bukkit.inventory.ItemStack
    type: DIRT
    amount: 4
    - ==: org.bukkit.inventory.ItemStack
    type: DIRT
    - ==: org.bukkit.inventory.ItemStack
    type: DIRT
    - ==: org.bukkit.inventory.ItemStack
    type: DIRT
    - ==: org.bukkit.inventory.ItemStack
    type: DIRT
    amount: 4
    - ==: org.bukkit.inventory.ItemStack
    type: DIRT
    amount: 4
    - ==: org.bukkit.inventory.ItemStack
    type: DIRT
    amount: 4
    - ==: org.bukkit.inventory.ItemStack
    type: DIRT
    amount: 4
    - ==: org.bukkit.inventory.ItemStack
    type: DIRT
    amount: 4
    - ==: org.bukkit.inventory.ItemStack
    type: DIRT
    amount: 4
    - ==: org.bukkit.inventory.ItemStack
    type: DIRT
    - ==: org.bukkit.inventory.ItemStack
    type: DIRT
    damage: 1
    amount: 4
    - ==: org.bukkit.inventory.ItemStack
    type: DIRT
    damage: 1
    amount: 3
    - ==: org.bukkit.inventory.ItemStack
    type: DIRT
    damage: 1
    amount: 4
    - ==: org.bukkit.inventory.ItemStack
    type: DIRT
    damage: 1
    amount: 4
    - ==: org.bukkit.inventory.ItemStack
    type: DIRT
    damage: 1
    amount: 4
    - ==: org.bukkit.inventory.ItemStack
    type: DIRT
    damage: 1
    amount: 4
    - ==: org.bukkit.inventory.ItemStack
    type: DIRT
    damage: 1
    amount: 4
    - ==: org.bukkit.inventory.ItemStack
    type: DIRT
    damage: 1
    amount: 4
    - null
    - null
    - null
    - null
    - null


    these are the kits meow and dirt.
     
    #3 DarkEyeDragon, Apr 17, 2017
    Last edited: Apr 17, 2017
  4. You must check, itemeta.

    if(itemmeta == null) return;

    or ItemStack, up .