Jump to content

[1.16.2] How to open Ender Chest container with an item ?


Recommended Posts

Posted (edited)

Hi everybody !

 

I want to create an item which, when the player right click with it in his main hand, open the Ender Chest container, like if he right click in an Ender Chest. 

@SubscribeEvent
public static void onBackpackUsing(PlayerInteractEvent.RightClickItem event) {
    PlayerEntity player = event.getPlayer();
    World world = event.getWorld();
    if (player.getHeldItemMainhand().getItem() == ModItems.ENDER_BACKPACK.get()) {
        world.setBlockState(player.func_233580_cy_().add(0,-1,0), Blocks.ENDER_CHEST.getDefaultState());
    } else if (player.getHeldItemMainhand().getItem() == ModItems.BACKPACK.get()) {
        world.setBlockState(player.func_233580_cy_().add(0,-1,0), Blocks.CHEST.getDefaultState());
    }
}

With this code, I can place an Ender Chest (or a Chest, depending on the item that the player use) under the player, but I don't know how to open the container, instead of placing the chest.

 

Thanks for you help !

Edited by Korantin
Posted

I have another question...

How can I do the same thing, but with a normal chest ?

I want to create another item, a backpack, that when the player right click with it in the main hand, it open a container like if he was opening a chest. I also want to keep items that are in the container, to access to them in the future if the player right click with the same backpack.

I studied the chest code, but I still don't find how to do this.

Posted
4 hours ago, diesieben07 said:

Make your items provide a IItemHandler capability. You can then use that in a Container / ContainerScreen as normal.

I'm not verry familiar with Capabilities, and I don't really understand the official documentation (I tried to understand this by myself before asking you some help), so could you explain me how can I do this please ?

Thanks a lot for helping me !

Posted (edited)
18 minutes ago, diesieben07 said:

Make a class that implements ICapabiltySerializable.

package com.vkorantin.mcmod.init.capabilities;

import net.minecraft.nbt.INBT;
import net.minecraft.util.Direction;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilitySerializable;
import net.minecraftforge.common.util.LazyOptional;

public class ModCapabilities implements ICapabilitySerializable {

    @org.jetbrains.annotations.NotNull
    @Override
    public LazyOptional<T> getCapability(@org.jetbrains.annotations.NotNull Capability<T> cap, @org.jetbrains.annotations.Nullable Direction side) {
        return null;
    }

    @Override
    public INBT serializeNBT() {
        return null;
    }

    @Override
    public void deserializeNBT(INBT nbt) {

    }
}

I've done that, but my IDE underline in red the line where it is written "public LazyOptional<T> getCapability(@org.jetbrains.annotations.NotNull Capability<T> cap, @org.jetbrains.annotations.Nullable Direction side) {" and says me that there is an error.

 

And the word jetbrains is written in red in all the class.

 

What am I supposed to do to fix it ?

Edited by Korantin
Posted (edited)

I've searched how to expose my ItemStackHandler as a capacity and still don't understand how to do this...

Sorry, but could you explain me how to do this please ?

 

My class :

package com.vkorantin.mcmod.init.capabilities;

import net.minecraft.nbt.INBT;
import net.minecraft.util.Direction;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityInject;
import net.minecraftforge.common.capabilities.ICapabilitySerializable;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.items.ItemStackHandler;

public class ModBackpackCapability implements ICapabilitySerializable {

    ItemStackHandler myItemStackHandler = new ItemStackHandler(9);

    @CapabilityInject(ItemStackHandler.class)
    static Capability<ItemStackHandler> myCapability = null;

    @Override
    public INBT serializeNBT() {
        return null;
    }

    @Override
    public void deserializeNBT(INBT nbt) {

    }

    @Override
    public <T> LazyOptional<T> getCapability(Capability<T> cap, Direction side) {
        return null;
    }

}

 

Edited by Korantin
Posted

I've done that :

package com.vkorantin.mcmod.init.capabilities;

import net.minecraft.nbt.INBT;
import net.minecraft.util.Direction;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityInject;
import net.minecraftforge.common.capabilities.ICapabilitySerializable;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemStackHandler;

public class ModBackpackCapability implements ICapabilitySerializable {

    ItemStackHandler myItemStackHandler = new ItemStackHandler(9);

    @CapabilityInject(ItemStackHandler.class)
    static Capability<ItemStackHandler> ITEM_HANDLER_CAPABILITY = null;


    @Override
    public INBT serializeNBT() {
        return null;
    }

    @Override
    public void deserializeNBT(INBT nbt) {

    }

    @Override
    public <T> LazyOptional<T> getCapability(Capability<T> cap, Direction side) {
        return null;
    }

    private LazyOptional<IItemHandler> handler = LazyOptional.of(() -> ITEM_HANDLER_CAPABILITY).cast();

    @Override
    public boolean hasCapability(Capability<?> capability) {
        if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
            return true;
        }
        return super.hasCapability(capability);
    }

    @Override
    public <T> T getCapability(Capability<T> capability) {
        if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
            return (T) inventory;
        }
        return super.getCapability(capability);
    }

}

But in the hasCapability methods, hasCapability is written in red, and the IDE says that "Cannot resolve method 'hasCapability' in 'Object'.

Please can you help me to fix it ? I don't really understand what goes wrong...

Thanks a lot for all the time you spend for me !

Posted
package com.vkorantin.mcmod.init.capabilities;

import net.minecraft.nbt.INBT;
import net.minecraft.util.Direction;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilitySerializable;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemStackHandler;

public class ModBackpackCapability implements ICapabilitySerializable {

    ItemStackHandler myItemStackHandler = new ItemStackHandler(9);

    private final LazyOptional<IItemHandler> handler = LazyOptional.of(() -> myItemStackHandler);

    @Override
    public INBT serializeNBT() {
        return null;
    }

    @Override
    public void deserializeNBT(INBT nbt) {

    }

    @Override
    public <T> LazyOptional<T> getCapability(Capability<T> cap, Direction side) {
        return null;
    }

}

Is it something like this for the LazyOptional<ItemHandler> ?

Posted

I study Java in my studies, but we are at the very beginnings. I learn a bit more by myself, but just the basics.

I don't understand how to write and read data, like if the player was using a chest, when he right click with the item in his main hand.

I know how to detect when he right click with the item in his main hand, but no how to open the same gui as the chest gui, an how to write and read data.

Posted

Okay, so, now I've done that :

package com.vkorantin.mcmod.init.capabilities;

import net.minecraft.nbt.INBT;
import net.minecraft.util.Direction;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilitySerializable;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemStackHandler;


public class ModBackpackCapability implements ICapabilitySerializable {

    ItemStackHandler myItemStackHandler = new ItemStackHandler(9);

    private LazyOptional<IItemHandler> handler = LazyOptional.of(() -> myItemStackHandler);

    @Override
    public INBT serializeNBT() {
        return myItemStackHandler.serializeNBT();
    }

    @Override
    public void deserializeNBT(INBT nbt) {
        myItemStackHandler.deserializeNBT(myItemStackHandler.serializeNBT());
    }

    @Override
    public <IItemHandler> LazyOptional<IItemHandler> getCapability(Capability<IItemHandler> cap, Direction side) {
        return (LazyOptional<IItemHandler>) handler;
    }

}

But in my other class that manages events, how I open the GUI ? I just have to call the getCapability method, or I have to do something like in ChestBlock's class to open a chest ?

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.