SSync
SSync

Reputation: 3

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.

Upvotes: 0

Views: 2379

Answers (2)

bn4t
bn4t

Reputation: 1640

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.

Upvotes: 0

LeoColman
LeoColman

Reputation: 7143

For your problem and what was discussed in the comments, the solution would be to double-check your imports, and verify that you're importing both ItemStack and Material from the Bukkit API.

import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;

Instead of importing anything from net.minecraft.server.vXX for this behaviour, as you were doing.

Upvotes: 2

Related Questions