Jump to content

Recommended Posts

Posted (edited)

Hello, I want to save an ArrayList of BlockPos for each world. I've seen this forge documentation but I don't really understand it. Additionally I think something has changed with 1.20.2. For example the computeIfAbsent method now takes different arguments such as a SavedData.Factory. Could someone please help me?

EDIT

After a bit of try-and-error I got it. Here is the code if anyone is interested in it:

package de.chrisicrafter.loadit.utils;

import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtUtils;
import net.minecraft.util.datafix.DataFixTypes;
import net.minecraft.world.level.saveddata.SavedData;

import java.util.ArrayList;

public class BeaconData extends SavedData {
    public final ArrayList<BlockPos> beaconPositions;

    public static SavedData.Factory<BeaconData> factory() {
        return new SavedData.Factory<>(BeaconData::new, BeaconData::load, DataFixTypes.LEVEL);
    }

    public BeaconData() {
        beaconPositions = new ArrayList<>();
    }

    public BeaconData(ArrayList<BlockPos> list) {
        beaconPositions = list;
    }

    @Override
    public CompoundTag save(CompoundTag tag) {
        //save to tag
        int size = beaconPositions.size();
        tag.putInt("size", size);
        for(int i = 0; i < size; i++) {
            tag.put("beacon_pos_" + i, NbtUtils.writeBlockPos(beaconPositions.get(i)));
        }
        return tag;
    }

    public static BeaconData load(CompoundTag tag) {
        ArrayList<BlockPos> list = new ArrayList<>();
        //load from tag
        int size = tag.getInt("size");
        for(int i = 0; i < size; i++) {
            list.add(NbtUtils.readBlockPos((CompoundTag) tag.get("beacon_pos_" + i)));
        }
        return new BeaconData(list);
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        int size = beaconPositions.size();
        for(int i = 0; i < size; i++) {
            builder.append("beacon_pos_");
            builder.append(i);
            builder.append(" ");
            builder.append(beaconPositions.get(i).toString());
            builder.append(System.getProperty("line.separator"));
        }
        return builder.toString();
    }
}

 

Edited by chrisicrafter27
  • chrisicrafter27 changed the title to Saving Data per World [1.20.2] [SOLVED]
Posted

1. Pass the ServerLevel parameter into factory().

2. Call the getDataStorage() method within ServerLevel.

3. Call the computeIfAbsent() method within the ServerLevel::getDataStorage() method.

4. Pass the new SavedData.Factory<BeaconData>(BeaconData::new, BeaconData::load, DataFixTypes.LEVEL), yourmodID parameters into your computeIfAbsent() method.

5. Return it out as the factory() method.

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.