Can't give player my item

Discussion in 'Spigot Plugin Help' started by JLMR_, Jul 7, 2021.

  1. package com.JLMR.Gotaria.commands;

    import com.JLMR.Gotaria.items.ItemManager;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    public class commands implements CommandExecutor {

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    if (!(sender instanceof Player)) {return true;}
    sender.sendMessage("Only players can use this command!");


    // /givedelta
    Player player = (Player) sender;
    if (command.getName().equalsIgnoreCase("givedelta")) {
    player.getInventory().addItem(ItemManager.staff);
    }


    return true;
    }
    }

    I have this, but if the player runs the command they don't get the item
    What do i do?
     
  2. Did you register the class "commands" as the command executor?
    Did you add the command in the plugin's "plugin.yml" file as a new command?
     
  3. I did
     
  4. Change the function to this:
    Code (Java):
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (!(sender instanceof Player)) {
            sender.sendMessage("Only players can use this command!");
            return true;
        }
        Player player = (Player) sender;
        player.getInventory().addItem(ItemManager.staff);
        player.sendMessage("You received the staff!");
        return true;
    }
    If this doesn't work then you didn't register the command executor properly.
     
  5. Strahan

    Benefactor

    Is ItemManager.staff actually returning something? If the above code does nothing still, add this as the first line in the method:
    Code (Text):
    sender.sendMessage("Staff is a " + (ItemManager.staff == null?"null item":ItemManager.staff.getType().name()));
    and see if it says the material of the staff or null item.