Jump to content

[1.14.4] arraylist capability


andGarrett

Recommended Posts

I've got and arraylist of objects. the objects contain a string and a double. the purpose of this list is to contain monetary value for every item in the game. it's basically the Energy-Matter Currency (EMC) from "Equivalent Exchange." I started writing a capability to store the array. I'm not sure how to write the storage class for this capability. specifically the read and write methods. I tried returning a compoundNBT in the read method, but I don't yet understand compoundNBT, and I'm not even sure that's what I should be returning.

 

package com.Garrett.backtobasics.capabilities.itemValues;

import com.Garrett.backtobasics.capabilities.blockPopulation.IBlockPopulation;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.INBT;
import net.minecraft.nbt.IntNBT;
import net.minecraft.util.Direction;
import net.minecraftforge.common.capabilities.Capability;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.annotation.Nullable;

public class ItemValuesStorage implements Capability.IStorage<ItemValues> {

    @Nullable
    @Override
    public CompoundNBT writeNBT(Capability<ItemValues> capability, ItemValues instance, Direction side) {
        return new CompoundNBT(instance.getValueList());
    }

    @Override
    public void readNBT(Capability<ItemValues> capability, ItemValues instance, Direction side, INBT nbt) {
    }
}

 

Link to comment
Share on other sites

19 minutes ago, andGarrett said:

and I'm not even sure that's what I should be returning.

If you're just saving an array list that stores two values you should store a ListNBT or a TagListNBT. I'm not sure what it was changed to recently. But that tag list needs to store a CompoundNBT in which you will call CompoundNBT#putDouble(ID, double) and CompoundNBT#putString(ID, string)

 

  • Thanks 1

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

13 hours ago, Animefan8888 said:

If you're just saving an array list that stores two values you should store a ListNBT or a TagListNBT. I'm not sure what it was changed to recently. But that tag list needs to store a CompoundNBT in which you will call CompoundNBT#putDouble(ID, double) and CompoundNBT#putString(ID, string)

 

for now what I did is pointless, as I don't need to change any data in the capability yet, but I was able to learn how to work with ListNBT and CompoundNBT. what if I never need to change any data? how might I code the write method in such a case?

 

 

here's what I did:

    @Nullable
    @Override
    public ListNBT writeNBT(Capability<ItemValues> capability, ItemValues instance, Direction side) {
        ListNBT itemValueListNBT = new ListNBT();
        int index = 0;
        for (TradeItem tradeItem:instance.getItemValueList()) {
            CompoundNBT compoundNBT = new CompoundNBT();
            compoundNBT.putString("item", tradeItem.getItemRegistryName());
            compoundNBT.putDouble("value", tradeItem.getMonetaryValue());
            itemValueListNBT.add(index, compoundNBT);
            index++;
        }
        return itemValueListNBT;
    }

 

Edited by andGarrett
Link to comment
Share on other sites

15 minutes ago, andGarrett said:

for now what I did is pointless, as I don't to change any data in the capability yet, but I was able to learn how to work with ListNBT and CompoundNBT. what if I never need to change any data? how might I code the write method in such a case?

 

 

here's what I did:


    @Nullable
    @Override
    public ListNBT writeNBT(Capability<ItemValues> capability, ItemValues instance, Direction side) {
        ListNBT itemValueListNBT = new ListNBT();
        int index = 0;
        for (TradeItem tradeItem:instance.getItemValueList()) {
            CompoundNBT compoundNBT = new CompoundNBT();
            compoundNBT.putString("item", tradeItem.getItemRegistryName());
            compoundNBT.putDouble("value", tradeItem.getMonetaryValue());
            itemValueListNBT.add(index, compoundNBT);
            index++;
        }
        return itemValueListNBT;
    }

 

you change the data with the methods in your capability class. 

Edited by eatthenight
Link to comment
Share on other sites

1 hour ago, andGarrett said:

what if I never need to change any data?

Then you dont ever need to save it. You could just compute it at startup. And store it in a static variable. And if it is customizable/world dependent sync it from the server to the client.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

3 minutes ago, andGarrett said:

where would I put this static variable, and how would I access it?

You'd put it anywhere. Static variables only exist once.

 

3 minutes ago, andGarrett said:

it would need to compute the list every time a such a block was created.

No it would only compute it where you told it to compute it. I would need more information on what would affect the values to determine where it should be computed.

  • Thanks 1

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

2 hours ago, diesieben07 said:

I don't think you understand how static works.

hmm, I thought I did, but after double checking, there were some key aspects of the static keyword that I had either forgotten or never learnt.  I could just create a static method  essentially anywhere that would return the list. I feel silly for not remembering this is possible in java. Sorry to bring a java issue to a forge form :S

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



×
×
  • Create New...

Important Information

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