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!

 

Title.

The Forge Documentation page doesn't show examples of how to save/load data from other classes (like MyEventHandler), it kind of explains which methods to use, but I'm still unsure how to do it properly and I want to do it properly.

 

Both the MyWorldSavedData#write/readNBTTagCompounds() are run, and MyEventHandler#countFullMoons() is run properly.

 

MyWorldSavedData:

Spoiler

package com.ani.s;

import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import net.minecraft.world.storage.MapStorage;
import net.minecraft.world.storage.WorldSavedData;

public class MyWorldSavedData extends WorldSavedData {

    static final String DATA_NAME = MyVariousVariables.MODID + "_MyWorldSavedData";

    // Do I need to create a new variable in this class for every variable I want to save?
    private int amount_of_full_moons;

    public MyWorldSavedData() {
        super(DATA_NAME);
    }

    public MyWorldSavedData(String s) {
        super(s);
    }

    @Override
    public void readFromNBT(NBTTagCompound nbt) {
        amount_of_full_moons = nbt.getInteger("amount_of_full_moons");

        System.out.println("READ FROM NBT " + amount_of_full_moons);
    }

    @Override
    public NBTTagCompound writeToNBT(NBTTagCompound nbt) {
        nbt.setInteger("amount_of_full_moons", amount_of_full_moons);

        System.out.println("WRITE TO NBT " + amount_of_full_moons);
        return nbt;
    }

    public static MyWorldSavedData get(World world) {
        MapStorage storage = world.getPerWorldStorage();
        MyWorldSavedData instance = (MyWorldSavedData) storage.getOrLoadData(MyWorldSavedData.class, DATA_NAME);
        if (instance == null) {
            instance = new MyWorldSavedData();
            storage.setData(DATA_NAME, instance);
        }
        return instance;
    }
}

MyEventHandler:

Spoiler

package com.ani.s;

import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent.WorldTickEvent;

@Mod.EventBusSubscriber
public class MyEventHandler {

    public int amount_of_full_moons;

    @SubscribeEvent
    public void countFullMoons(WorldTickEvent event) {
        if (event.phase == event.phase.START && event.world.provider.isSurfaceWorld()) {
            // How do I properly use MyWorldSavedData to load 'amount_of_full_moons' here?

            if (event.world.getWorldTime() == 13000 && event.world.getCurrentMoonPhaseFactor() >= 1F) {
                ++amount_of_full_moons;

                // How do I properly use MyWorldSavedData to save 'amount_of_full_moons' here?
                MyWorldSavedData.get(event.world).markDirty();
            }
        }
    }
}

 

MyWorldSavedData.get(any_world_ref).amount_of_full_moons?

 

You'd have to make it a public variable (or add a getter) though.

Edited by Draco18s

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.

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.