Jump to content

[1.15.2] Render Item on Block


Ryze

Recommended Posts

How would I go about rendering an item on my block, when right-clicked with the item.

 

Reading through forum posts, I've seen people suggesting using a TileEntityRenderer and also baking the Item into the block using the IModel(s) (however I cant find that in the same package in 1.15 although its on the 1.15 docs, is something else used now?)

 

Which approach is better in certain circumstances and how would I go about implementing either of them? Any advice is greatly appreciated.

Link to comment
Share on other sites

4 hours ago, Ryze said:

How would I go about rendering an item on my block, when right-clicked with the item.

It depends on your situation. Is there a finite number of possible Items? Will there be animation IE moving parts?

If there are finite possible items just make a model for each state.

If there are moving parts you have to use a TER.

If there are no moving parts, but an infinite amount of possible Items then you should have a custom IBakedModel.

 

The first is obvious how to do. The second vanilla has a Model class that it uses, you should probably use that. Take a peek at the Vanilla TileEntityRenderers. For the third option you need your own IBakedModel implementation that combines the Quads of an Item(s) with the Quads of your Block model. Then you must register it using the ModelBakeEvent by just putting it in the registry.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

I have the Item rendering on the block using a custom IBakedModel implementation, but the item seems to have a 16 x 16 invisible background that clips through the model at certain angles, any ideas?

 

The model without an item

Spoiler

TQPd0IE.png

And with

Spoiler

ZoT9Efq.png

My IBakedModel implementation

Spoiler

public class MicroscopeBakedModel implements IBakedModel {

    private IBakedModel baseModel;

    public ModelProperty<ItemStack> RENDER_ITEM = new ModelProperty<>();

    public ModelDataMap getEmptyIModelData() {
        ModelDataMap.Builder builder = new ModelDataMap.Builder();
        builder.withInitial(RENDER_ITEM, null);
        return builder.build();
    }

    public MicroscopeBakedModel(IBakedModel baseModel) {
        this.baseModel = baseModel;
    }

    @Nonnull
    @Override
    public List<BakedQuad> getQuads(@Nullable BlockState state, @Nullable Direction side, @Nonnull Random rand, @Nonnull IModelData extraData) {
        IBakedModel base = this.baseModel;
        if (!extraData.hasProperty(RENDER_ITEM)) {
            return base.getQuads(state, side, rand, extraData);
        }
        ItemStack item = extraData.getData(RENDER_ITEM);
        if (item == null) {
            return base.getQuads(state, side, rand, extraData);
        }
        ItemRenderer itemRenderer = Minecraft.getInstance().getItemRenderer();
        IBakedModel itemModel = itemRenderer.getItemModelWithOverrides(item, null, null);
        Matrix4f matrix = new Matrix4f(new Quaternion(-90.0f, 0.0f, 0.0f, true));
        matrix.mul(Matrix4f.makeScale(0.4f, 0.4f, 0.4f));
        matrix.mul(Matrix4f.makeTranslate(0.7f, -2.05f, 0.45f));
        TransformationMatrix tm = new TransformationMatrix(matrix);
        QuadTransformer transformer = new QuadTransformer(tm);
        List<BakedQuad> updatedItemQuads = transformer.processMany(itemModel.getQuads(state, side, rand, extraData));
        List<BakedQuad> quads = new ArrayList<>();
        quads.addAll(base.getQuads(state, side, rand, extraData));
        quads.addAll(updatedItemQuads);
        return quads;
    }

    @Nonnull
    @Override
    public IModelData getModelData(@Nonnull ILightReader world, @Nonnull BlockPos pos, @Nonnull BlockState state, @Nonnull IModelData tileData) {
        if (state == world.getBlockState(pos)) {
            TileEntity te = world.getTileEntity(pos);
            if (te instanceof MicroscopeTileEntity) {
                if (((MicroscopeTileEntity) te).getItemStack().getItem() != Blocks.AIR.asItem()) {
                    ItemStack item = ((MicroscopeTileEntity) te).getItemStack().copy();
                    ModelDataMap map = getEmptyIModelData();
                    map.setData(RENDER_ITEM, item);
                    return map;
                }
            }
        }
        return getEmptyIModelData();
    }

    @Override
    public TextureAtlasSprite getParticleTexture(@Nonnull IModelData data) {
        return baseModel.getParticleTexture();
    }

    // IBakedModel Methods
    @Override
    public List<BakedQuad> getQuads(@Nullable BlockState state, @Nullable Direction side, Random rand) {
        return null;
    }

    @Override
    public boolean isAmbientOcclusion() {
        return baseModel.isAmbientOcclusion();
    }

    @Override
    public boolean isGui3d() {
        return baseModel.isGui3d();
    }

    @Override
    public boolean func_230044_c_() {
        return baseModel.func_230044_c_();
    }

    @Override
    public boolean isBuiltInRenderer() {
        return baseModel.isBuiltInRenderer();
    }

    @Override
    public TextureAtlasSprite getParticleTexture() {
        return baseModel.getParticleTexture();
    }

    @Override
    public ItemCameraTransforms getItemCameraTransforms() {
        return baseModel.getItemCameraTransforms();
    }

    @Override
    public ItemOverrideList getOverrides() {
        return baseModel.getOverrides();
    }
}

 

 

Link to comment
Share on other sites

9 minutes ago, Ryze said:

I have the Item rendering on the block using a custom IBakedModel implementation, but the item seems to have a 16 x 16 invisible background that clips through the model at certain angles, any ideas?

Nevermind, just had to change the RenderType for the block.

Link to comment
Share on other sites

check net.minecraft.client.renderer.ItemRenderer#renderItem out

you can get it with Minecraft#getItemRenderer

and the minecraft instance just with Minecraft#getInstance

 

 I will make fun of you if you are not able to look into the (vanilla-) code.

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.



×
×
  • Create New...

Important Information

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