Jump to content

[1.16] Simple Custom Chest


Zemelua

Recommended Posts

I'm trying to extend a LockableLootTileEntity to make a simple custom chest, similar to a vanilla barrel or chest, but looking at the code for some mods seems to require the use of packets and capabilities. , I don't understand this well. When and what should I do when using capabilities?

Link to comment
Share on other sites

1 hour ago, Zemelua said:

I don't understand this well. When and what should I do when using capabilities?

if you want to create a chest / barrel that the item drops when it is destroyed you don't need a capability.

if you want to create a block like the enderchest that has its own inventory for each player, you need a capability

Link to comment
Share on other sites

  • 2 weeks later...

Try This:

public class TestChestTile extends LockableLootTileEntity
{
    protected int Size = 36;
    private NonNullList<ItemStack> chest_items = NonNullList.withSize(Size,ItemStack.EMPTY);
    protected int numPlayerUsing;
    private IItemHandlerModifiable items = createHandler();
    private LazyOptional<IItemHandlerModifiable> itemHandler = LazyOptional.of(()-> items);
    public TestChestTile(TileEntityType<?> p_i48284_1_) {
        super(p_i48284_1_);
    }
    public TestChestTile() {
        this(TileEntityInit.TEST_CHEST_TILE.get());
    }

    @Override
    public int getSizeInventory() {
        return Size;
    }

    @Override
    public NonNullList<ItemStack> getItems() {
        return chest_items;
    }

    public void setItems(NonNullList<ItemStack> items) {
        this.chest_items = items;
    }

    @Override
    protected ITextComponent getDefaultName() {
        return new TranslationTextComponent("container.test_chest");
    }

    @Override
    protected Container createMenu(int id, PlayerInventory player) {
        return new TestChestContainer(id,player,this);
    }

    @Override
    public CompoundNBT write(CompoundNBT compound) {
        super.write(compound);
        if (!this.checkLootAndWrite(compound)) {
            ItemStackHelper.saveAllItems(compound, this.chest_items);
        }
        return compound;
    }

    @Override
    public void read(BlockState state,CompoundNBT compound) {
        super.read(state,compound);
        this.chest_items = NonNullList.withSize(this.getSizeInventory(), ItemStack.EMPTY);
        if (!this.checkLootAndRead(compound)) {
            ItemStackHelper.loadAllItems(compound, this.chest_items);
        }
    }

    @Override
    public boolean receiveClientEvent(int id, int type) {
        if(id == 1){
            this.numPlayerUsing = type;
            return true;
        } else {
            return super.receiveClientEvent(id, type);
        }
    }

    @Override
    public void openInventory(PlayerEntity playerEntity) {
        if(playerEntity.isSpectator()){
            if(this.numPlayerUsing < 0){
                this.numPlayerUsing = 0;
            }
            ++numPlayerUsing;
            this.openOrClose();
        }
    }

    @Override
    public void closeInventory(PlayerEntity playerEntity) {
        if(!playerEntity.isSpectator()){
            --this.numPlayerUsing;
            this.openOrClose();
        }
    }

    protected void openOrClose(){
        Block block = this.getBlockState().getBlock();
        if (block instanceof TestChestBlock) {
            this.world.addBlockEvent(this.pos, block, 1, this.numPlayerUsing);
            this.world.notifyNeighborsOfStateChange(this.pos, block);
        }
    }
    
    public static int getPlayersUsing(IBlockReader reader, BlockPos pos) {
        BlockState blockstate = reader.getBlockState(pos);
        if (blockstate.hasTileEntity()) {
            TileEntity tileentity = reader.getTileEntity(pos);
            if (tileentity instanceof TestChestTile) {
                return ((TestChestTile) tileentity).numPlayerUsing;
            }
        }
        return 0;
    }

    public static void swapContents(TestChestTile te, TestChestTile otherTe) {
        NonNullList<ItemStack> list = te.getItems();
        te.setItems(otherTe.getItems());
        otherTe.setItems(list);
    }

    @Override
    public void updateContainingBlockInfo() {
        super.updateContainingBlockInfo();
        if (this.itemHandler != null) {
            this.itemHandler.invalidate();
            this.itemHandler = null;
        }
    }

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

    private IItemHandlerModifiable createHandler() {
        return new InvWrapper(this);
    }

    @Override
    public void remove() {
        super.remove();
        if(itemHandler != null) {
            itemHandler.invalidate();
        }
    }
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.