Jump to content

[1.18.2] Render an entity at a certain position


Sweetmimike

Recommended Posts

Hey !

I'm trying to render an entity (for example a cow) just above a custom block entity. Of course, the renderer is correctly registered.

final EntityRenderDispatcher entityDispatcher = Minecraft.getInstance().getEntityRenderDispatcher();
final BlockRenderDispatcher dispatcher = this.context.getBlockRenderDispatcher();
//        pPoseStack.pushPose();
//        pPoseStack.translate(0.5f, 0.5f, 0.5f);
//        dispatcher.renderSingleBlock(Blocks.GLASS.defaultBlockState(), pPoseStack, pBufferSource, pPackedLight, pPackedOverlay,
//                EmptyModelData.INSTANCE);

//        pPoseStack.popPose();

Entity entityToSpawn = EntityType.COW.create(pBlockEntity.getLevel());
//        LOGGER.debug("ENTITY TO SPAWN " + entityToSpawn.getName());
//        pPoseStack.pushPose();
pPoseStack.translate(0.5f, 2f, 0.5f);
pPoseStack.scale(2f, 2f, 2f);
entityDispatcher.render(entityToSpawn, pBlockEntity.getBlockPos().getX(), pBlockEntity.getBlockPos().getY(), pBlockEntity.getBlockPos().getZ(),
                0.0f, pPartialTick, pPoseStack, pBufferSource, pPackedLight);
pPoseStack.popPose();

Is there something I do not correctly ?

Edited by Sweetmimike
Add info
Link to comment
Share on other sites

I finally solved the problem !

If anyone is interested on how render an entity, you can find the code I used just below

final EntityRenderDispatcher entityDispatcher = Minecraft.getInstance().getEntityRenderDispatcher();
Entity entityToSpawn = entityType.create(pBlockEntity.getLevel());

float scale = 0.3f;
pPoseStack.pushPose();
pPoseStack.translate(0.5f, 1f, 0.5f);
pPoseStack.scale(scale, scale, scale);

entityDispatcher.render(entityToSpawn, 0.0D, 0.0D, 0.0D, 0.0F, 1.0F, pPoseStack, pBufferSource, 15728880);
pPoseStack.popPose();

 

Link to comment
Share on other sites

22 minutes ago, diesieben07 said:

Do not create a new entity instance every time.

Right.

I modified the code just as follows

	final EntityRenderDispatcher entityDispatcher = Minecraft.getInstance().getEntityRenderDispatcher();
        Entity entityToSpawn = pBlockEntity.getEntityToDisplay();
        if(entityToSpawn == null || entityToSpawn.getType() != entityType) {
            LOGGER.debug("NEED TO CREATE A NEW ENTITY");
            entityToSpawn = entityType.create(pBlockEntity.getLevel());
            pBlockEntity.setEntityToDisplay(entityToSpawn);
        }

        float scale = 0.3f;
        pPoseStack.pushPose();
        pPoseStack.translate(0.5f, 1f, 0.5f);
        pPoseStack.scale(scale, scale, scale);

        entityDispatcher.render(entityToSpawn, 0.0D, 0.0D, 0.0D, 0.0F, 1.0F, pPoseStack, pBufferSource, 15728880);
        pPoseStack.popPose();

So the entity I want to display is stored in a block entity

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

Announcements



×
×
  • Create New...

Important Information

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