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.

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

Featured Replies

Posted

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

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

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

 

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.