Search code examples
javaminecraftbukkit

new ItemStack() The constructor ItemStack(Material, int) is undefined


So I am making a Minecraft server currently with my friend, and I have come across an error that I can't seem to fix. I am making a /hat command, so players can put items and blocks on their heads. So I am trying to make it so it removes their item from their hand after they put it on their head. But I get this for the air item

The constructor ItemStack(Material, int) is undefined

Here is my code: `

import org.bukkit.ItemStack;
import net.minecraft.server.v1_8_R3.Material;

//{Class definition and other methods}

@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    Player user = (Player) sender;
    if(sender instanceof Player){
        ItemStack userItem = new ItemStack(user.getItemInHand());
        if(!userItem.equals(Material.AIR)){     
            user.getInventory().setHelmet(userItem);
            ItemStack a = new ItemStack(Material.AIR, 1); // Error happens here
            user.getInventory().setItemInHand(a);
        } else {
            user.sendMessage(ChatColor.RED+"Put an item in your hand");
        }


    }
    return true;
}

If you could fix it, that would be greatly appreciated.


Solution

  • You just imprted the wrong thing. Instead of importing

    import net.minecraft.server.v1_8_R3.Material;
    

    You need to import

    import org.bukkit.Material;
    

    Or just a simple workaround:

    Replacing

        if(!userItem.equals(Material.AIR)){     
            user.getInventory().setHelmet(userItem);
            ItemStack a = new ItemStack(Material.AIR, 1); // Error happens here
            user.getInventory().setItemInHand(a);
        } else {
            user.sendMessage(ChatColor.RED+"Put an item in your hand");
        }
    

    with

        if(!userItem.equals(Material.AIR)){     
            user.getInventory().setHelmet(userItem);
            user.getInventory().setItemInHand(null);
        } else {
            user.sendMessage(ChatColor.RED+"Put an item in your hand");
        }
    

    Both methods should fix this problem.

    Setting an item slot to null is pretty much the same as setting it to air.