Removing Items

Discussion in 'Plugin Development' started by KoolzSkillz, Nov 28, 2014.

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

    KoolzSkillz

    Hey, im making a plugin where when you click a sign it removes a cookie from your inventory. only problem is if you have more than 1 cookie it doesnt remove it

    Code:
                    } if (sign.getLine(2).equalsIgnoreCase("Cookie") && sign.getLine(3).equalsIgnoreCase("1")) {
                        if (!pi.contains(Material.COOKIE)) {
                            p.sendMessage(ChatColor.RED + "You must have at least 1 cookie to use this sign");
                            return;
                        }
                                p.sendMessage(ChatColor.GREEN + "Pickaxe Dig Speed, Upgraded");
                                        settings.saveConfig();
                                        iih.addUnsafeEnchantment(
                                                Enchantment.DIG_SPEED,
                                                iih.getEnchantmentLevel(Enchantment.DIG_SPEED) + 1);   
                                        ItemStack Cookie = new ItemStack (Material.COOKIE, 1);
                                        pi.remove(Cookie);
                                        p.updateInventory();
     
  2. Offline

    Skionz

    Iterate through the players inventory, check if the current ItemStacks type is a cookie, remove it.
     
  3. Offline

    KoolzSkillz

    Skionz
    i just want to remove 1 cookie from a stack tho, not the whole stack
     
  4. Offline

    Skionz

    KoolzSkillz Then remove the ItemStack and add a similar ItemStack with one less cookie.
     
  5. Offline

    KoolzSkillz

    Skionz
    How Would i do this if the player isnt holding the cookies?
     
  6. Offline

    Skionz

    KoolzSkillz As I said before, Iterate through the players inventory, check if the current ItemStack's type is a cookie, remove that ItemStack and replace it with a similar ItemStack with 1 less amount.
     
  7. Offline

    KoolzSkillz

    Skionz

    I already checked if its a cookie, but it was scrolled up in the coding
    Code:
        @EventHandler
        public void interactSign(PlayerInteractEvent e) {
            Player p = e.getPlayer();
            PlayerInventory pi= p.getInventory();
            ItemStack iih = p.getInventory().getItemInHand();
     
            if (e.getAction() != Action.RIGHT_CLICK_BLOCK) {
                return;
            }
            if (!(e.getClickedBlock().getState() instanceof Sign)) {
                return;
            }
            final Sign sign = (Sign) e.getClickedBlock().getState();
            if (sign.getLine(0).equalsIgnoreCase(
                    ChatColor.stripColor("[Pickaxe++]"))
                            && sign.getLine(1).equalsIgnoreCase("Dig Speed")) {
                if (!(iih.getType() == Material.DIAMOND_PICKAXE
                        || iih.getType() == Material.GOLD_PICKAXE
                        || iih.getType() == Material.IRON_PICKAXE
                        || iih.getType() == Material.STONE_PICKAXE
                        || iih.getType() == Material.WOOD_PICKAXE)) {
                    p.sendMessage("§cPlease Hold A Pickaxe");
                } if (iih.getType() == Material.DIAMOND_PICKAXE
                            || iih.getType() == Material.GOLD_PICKAXE
                            || iih.getType() == Material.IRON_PICKAXE
                            || iih.getType() == Material.STONE_PICKAXE
                            || iih.getType() == Material.WOOD_PICKAXE) {
                    if (!sign.getLine(2).equalsIgnoreCase("Cookie") || sign.getLine(2).equalsIgnoreCase("Cake") || sign.getLine(2).equalsIgnoreCase("Diamond")) {
                    p.sendMessage("§cSorry M8, Only Cookies, Cakes, and Diamonds work ATM");
                    } if (sign.getLine(2).equalsIgnoreCase("Cookie") && sign.getLine(3).equalsIgnoreCase("1")) {
                        if (!pi.contains(Material.COOKIE)) {
                            p.sendMessage(ChatColor.RED + "You must have at least 1 cookie to use this sign");
                            return;
                        }
                                p.sendMessage(ChatColor.GREEN + "Pickaxe Dig Speed, Upgraded");
                                        settings.saveConfig();
                                        iih.addUnsafeEnchantment(
                                                Enchantment.DIG_SPEED,
                                                iih.getEnchantmentLevel(Enchantment.DIG_SPEED) + 1);
                                        ItemStack is = new ItemStack (Material.COOKIE, 1);
                                        pi.remove(Material.COOKIE);
                                        p.updateInventory();
                       
                       
                       
                        
     
  8. Offline

    Skionz

    KoolzSkillz Read my above messages. remove(Material) removes all ItemStacks thats type equals Material.COOKIE
     
Thread Status:
Not open for further replies.

Share This Page