Solved Bukkit setItemInHand command.

Discussion in 'Plugin Development' started by ghksla2, Apr 5, 2020.

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

    ghksla2

    I'm trying to make a way to run the command /setitem {item} and have your selected hotbar slot be changed to that item. My script came up with no errors, but when I put it into a testing world and used the command it wouldn't work.
    My program:

    Code:
    package me.ghksla2.HelloWorld.commands;
    
    
    
    import org.bukkit.command.Command;
    
    import org.bukkit.command.CommandExecutor;
    
    import org.bukkit.command.CommandSender;
    
    import org.bukkit.entity.Player;
    
    import me.ghksla2.HelloWorld.Main;
    
    
    
    public class GiveCommand implements CommandExecutor{
    
    
    
        @SuppressWarnings("unused")
    
                private Main plugin;
    
              
    
                public GiveCommand(Main plugin) {
    
                    this.plugin = plugin;
    
                    plugin.getCommand("setitem").setExecutor(this);
    
                }
    
                @Override
    
            public boolean onCommand(CommandSender sender, Command cmd, String alias, String[] args) {
    
            if (!(sender instanceof Player)) {
    
                sender.sendMessage("Only players can hold items.");
    
            }
    
            Player p = (Player) sender;
    
            Object item = args[0];
    
            if (p.hasPermission("operator")) {
    
                p.getInventory().setItemInHand((org.bukkit.inventory.ItemStack)item);
    
                p.updateInventory();
    
            } else {
    
                p.sendMessage("You don't have permission to do this.");
    
                returntrue;
    
            }
    
            returnfalse;
    
    
    
                }
    
    }
    
    Can someone help me with this issue?
     
    Last edited by a moderator: Apr 5, 2020
  2. Offline

    timtower Administrator Administrator Moderator

    @ghksla2 You can't cast a string to an Itemstack
     
  3. To clarify, you should do:
    Code:
    p.getInventory().setItemInHand(new ItemStack(Material.valueOf(args[0]));
    Material#valueOf() gets the material, and then you can make a new itemstack with that material.

    EDIT: As @KarimAKL pointed out, it is better to use Material.matchMaterial(args[0])
     
    Last edited: Apr 6, 2020
  4. Offline

    KarimAKL

    @pietermantel Use Material#matchMaterial(String) instead, that doesn't throw an IllegalArgumentException, and it even ignores cases.
     
    pietermantel likes this.
  5. Offline

    ghksla2

    Thanks i'm very new at this so I didn't know that I couldn't set args to a string.

    One last question, when I put in matchMaterial or valueOf they come up with errors and say that the method is undefined, what does this mean?


    Edit: Never mind I figured it out

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Apr 6, 2020
  6. Offline

    Strahan

    Also stacking methods with potentially bad data is not advisable. You should validate the input as they may not pass a valid material name, handle that then once validated you create the ItemStack and give it to them.
     
  7. Offline

    ghksla2

    @Strahan I would like to validate the input of the player, however I don't know how.
     
  8. Offline

    Strahan

    Code:
    Material targetMaterial = Material.matchMaterial(args[0]);
    if (targetMaterial == null) {
      p.sendMessage("Dumbass");
      return true;
    }
    
    p.getInventory().setItemInHand(new ItemStack(targetMaterial));
    If you are using 1.9+, use setItemInMainHand to avoid deprecated functions. Also don't suppress warnings, you should address what it is telling you.
     
Thread Status:
Not open for further replies.

Share This Page