Double condition in an if statement

Discussion in 'Spigot Plugin Development' started by SolusDuo, Jun 16, 2015.

  1. Does anyone know what could be wrong with this? I am trying to get my plugin to log diamond ore break events, but not when the player uses silk touch. I also want players who use silk touch to receive a private message as shown, and non silk users to have a message broadcast in chat. Thanks!

    Code (Text):
    public final class DiamondListener implements Listener {
       
        MainClass configGetter;
       
        public DiamondListener(MainClass plugin) {
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
            // Talks to the config file
            configGetter = plugin;
        }
       
        @EventHandler
        public void inhanditem(BlockBreakEvent event) {
           
            // Player who breaks the block
            Player player = event.getPlayer();
            // Item in player hand
            ItemStack inhand=((HumanEntity) event).getItemInHand();
            Enchantment SILK_TOUCH = new EnchantmentWrapper(33);
           
            if(inhand.containsEnchantment(SILK_TOUCH) && event.getBlock().getType() == Material.DIAMOND_ORE) {
               
                player.sendMessage(ChatColor.RED + "Mine that ore with a fortune pick, to increase your diamond count!");
         
            }else if(event.getBlock().getType() == Material.DIAMOND_ORE); {
           
                Bukkit.broadcastMessage(player.getDisplayName() + " " + ChatColor.AQUA + "mined some diamond ore!");
           
            int diacount = configGetter.getConfig().getInt("DiamondOre");
           
            configGetter.getConfig().set("DiamondOre", diacount + 1);
               
            }
           
       
        }

    }
     
  2. Try listening for
    inhand.getItemMeta().hasEnchant(Enchantment.SILK_TOUCH)
     
  3. Still receiving errors, any idea why?

    http://pastebin.com/G1qKNyzs

    Code (Text):
    public final class DiamondListener implements Listener {
     
        MainClass configGetter;
     
        public DiamondListener(MainClass plugin) {
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
            // Talks to the config file
            configGetter = plugin;
        }
     
        @EventHandler
        public void inhanditem(BlockBreakEvent event) {
         
            // Player who breaks the block
            Player player = event.getPlayer();
            // Item in player hand
            ItemStack inhand = ((Player) event).getItemInHand();
         
            if(inhand.getItemMeta().hasEnchant(Enchantment.SILK_TOUCH) && event.getBlock().getType() == Material.DIAMOND_ORE) {
             
                player.sendMessage(ChatColor.RED + "Mine that ore with a fortune pick, to increase your diamond count!");
         
            }else if(event.getBlock().getType() == Material.DIAMOND_ORE); {
         
                Bukkit.broadcastMessage(player.getDisplayName() + " " + ChatColor.AQUA + "mined some diamond ore!");
         
            int diacount = configGetter.getConfig().getInt("DiamondOre");
         
            configGetter.getConfig().set("DiamondOre", diacount + 1);
             
            }
         
     
        }

    }
     
  4. Does the code inside the else if body work?

    I dont know why you're casing stuff. For the inhand variable, just make it = player.getItemInHand();
     
  5. Line 17: You are trying to cast BlockBreakEvent to a Player.
     
    • Agree Agree x 1
  6. Code (Text):
    ItemStack inhand = ((Player) event).getItemInHand();
    There's your problem
    replace it with
    Code (Text):
    ItemStack inhand = event.getPlayer().getItemInHand();
     
  7. Thank you for being awesome guys! :D It is working now! My if else statement was not working but i fixed it :) If anyone ever finds this and wants to see how i fixed it see below. I had to make the if else check the enchant as well and not log and print the message if it was silk touch.

    Code (Text):
    public final class DiamondListener implements Listener {
       
        MainClass configGetter;
       
        public DiamondListener(MainClass plugin) {
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
            // Talks to the config file
            configGetter = plugin;
        }
       
        @EventHandler
        public void inhanditem(BlockBreakEvent event) {
           
            // Player who breaks the block
            Player player = event.getPlayer();
            // Item in player hand
            ItemStack inhand = event.getPlayer().getItemInHand();
           
            if(inhand.getItemMeta().hasEnchant(Enchantment.SILK_TOUCH) && event.getBlock().getType() == Material.DIAMOND_ORE) {
               
                player.sendMessage(player.getDisplayName() + " " + ChatColor.RED + "Mine that ore with a fortune pick, to increase your diamond count!");
         
            }
           
            else if(event.getBlock().getType() == Material.DIAMOND_ORE && inhand.getItemMeta() != Enchantment.SILK_TOUCH) {
           
                Bukkit.broadcastMessage(player.getDisplayName() + " " + ChatColor.AQUA + "mined some diamond ore!");
           
            int diacount = configGetter.getConfig().getInt("DiamondOre");
           
            configGetter.getConfig().set("DiamondOre", diacount + 1);
               
            }
           
       
        }