Jump to content

How Do I Use MyWorldSavedData Outside of the Class?


Recommended Posts




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.




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() {

    public MyWorldSavedData(String s) {

    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);

    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;



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;

public class MyEventHandler {

    public int amount_of_full_moons;

    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) {

                // How do I properly use MyWorldSavedData to save 'amount_of_full_moons' here?


Link to comment
Share on other sites



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.

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.

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.


  • Create New...

Important Information

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