Jump to content
  • Home
  • Files
  • Docs
Topics
  • All Content

  • This Topic
  • This Forum

  • Advanced Search
  • Existing user? Sign In  

    Sign In



    • Not recommended on shared computers


    • Forgot your password?

  • Sign Up
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • [1.16] Attach Capability to Chunks?
Currently Supported: 1.16.X (Latest) and 1.15.X (LTS)
Sign in to follow this  
Followers 0
kiou.23

[1.16] Attach Capability to Chunks?

By kiou.23, December 1, 2020 in Modder Support

  • Reply to this topic
  • Start new topic

Recommended Posts

kiou.23    6

kiou.23

kiou.23    6

  • Creeper Killer
  • kiou.23
  • Members
  • 6
  • 161 posts
Posted December 1, 2020

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?

  • Quote

Share this post


Link to post
Share on other sites

Draco18s    2406

Draco18s

Draco18s    2406

  • Reality Controller
  • Draco18s
  • Members
  • 2406
  • 15937 posts
Posted December 1, 2020 (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 December 1, 2020 by Draco18s
  • Quote

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.

Share this post


Link to post
Share on other sites

kiou.23    6

kiou.23

kiou.23    6

  • Creeper Killer
  • kiou.23
  • Members
  • 6
  • 161 posts
Posted December 1, 2020
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

  • Quote

Share this post


Link to post
Share on other sites

Draco18s    2406

Draco18s

Draco18s    2406

  • Reality Controller
  • Draco18s
  • Members
  • 2406
  • 15937 posts
Posted December 1, 2020

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.

  • Quote

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.

Share this post


Link to post
Share on other sites

kiou.23    6

kiou.23

kiou.23    6

  • Creeper Killer
  • kiou.23
  • Members
  • 6
  • 161 posts
Posted December 1, 2020
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?

  • Quote

Share this post


Link to post
Share on other sites

Draco18s    2406

Draco18s

Draco18s    2406

  • Reality Controller
  • Draco18s
  • Members
  • 2406
  • 15937 posts
Posted December 1, 2020
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
  • Quote

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.

Share this post


Link to post
Share on other sites

kiou.23    6

kiou.23

kiou.23    6

  • Creeper Killer
  • kiou.23
  • Members
  • 6
  • 161 posts
Posted December 1, 2020 (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 December 1, 2020 by kiou.23
  • Quote

Share this post


Link to post
Share on other sites

Draco18s    2406

Draco18s

Draco18s    2406

  • Reality Controller
  • Draco18s
  • Members
  • 2406
  • 15937 posts
Posted December 1, 2020

Because of how Capabilities work.

  • Quote

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.

Share this post


Link to post
Share on other sites

vemerion    56

vemerion

vemerion    56

  • Creeper Killer
  • vemerion
  • Members
  • 56
  • 220 posts
Posted December 1, 2020
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
  • Quote

Share this post


Link to post
Share on other sites

kiou.23    6

kiou.23

kiou.23    6

  • Creeper Killer
  • kiou.23
  • Members
  • 6
  • 161 posts
Posted December 1, 2020
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?

  • Quote

Share this post


Link to post
Share on other sites

diesieben07    7610

diesieben07

diesieben07    7610

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7610
  • 55162 posts
Posted December 1, 2020
2 minutes ago, kiou.23 said:

who do I trust?

vemerion is right

  • Like 1
  • Quote

Share this post


Link to post
Share on other sites

desht    91

desht

desht    91

  • Creeper Killer
  • desht
  • Members
  • 91
  • 244 posts
Posted December 2, 2020

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
  • Quote

Share this post


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.   Paste as plain text instead

  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.

    • Insert image from URL
×
  • Desktop
  • Tablet
  • Phone
Sign in to follow this  
Followers 0
Go To Topic Listing



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • S-Spirit
      [1.16.4] Why recipe result calculated on server side?

      By S-Spirit · Posted 25 minutes ago

      I want to propose to user few options, which he/she may craft from items in inventory. Client will prepare all combinations, than select only few best (in my context). Server will validate only this few combinations. Ofcouse I plans to add precheck how many combination client may construct and prevent few-seconds freezes with message like "Sorry, so many ingredients for autocompletion."
    • P0SCH1T0
      Why my modded server keep saying: "Unexpected custom data from client"?

      By P0SCH1T0 · Posted 45 minutes ago

      Sorry I posted this comment, because I was inattentive.
    • diesieben07
      Best way to create a new dye.

      By diesieben07 · Posted 1 hour ago

      Dyes and colored objects are not handled through runtime coloring, they simply have different textures, there for ColorHandlerEvent / IItemColor / IBlockColor is not the way to go. It gets problematic, because Minecraft models dye color as an enum, which is of course inflexible and does not let you add things to it.   So really you just need to add new items, blocks, etc. as normal for all the colored "things" and ignore DyeColor. For things like sheep shearing, you probably either need to hook into the sheep rendering and have your own rendering for your own color (in addition to storing the fact that the sheep is "your color") or just spawn a custom sheep. Same goes for banners, you probably have to clone the complete logic, which is not really fun.   Overall: The basics are possible, but integrating it into the whole game is probably not possible / not easy.
    • diesieben07
      Datapack error and safemode

      By diesieben07 · Posted 2 hours ago

      Yes it is the error. I don't use a Mac, so I don't know how to fix this.
    • diesieben07
      Forge Server

      By diesieben07 · Posted 2 hours ago

      It should work fine, yes. However nobody on the core forge team has a mac, so no mac specific testing is done, but I have not heard any bug reports.
  • Topics

    • S-Spirit
      6
      [1.16.4] Why recipe result calculated on server side?

      By S-Spirit
      Started Friday at 09:43 PM

    • P0SCH1T0
      3
      Why my modded server keep saying: "Unexpected custom data from client"?

      By P0SCH1T0
      Started Yesterday at 10:12 AM

    • Eridani
      1
      Best way to create a new dye.

      By Eridani
      Started 10 hours ago

    • Intijir
      20
      Datapack error and safemode

      By Intijir
      Started December 20, 2020

    • Arthurmeade12
      1
      Forge Server

      By Arthurmeade12
      Started 13 hours ago

  • Who's Online (See full list)

    • loordgek
    • AzizD
    • Pavel1ewq
    • lovemalak8787@gmail.com
    • Choonster
    • Pl00py_R
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • [1.16] Attach Capability to Chunks?
  • Theme

Copyright © 2019 ForgeDevelopment LLC · Ads by Longitude Ads LLC Powered by Invision Community