Getting a player's item in hand

Discussion in 'Plugin Development' started by Gopaintman, Jul 2, 2013.

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

    Gopaintman

    Hi everyone.
    I'm trying to make a plugin that if a player has a sponge, portal, end portal frame or melonstem in their hand and do /tenchant the item will be enchanted. However I'm having a problem with the server detecting that the player has the specified item in their hand. I've tried it with Item IDs however that gives me the same problem
    Also if the player left/right clicks it will say the else statement message.

    Code:java
    1.  
    2.  
    3. package mc.katsaroucraft;
    4.  
    5. import java.util.logging.Logger;
    6.  
    7. import net.minecraft.server.v1_6_R1.Block;
    8.  
    9. import org.bukkit.Bukkit;
    10. import org.bukkit.Material;
    11. import org.bukkit.command.Command;
    12. import org.bukkit.command.CommandSender;
    13. import org.bukkit.entity.Player;
    14. import org.bukkit.inventory.ItemStack;
    15. import org.bukkit.plugin.PluginManager;
    16. import org.bukkit.plugin.java.JavaPlugin;
    17.  
    18. public class TokenEnchant extends JavaPlugin{
    19. private static final Logger log = Logger.getLogger("Minecraft");
    20. private final TEListener playerListener = new TEListener(this);
    21. public static TokenEnchant plugin;
    22.  
    23.  
    24. @Override
    25. public void onEnable(){
    26. log.info("==TokenEnchant has been enabled!==");
    27. PluginManager pm = Bukkit.getPluginManager();
    28. pm.registerEvents(this.playerListener, this);
    29. }
    30. @Override
    31. public void onDisable(){
    32. log.info("==TokenEnchant has been disabled!");
    33. }
    34. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    35. Player player = (Player)sender;
    36.  
    37.  
    38. //Checks to see if /tenchant is said
    39. if((cmd.getName().equalsIgnoreCase("TEnchant"))){
    40. player.sendMessage("Test to make sure plugin is functioning");
    41. //String to match items with their respective names
    42. String mstem = "Melonstem";
    43. String spnge = "Sponge";
    44. String endpFrame = "End Portal Frame";
    45. String portal = "Portal";
    46. //String of what the player is holding in game.
    47. String y = new ItemStack(player.getInventory().getItemInHand()).toString();
    48.  
    49. if(player.isOp() || player.hasPermission("tokenenchant.use")){
    50. //Checks to see if player's in hand item matches up with String.
    51. if(spnge == y || mstem == y || endpFrame == y || portal == y){
    52. player.sendMessage("Enchanting...");
    53.  
    54.  
    55. }
    56. else{
    57. player.sendMessage("You don't have a token ");
    58. }
    59.  
    60.  
    61. }
    62.  
    63. else{
    64. player.sendMessage("You don't have permissions");
    65. }
    66. }
    67.  
    68.  
    69. return false;
    70.  
    71.  
    72.  
    73.  
    74. }
    75. }
    76.  
     
  2. Offline

    Jake0oo0

    Just try
    if(player.getInventory().getItemInHand() == Material.SOMETHING || player.getInventory().getItemInHand() == Material.SOMETHING)){
    //run code
     
  3. Offline

    Henzz

    Gopaintman
    player.getItemInHand().getType().equals(Material.SPONGE)
     
    foodyling likes this.
  4. Offline

    Jake0oo0

    Henzz No Material is an Enum
     
  5. Offline

    Henzz

    Jake0oo0
    You are checking if an itemstack is equal to a Material.
     
    foodyling likes this.
  6. Offline

    Jake0oo0

    Oops derp then:
    if(player.getInventory().getItemInHand().getType() == Material.SOMETHING || player.getInventory().getItemInHand() == Material.SOMETHING)){
     
  7. Offline

    foodyling

    You *could* do this if you have a huge list of items.
    Code:
    if (player.getItemInHand() != null && Arrays.asList(Material.SOMETHING1, Material.SOMETHING2).contains(player.getItemInHand().getType())) {
        // Code         
    }
    However I'm unsure on how efficient it would be :x
     
  8. Offline

    Gopaintman

    Thanks! Alright you guys got it right.
     
Thread Status:
Not open for further replies.

Share This Page