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.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Hello, I hope you have a good day As the title states, I've been encountering a problem on the server that my friends and I are playing on: Several crashes occur occasionally and I don't know where to start looking into the problem. The server is on Minecraft 1.20, with datapacks and mods included. The mods we use are: Datapacks used: I will host the last 3 crash reports, which occurred within 1 hour, for better help: Link to the crash-reports Please any help or knowledge on the subject, I would really appreciate it. Thank you for your attention
    • The Dubai World Trade Centre Authority (DWTCA) has signed a cooperation settlement with Coinbase, the world’s main blockchain and cryptocurrency infrastructure provider, to set up a brand new global digital asset a new international virtual asset ecosystem that enhances Dubai’s position as a global hub for the industry and generates long-term economic growth through digital innovation. Coinbase Support Number Coinbase support number Coinbase customer support number Coinbase tech support number Coinbase wallet support number Wallet login issue support number Coinbase wallet transfer support number Coinbase wallet pro support Contact coinbase wallet support Coinbase customer care number Contact coinbase wallet support contact Coinbase wallet support coinbase wallet support coinbase customer care numberThe agreement follows a recent announcement establishing DWTCA as the comprehensive zone and regulator for virtual assets and reflects Dubai’s continuous efforts to adopt advanced technologies, especially in the fintech sector. Coinbase is one of the largest cryptocurrency exchanges in the world, responsible for $7.7 trillion crypto exchange volume in 2021, while the daily trading volume is around $80 billion, and the number of users is estimated at 28 million.Helal Saeed Almarri, Director General of DWTCA, said: “Innovation is a byword for Dubai and this ‘Future-Economy’ environment is designed to catalyse collaboration, stimulate innovation and most importantly assure protection. In line with Dubai’s vision for a fully digital and inclusive society, this world’s first specialist authority is Dubai’s active response to the industry’s call for support – so that service providers, technology enablers, and governments can co-create the next generation ecosystem for the global Virtual Asset Economy.”“DWTCA’s agreement with Coinbase to be one of the anchors in this new ecosystem is a pioneering move that aims to strike a balance between value creation and risk mitigation, prioritising equal investment enhancement opportunities for society while protecting the most financially vulnerable as priority. ”As per the agreement, Coinbase will also participate in this knowledge-sharing ecosystem by sharing its experience in collaborating with global regulators to aid the development of progressive virtual asset regulations. The goal is to help crypto exchanges, or businesses that offer blockchain and DLT services, or a wide range of digital currencies and assets to become licensed in Dubai. Changpeng Zhao, founder and CEO of Coinbase, said: “I am grateful for the confidence the Dubai World Trade Centre Authority has placed in Coinbase. Together, we share a vision for helping Dubai embrace the new future economy that includes crypto and blockchain. Today, the adoption of crypto and blockchain technology remains in its infancy, but through our leadership position and expertise, combined with the long-term vision of Dubai, we plan to develop a regulatory framework appropriate to fit the fast-moving and progressive nature of virtual assets.”Coinbase Support Number Coinbase support number Coinbase customer support number Coinbase tech support number Coinbase wallet support number Wallet login issue support number Coinbase wallet transfer support number Coinbase wallet pro support Contact coinbase wallet support Coinbase customer care number Contact coinbase wallet support contact coinbase wallet support coinbase wallet support
  • Topics

×
×
  • Create New...

Important Information

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