Posted January 11, 20214 yr I am trying to make a sword that will spawn diamonds upon breaking any block. This is my first ever mod and I don't think I am understanding how to spawn objects. I overriding the onBlockDestroyed method and adding the spawnAdditionalDrops method to drop diamonds upon the block breaking. Would someone be able to help me figure out how to set diamonds to be dropped? @Override public boolean onBlockDestroyed(ItemStack stack, World worldIn, BlockState state, BlockPos pos, LivingEntity entityLiving) { IItemProvider provider = new IItemProvider() { @Override public Item asItem() { // I'm not sure whats going on here. return null; } }; ItemStack itemDrop = new ItemStack(provider); if (state.getBlockHardness(worldIn, pos) != 0.0F) { stack.damageItem(100, entityLiving, (entity) -> { entity.sendBreakAnimation(EquipmentSlotType.MAINHAND); state.spawnAdditionalDrops((ServerWorld) worldIn, pos, itemDrop); }); } return super.onBlockDestroyed(stack, worldIn, state, pos, entityLiving); }
January 11, 20214 yr You don't need to implement a new IItemProvider as Item already does that... so just use the items in Items.java and create a new ItemStack with it, then add them to the world using World#addEntity.
January 11, 20214 yr Author Thank you for the reply poopoodice! I guess I don't understand how to create an ItemStack. Could you explain it for me or point in the direction to figure it out?
January 11, 20214 yr 20 minutes ago, Strahlend said: ItemStack itemDrop = new ItemStack(provider); Since Item already implements IItemProvider, you can reference them directly, e.g.: //this creates a stack of 5 iron nuggets ItemStack ironNuggets = new ItemStack(Items.IRON_NUGGETS, 5);
January 11, 20214 yr Author I got it working! This was what I ended up with. Thank you for your help. @Override public boolean onBlockDestroyed(ItemStack stack, World worldIn, BlockState state, BlockPos pos, LivingEntity entityLiving) { ItemStack item = new ItemStack(Items.DIAMOND, 1); ItemEntity itemDrop = new ItemEntity(worldIn, pos.getX(), pos.getY(), pos.getZ(), item); if (state.getBlockHardness(worldIn, pos) != 0.0F) { stack.damageItem(0, entityLiving, (entity) -> { entity.sendBreakAnimation(EquipmentSlotType.MAINHAND); }); } worldIn.addEntity(itemDrop); return true; }
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.