Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[1.16.4]After worldGen, remove block and drop its item


Cratthorax
 Share

Recommended Posts

Good evening,

I'm using a NoFeature generator to spawn block types of ores, that resemble deposits at lower layers. Right now they look the same as the ore blocks some levels lower, but I actually want them to exist as items on the surface.

In the past I was utilizing onBlockAdded(), and dropBlockAsItem() directly inside my blocks to do exactly that, but this does no longer seem to be an option, because any of the block methods, that handle "BlockAdded" logic can't be used during world generation, when starting a new game. Which is unfortunate to say the least.

The problem I'm facing right now: dropping an item from block after it is placed in the world, without player interaction, doesn't seem to be an option either or I just fail to find it. At least none of the block or item .classes contain anything useful.

Can someone direct me where to look, please?

Link to comment
Share on other sites

3 hours ago, diesieben07 said:

My question would, why you're even spawning a block in the first place, if you're just gonna remove it immediately anyways? You can spawn entities during worldgen, too.

Because I was used to doing it like that in the past. Also, a block is very sparse in code quantity. Also, also, I would use the blocks to check against blocks of same type at lower levels, to mark as an indicator to what can be found digging deeper into the ground. Doing that with anything but the same object type is just unnecessarily bloating code.

However, I had to use creative ways to get something working. What I did was rendering my block and its voxel shape invisible, make it nonsolid and notBlockMovement, then apply the randomTick() method, and using this: worldIn.destroyBlock(pos, true);...which basically drops the item after the blocks gets randomly destroyed.

The good thing about that is, with the new DataPack function, I can do all that stuff with a single block object, and then just use instances of other block registers and their properties. I can also customize the loot tables for any of them, but don't need an actual block.class for any of them. Given how I have more then 60 ore blocks in my old mod, that is a really great thing about newer MC versions.

The final solution was actually found in vanilla Block.AIR, after messing around with various methods, none of them doing what was easily done in the past MC versions with onBlockAdded(), and dropBlockAsItem(). I'll just leave the block code, and register code here for reference, which should give you a picture of the many things I was trying without success:

 
	public class BlockRock extends Block implements IWaterLoggable {
    
    public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
    PlayerEntity player;
	    public BlockRock() {
        super(Properties.create(Material.ROCK, MaterialColor.STONE)
                .hardnessAndResistance(3.0F, 30.0F).sound(SoundType.STONE).doesNotBlockMovement()
                .notSolid()/*.harvestLevel(3)
                .harvestTool(ToolType.PICKAXE)*/.tickRandomly());
        this.setDefaultState(this.stateContainer.getBaseState().with(WATERLOGGED, Boolean.FALSE));
    }
	    @Override
    public BlockState getStateForPlacement(BlockItemUseContext context) {
        if (context.getWorld().getBlockState(context.getPos()).getBlock() == Blocks.WATER) {
            return this.getDefaultState().with(WATERLOGGED, Boolean.TRUE);
        }
        return this.getDefaultState();
    }
    
    @Override
    public BlockRenderType getRenderType(BlockState state) {
        return BlockRenderType.INVISIBLE;
    } 
    
    @Override
    public VoxelShape getShape(BlockState state, IBlockReader worldIn, BlockPos pos, ISelectionContext context) {
        return VoxelShapes.empty();
    }
	    @Override
    public void onFallenUpon(World worldIn, BlockPos pos, Entity entityIn, float fallDistance) {
        super.onFallenUpon(worldIn, pos, entityIn, fallDistance);
        // One in ten chance for the sample to break when fallen on
        Random random = new Random();
        if (((int) fallDistance) > 0) {
            if (random.nextInt((int) fallDistance) > 5) {
                worldIn.destroyBlock(pos, true);
            }
        }
    }
	    @Override
    public ActionResultType onBlockActivated(BlockState state, World worldIn, BlockPos pos,
            PlayerEntity player, Hand handIn, BlockRayTraceResult hit) {
        if (!player.isCrouching()) {
            worldIn.destroyBlock(pos, true);
            player.swingArm(handIn);
            return ActionResultType.SUCCESS;
        }
        return ActionResultType.PASS;
    }
	    @Override
    public boolean isValidPosition(BlockState state, IWorldReader worldIn, BlockPos pos) {
        return hasEnoughSolidSide(worldIn, pos.down(), Direction.UP);
    }
	    @Override
    protected void fillStateContainer(StateContainer.Builder<Block, BlockState> builder) {
        builder.add(WATERLOGGED);
    }
    @Override
    @Nonnull
    @SuppressWarnings("deprecation")
    public FluidState getFluidState(BlockState state) {
        return state.get(WATERLOGGED) ? Fluids.WATER.getStillFluidState(false)
                : super.getFluidState(state);
    }
	    @Override
    @SuppressWarnings("deprecation")
    public void neighborChanged(BlockState state, World worldIn, BlockPos pos, Block blockIn,//pos is your block, fromPos is a neighboring block
            BlockPos fromPos, boolean isMoving) {
        //System.out.println(blockIn.toString() + state.getBlockState() + worldIn.getBlockState(pos) + worldIn.getBlockState(fromPos));
        super.neighborChanged(state, worldIn, pos, blockIn, fromPos, isMoving);
        if (!this.isValidPosition(state, worldIn, pos)) {
            worldIn.destroyBlock(pos, true);
        }
        else if (state.get(WATERLOGGED)) {
            worldIn.getPendingFluidTicks().scheduleTick(pos, Fluids.WATER,
                    Fluids.WATER.getTickRate(worldIn));
        }
    }
	    public BlockState asWaterlogged() {
        return this.getDefaultState().with(WATERLOGGED, Boolean.TRUE);
    }
	    @Override
    public boolean isReplaceable(BlockState state, BlockItemUseContext useContext) {
        if (useContext.getItem().getItem() instanceof BlockItem) {
            if (((BlockItem) useContext.getItem().getItem()).getBlock() instanceof BlockRock) {
                return false;
            }
        }
        return ConfigSpecHandler.ARE_ROCKS_REPLACEABLE.get();
    }
    
    @Override
    @SuppressWarnings("resource")
    @Deprecated
    public void randomTick(BlockState state, ServerWorld worldIn, BlockPos pos, Random random) {
        
        player = Minecraft.getInstance().player;
        if (!worldIn.isRemote && player != null) {
                
            worldIn.destroyBlock(pos, true);
            System.out.println(this.getBlock().toString() + " destroyed!");
            state.tick(worldIn, pos, worldIn.rand); 
            worldIn.getPendingBlockTicks().scheduleTick(pos, this, 1);
        }
    }
    
    @Override
    public void onBlockAdded(BlockState state, World worldIn, BlockPos pos, BlockState oldState, boolean isMoving) {
        worldIn.getPendingBlockTicks().scheduleTick(pos, this, 1);
    }  
    
    public void tick(BlockState state, ServerWorld worldIn, BlockPos pos, Random rand) {
        worldIn.getPendingBlockTicks().scheduleTick(pos, this, 1);//MathHelper.nextInt(rand, 1, 1));
     }
    
    /*@Override
    @SuppressWarnings({ "deprecation", "resource" })
    public BlockState updatePostPlacement(BlockState stateIn, Direction facing, BlockState facingState, IWorld worldIn, BlockPos currentPos, BlockPos facingPos) {
        
        player = Minecraft.getInstance().player;
        if (!this.isValidPosition(stateIn, worldIn, currentPos)) {
            worldIn.getPendingBlockTicks().scheduleTick(currentPos, this, 1);
        }
        
        if(player == null) {
            
            return this.getDefaultState();
        
        } else if (player != null && !worldIn.isRemote()) {
                
            if(worldIn.isBlockLoaded(currentPos)) {
                worldIn.destroyBlock(currentPos, true);
                System.out.println(this.getBlock().toString() + " destroyed!");
                return this.getDefaultState();
            }
        }
        return this.getDefaultState();
    } */   
	}
 
public class RegisterRocks {
	
    public static HashMap<Block, Block> blocksToRocks = new HashMap<>();
    private static ArrayList<Block> matLibBlocks = new ArrayList<>();

    private static void registerRocks(String modid, String path) {
        ResourceLocation oreRL = new ResourceLocation(modid, path);
        ResourceLocation rockRL = new ResourceLocation(MatLibMain.MODID,
                (modid.toLowerCase() == "minecraft" ? "" : ("")) + path + "rock");
        Block blocks = ForgeRegistries.BLOCKS.getValue(oreRL);

        if (blocks != null && blocks != Blocks.AIR) {
            Block blockRock = new BlockRock().setRegistryName(rockRL);
            matLibBlocks.add(blockRock);
            blocksToRocks.put(blocks, blockRock);
        } else {
        	//ConfigDebug.debug("Missing replacer, no rocks placed!");
        }
    }

    public static void registerBlocks(final RegistryEvent.Register<Block> blockRegistryEvent) {
        
        //minecraft
    	registerRocks("minecraft", "andesite");
    	registerRocks("minecraft", "diorite");
    	registerRocks("minecraft", "granite");
    	registerRocks("minecraft", "sandstone");
    	registerRocks("minecraft", "red_sandstone");
        
    	//matlib
    	registerRocks("matlib", "blockbasalt");
    	registerRocks("matlib", "blockbluestone");
    	registerRocks("matlib", "blockdiabase");
        //registerRocks("matlib", "blockgranite");
    	registerRocks("matlib", "blocklimestone");
    	registerRocks("matlib", "blockmarble");
    	registerRocks("matlib", "blockquartzite");
        //registerRocks("matlib", "blocksandstone");
    	registerRocks("matlib", "blockslate");
    	registerRocks("matlib", "blocktuff");

        for (Block block : matLibBlocks) {
            blockRegistryEvent.getRegistry().register(block);
        }
    }

    public static void registerBlockItems(final RegistryEvent.Register<Item> itemRegistryEvent) {
        for (Block block : matLibBlocks) {
            if (block instanceof BlockRock) {
                Item iBlock = new BlockRockItem(block)
                        .setRegistryName(Objects.requireNonNull(block.getRegistryName()));
                itemRegistryEvent.getRegistry().register(iBlock);
            } else {
                Item iBlock = new BlockItem(block,
                        new Item.Properties().group(RegisterMain.MATLIB_GROUP))
                                .setRegistryName(Objects.requireNonNull(block.getRegistryName()));
                itemRegistryEvent.getRegistry().register(iBlock);
            }
        }
    }
}

 

Edited by Cratthorax
Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.