Problems with setting cursor and current item inventoryclickevent? And it fires twice

Discussion in 'Spigot Plugin Development' started by danablend, May 21, 2016.

  1. I have this code currently and what it should do is take the cursor item, check it for lore etc.. if it meets requirements it will set the current item (the item that was clicked) to a specific item with specific lore. Now my problem is that when it happens, in my debug messages it says that the new item was created, but the problem is that when I do event.setCursor(Material.AIR) OR event.getCursor().setType(Material.AIR) it does not actually remove the item i clicked with that should make the new one.
    So for example if i have a nether star with the required lores, I have it on my cursor, I click the item i want to use it on, that item will then disappear and the nether star will be put into the slot where the clicked item was. Debug messages says that the new item got its lore applied, but apparently the cursor item never was removed? This is the current code as it looks:
    Code (Text):
                    List<String> newLore = new ArrayList<String>();
                    newLore.add(applyLore);
                    currentItemMeta.setLore(newLore);
                    currentItem.setItemMeta(currentItemMeta);
                    System.out.println(event.getCursor().getType());
                    event.setCancelled(true);
                    event.setCursor(new ItemStack(Material.AIR));
                    ParticleUtil.spellParticles(player);
                    player.getWorld().playSound(player.getLocation(), Sound.LEVEL_UP, 1, 0);
                    player.updateInventory();
    Debug messages look something like this (WHEN APPLYING):
    Code (Text):
    [12:16:59 INFO]: ItemStack{DIAMOND_BOOTS x 1, UNSPECIFIC_META:{meta-type=UNSPECIFIC, lore=[ºbStrength I}}
    [12:16:59 INFO]: ItemStack{BOOK x 1, UNSPECIFIC_META:{meta-type=UNSPECIFIC, display-name = "ºcStrength Upgrader", lore=[Use this on a piece of gear, to upgrade its strength!]}}
    After this, the book still exists, and the Diamond Boots are gone
     
    #1 danablend, May 21, 2016
    Last edited: May 21, 2016
  2. Ok I managed to make it "work" better. It still doesnt work properly though. I moved the event.setCancelled(true) up in the code and now it applies the upgrade to the boots, but if it already has the upgrade on, it will keep destroying books. I already have a check to look after that but it doesnt work properly.
     
  3. try
    Code (Java):
    event.setCursor(null);
     
  4. That doesn't change anything for me, also I have another problem being that the event seems to fire multiple times. I have seen it fire 2-3 times, never only once, always 2-3 times. I have made 100% sure its not registered twice or 3 times, and also ill just paste the whole class in here:
    Code (Text):
    package dev.danablend.randomArmorEffects.Books.Apply;

        private ArrayList<CustomEnchantment> eliteEnchants = new ArrayList<CustomEnchantment>();

        private String whiteScroll;

        public EliteApplyListener() {
            whiteScroll = ChatColor.WHITE + "" + ChatColor.BOLD + "PROTECTED";
            for (CustomEnchantment ench : CustomEnchantment.getAllInstances()) {
                if (ench.getTier().equals(CustomEnchantTier.Elite) && !eliteEnchants.contains(ench)) {
                    eliteEnchants.add(ench);
                }
            }
        }

        @SuppressWarnings("deprecation")
        @EventHandler
        private void inventoryClickEvent(InventoryClickEvent event) {
            if(event.getAction() != InventoryAction.PLACE_ALL) return;
            if (event.getCursor() == null || event.getCursor().getType().name().endsWith("ENCHANTED_BOOK") || !(event.getCursor().getType().name().endsWith("BOOK"))) {
                return;
            }
            Player player = (Player) event.getWhoClicked();
            if (!(event.getCursor().hasItemMeta()) || !(event.getCursor().getItemMeta().hasDisplayName()) || !(event.getCursor().getItemMeta().hasLore()) || event.getCursor() == null || event.getCurrentItem() == null) {
                return;
            }
            ItemStack currentItem = event.getCurrentItem();
            ItemMeta currentItemMeta = currentItem.getItemMeta();

            String display = event.getCursor().getItemMeta().getDisplayName();
            int level = MathUtil.romanToDecimal(display.substring(display.lastIndexOf(" ")).trim());

            String applyLore = ChatColor.translateAlternateColorCodes('&', "&b" + ChatColor.stripColor(display.substring(0, display.lastIndexOf(" "))) + " " + MathUtil.convertToRoman(level));
            String identifier = ChatColor.stripColor(applyLore.substring(0, applyLore.lastIndexOf(" ")).trim().replaceAll(" ", ""));

            if (!EnumUtils.isValidEnum(CustomEnchant.class, identifier))
                return;

            CustomEnchantment enchantment = null;
            for (CustomEnchantment ench : eliteEnchants) {
                if (ench.getType().equals(CustomEnchant.valueOf(identifier))) {
                    enchantment = ench;
                }
            }
            if (enchantment == null)
                return;
            boolean shouldReturn = true;
            switch (enchantment.getPiece()) {
            case ARMOR:
                if (ItemUtil.isArmor(currentItem.getType())) {
                    shouldReturn = false;
                }
                break;
            case WEAPONS:
                if (ItemUtil.isWeapon(currentItem.getType())) {
                    shouldReturn = false;
                }
                break;
            case TOOLS:
                if (ItemUtil.isTool(currentItem.getType())) {
                    shouldReturn = false;
                }
                break;
            case HELMET:
                if (ItemUtil.isHelmet(currentItem.getType())) {
                    shouldReturn = false;
                }
                break;
            case CHESTPLATE:
                if (ItemUtil.isChestplate(currentItem.getType())) {
                    shouldReturn = false;
                }
                break;
            case LEGGINGS:
                if (ItemUtil.isLeggings(currentItem.getType())) {
                    shouldReturn = false;
                }
                break;
            case BOOTS:
                if (ItemUtil.isBoots(currentItem.getType())) {
                    shouldReturn = false;
                }
                break;
            case SWORD:
                if (ItemUtil.isSword(currentItem.getType())) {
                    shouldReturn = false;
                }
                break;
            case AXE:
                if (ItemUtil.isAxe(currentItem.getType())) {
                    shouldReturn = false;
                }
                break;
            case BOW:
                if (ItemUtil.isBow(currentItem.getType())) {
                    shouldReturn = false;
                }
                break;
            case PICKAXE:
                if (ItemUtil.isPickaxe(currentItem.getType())) {
                    shouldReturn = false;
                }
                break;
            case SHOVEL:
                if (ItemUtil.isShovel(currentItem.getType())) {
                    shouldReturn = false;
                }
                break;
            case HOE:
                if (ItemUtil.isHoe(currentItem.getType())) {
                    shouldReturn = false;
                }
                break;
            }
            if (shouldReturn)
                return;
            int success = MathUtil.firstNumberSequence(ChatColor.stripColor(event.getCursor().getItemMeta().getLore().get(0)));
            int destroy = MathUtil.firstNumberSequence(ChatColor.stripColor(event.getCursor().getItemMeta().getLore().get(1)));
            Random r = new Random();
            int randomSuccess = r.nextInt(100);
            int randomDestroy = r.nextInt(100);
            System.out.println("Success: " + success + "   Random Success: " + randomSuccess);
            System.out.println("Destroy: " + destroy + "   Random Destroy: " + randomDestroy);
            System.out.println("It has succeeded is " + (randomSuccess <= success));
            System.out.println("It has failed is " + (randomDestroy <= destroy));
            if (randomSuccess <= success) {
                if (event.getCurrentItem().hasItemMeta() && event.getCurrentItem().getItemMeta().hasLore()) {
                    List<String> currentItemLore = currentItemMeta.getLore();
                    if (currentItemLore.contains(applyLore))
                        return;
                    if (event.getCurrentItem().getItemMeta().getLore().contains(whiteScroll)) {
                        event.setCancelled(true);
                        currentItemLore.remove(whiteScroll);
                        currentItemLore.add(applyLore);
                        currentItemLore.add(whiteScroll);
                        currentItemMeta.setLore(currentItemLore);
                        currentItem.setItemMeta(currentItemMeta);
                        System.out.println(event.getCursor().getType());
                        event.setCursor(null);
                        System.out.println(event.getCursor().getType());
                        player.updateInventory();
                        System.out.println("Added - Whitescroll");

                        ParticleUtil.spellParticles(player);
                        player.getWorld().playSound(player.getLocation(), Sound.LEVEL_UP, 1, 0);
                        player.updateInventory();
                        event.setCancelled(true);
                        return;
                    } else {
                        event.setCancelled(true);
                        currentItemLore.add(applyLore);
                        currentItemMeta.setLore(currentItemLore);
                        currentItem.setItemMeta(currentItemMeta);
                        System.out.println(event.getCursor().getType());
                        event.setCursor(null);
                        System.out.println(event.getCursor().getType());
                        System.out.println("Added - Normal with lore");

                        ParticleUtil.spellParticles(player);
                        player.getWorld().playSound(player.getLocation(), Sound.LEVEL_UP, 1, 0);
                        player.updateInventory();
                        event.setCancelled(true);
                        return;
                    }
                } else {
                    event.setCancelled(true);
                    List<String> newLore = new ArrayList<String>();
                    newLore.add(applyLore);
                    event.setCursor(null);
                    currentItemMeta.setLore(newLore);
                    currentItem.setItemMeta(currentItemMeta);
                    ParticleUtil.spellParticles(player);
                    player.getWorld().playSound(player.getLocation(), Sound.LEVEL_UP, 1, 0);
                    player.updateInventory();
                    System.out.println("Added - Normal without lore");
                    event.setCancelled(true);
                    return;
                }
            } else if (randomDestroy <= destroy) {
                System.out.println("Destroy is higher than random destroy");
                if (event.getCurrentItem().hasItemMeta() && event.getCurrentItem().getItemMeta().hasLore()) {
                    List<String> currentItemLore = currentItemMeta.getLore();
                    if (currentItemLore.contains(applyLore))
                        return;
                    if (event.getCurrentItem().getItemMeta().getLore().contains(whiteScroll)) {
                        event.setCancelled(true);
                        currentItemLore.remove(whiteScroll);
                        currentItemMeta.setLore(currentItemLore);
                        currentItem.setItemMeta(currentItemMeta);
                        event.setCursor(null);
                        player.updateInventory();
                        ParticleUtil.lavaParticles(player);
                        player.playSound(player.getLocation(), Sound.LAVA_POP, 1, 1);
                        System.out.println("destroyed - whitescroll");
                        return;
                    } else {
                        event.setCancelled(true);
                        event.setCurrentItem(new ItemStack(Material.AIR));
                        event.setCursor(null);
                        player.updateInventory();
                        ParticleUtil.lavaParticles(player);
                        player.playSound(player.getLocation(), Sound.LAVA_POP, 1, 1);
                        System.out.println("destroyed - lore");
                        return;
                    }
                } else {
                    event.setCancelled(true);
                    event.setCurrentItem(new ItemStack(Material.AIR));
                    event.setCursor(null);
                    player.updateInventory();
                    ParticleUtil.lavaParticles(player);
                    System.out.println("destroyed - no lore");
                    player.playSound(player.getLocation(), Sound.LAVA_POP, 1, 1);
                    return;
                }
            } else {
                event.setCancelled(true);
                event.getCursor().setType(Material.AIR);
                player.updateInventory();
                System.out.println("silent fail");
                ParticleUtil.lavaParticles(player);
                player.playSound(player.getLocation(), Sound.LAVA_POP, 1, 1);
                return;
            }
        }
    }
     
     
  5. what does the console logs' said when its firing 2-3 times like what you've said ?
     
  6. Copied from console when i used a book 1 time on the boots:
    Code (Text):
    [14:20:48 INFO]: Success: 58   Random Success: 73
    [14:20:48 INFO]: Destroy: 40   Random Destroy: 78
    [14:20:48 INFO]: It has succeeded is false
    [14:20:48 INFO]: It has failed is false
    [14:20:48 INFO]: silent fail
    [14:20:48 INFO]: Success: 58   Random Success: 1
    [14:20:48 INFO]: Destroy: 40   Random Destroy: 60
    [14:20:48 INFO]: It has succeeded is true
    [14:20:48 INFO]: It has failed is false
    [14:20:48 INFO]: Added - Normal without lore
    [14:20:48 INFO]: Success: 58   Random Success: 61
    [14:20:48 INFO]: Destroy: 40   Random Destroy: 27
    [14:20:48 INFO]: It has succeeded is false
    [14:20:48 INFO]: It has failed is true
    [14:20:48 INFO]: Destroy is higher than random destroy
    In this case it called 3 times in a single click.
     
  7. well thats strange

    would you mind trying this way

    at the last else statement

    Code (Java):
            } else {
                event.setCancelled(true);
                event.getCursor().setType(Material.AIR);
                player.updateInventory();
                System.out.println("silent fail");
                ParticleUtil.lavaParticles(player);
                player.playSound(player.getLocation(), Sound.LAVA_POP, 1, 1);
                return;
            }
       // add return statement here
       return;
       // over and over those after "}" only at the public void "Event"
    }
     
  8. Still doesnt cut it sadly :/ Still fires 2-3 times each time.