Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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]

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.