Zemelua Posted February 20, 2021 Posted February 20, 2021 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? Quote
Luis_ST Posted February 20, 2021 Posted February 20, 2021 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 Quote
Zemelua Posted February 21, 2021 Author Posted February 21, 2021 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? Quote
UserMC123 Posted March 4, 2021 Posted March 4, 2021 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(); } } Quote
Recommended Posts
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.