Jump to content

[1.18.2] ItemStack in Renderer Empty after reloading the World


TobiasFiller

Recommended Posts

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

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.

Announcements



×
×
  • Create New...

Important Information

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