Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

Hi!
I was creating a block that is bigger than 1x1x1 with a custom tile entity.
When i place the block, a part of the struct is spawned around (for now 1 single block above) and the tile entity is copied in the other position of the struct, but it result to be null.

Here is the code:

The Central Block:

public class ColossalChest extends BaseChestBlock {

    protected static final int SIZE = 5;

    public ColossalChest() {
        super(Block.Properties.of(Material.METAL), BaseChestTileEntityType.COLOSSAL_CHEST::get);
    }

    @Override
    public void playerWillDestroy(World world, BlockPos pos, BlockState state, PlayerEntity player) {
        super.playerWillDestroy(world, pos, state, player);
        destroyStructure(world, pos);
    }

    private static final BlockState AIR = Blocks.AIR.defaultBlockState();

    private void destroyStructure(World world, BlockPos pos) {
        final BlockState STRUCT_BLOCK = com.ike.tenchest.Blocks.COLOSSAL_CHEST_PART.get().defaultBlockState();
        world.setBlockAndUpdate(pos, AIR);
        world.setBlockEntity(pos, null);
        BlockPos above = pos.above();
        if (world.getBlockState(above).equals(STRUCT_BLOCK))
            destroyStructure(world, above);
        BlockPos west = pos.west();
        if (world.getBlockState(west).equals(STRUCT_BLOCK))
            destroyStructure(world, west);
        BlockPos east = pos.east();
        if (world.getBlockState(east).equals(STRUCT_BLOCK))
            destroyStructure(world, east);
        BlockPos south = pos.south();
        if (world.getBlockState(south).equals(STRUCT_BLOCK))
            destroyStructure(world, south);
        BlockPos north = pos.north();
        if (world.getBlockState(north).equals(STRUCT_BLOCK))
            destroyStructure(world, north);
        BlockPos below = pos.below();
        if (world.getBlockState(below).equals(STRUCT_BLOCK))
            destroyStructure(world, below);
    }

    @Override
    public void setPlacedBy(World world, BlockPos pos, BlockState state, @Nullable LivingEntity entity, ItemStack stack) {
        if (entity instanceof PlayerEntity) {
            placeStructure(world, pos);
        }
        super.setPlacedBy(world, pos, state, entity, stack);
    }

    private void placeStructure(World world, BlockPos center) {
        final BlockState STRUCT_BLOCK = com.ike.tenchest.Blocks.COLOSSAL_CHEST_PART.get().defaultBlockState();
        TileEntity blockEntity = world.getBlockEntity(center);
        System.out.println(blockEntity);
        BlockPos above = center.above();
        world.setBlockAndUpdate(above, STRUCT_BLOCK);
        world.setBlockEntity(above, blockEntity);
    }

    @Override
    public boolean hasTileEntity(BlockState state) {
        return true;
    }

    @Nullable
    @Override
    public TileEntity createTileEntity(BlockState state, IBlockReader world) {
        return new ColossalChestTileEntity();
    }
}

And the Block of the struct: 

public class ColossalChestPart extends Block {

    public ColossalChestPart() {
        super(AbstractBlock.Properties.of(Material.BARRIER).strength(-1.0F, 3600000.8F).noDrops().noOcclusion());
    }

    @Override
    public ActionResultType use(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockRayTraceResult rayTrace) {
        if (world.isClientSide) {
            return ActionResultType.SUCCESS;
        } else {
            INamedContainerProvider inamedcontainerprovider = this.getMenuProvider(state, world, pos);
            System.out.println(inamedcontainerprovider);
            if (inamedcontainerprovider != null) {
                player.openMenu(inamedcontainerprovider);
                player.awardStat(Stats.CUSTOM.get(Stats.OPEN_CHEST));
                PiglinTasks.angerNearbyPiglins(player, true);
            }
            return ActionResultType.CONSUME;
        }
    }

    @Nullable
    @Override
    public INamedContainerProvider getMenuProvider(BlockState state, World world, BlockPos pos) {
        TileEntity entity = world.getBlockEntity(pos);
        System.out.println(entity);
        return entity instanceof INamedContainerProvider ? (INamedContainerProvider) entity : null;
    }

    public boolean propagatesSkylightDown(BlockState p_200123_1_, IBlockReader p_200123_2_, BlockPos p_200123_3_) {
        return true;
    }

    public BlockRenderType getRenderShape(BlockState p_149645_1_) {
        return BlockRenderType.INVISIBLE;
    }

    @OnlyIn(Dist.CLIENT)
    public float getShadeBrightness(BlockState p_220080_1_, IBlockReader p_220080_2_, BlockPos p_220080_3_) {
        return 1.0F;
    }
}

In the getMenuProvider function of the ColossalChestPart the TileEntity result to be Null.

  • Author

How can I make multiple blocks interact with a single main TileEntity?
Maybe I could create a TileEntity for the ColossalChestPart to which I pass the main TileEntity?

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
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

Important Information

By using this site, you agree to our Terms of Use.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.