Jump to content

ItemRenderer doesn't get item from inventory


Cron3x

Recommended Posts

I want to write a mod with pedestals, I know nothing new but I need them.

I wrote a method that gets the first item out of the inventory. If I use it on my use method in the Block Class everything works but if i call the same method from my renderer class it just get air.

 

The code that gets the item:

private final ItemStackHandler inventory = new ItemStackHandler(1);
private final LazyOptional<IItemHandler> optional = LazyOptional.of(() -> this.inventory);

public ItemStack getDisplayItem(Boolean simulate) {
    IItemHandler h = optional.orElse(null);
    ItemStack returner = h.extractItem(0,1,simulate);
    if(!simulate) update(); 
    //System.out.println(""+returner);
    return returner; 
}

the debug print shows that the returner is air when in rendering and the real item in other classes.

Link to comment
Share on other sites

Can you show me the full container and blockentity classes?

I haven't worked with this very much/at all but since I am working on a mod that requires a custom recipe slot things, I might be able to help.

I'm not good at modding, but at least I can read a crash report (well enough). That's something, right?

Link to comment
Share on other sites

9 minutes ago, Hipposgrumm said:

Can you show me the full container and blockentity classes?

I haven't worked with this very much/at all but since I am working on a mod that requires a custom recipe slot things, I might be able to help.

I don't have a seperate container class, maybe thats the Problem?

 

public class PedestalBlockEntity extends BlockEntity {

    private final ItemStackHandler inventory = new ItemStackHandler(1);
    private final LazyOptional<IItemHandler> optional = LazyOptional.of(() -> this.inventory);

    public PedestalBlockEntity(BlockPos pos, BlockState state) {
        super(BlockEntityRegister.PEDESTAL.get(), pos, state);
    }

    @Override
    public void load(CompoundTag nbt) {
        super.load(nbt);
        this.inventory.deserializeNBT(nbt.getCompound("Inventory"));
        System.out.println("load: " + nbt.getCompound("Inventory"));
    }

    @Override
    public void saveAdditional(@NotNull CompoundTag nbt) {
        super.saveAdditional(nbt);
        nbt.put("Inventory", this.inventory.serializeNBT());
        System.out.println("saveAdditional: " + this.inventory.serializeNBT());
    }

    @Override
    public @NotNull <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap) {
        return cap == ForgeCapabilities.ITEM_HANDLER ? this.optional.cast() : super.getCapability(cap, null);
    }

    @Override
    public void invalidateCaps() {
        this.optional.invalidate();
    }

    public ItemStackHandler getInventory() {
        return inventory;
    }

    public ItemStack addItem(ItemStack itemStack, Boolean simulate){
        IItemHandler h = optional.orElse(null);
        ItemStack returner = h.insertItem(0,itemStack.copy(), simulate);
        if(!simulate) update();
        return returner;
    }

    public ItemStack getDisplayItem(Boolean simulate) {
        IItemHandler h = optional.orElse(null);
        ItemStack returner = h.extractItem(0,1,simulate);
        if(!simulate) update();
        //System.out.println(""+returner);
        return returner;
    }

    public void update() {
        BlockState state = level.getBlockState(this.worldPosition);
        this.level.sendBlockUpdated(this.worldPosition, state, state, 3);
        this.setChanged();
    }
}

Do You want the render class too?

Edited by Cron3x
wrong class
Link to comment
Share on other sites

You probably should handle it with a container. Especially since a pedestal will typically hold things and probably won't have a gui associated with it.

However, if you do have a gui associated, you can pull from that, but you will need a container to prevent overlap (like chest and brewing stand).

I'm not good at modding, but at least I can read a crash report (well enough). That's something, right?

Link to comment
Share on other sites

1 minute ago, Hipposgrumm said:

You probably should handle it with a container. Especially since a pedestal will typically hold things and probably won't have a gui associated with it.

However, if you do have a gui associated, you can pull from that, but you will need a container to prevent overlap (like chest and brewing stand).

Ok, Thank you. I will try to make it with a container. I thought containers where only needed if you use a GUI.

Link to comment
Share on other sites

If enyone has the same problem, here is my solution: You have to sync the client and server by overwriting this 4 methods: onDataPacket, the handleUpdateTag, getUpdatePacket, getUpdateTag.

I also copied these methods, so credits to https://github.com/Mowmaster/

For me the methods are:

Spoiler

 

@Override
public CompoundTag getUpdateTag() {
System.out.println("getUpdateTag");
  return save(new CompoundTag());
}

@Nullable
  @Override
  public ClientboundBlockEntityDataPacket getUpdatePacket() {
  return ClientboundBlockEntityDataPacket.create(this);
}

@Override
  public void onDataPacket(Connection net, ClientboundBlockEntityDataPacket pkt) {
  super.onDataPacket(net,pkt);
  BlockState state = this.level.getBlockState(this.worldPosition);
  this.handleUpdateTag(pkt.getTag());
  this.level.sendBlockUpdated(this.worldPosition, state, state, 3);
}

@Override
  public void handleUpdateTag(CompoundTag tag) {
  this.load(tag);
}

Source: https://github.com/Mowmaster/Pedestals/blob/1.19-Forge/src/main/java/com/mowmaster/pedestals/blocks/Pedestal/BasePedestalBlockEntity.java (slightly modified)

 

The save Method is just a helper Method and is used in the saveAdditional too, it contains all things you want to save, e.g. nbt.put("Inventory", this.inventory.serializeNBT());

Edited by Cron3x
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.