Jump to content

[1.14.4] Can someone explain to me the correct way of using LazyOptional and some other questions regarding Interfaces


Samaritans

Recommended Posts

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

Link to comment
Share on other sites

@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?

Link to comment
Share on other sites

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

 

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.