Posted January 9, 20205 yr @Override public void read(CompoundNBT tag) { CompoundNBT invTag = tag.getCompound("inv"); getHandler().deserializeNBT(invTag); super.read(tag); } @Override public CompoundNBT write(CompoundNBT tag) { CompoundNBT compound = getHandler().serializeNBT(); tag.put("inv", compound); return super.write(tag); } private ItemStackHandler getHandler() { if (handler == null) { handler = new ItemStackHandler(1); } return handler; } @Nonnull @Override public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap) { if (cap == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) { return LazyOptional.of(() -> (T) getHandler()); } return super.getCapability(cap); } I've been watching YouTube tutorials and when trying to make a tile entity block which holds items, he wrote this. Could someone clarify what is going on in this section of code? Thanks.
January 9, 20205 yr Author 6 minutes ago, diesieben07 said: You can learn more about capabilities in the documentation. The lazy initialization in getHandler is pointless. LazyOptional is already lazy. Plus you are calling getHandler in readFromNbt, which is like, the first thing that happens in a tile entity. So there isn't even any point lazily initializing the handler. Just create it in the constructor and use a final field. Thanks!
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.