I've been digging around for a while, looking for a way to spawn items in the world. In essence, I have the player, I want to take an ItemStack from their inventory (so far so good), and drop it. Now I know that my question is about entity spawning, but this says dropping, but I couldn't get either to work. The first thing I did was try to use ClientPlayerEntity.dropItem, which did nothing, so I assumed that was because I used InvWrapper.extractItem beforehand. That is when I transitioned to spawning. I tried to create a new ItemEntity and use World.addEntity on it. I know I have a valid ItemEntity, and I saw some form of this idea used in other projects & minecraft code. When executing however, nothing happens. I also tried to add the ItemStack back to the inventory and drop it, but that did nothing either.
I am not sure where to go from here. Here is my code (Really starts at Functions.java):
Mod.java
@Mod("Mod")
public class Mod {
MinecraftForge.EVENT_BUS.register(clientEventHandlers);
}
ClientEventHandlers.java
class ClientEventHandlers {
@SubscribeEvent
public void onKeyRelease(GuiScreenEvent.KeyboardKeyReleasedEvent.Post event) {
if (true) { // Ignore what this says, this is just used for simplicity
Functions.myFunction();
}
}
}
Functions.java
class Functions {
static void myFunction(){
ClientPlayerEntity player = Minecraft.getInstance().player;
InvWrapper invWrapper = new InvWrapper(player.inventory);
ArrayList<ItemStack> removed;
removed.add(invWrapper.extractItems(my, logic, false); // Removing item from inventory, works as expected
// Important Bit Starts here
BlockPos pos = player.getPosition();
for (ItemStack itemStack: removed){
// Each of the following comment blocks is something I tried, all to the same end
/*
ItemEntity itemEntity = new ItemEntity(player.world, pos.getX(), pos.getY(), pos.getZ(), itemStack.getStack());
player.world.addEntity(itemEntity); */
/*
ItemEntity itemEntity = new ItemEntity(player.world, pos.getX(), pos.getY(), pos.getZ(), itemStack.getStack());
itemEntity.world.addEntity(itemEntity); */
/*
invWrapper.setStackInSlot(someSlot, itemStack);
player.dropItem(itemStack, true, true); // I tried every combination of bools here */
}
}
}
(Player has to be correct because all other logic works as expected)
An example value of itemEntity: "ItemEntity['Spruce Wood'/4, l='MpServer', x=4.00, y=56.00, z=-7.00]"