Solved Offloading SOME items from inventory into chest

Discussion in 'Plugin Development' started by TheManiacGamers, Nov 6, 2018.

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

    TheManiacGamers

    Hello,
    I am here because I've hit a brick wall.

    I am trying to make a plugin that allows you to type /offload whilst looking at a chest, and it will put everything in your inventory, into the chest (except for a few things.. eg: diamond armor, diamond tools, bow, torch, cooked chicken and baked potatoes)..

    I am handling the items not allowed in the code itself.

    This method (below) is what happens when a user types /offload:

    Code:
       public void offLoad(CommandContext args, CommandSender sender) {
            if (sender instanceof Player) {
                Player p = (Player) sender;
                Block block = p.getTargetBlock(null, 100);
                boolean finish;
                if (block.getType().equals(Material.CHEST)) {
                    Block theChest = block.getLocation().getBlock();
                    Chest chestz = (Chest) theChest.getState();
                    Inventory inv = chestz.getInventory();
                    ItemStack[] itemList = p.getInventory().getContents();
                    for (ItemStack is : itemList) {
                        if (!is.getType().equals(Material.DIAMOND_SWORD) // ERROR ON THIS LINE
                                && (!is.getType().equals(Material.DIAMOND_PICKAXE)
                                && (!is.getType().equals(Material.DIAMOND_AXE)
                                && (!is.getType().equals(Material.DIAMOND_SHOVEL)
                                && (!is.getType().equals(Material.DIAMOND_HOE)
                                && (!is.getType().equals(Material.DIAMOND_HELMET)
                                && (!is.getType().equals(Material.DIAMOND_CHESTPLATE)
                                && (!is.getType().equals(Material.DIAMOND_LEGGINGS)
                                && (!is.getType().equals(Material.DIAMOND_BOOTS)
                                && (!is.getType().equals(Material.COOKED_CHICKEN)
                                && (!is.getType().equals(Material.TORCH)
                                && (!is.getType().equals(Material.BAKED_POTATO)
                                && (!is.getType().equals(Material.BOW)))))))))))))) {
                            if (!is.hasItemMeta()) { // ERROR ON THIS LINE (when above is removed)
                                int foundcount = is.getAmount();
                                if (is == null) foundcount -= is.getMaxStackSize();
                                if (is.getType() == is.getType()) {
                                    if (is.getDurability() == is.getDurability()) {
                                        foundcount -= is.getMaxStackSize() - is.getAmount();
                                        boolean canContainitem = foundcount <= 0;
                                        finish = canContainitem;
                                        if (finish = true) {
                                            inv.addItem(is);
                                            p.getInventory().remove(is);
                                            sender.sendMessage(is.toString());
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if (finish = false) {
                        sender.sendMessage("[OffLoader] Not completed. The chest is too full.");
                        return;
                    }
                    sender.sendMessage("[OffLoader] Completed.");
                } else {
                    sender.sendMessage("You must be looking at a chest to use this command.");
                }
            } else
    
            {
                sender.sendMessage("You must be a player.");
            }
        }
    Any help would be greatly appreciated :)
    Thanks.

    EDIT : // Please note, the code is all over the place as I've been trying to do this for an hour or so and have tried a few things.. can't get it. Thanks :)
     
  2. Offline

    KarimAKL

    @TheManiacGamers
    1. I'm pretty sure you are supposed to use '==' and not '.equals()' with 'is.getType()', don't know how much of a difference it does tho. :7
    2. What does the error say?
     
  3. Offline

    TheManiacGamers

    Oops, forgot to post the log.
    It was saying null pointer exception (from memory) I’ll check ASAP

    Edit : // just confirmed, it was in fact saying NullPointerException.

    I’ll try changing it to == instead, if someone else has any other suggestions, or knows it’s the == that’s causing it, that would be greatly appreciated. Thanks :)

    Edit 2 : // I would like to say, I don’t know if that would be the problem as the error is happening on another line (!is.hasItemMeta).. I’m stumped lol.
     
    Last edited: Nov 6, 2018
  4. Offline

    timtower Administrator Administrator Moderator

  5. Offline

    TheManiacGamers

    I actually cannot believe I didn’t do a null check. I’ll do that when I get on my PC.

    Thank you. Will mark as solved once tested
     
    timtower likes this.
  6. Offline

    KarimAKL

    TheManiacGamers likes this.
Thread Status:
Not open for further replies.

Share This Page