InventoryClickEvent Trouble

Discussion in 'Plugin Development' started by xxPatterson, Nov 30, 2015.

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

    xxPatterson

    Code:
    @EventHandler
        public void onScrollUse(InventoryClickEvent e) {
            if(e.getCursor() == null) { return; }
            if(e.getCurrentItem() == null) { return; }
            ItemStack cursor = e.getCursor();
            ItemStack in_slot = e.getCurrentItem();
            Material in = e.getCurrentItem().getType();
            boolean win = true;
           
            if(!e.getInventory().getName().equalsIgnoreCase("container.crafting")) { return; }
            if(e.getInventory().getViewers().size() > 1) { return; }
            if(e.getSlotType() == SlotType.ARMOR) { return; }
           
            Player p = (Player) e.getWhoClicked();
           
            if(isWhiteScroll(cursor) &&  CustomEnchants.isHelmet(in) || CustomEnchants.isChestplate(in) ||CustomEnchants.isLeggings(in) ||CustomEnchants.isBoots(in) ||
                    CustomEnchants.isSword(in) ||CustomEnchants.isAxe(in) ||CustomEnchants.isPick(in)) {
                p.sendMessage("WORKS");
                // Add protection.
               
               
                if(hasProtection(in_slot)) {
                    p.sendMessage(ChatColor.RED + "This item already has 'protected' enchantment status.");
                    e.setCancelled(true);
                    p.updateInventory();
                    return;
                }
               
                if(cursor.getAmount() == 1) {
                    e.setCancelled(true);
                    e.setCursor(new ItemStack(Material.AIR));
                } else if(cursor.getAmount() > 1) {
                    e.setCancelled(true);
                    cursor.setAmount(cursor.getAmount() - 1);
                    e.setCursor(cursor);
                }
               
                e.setCurrentItem(addProtection(in_slot));
                p.sendMessage(ChatColor.GREEN + "Your " + in_slot.getItemMeta().getDisplayName() + ChatColor.GREEN + " is now protected -- even if an enchant book fails, it will " + ChatColor.UNDERLINE + "NOT" + ChatColor.GREEN + " be destroyed.");
                p.playSound(p.getLocation(), Sound.LEVEL_UP, 1F, 1F);
               
                Firework fw = (Firework) p.getWorld().spawnEntity(p.getLocation(), EntityType.FIREWORK);
                FireworkMeta fwm = fw.getFireworkMeta();
                FireworkEffect effect = FireworkEffect.builder().flicker(false).withColor(Color.GREEN).withFade(Color.GREEN).with(Type.STAR).trail(true).build();
                fwm.addEffect(effect);
                fwm.setPower(0);
                fw.setFireworkMeta(fwm);
            }
           
            if(isEnchantBook(cursor) &&  CustomEnchants.isHelmet(in) || CustomEnchants.isChestplate(in) ||CustomEnchants.isLeggings(in) ||CustomEnchants.isBoots(in) ||
                    CustomEnchants.isSword(in) ||CustomEnchants.isAxe(in) ||CustomEnchants.isPick(in)) {
                String ench = getCustomEnchant(cursor);
               
                if(!isCorrectBook(cursor, in_slot)) { return; }
                if(hasEnchant(cursor, ench)) { return; }
               
                boolean white_scroll = hasProtection(in_slot);
    
                if(cursor.getAmount() == 1) {
                    e.setCancelled(true);
                    e.setCursor(new ItemStack(Material.AIR));
                } else if(cursor.getAmount() > 1) {
                    e.setCancelled(true);
                    cursor.setAmount(cursor.getAmount() - 1);
                    e.setCursor(cursor);
                }
                  Random r = new Random();
                 int Chance = r.nextInt(100) + 1;
                    int Success = getSuccessRate(cursor);
                    int Destroy = getDestroyRate(cursor);
                    if(Chance < Success) {
                        e.setCurrentItem(addEnchant(in_slot, ench));
                        return;
                }
                    ItemStack nothing = new ItemStack(Material.AIR);
                    e.setCurrentItem(nothing);
            }
            }
    
    Currently I am having trouble with this, I have registered all the events and yet when I place the whitescroll on any piece of sword/armor it doesn't work, any ideas?
     
  2. Offline

    Zombie_Striker

    @xxPatterson
    Can you pinpoint what lines might be the problem? What line applies and/or deals with the data you are looking for?
     
  3. Offline

    xxPatterson

    Code:
    e.getWhoClicked().sendMessage(Main.getPrefix() + " Works"); 
    I added this line directly at the top of the event to see if the event is even working and turns out when I click in my inventory this message isn't sending therefore the whole even is broken, I have no clue why and I'm not getting any StackTraces
     
  4. Offline

    Zombie_Striker

    @xxPatterson
    Are you registering the events in that class? Do other events work?
     
  5. Offline

    sgavster

Thread Status:
Not open for further replies.

Share This Page