Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[1.16] Attach Capability to Chunks?


Recommended Posts

Say I simply want to hold a single random Integer in each chunk, I know I can attach Capabilities to Chunks through the  AttachcapabilitiesEvent, but then I would need to create my own class that extends Capability<Integer>? Would I need to serialize the data to nbt for it to be saved? any tips on how I should go about doing this?

Link to post
Share on other sites

No, you would create your own class that implements MySavedIntegerStorage<MySavedIntegerInterface>

 

https://github.com/Draco18s/ReasonableRealism/blob/1.12.1/src/main/java/com/draco18s/farming/entities/capabilities/MilkStorage.java

Edited by Draco18s

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to post
Share on other sites
30 minutes ago, Draco18s said:

No, you would create your own class that implements MySavedIntegerStorage<MySavedIntegerInterface>

 

https://github.com/Draco18s/ReasonableRealism/blob/1.12.1/src/main/java/com/draco18s/farming/entities/capabilities/MilkStorage.java

Okay, like this?

public interface IChunkValue {

    int getValue();
    void setValue(int value);

}

 

public class ChunkValue implements IChunkValue {

    private int value;

    public ChunkValue(int value) {
        this.value = value;
    }

    @Override
    public int getValue() {
        return value;
    }

    @Override
    public void setValue(int value) {
        this.value = value;
    }
}

 

public class ChunkValueStorage implements Capability.IStorage<IChunkValue> {

    @Override
    public INBT writeNBT(Capability<IChunkValue> capability, IChunkValue instance, Direction side) {
        CompoundNBT nbt = new CompoundNBT();
        nbt.putInt("value", instance.getValue());
        return nbt;
    }

    @Override
    public void readNBT(Capability<IChunkValue> capability, IChunkValue instance, Direction side, INBT nbt) {
        instance.setValue(((CompoundNBT)nbt).getInt("value"));
    }

    public class Factory implements Callable<IChunkValue> {
        @Override
        public IChunkValue call() throws Exception {
            return new ChunkValue(0);
        }
    }
}

 

Now what?... I don't really get where to go after this

Link to post
Share on other sites

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to post
Share on other sites
13 minutes ago, Draco18s said:

yeah, I did that, but I'm getting a bunch of NullPointerExceptions at this line:

    @Override
    public INBT serializeNBT() {
        return TutorialMod.CHUNK_VALUE_CAPABILITY.writeNBT(getCachedChunkValue(), null);
    }

 

I'm guessing that it's because I'm defining TutorialMod.CHUNK_VALUE_CAPABILITY as null, but I don't know what to do then

 

    @CapabilityInject(IChunkValue.class)
    public static final Capability<IChunkValue> CHUNK_VALUE_CAPABILITY = null;

 

should I initialise it as something else?

Link to post
Share on other sites
1 minute ago, kiou.23 said:

should I initialise it as something else?

No, that's how its supposed to work. Forge injects the value to non-null at runtime.

You still need to register the capability.

https://github.com/Draco18s/ReasonableRealism/blob/1.12.1/src/main/java/com/draco18s/farming/FarmingBase.java#L189

  • Thanks 1

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to post
Share on other sites
22 minutes ago, Draco18s said:

No, that's how its supposed to work. Forge injects the value to non-null at runtime.

You still need to register the capability.

https://github.com/Draco18s/ReasonableRealism/blob/1.12.1/src/main/java/com/draco18s/farming/FarmingBase.java#L189

OHH that is what I was missing, thanks!

just a final question tho, why do I need an interface? if it's only going to be implemented once, why can't I just reference the class (in this case, ChunkValue)?

Edited by kiou.23
Link to post
Share on other sites

Because of how Capabilities work.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to post
Share on other sites
1 hour ago, kiou.23 said:

just a final question tho, why do I need an interface? if it's only going to be implemented once, why can't I just reference the class (in this case, ChunkValue)?

You don't need an interface, you could use the ChunkValue class only, without an interface.

  • Thanks 1
Link to post
Share on other sites

Expanding on this:

  • If you intend for your capability to ever be used by other mods (i.e. you're making it part of your mod's API) then using an interface is very strongly advised, simply as good programming practice (expose the interface, not the implementation).
  • If you only ever intend for your capability to be used for storing data for your own purposes, an interface is very much optional.  You might decide it's useful to have one, just for tidiness in your own code, but then again you're free just to use a class.

 

  • Like 1
  • Thanks 1
Link to post
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.

Guest
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 Privacy Policy.