Jump to content

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


Recommended Posts

Posted (edited)

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
Posted

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.

  • 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.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • May this question can't be answered but is for a project. The question is more of how Forge load and manage mods, it's not required code or anything, just an explanation about how do it works on the load/manage of mods internally.
    • I myself am quite new to the modding scene, I started modding in February but have been in development hell a lot and have struggled to focus on one mod though I have been developing the current mod I've been working on since September and have made a lot of progress; during this time I have found a lot of helpful resources and have learnt a lot of things.    1. You can, I have had no Java experience previously and since modding have managed to be quite knowledgeable in Java.   2. Modding does not require massive teams, I and many others work alone, though having a team is certainly helpful in the modding process to get things done in a quicker and more organised way.   3. I absolutely have to recommend ModdingByKaupenjoe, he makes loads of tutorials for Minecraft from 1.16.5 and above with support for Forge, Fabric, and as of his 1.21 tutorials, Neoforge. These tutorials are really well made, covering almost every modding topic, (such as items, blocks, mobs, worldgen, etc.) and are pretty easy to follow, and Kaupenjoe always leaves the link to his GitHub repositories where you can view the code of that tutorial at your own pace as well as linking textures in the description of his videos for you to use. These forums are also quite good if you need help, though I have found that it sometimes takes a little while for a response but it is always worth the wait; from my experience the people on these forums have always been kind and helpful. There are also user-submitted tutorials that may be helpful as well.  Using GitHub to search up a certain class that you are wanting to use in your mod is also quite helpful, the video I have linked goes into more detail. I would also recommend planning out your mod before you make it to have a clearer idea of how you want your mod to be, including sketches and annotations are also a good idea for this. It has helped me make progress a lot quicker when programming as I already know how I want things to look/act.   I hope all this information helps!
    • I don't know what I did differently, but the error code changed to "Invalid signature for profile public key" so I set "enforce-secure-profile" to false since only my friend will be allowed to join anyway and then everything worked properly. Here's the logs anyway, but I hope the problem doesn't come back. https://paste.ee/p/Ri44L
  • Topics

×
×
  • Create New...

Important Information

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