I'm new in coding minecraft mods, and i wanna make a button to when a player push it, he get some random items, and if his inventory is full, the items drops. I'm already did the GUI and button, its working when he push the button and get the random item, but when his inventory is full, the items just dont appear. Heres the code im using:
@Override
public void handleServerSide(MessageGiveItems message, EntityPlayer player) {
Item[] items = new Item[]{Items.APPLE, Items.BED, Items.REDSTONE, Items.DIAMOND};
int random = player.worldObj.rand.nextInt(items.length);
player.inventory.addItemStackToInventory(new ItemStack(items[random]);
}
For these sorts of things, you should learn to inspect the vanilla code to see what it is doing. Since you're adding to an inventory and there is a case where nothing happens, then there must be logic inside that method that checks for full. You can do the same thing.
However, it can even be easier. In programming often methods will have a return value to indicate if the method was successful. The addItemStackToInventory() method has that. If you look at the code for that method the comment says: "Adds the item stack to the inventory, returns false if it is impossible."
So all you have to do is check if the return value is false, and if so then spawn an entity item instead.
I'm new in coding minecraft mods, and i wanna make a button to when a player push it, he get some random items, and if his inventory is full, the items drops. I'm already did the GUI and button, its working when he push the button and get the random item, but when his inventory is full, the items just dont appear. Heres the code im using:
For these sorts of things, you should learn to inspect the vanilla code to see what it is doing. Since you're adding to an inventory and there is a case where nothing happens, then there must be logic inside that method that checks for full. You can do the same thing.
However, it can even be easier. In programming often methods will have a return value to indicate if the method was successful. The addItemStackToInventory() method has that. If you look at the code for that method the comment says: "Adds the item stack to the inventory, returns false if it is impossible."
So all you have to do is check if the return value is false, and if so then spawn an entity item instead.