Posted January 8, 20223 yr Hi, I have made a block that once it is activated, it removes all other nearby blocks. To accomplish that, I have used level.destroyBlock(..) method. However, what I would like to accomplish is that once the block is destroyed, it should leave item that is equal what would drop after using silk touch. I.e. after block with iron ore is destroyed, a block of iron ore should remain on the ground that can be looted. I am using Forge for Minecraft 1.18.1. I have read that there used to be a method harvestBlock, but I can't find it anymore. What would be a proper way to make effect of block being harvested with silk touch effect? Thanks! EDIT: This is the code of use method on my TerraformerBlock: public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) { int x = pos.getX(); int y = pos.getY(); int z = pos.getZ(); for (int i = -8; i <= 8; i++) { for (int j = -8; j <= 8; j++) { if (!(i == 0 && j == 0)) { BlockPos blockPos = new BlockPos(x + i, y, z + j); level.destroyBlock(blockPos, false); } } } return InteractionResult.SUCCESS; } Edited January 8, 20223 yr by Adil Yilan
January 8, 20223 yr Author As tool - I should create i.e. diamond pickaxe with silk touch and pass it in as argument? What is the reason that I have to call level.destroyBlock afterwards once more? Sorry for asking, I like to understand what am I doing. Thanks!
January 8, 20223 yr Author Thanks It's working, I will post code as soon as I clean it up for others to use.
January 9, 20223 yr Author Final version of working code, with comments: public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) { // Create diamond pickaxe ItemStack pickaxe = new ItemStack(Items.DIAMOND_PICKAXE); // Enchant it with silk touch pickaxe.enchant(Enchantments.SILK_TOUCH, 1); // Read coordinates of terraformer int x = pos.getX(); int y = pos.getY(); int z = pos.getZ(); for (int i = -8; i <= 8; i++) { for (int j = -8; j <= 8; j++) { // IF: it is not location of terraformer if (!(i == 0 && j == 0)) { // Get position of nearby block BlockPos blockPos = new BlockPos(x + i, y, z + j); // Get state of nearby block BlockState blockstate = level.getBlockState(blockPos); // IF: Block is not air if (!blockstate.isAir()) { // Try resolve blockentity for block BlockEntity blockentity = blockstate.hasBlockEntity() ? level.getBlockEntity(blockPos) : null; // Drop resources from loot table as if they were mined with silk touch Block.dropResources(blockstate, level, blockPos, blockentity, null, pickaxe); // Destroy the block without loot drops level.destroyBlock(blockPos, false); } } } } return InteractionResult.SUCCESS; } Thanks @diesieben07!
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.