Posted December 29, 20195 yr Hey, moving from 1.12 to 1.14 modding, looks like capabilities are in a LazyOptional container of some sort. Still a bit confused. I looked at the code in vanilla chest, if my tile with an inventory, would this code be the correct way: @Override public <T> LazyOptional<T> getCapability(net.minecraftforge.common.capabilities.Capability<T> cap, Direction side) { if (!this.removed && cap == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) { if (this.potHandler == null) { this.potHandler = LazyOptional.of(this::createHandler); } return this.potHandler.cast(); } return super.getCapability(cap, side); } private IItemHandlerModifiable createHandler() { return new InvWrapper(this); } So what if i want a tile with 2 capabilities, Fluid AND Item, would this be correct? @Nonnull @Override public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) { if (!this.removed) { if (cap == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) { if (this.itemStackHandler == null) { this.itemStackHandler = LazyOptional.of(this::createItemHandler); } return this.itemStackHandler.cast(); } if (cap == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY) { if (this.fluidHandler == null) { this.fluidHandler = LazyOptional.of(this::createFluidHandler); } return this.fluidHandler.cast(); } } return super.getCapability(cap, side); } private IItemHandlerModifiable createItemHandler() { return new ItemStackHandler(1) { @Override protected void onContentsChanged(int slot) { getWorld().addBlockEvent(getPos(), getBlockState().getBlock(), 1, 0); getWorld().notifyNeighborsOfStateChange(getPos(), getBlockState().getBlock()); markDirty(); } }; } private FluidTank createFluidHandler() { return new FluidTank(capacity) { @Override protected void onContentsChanged() { markDirty(); } }; } Now, if I want a tile that has both item and fluid capabilites, should my tile implement IInventory, IItemHandler, IFluidTank, IFluidHandler, etc. what is even the difference and what do i use? Thanks
December 29, 20195 yr Author @diesieben07 So I would do private final LazyOptional<IItemHandlerModifiable> potHandler = null; And then in get Capability I'm not sure what to do? How do i actually get whats inside the LazyOptional @Override public <T> LazyOptional<T> getCapability(net.minecraftforge.common.capabilities.Capability<T> cap, Direction side) { if (!this.removed && cap == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) { this.potHandler = LazyOptional.of(this::createHandler); return this.potHandler.cast(); // Wouldn't this just be creating new handler all the time? } return super.getCapability(cap, side); } So when i interact with my capability, what would i to do actually get them? Do i do something like IItemHandler itemStackHandler = te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null).orElse(null); and then check null? Also, what if i want IItemHandlerModifiable, how would I get that capability since it just returns an IItemHandler, can I just straight up cast it?
December 29, 20195 yr Author @diesieben07 Ah i see, so would something like this be correct: CrushingTubTileEntity.java public class CrushingTubTileEntity extends TileEntity { public static int capacity = FluidAttributes.BUCKET_VOLUME * 8; public LazyOptional<ItemStackHandler> itemStackHandler = LazyOptional.of(this::createItemHandler); public LazyOptional<FluidTank> fluidHandler = LazyOptional.of(this::createFluidHandler); public CrushingTubTileEntity() { super(ModTileEntityType.CRUSHING_TUB); } @Override public void read(CompoundNBT tag) { super.read(tag); getFluidHandler().readFromNBT(tag); if (tag.contains("items")) { getItemHandler().deserializeNBT((CompoundNBT) tag.get("items")); } } @Override public CompoundNBT write(CompoundNBT tag) { tag = super.write(tag); getFluidHandler().writeToNBT(tag); tag.put("items", getItemHandler().serializeNBT()); return tag; } @Nullable @Override public SUpdateTileEntityPacket getUpdatePacket() { return new SUpdateTileEntityPacket(getPos(), 3, getUpdateTag()); } @Override public void onDataPacket(NetworkManager net, SUpdateTileEntityPacket pkt) { this.read(pkt.getNbtCompound()); } @Override public CompoundNBT getUpdateTag() { return this.write(new CompoundNBT()); } @Nonnull @Override public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) { if (!this.removed) { if (cap == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) { return this.itemStackHandler.cast(); } if (cap == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY) { return this.fluidHandler.cast(); } } return super.getCapability(cap, side); } public ItemStackHandler getItemHandler() { return itemStackHandler.orElseThrow(RuntimeException::new); } public FluidTank getFluidHandler() { return fluidHandler.orElseThrow(RuntimeException::new); } private ItemStackHandler createItemHandler() { return new ItemStackHandler(1) { @Override protected void onContentsChanged(int slot) { getWorld().addBlockEvent(getPos(), getBlockState().getBlock(), 1, 0); getWorld().notifyNeighborsOfStateChange(getPos(), getBlockState().getBlock()); markDirty(); } }; } private FluidTank createFluidHandler() { return new FluidTank(capacity) { @Override protected void onContentsChanged() { markDirty(); } }; }
December 29, 20195 yr Author @diesieben07 a little unrelated, but is there any situation tileentity world actually returns null?
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.