Jump to content

Recommended Posts

Posted

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?

Posted (edited)

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.

Posted
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

Posted

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.

Posted
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?

Posted
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.

Posted (edited)
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
Posted

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.

Posted
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
Posted
12 hours ago, Draco18s said:

Because of how Capabilities work.

 

11 hours ago, vemerion said:

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

 

who do I trust?

Posted

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

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

    • The game crashed whilst unexpected error Error: net.minecraftforge.fml.ModLoadingException: Advanced Mining Dimension (mining_dimension) encountered an error during the done event phase
    • Here is the end of the log. it was way too big to put in pastebin, but I started from when I was online and everything was fine. Error should be in here: https://pastebin.com/Sdhdq593
    • Update: I stand corrected as I was able to dig up a relevant log from my earlier testing which highlights the overall issue but does not explain how to solve it. See, Diagnostic Logs for pack.mcmeta not found: https://pastebin.com/LXS8Rtna
    • Which mod was this? What are all the mods that were in use? It will help if enough information to replicate the problem is available.
    • I have been attempting to create a supplementary resource pack with Patchouli in order to add back the guidebook for Better End but unfortunately in every prototype I have made pack.mcmeta and my resource pack are not recognized. I have tested both zipped and unzipped and either way curseforge does not recognize my resource pack as existing. For testing I stripped my pack down to just the pack.mcmeta file and two empty folders labelled data and assets and I know the data folder is not the problem as firstly my first attempts just had an assets folder following Patchouli instructions and data came later in my flailing attempts to make anything in my pack work. The mcmeta file is not recognized whether or not I use this:  { "pack": { "pack_format": 15, "description": "A replacement for the BetterEnd Guide Book." } "language": { "en_US": { "name": "English", "region": "United States" } } } ,or this:  { "pack": { "pack_format": 15, "description": "A replacement for the BetterEnd Guide Book." } } I have made sure to only use lowercase and the pack folder is named better-end-guide. Is this some magic nonsense from me doing this manually instead of using an IDE or similar tool? Could it be because my files are by default in UTF-8 even though ANSI gives the same results? Is there a specific community secret tool I am supposed to use for zipping or specific settings? I am pulling my hair in distress. Unfortunately as there are no errors involved I lack logs to offer, if that disqualifies this thread please do not be harsh and instead if you can then please direct me to a forum with different rules and sufficiently respectful but knowledgeable as to be of assistance. Most of my experience with the programming community has been poor and so naturally I find myself wary.
  • Topics

  • Who's Online (See full list)

    • There are no registered users currently online
×
×
  • Create New...

Important Information

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