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.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • https://pastebin.com/VwpAW6PX My game crashes upon launch when trying to implement the Oculus mod to this mod compilation, above is the crash report, I do not know where to begin to attempt to fix this issue and require assistance.
    • This has been released asย 49.0.49.
    • https://youtube.com/shorts/gqLTSMymgUg?si=5QOeSvA4TTs-bL46
    • CubeHaven is a SMP server with unique features that can't be found on the majority of other servers! Java: MC.CUBEHAVEN.NET Bedrock: MC.CUBEHAVEN.NET:19132 3 different stores: - CubeHaven Store: Our store to purchase using real money. - Bitcoin Store: Store for Bitcoin. Bitcoin can be earned from playing the server. Giving options for players if they want to spend real money or grind to obtain exclusive packages. - Black Market: A hidden store for trading that operates outside our traditional stores, like custom enchantments, exclusive items and more. Some of our features include: Rank Up: Progress through different ranks to unlock new privileges and perks. ๐Ÿ“ˆ Skills: RPG-style skill system that enhances your gaming experience! ๐ŸŽฎ Leaderboards: Compete and shine! Top players are rewarded weekly! ๐Ÿ† Random Teleporter: Travel instantly across different worlds with a click! ๐ŸŒ Custom World Generation: Beautifully generated world. ๐ŸŒ Dungeons: Explore challenging and rewarding dungeons filled with treasures and monsters. ๐Ÿฐ Kits: Unlock ranks and gain access to various kits. ๐Ÿ› ๏ธ Fishing Tournament: Compete in a friendly fishing tournament! ๐ŸŽฃ Chat Games: Enjoy games right within the chat! ๐ŸŽฒ Minions: Get some help from your loyal minions. ๐Ÿ‘ฅ Piรฑata Party: Enjoy a festive party with Piรฑatas! ๐ŸŽ‰ Quests: Over 1000 quests that you can complete! ๐Ÿ“œ Bounty Hunter: Set a bounty on a player's head. ๐Ÿ’ฐ Tags: Displayed on nametags, in the tab list, and in chat. ๐Ÿท๏ธ Coinflip: Bet with other players on coin toss outcomes, victory, or defeat! ๐ŸŸข Invisible & Glowing Frames: Hide your frames for a cleaner look or apply a glow to it for a beautiful look. ๐Ÿ”ฒโœจ[ Player Warp: Set your own warp points for other players to teleport to. ๐ŸŒŸ Display Shop: Create your own shop and sell to other players! ๐Ÿ›’ Item Skins: Customize your items with unique skins. ๐ŸŽจ Pets: Your cute loyal companion to follow you wherever you go! ๐Ÿพ Cosmetics: Enhance the look of your character with beautiful cosmetics! ๐Ÿ’„ XP-Bottle: Store your exp safely in a bottle for later use! ๐Ÿถ Chest & Inventory Sorting: Keep your items neatly sorted in your inventory or chest! ๐Ÿ“ฆ Glowing: Stand out from other players with a colorful glow! โœจ Player Particles: Over 100 unique particle effects to show off. ๐ŸŽ‡ Portable Inventories: Over virtual inventories with ease. ๐Ÿงณ And a lot more! Become part of our growing community today! Discord:ย https://cubehaven.net/discord Java: MC.CUBEHAVEN.NET Bedrock: MC.CUBEHAVEN.NET:19132
    • # Problematic frame: # C [libopenal.so+0x9fb4d] It is always the same issue - this refers to the Linux OS - so your system may prevent Java from working ย  I am not familiar with Linux - check for similar/related issues ย 
  • Topics

×
×
  • Create New...

Important Information

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