Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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?

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

  • Author
12 hours ago, diesieben07 said:

Do not use LockableLootTileEntity, use the ItemStackHandler to add an inventory to your tile entity.

What is the reason for doing so? Why use ItemStackHandler?

  • 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();
        }
    }

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.