Development Assistance Help with ints

Discussion in 'Plugin Help/Development/Requests' started by UniqueNameDen, Feb 16, 2015.

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

    UniqueNameDen

    So, I'm coding a plugin for my server autopickup [a lightweight 1], everything works fine, but I want fortune to work so I thought I could
    Code:
    int amount = if (e.getPlayer.getItemInHand.hasItemMeta) {
        if (e.getPlayer.getItemInHand.getEnchantment == "FORTUNE") {
            int FORTUNELVL = e.getPlayer.getItemInHand.getEnchantment.getEnchantmentLevel();
            return FORTUNELVL;
        } else {
            return 0;
        }
    }
    
    but that clearly doesn't work any help...?

    Full EventHandler code:
    http://pastebin.com/etFGX3z3
     
  2. Offline

    timtower Administrator Administrator Moderator

    @UniqueNameDen You are using an if statement to return something.
    That is not possible in java.
    Nor do you call functions, you are trying to get field values.
    And then you check if an enchantment equals a string.

    Are you using an IDE? And how long have you been coding in java?
     
  3. Offline

    pie_flavor

    @UniqueNameDen That is not valid java code. return automatically exits the method with the return value. And you can't use an if statement like that. What's more, you didn't put parentheses on any of the method calls (e.g. getItemInHand(), getEnchantmentLevel()). And almost none of that is actual Bukkit code. Here's how to do it exactly as you intended:
    Code:
    int amount = (e.getPlayer().getItemInHand().hasItemMeta() ? (e.getPlayer().getItemInHand().containsEnchantment(Enchantment.LOOT_BONUS_BLOCKS) ? e.getPlayer().getItemInHand().getEnchantmentLevel(Enchantment.LOOT_BONUS_BLOCKS) : 1) : 1);
     
    Last edited: Feb 17, 2015
  4. Offline

    UniqueNameDen

    @pie_flavor Thanks, it worked I just don't have a lot of knowledge about Strings and that kind of stuff.

    @timtower I have a decent java knowledge, and yes I use IntelliJ

    also if someone ends up using this use:1):1);, because otherwise it will give you an item with a Stacksize of 0
     
  5. Offline

    pie_flavor

Thread Status:
Not open for further replies.

Share This Page