Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

Hi, i am having a problem with my BlockEntityRenderer.

I am trying to render the Item that the BlockEntity is storing, but after i close and open the world again the itemRenderer dosn't render the item anymore.

@OnlyIn(Dist.CLIENT)
public class StonePedestalRenderer implements BlockEntityRenderer<StonePedestalBlockEntity> {
    public final ItemRenderer itemRenderer = Minecraft.getInstance().getItemRenderer();

    public StonePedestalRenderer(BlockEntityRendererProvider.Context pContext){
    }

    @Override
    public void render(StonePedestalBlockEntity pBlockEntity, float pPartialTick, PoseStack pPoseStack, MultiBufferSource pBufferSource, int pPackedLight, int pPackedOverlay) {
        ItemStack stack = new ItemStack(pBlockEntity.getItem());

        if (!stack.isEmpty()){
            pPoseStack.pushPose();
            pPoseStack.translate(0.5D, 1.3225D, 0.5D);
            pPoseStack.scale(0.6f,0.6f,0.6f);
            float f3 = pBlockEntity.getSpin(pPartialTick);
            pPoseStack.mulPose(Vector3f.YP.rotationDegrees(f3));
            itemRenderer.renderStatic(Minecraft.getInstance().player,stack, ItemTransforms.TransformType.FIXED,false,pPoseStack,pBufferSource,Minecraft.getInstance().level,pPackedLight,pPackedOverlay,0);
            pPoseStack.popPose();
        }
    }
}

and here is the code fore the Block Entity:

public class StonePedestalBlockEntity extends BlockEntity {
    protected ItemStackHandler itemHandler = new ItemStackHandler(1) {
        @Override
        protected void onContentsChanged(int slot) {
            setChanged();
        }
    };
    protected LazyOptional<IItemHandler> lazyItemHandler = LazyOptional.empty();
    private float age = 0.0f;
    public final float bobOffs;
    private final Random random = new Random();;


    public StonePedestalBlockEntity(BlockPos pWorldPosition, BlockState pBlockState) {
        super(BlockEntityRegistry.STONE_PEDESTAL_BLOCK_ENTITY.get(), pWorldPosition, pBlockState);
        this.bobOffs = this.random.nextFloat() * (float)Math.PI * 2.0F;
    }

    public boolean setItem(ItemStack stack){
        if (this.itemHandler.getStackInSlot(0).isEmpty()){
            stack = new ItemStack(stack.getItem(),1);
            this.itemHandler.setStackInSlot(0,stack);
            return true;
        }
        return false;
    }

    public Item getItem(){
        return this.itemHandler.getStackInSlot(0).getItem();
    }

    public ItemStack getAndRemoveItem(){
        if (!this.itemHandler.getStackInSlot(0).isEmpty()){
            ItemStack stack = this.itemHandler.getStackInSlot(0);
            this.itemHandler.setStackInSlot(0,ItemStack.EMPTY);
            return stack;
        }
        return this.itemHandler.getStackInSlot(0);
    }

    public void removeItem(){
        this.itemHandler.setStackInSlot(0,ItemStack.EMPTY);
    }

    public float getSpin(float pPartialTicks) {
        this.age += pPartialTicks;
        return (this.age) + this.bobOffs;
    }

    @NotNull
    @Override
    public <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, @Nullable Direction side) {

        if (cap == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY){
            return lazyItemHandler.cast();
        }
        return super.getCapability(cap, side);
    }

    @Override
    public void onLoad() {
        super.onLoad();
        lazyItemHandler = LazyOptional.of(() -> itemHandler);
    }

    @Override
    public void invalidateCaps() {
        super.invalidateCaps();
        lazyItemHandler.invalidate();
    }

    @Override
    protected void saveAdditional(CompoundTag nbt) {
        nbt.put("inventory", itemHandler.serializeNBT());

        super.saveAdditional(nbt);
    }

    @Override
    public void load(CompoundTag nbt) {
        super.load(nbt);
        itemHandler.deserializeNBT(nbt.getCompound("inventory"));
    }

    public void drops() {
        SimpleContainer inventory = new SimpleContainer(itemHandler.getSlots());
        for (int i = 0; i < itemHandler.getSlots(); i++) {
            inventory.setItem(i, itemHandler.getStackInSlot(i));
        }

        Containers.dropContents(this.level, this.worldPosition, inventory);
    }
}

When I click on the Block i get the Item back i put in even after logging out and back in. It is only the Renderer that dosen't work.

what am I missing here?

full source code: https://github.com/TobiasFiller/MiltenMagic

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

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.