Jump to content

[1.19] [SOLVED] Render mini entites inside block entites like spawner


FantaLaTone

Recommended Posts

I have a block entity and a block entity renderer class. I want to render an entity inside block and I am kinda able to do that. When I first placed the block everything works fine. But when I save & load back the world entity is gone. How I can save the entity on server-side or render entity without spawning it. I need some answers how I can do that?

Block Entity Class

Spoiler
package com.fantalatone.mobblocks.block.entity;

public class AnimatedCageBlockEntity extends BlockEntity {
    public Entity insideEntity;
    public String insideEntityType;

    public AnimatedCageBlockEntity(BlockPos pPos, BlockState pState) {
        super(ModBlockEntities.ANIMATED_CAGE_BLOCK_ENTITY.get(), pPos, pState);
    }

    public static void tick(Level pLevel, BlockPos pPos, BlockState pState, AnimatedCageBlockEntity pBlockEntity) {
        System.out.println("ENTITY->\n"+ pBlockEntity.insideEntity + "\n");
    }
  
    @Override
    protected void saveAdditional(CompoundTag tag) {
        tag.putString("insideEntity", this.insideEntityType);
        super.saveAdditional(tag);
    }

    @Override
    public void load(CompoundTag tag) {
        this.insideEntityType = tag.getString("insideEntity");
        super.load(tag);
    }
}

 

Base Block Class

Spoiler
public class AnimatedCageBlock extends BaseEntityBlock {

    private final String entity;

    public AnimatedCageBlock(Properties pProps, String type) {
        super(pProps);
        this.entity = type;
    }

    @Nullable
    @Override
    public BlockEntity newBlockEntity(BlockPos p_153215_, BlockState p_153216_) {
        return new AnimatedCageBlockEntity(p_153215_, p_153216_);
    }

    @Override
    public void setPlacedBy(Level p_49847_, BlockPos p_49848_, BlockState p_49849_, @Nullable LivingEntity p_49850_, ItemStack p_49851_) {
        AnimatedCageBlockEntity e = (AnimatedCageBlockEntity) p_49847_.getBlockEntity(p_49848_);
        e.insideEntityType = entity;
        e.insideEntity = ForgeRegistries.ENTITY_TYPES.getValue(new ResourceLocation(this.entity)).create(p_49847_);

        super.setPlacedBy(p_49847_, p_49848_, p_49849_, p_49850_, p_49851_);
    }

    @Nullable
    @Override
    public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level p_153212_, BlockState p_153213_, BlockEntityType<T> p_153214_) {
        return createTickerHelper(p_153214_, ModBlockEntities.ANIMATED_CAGE_BLOCK_ENTITY.get(),
                AnimatedCageBlockEntity::tick);
    }

    @Override
    public RenderShape getRenderShape(BlockState p_49232_) {
        return RenderShape.ENTITYBLOCK_ANIMATED;
    }
}

Renderer Class

Spoiler
public class RendererAnimatedCageBlock implements BlockEntityRenderer<AnimatedCageBlockEntity> {

    private final BlockEntityRendererProvider.Context context;
    private final EntityRenderDispatcher entityRenderer;

    public RendererAnimatedCageBlock(BlockEntityRendererProvider.Context ctx) {
        this.context = ctx;
        this.entityRenderer = Minecraft.getInstance().getEntityRenderDispatcher();
    }

    @Override
    public void render(AnimatedCageBlockEntity tile, float partialTicks, PoseStack stack, MultiBufferSource bufferIn, int combinedOverlayIn, int combinedLightIn) {
        final BlockRenderDispatcher dispatcher = this.context.getBlockRenderDispatcher();

        dispatcher.renderSingleBlock(Blocks.WHITE_STAINED_GLASS.defaultBlockState(), stack, bufferIn, combinedOverlayIn, combinedLightIn,
                ModelData.builder().build(), RenderType.cutoutMipped());

        stack.pushPose();
        stack.translate(0.5f, 0f, 0.5f);
        stack.scale(0.75f, 0.75f, 0.75f);

        if (tile.insideEntity != null) {

            float f = 0.53125F;
            float f1 = Math.max(tile.insideEntity.getBbWidth(), tile.insideEntity.getBbHeight());
            f *= 1.5f;

            stack.translate(0.0D, (double)0.4F, 0.0D);
            stack.translate(0.0D, (double)-0.2F, 0.0D);
            stack.scale(f, f, f);

            entityRenderer.setRenderShadow(false);
            entityRenderer.render(tile.insideEntity,
                    0.0d, 0.0d, 0.0d,
                    0.0f, 0.0f, stack, bufferIn, 15);
        }
        stack.popPose();
    }
}

 

Edited by FantaLaTone
solved
Link to comment
Share on other sites

You don't want to store data in your Block like that. The whole point of the BlockEntity is to store data.

What you are missing is networking code to send the server's block entity state to the client. e.g. when the game reloads the data

https://forge.gemwire.uk/wiki/Block_Entities

https://forums.minecraftforge.net/topic/115679-1182-block-entity-renderer-disappears-after-leaving-the-world/#comment-511734

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

  • 3 weeks later...
  • FantaLaTone changed the title to [1.19] [SOLVED] Render mini entites inside block entites like spawner

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.



×
×
  • Create New...

Important Information

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