Jump to content

How have Capabilities in 1.17 changed?


Twingemios

Recommended Posts

I'm trying to make some capabilities in 1.17 after moving from 1.16 and I can't figure out how I am supposed to do them now.


This is how I did them in 1.16.

public class Cooldowns implements ICooldowns {
    private int summonCooldown = 0;

    @Override
    public void setSummonCooldown(int var) {
        this.summonCooldown = var;
    }

    @Override
    public int getSummonCooldown() {
        return this.summonCooldown;
    }
}
public class CooldownCapability implements ICapabilitySerializable<CompoundNBT> {

    @CapabilityInject(ICooldowns.class)
    public static final Capability<ICooldowns> CAPABILITY = null;
    private LazyOptional<ICooldowns> instance = LazyOptional.of(CAPABILITY::getDefaultInstance);

    @Override
    public <T> LazyOptional<T> getCapability(Capability<T> cap, Direction side) {
        return cap == CAPABILITY ? instance.cast() : LazyOptional.empty();
    }

    @Override
    public CompoundNBT serializeNBT() {
        return (CompoundNBT) CAPABILITY.getStorage().writeNBT(CAPABILITY, instance.orElseThrow(() -> new IllegalArgumentException("LazyOptional must not be empty!")), null);
    }

    @Override
    public void deserializeNBT(CompoundNBT nbt) {
        CAPABILITY.getStorage().readNBT(CAPABILITY, instance.orElseThrow(() -> new IllegalArgumentException("LazyOptional must not be empty!")), null, nbt);
    }
}
public class CooldownStorage implements IStorage<ICooldowns> {

    @Override
    public CompoundNBT writeNBT(Capability<ICooldowns> capability, ICooldowns instance, Direction side) {
        CompoundNBT cnbt = new CompoundNBT();
        cnbt.putInt("summoncooldown",instance.getSummonCooldown());
        return cnbt;
    }

    @Override
    public void readNBT(Capability<ICooldowns> capability, ICooldowns instance, Direction side, INBT nbt) {
        CompoundNBT inbt = (CompoundNBT) nbt;
        instance.setSummonCooldown(inbt.getInt("summoncooldown"));
    }
}

 

From I understand IStorage and getDefaultInstance are depreciated. What do I need to change in order to run this code on 1.17? I've tired to do a lot of research on this but it seems forge's read the docs for 1.17 is outdated as it still tells me to use IStorage.

Link to comment
Share on other sites

18 minutes ago, Luis_ST said:

nothing you need to remove them,
move the code from CooldownStorage#writeNBT & CooldownStorage#readNBT
into the CooldownCapability#serializeNBT & CooldownCapability#deserializeNBT

Then what do I do with this? It needs the instance

    @Override
    public <T> LazyOptional<T> getCapability(Capability<T> cap, Direction side) {
        return cap == CAPABILITY ? instance.cast() : LazyOptional.empty();
    }

 

Link to comment
Share on other sites

6 minutes ago, diesieben07 said:

You just create a new instance of your capability.

How can I do that? It requires that a LazyOptional must be returned in getCapability
 

public class ChaosActsProvider implements ICapabilitySerializable<CompoundTag> {
    @CapabilityInject(IChaosActs.class)
    public static final Capability<IChaosActs> CAPABILITY = null;
    public static final ChaosActs instance = new ChaosActs();

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

    @Override
    public CompoundTag serializeNBT() {
        CompoundTag cnbt = new CompoundTag();

        cnbt.putInt("timer",instance.getTimer());

        return cnbt;
    }

    @Override
    public void deserializeNBT(CompoundTag nbt) {
        instance.setTimer(nbt.getInt("timer"));

    }
}

 

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.