Posted October 21, 20232 yr 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 October 21, 20232 yr by chrisicrafter27
October 25, 20232 yr 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.