Jump to content

1.18.1 Capability change from 1.16.5 Help.


dun

Recommended Posts

Hello, it's me again! I'm currently in the process of making a port to 1.18.1 when I stumbled across a roadblock. 

package com.Polarice3.FireNBlood.items.capability;

import com.Polarice3.FireNBlood.items.handler.SoulUsingItemHandler;
import net.minecraft.core.Direction;
import net.minecraft.nbt.Tag;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilitySerializable;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class SoulUsingItemCapability implements ICapabilitySerializable<Tag> {
    private final ItemStack stack;
    private final LazyOptional<IItemHandler> holder = LazyOptional.of(this::getHandler);
    private SoulUsingItemHandler handler;

    public SoulUsingItemCapability(ItemStack stack) {
        this.stack = stack;
    }

    @Nonnull
    private SoulUsingItemHandler getHandler() {
        if (handler == null) {
            handler = new SoulUsingItemHandler(stack);
        }
        return handler;
    }

    @Nonnull
    @Override
    public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) {
        return cap == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY ? holder.cast() : LazyOptional.empty();
    }

    public Tag serializeNBT() {
        return CapabilityItemHandler.ITEM_HANDLER_CAPABILITY.writeNBT(getHandler(), null);
    }

    public void deserializeNBT(Tag nbt) {
        CapabilityItemHandler.ITEM_HANDLER_CAPABILITY.readNBT(getHandler(), null, nbt);
    }
}

In 1.18.1, the write and readNBTs are now obsolete so now I've no clue on how to properly set up my Capability again. Any suggestions?

Link to comment
Share on other sites

add a serialize and deserialize method to your CapabilityHandler call there the logic from write and read method, then call these two methods from serializeNBT and deserializeNBT of your CapabilityProvider. Note you can call the logic from the read and write method directly from your CapabilityProvider if you hava setters to set the values of your Capability 

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.



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.