Posted September 29, 20214 yr 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.
September 29, 20214 yr the Factory and Storage of the Capability has been removed also you need to use RegisterCapabilitiesEvent to register your Capabilities
September 29, 20214 yr Author I am aware that I need to register the capabilities, I just haven't provided that code as far as I am pretty sure nothing has changed in that department. What do I do in place of the Factory and Storage for the capability?
September 29, 20214 yr nothing you need to remove them, move the code from CooldownStorage#writeNBT & CooldownStorage#readNBT into the CooldownCapability#serializeNBT & CooldownCapability#deserializeNBT
September 29, 20214 yr Author 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(); }
September 29, 20214 yr Author 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")); } }
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.