Jump to content

[1.17.1] Custom Capabilities


TheOrangeInd

Recommended Posts

I'm trying to add a radiation to my mod and I need to store its value for each player. As I understand it, I should use capability for this.
The problem is that I don't understand well the new capability system in 1.17.
So, can I create a capability to store an integer value? What are the main components of capability in its present form?

Link to comment
Share on other sites

Okay. The second link was really helpful to me. Thanks for it.
I managed to implement the capability I needed, but I'm not sure about the correctness of my approach to serialization.
Anyway, I post the resulting code here.
Thanks again for your help.

My interface:

 
Spoiler

 


public interface IRadiation extends INBTSerializable<CompoundTag>
{
    int getRadLevel();
    void setRadLevel(int value);
}

Its implementation:

Spoiler

public class RadiationCapability implements IRadiation
{
    public static final ResourceLocation ID = new ResourceLocation(MainClass.MODID, "radiation_level");
    private static final String NBT_KEY = "thrad.rad";

    @CapabilityInject(IRadiation.class)
    public static Capability<IRadiation> RADIATION_CAPABILITY = null;
    public static void register() { CapabilityManager.INSTANCE.register(IRadiation.class); }

    private int radLevel;

    public RadiationCapability() { this(Configuration.DEFAULT_RAD_LEVEL); }
    public RadiationCapability(int radLevel) { this.radLevel = radLevel; }

    @Override
    public int getRadLevel() { return radLevel; }

    @Override
    public void setRadLevel(int value) { radLevel = value; }

    @Override
    public CompoundTag serializeNBT()
    {
        CompoundTag nbt = new CompoundTag();
        nbt.putInt(NBT_KEY, getRadLevel());
        return nbt;
    }

    @Override
    public void deserializeNBT(CompoundTag nbt)
    {
        setRadLevel(nbt.getInt(NBT_KEY));
    }
}

 

And its provider:

Spoiler

public class RadiationCapProvider implements ICapabilitySerializable<CompoundTag>
{
    private final Capability<IRadiation> capability;
    private final LazyOptional<IRadiation> implementation;

    public RadiationCapProvider(Capability<IRadiation> cap, LazyOptional<IRadiation> impl)
    {
        capability = cap;
        implementation = impl;
    }

    public static RadiationCapProvider from(Capability<IRadiation> cap, NonNullSupplier<IRadiation> impl)
    {
        return new RadiationCapProvider(cap, LazyOptional.of(impl));
    }

    @Nonnull
    @Override
    public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side)
    {
        if(cap == capability) return implementation.cast();
        return LazyOptional.empty();
    }

    @Override
    public CompoundTag serializeNBT()
    {
        return implementation.isPresent() ? implementation.orElse(null).serializeNBT() : new CompoundTag();
    }

    @Override
    public void deserializeNBT(CompoundTag nbt)
    {
        if(implementation.isPresent()) implementation.orElse(null).deserializeNBT(nbt);
    }
}

 

 

Link to comment
Share on other sites

Should I use an instance of my RadiationCapability for this?

Spoiler

public class RadiationCapProvider implements ICapabilitySerializable<CompoundTag>
{
    private final RadiationCapability instance;
    private final Capability<IRadiation> capability;
    private final LazyOptional<IRadiation> implementation;

    public RadiationCapProvider(Capability<IRadiation> cap, LazyOptional<IRadiation> impl)
    {
        capability = cap;
        implementation = impl;
        instance = (RadiationCapability) impl.orElse(null);
    }

    public static RadiationCapProvider from(Capability<IRadiation> cap, NonNullSupplier<IRadiation> impl)
    {
        return new RadiationCapProvider(cap, LazyOptional.of(impl));
    }

    @Nonnull
    @Override
    public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side)
    {
        if(cap == capability) return implementation.cast();
        return LazyOptional.empty();
    }

    @Override
    public CompoundTag serializeNBT()
    {
        return instance.serializeNBT();
    }

    @Override
    public void deserializeNBT(CompoundTag nbt)
    {
        instance.deserializeNBT(nbt);
    }
}

 

 

Link to comment
Share on other sites

Okay. Now I think that I figured out my mistake and this is how my Provider looks now:

Spoiler

public class RadiationCapProvider implements ICapabilitySerializable<CompoundTag>
{
    private final Capability<IRadiation> capability = RadiationCapability.RADIATION_CAPABILITY;
    private final RadiationCapability instance = new RadiationCapability();

    @Nonnull
    @Override
    public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side)
    {
        if(cap == capability) return LazyOptional.of(()->instance).cast();
        return LazyOptional.empty();
    }

    @Override
    public CompoundTag serializeNBT() { return instance.serializeNBT(); }

    @Override
    public void deserializeNBT(CompoundTag nbt) { instance.deserializeNBT(nbt); }
}

 

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Yes - maybe a conflict/incompatibility with another mod
    • LINK ADA DI BAWAH INI  LINK LOGIN LINK DAFTAR EXTRAWD ADALAH AGEN SLOT , TOGEL DAN BOLA TERBESAR INDONESIA YANG MERUPAKAN SITUS ONLINE PERTAMA YANG MENYEDIAKAN BERBAGAI JENIS PERMAINAN DAN BETTING ONLINE TELENGKAP, SEPERTI PERMAINAN CASINO ONLINE, SLOT ONLINE, TOGEL ONLINE, BOLA ONLINE, DAN MASIH BANYAK JENIS PILIHAN PERMAINAN LAINNYA. PROVIDER TERLENGKAP DAN TERNAMA UNTUK SLOT : PRAGMATIC PLAY-PG SOFT-NOLIMIT CITY-JOKER-PNG SLOT-BIG TIM GAMING-NETENT-TIGER PROVIDDER TERLENGKAP LIVEGAME : PRAGMATIC PLAY - VEGAS LOUNGE - SA GAMING - BG LIVE - EVOLUTION GAMING SWEET BONANZA CANDYLAND, MEGAWHEEL X 500, BACCARAT, ROULETTE PELAYANAN TERBAIK DARI OPERATOR KAMI YANG SIAP MELAYANI ANDA SELAMA 24 JAM
    • LINK ADA DI BAWAH INI  LINK LOGIN LINK DAFTAR EXTRAWD ADALAH AGEN SLOT , TOGEL DAN BOLA TERBESAR INDONESIA YANG MERUPAKAN SITUS ONLINE PERTAMA YANG MENYEDIAKAN BERBAGAI JENIS PERMAINAN DAN BETTING ONLINE TELENGKAP, SEPERTI PERMAINAN CASINO ONLINE, SLOT ONLINE, TOGEL ONLINE, BOLA ONLINE, DAN MASIH BANYAK JENIS PILIHAN 
    • Platform tempat untuk melangsungkan perjudian togel yang paling aman saat ini dan bisa merasakan sensasi hadiah JP jeda tentunya hanya bisa menggunakan situs agen toto togel resmi EXTRAWD . Bersama website agen togel ini anda akan mendapatkan jaminan bahwa hadiah kemenangan pasti dibayar tanpa ada potongan biaya administrasi. Hal terpenting di dalam permainan judi togel melalui situs EXTRAWD adalah wajib memiliki legalitas demi menghindari risiko terhadap hadiah kemenangan. Legalitas tersebut merupakan badan hukum untuk mengawasi terjadinya proses pengundian, sehingga cita-cita untuk mendapatkan JP togel semakin mudah untuk direalisasikan.  
    • I'm having trouble updating my mod Primal Magick to MC1.20.4 on Forge 49.0.48 with JEI 17.3.0.49. I believe I've followed the Forge and JEI instructions correctly, but I'm getting the following exception when I try to run the data generator: java.lang.module.ResolutionException: Modules jei._1._20._4.common.api and jei export package mezz.jei.api.runtime to module primalmagick Full output sample here, and the build.gradle file in question is here. Trying to run the game client results in a different type of exception, but with the same inner error. It looks like I'm getting two separate JEI modules which are interfering with each other, but I'm not sure why that would be. Things I've discovered in my investigations so far: Blowing away my repository and re-cloning from remote does not fix the error, even in combination with nuking my entire gradle cache.  I'm *mostly* sure that it's not just because my workspace is in a bad state. Commenting out line 144 of the build.gradle file, where it specifies the runtime-only deobf dependency of JEI makes the exception not get thrown, but means that JEI is not active in my debug runtime.  While this is a partial workaround for the issue, it leaves me unable to debug any of my JEI integration code, and so is far from ideal. Most maddeningly, running the client and/or data generator from the gradle CLI *also* causes the bug to not reproduce.  Data files get generated, and the client even runs with JEI; it just leaves me unable to trace through the code. I've already reached out to Mezz on the JEI Discord and he's stumped as well.  If I didn't know better, I'd say that one of the "compileOnly" or "runtimeOnly" directives are being executed when they shouldn't be, but why that would be happening now, or happening only when run in Eclipse, is beyond me.  If anyone knows how to resolve this issue, I would be very grateful.  Thank you, and stay safe.
  • Topics

×
×
  • Create New...

Important Information

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