Posted December 1, 20204 yr 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?
December 1, 20204 yr 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 December 1, 20204 yr 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.
December 1, 20204 yr Author 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
December 1, 20204 yr https://github.com/Draco18s/ReasonableRealism/blob/1.12.1/src/main/java/com/draco18s/farming/FarmingBase.java#L86-L87 Pretty sure this hasn't changed. 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.
December 1, 20204 yr Author 13 minutes ago, Draco18s said: https://github.com/Draco18s/ReasonableRealism/blob/1.12.1/src/main/java/com/draco18s/farming/FarmingBase.java#L86-L87 Pretty sure this hasn't changed. 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?
December 1, 20204 yr 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 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.
December 1, 20204 yr Author 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 December 1, 20204 yr by kiou.23
December 1, 20204 yr 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.
December 1, 20204 yr 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.
December 1, 20204 yr Author 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?
December 2, 20204 yr 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.
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.