Jump to content

ItemHandler - Allow player extract/insert


holst666

Recommended Posts

I am really in doubt on how to allow player to manually extract and insert.

I have made my own extract / insert where i disallow to insert into specific slot range, but i want to allow player to do it manually.
I that possible some how?

 

I want to avoid hoppers to extract from slot 0-4 but i want to allow players to do it.

And then i want to avoid hoppers inserting into slot 0-4, but i want to allow players to do it.


Running 1.14.4 MC.

Edited by holst666
Link to comment
Share on other sites

15 minutes ago, holst666 said:

I that possible some how?

Store a NonNullList in your TE give that NonNullList to two IItemHandler instances. One that let's you interact with 0-4 and one that doesnt. Use the one that doesnt in getCapability and allow the one that does allow use in the Container with your own get method. 

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

 

1 hour ago, Animefan8888 said:

Store a NonNullList in your TE give that NonNullList to two IItemHandler instances.

Would you be able to explain that a bit further, not sure i get it. Have not made a custom TE.

 

This one you talk about in the container, is that when you create the slots?

Edited by holst666
Link to comment
Share on other sites

1 hour ago, holst666 said:

Have not made a custom TE.
 

This one you talk about in the container, is that when you create the slots?

Tile Entities are not Containers.

A Tile Entity is the thing-that-is-sort-of-a-block and exists in the world and stores all of the data associated with that instance of that block.

 

A Container is a class that connects a GUI with that TE's contents.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

2 minutes ago, Draco18s said:

Tile Entities are not Containers.

A Tile Entity is the thing-that-is-sort-of-a-block and exists in the world and stores all of the data associated with that instance of that block.

 

A Container is a class that connects a GUI with that TE's contents.

I know, just simply stating that i have not implemented my own TE.

And asking if the "get" is when i'm creating the slots

Link to comment
Share on other sites

1 hour ago, holst666 said:

And asking if the "get" is when i'm creating the slots

No.

There is a specific method that has the name getCapability. It is to which Animefan8888 referrs.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

1 hour ago, holst666 said:

I know, just simply stating that i have not implemented my own TE.

Then where are you storing the Items?

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

10 hours ago, Animefan8888 said:

Then where are you storing the Items?

Oh Sorry.
I'm being an idiot. yes i have a class that extends TileEntity.

 

    public int progress = 0;
    public final int cooldownTicks = 8;

    private newBlock bl;
    public newBlockPlayerItemHandler itemHandler = new newBlockPlayerItemHandler(this);
    public LazyOptional<IItemHandler> handler = LazyOptional.of(() -> this.itemHandler);
    public newBlockTile()
    {
        super(NEWBLOCK_TILE);
    }

@Override
    public void read(CompoundNBT tag)
    {
        CompoundNBT invtag = tag.getCompound("inv");
        handler.ifPresent(h -> {
            CompoundNBT compound = ((INBTSerializable<CompoundNBT>) h).serializeNBT();
            tag.put("inv", compound);
        });

        super.read(tag);
    }


    @Override
    public CompoundNBT write(CompoundNBT tag)
    {
        handler.ifPresent(h -> {
            CompoundNBT compound = ((INBTSerializable<CompoundNBT>) h).serializeNBT();
            tag.put("inv", compound);
        });

        return super.write(tag);
    }



    public <T> LazyOptional<T> getCap(@Nonnull Capability<T> cap, @Nullable Direction side)
    {
        if(cap == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
        {
            return handler.cast();
        }
        return super.getCapability(cap, side);
    }


    @Override
    public ITextComponent getDisplayName() {
        return new StringTextComponent(getType().getRegistryName().getPath());
    }

    @Nullable
    @Override
    public Container createMenu(int i, PlayerInventory playerInventory, PlayerEntity playerEntity) {
        return new newBlockContainer(i, world, pos, playerInventory, playerEntity);
    }



Thats some of the code, then i have some function that handles extraction and insert that calls ExtractItem and InsertItem.

Link to comment
Share on other sites

34 minutes ago, holst666 said:

I'm being an idiot. yes i have a class that extends TileEntity.

Ok now.

 

14 hours ago, Animefan8888 said:

Store a NonNullList in your TE give that NonNullList to two IItemHandler instances. One that let's you interact with 0-4 and one that doesnt. Use the one that doesnt in getCapability and allow the one that does allow use in the Container with your own get method. 

 

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

7 hours ago, holst666 said:

I'm not sure how the get should look like and what that list should be used for.

OK let me spell it out a bit more.

 

Create a NonNullList<ItemStack> that will represent your inventory in your TileEntity. Next create two IItemHandler instances in your TileEntity. Create the ItemStackHandlers by using the constructor that takes in a NonNullList<ItemStack>.This ensures that the IItemHandlers are operating on the same ItemStacks that are in the TileEntity. Make your one initialization of the ItemStackHandlers anonymous like so

 

new ItemStackHandler([NonNullList goes here.]) {
	
	
};

Now in this anonymous class make it so the insertItem and extractItem methods so it can't interact with the slots you want. Hint: Use if statement(s) on the slot number.(Make sure to listen to the JavaDoc above those methods).

 

 

Next create a LazyOptional with LazyOptional.of(() -> [ANONYMOUS CLASS IITEMHANDLER HERE]). And store it in the TileEntity class. This is what you will return in the getCapability method.

 

Now if you made the other IItemHandler(The one that isn't an anonymous class instance) private create a getter method in your TileEntity. This is the one that will be used inside the Container. While the one you returned in the getCapability method will be used for interactions(Hoppers. Pipes, Conduits, etc).

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

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.