Jump to content

Recommended Posts

Posted

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.

Posted

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?

Posted (edited)
  On 2/17/2023 at 6:26 PM, 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.

Expand  

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
Posted

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?

Posted
  On 2/17/2023 at 6:54 PM, 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).

Expand  

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

Posted (edited)

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:

  Reveal hidden contents
Edited by Cron3x

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 , when I try to launch the forge installer it just crash with a message for 0,5 secondes. I'm using java 17 to launch it. Here's the link of the error :https://cdn.corenexis.com/view/?img=d/ma24/qs7u4U.jpg  
    • You will find the crash-report or log in your minecraft directory (crash-report or logs folder)
    • Use a modpack which is using these 2 mods as working base:   https://www.curseforge.com/minecraft/modpacks/life-in-the-village-3
    • inicie un mundo donde instale Croptopia y Farmer's Delight, entonces instale el addon Croptopia Delight pero no funciona. es la version 1.18.2
    • Hello all. I'm currently grappling with the updateShape method in a custom class extending Block.  My code currently looks like this: The conditionals in CheckState are there to switch blockstate properties, which is working fine, as it functions correctly every time in getStateForPlacement.  The problem I'm running into is that when I update a state, the blocks seem to call CheckState with the position of the block which was changed updated last.  If I build a wall I can see the same change propagate across. My question thus is this: is updateShape sending its return to the neighbouring block?  Is each block not independently executing the updateShape method, thus inserting its own current position?  The first statement appears to be true, and the second false (each block is not independently executing the method). I have tried to fix this by saving the block's own position to a variable myPos at inception, and then feeding this in as CheckState(myPos) but this causes a worse outcome, where all blocks take the update of the first modified block, rather than just their neighbour.  This raises more questions than it answers, obviously: how is a different instance's variable propagating here?  I also tried changing it so that CheckState did not take a BlockPos, but had myPos built into the body - same problem. I have previously looked at neighbourUpdate and onNeighbourUpdate, but could not find a way to get this to work at all.  One post on here about updatePostPlacement and other methods has proven itself long superceded.  All other sources on the net seem to be out of date. Many thanks in advance for any help you might offer me, it's been several days now of trying to get this work and several weeks of generally trying to get round this roadblock.  - Sandermall
  • Topics

×
×
  • Create New...

Important Information

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