Solved Check If Player Has Item IN Hand

Discussion in 'Plugin Development' started by xXkguyXx, Jul 7, 2015.

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

    xXkguyXx

    I want OT check if a player has a DIAMOND_SWORD in there hand if not or they have nothing it sends them a message saying it can only be put on a DiamondSword, And if they do have a diamond sword, it adds a Enchantment.

    package MagEnchant.Main;

    import net.milkbowl.vault.economy.Economy;

    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;

    import com.rit.sucy.EnchantmentAPI;

    public class Main extends JavaPlugin {

    public static Economy econ = null;

    public void onEnable() {
    }

    public void onDisable() {
    }

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label,
    String[] args) {
    Player p = (Player) sender;
    if (cmd.getName().equalsIgnoreCase("Test"));

    if (p.getItemInHand().equals(Material.DIAMOND_SWORD));
    ItemStack test = p.getItemInHand();
    EnchantmentAPI.getEnchantment("Lifesteal").addToItem(test, 10);
    p.sendMessage(ChatColor.GREEN + "Your Enchantment has Been Added!");




    return false;
    }
    }
     
  2. Offline

    CrystallFTW

    @xXkguyXx Something like

    Code:
    if(p.getItemInHand().getType() == Material.DIAMOND_SWORD){
    p.getItemInHand().addEnchantment(Enchantment.DAMAGE_ALL,1);
    }else{
    p.sendMessage("it can only be put on a DiamondSword");
    }
    
    This will add sharpness if a player has a diamond sword in their hand.

    And learn basic java before starting to use BukkitAPI :D You have so many mistakes, for example
    your command wouldn't work because you placed a ; after the command. You should put a { and
    close it when you want it to finish what to do.
    For example

    Code:
    if (cmd.getName().equalsIgnoreCase("Test")){
    if(p.getItemInHand().equals(Material.DIAMOND_SWORD)){
    p.getItemInHand().addEnchantment(Enchantment.DAMAGE_ALL,1);
    }else{
    p.sendMessage("it can only be put on a DiamondSword");
    }
    return true;
    }
    BTW: Before casting player to sender, check if sender instanceof Player
     
    Last edited: Jul 7, 2015
  3. Offline

    xXkguyXx

    It doesnt work even if the diamond_sword is in the Hand.
     
  4. Offline

    CrystallFTW

  5. @CrystallFTW
    p.getItemInHand().getType()* and it's recommended to compare enums using == :p
     
  6. Offline

    CrystallFTW

  7. Offline

    xXkguyXx

    Thank you guys it works.
     
  8. Offline

    BagduFagdu

    @megamichiel I've read that, but do you know why by any chance?
     
  9. @BagduFagdu
    I wasn't sure actually, but I heard people say it :p. I just looked on google, and the main reason is that it's null safe, meaning that when you do object.equals(Enum) you get a nullpointerexception if object is null, but when you do object == Enum, you don't get a nullpointerexception since no methods are called on the object.
     
  10. Check if its null or if its air:
    Code:
    boolean hasItemInHand = false;
    ItemStack inHand = p.getItemInHand();
    if(inHand==null || inHand.getType()==Material.AIR) hasItemInHand=true;
     
  11. Offline

    DoggyCode™

    Umm.. btw, next time you need help with your existing code, could you use this feature:?

    (open-bracket)code=java(closed-bracket)
    your code
    (open-bracket)/code(closed-bracket)

    So your code would appear like this:
    Code:
    package MagEnchant.Main;
    
    import net.milkbowl.vault.economy.Economy;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import com.rit.sucy.EnchantmentAPI;
    
    public class Main extends JavaPlugin {
    
    public static Economy econ = null;
    
    public void onEnable() {
    }
    
    public void onDisable() {
    }
    
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label,
    String[] args) {
    Player p = (Player) sender;
    if (cmd.getName().equalsIgnoreCase("Test"));
    
    if (p.getItemInHand().equals(Material.DIAMOND_SWORD));
    ItemStack test = p.getItemInHand();
    EnchantmentAPI.getEnchantment("Lifesteal").addToItem(test, 10);
    p.sendMessage(ChatColor.GREEN + "Your Enchantment has Been Added!");
    
    
    
    
    return false;
    }
    }
    
    Much easier to read :p
     
  12. Offline

    xXkguyXx

    Code:
    ahhhh thanks
    
     
Thread Status:
Not open for further replies.

Share This Page