Spawn an item


#1

I want to spawn an item as an entity, but I can´t get information about, and the information I got is outdated… also I want to know if it´s posible to change the variant from an ItemStack. Thanks for your help.


#2

So spawning Entities hasnt really changed much for a while I believe but here we go. First of all we need the location of where we will spawn the entity.

Location<World> loc;

After that we want to create the entity (but not spawn it in the world yet)

Item item = (Item)loc.createEntity(EntityTypes.ITEM);

From here we want to apply what we want to the item. So im just going to change what item it is, change anything you want.

ItemStackSnapshot stack;
item.offer(Keys.REPRESENTED_ITEM, stack);

After all the changes you want are applied you need to spawn the entity.

loc.spawnEntity(item);