Jump to content

[1.12.2] Saving World Data


Lea9ue

Recommended Posts

I'm trying to continue some work from previous topic HERE.  I'm trying to track the amount of times a diamond is destroyed by fire. I have that working great but need to save the data to world.  If I quit after throwing diamonds in fire, the data is saved fine.  When I reload the save the data is loaded fine. If I join another save where diamonds have been destroyed by fire already, that data remains fine in that save too. The problem is if I create a new world or load a save that has never seen a diamond destroyed by fire, the data from the last save where diamonds were destroyed by fire is loaded.  I hope I'm explaining that in a way everyone can understand.  Basically I need to see that 0 diamonds were destroyed when I enter a save where 0 diamonds were destroyed.  Any help as always is appreciated.

 Here is my class that extends WorldSavedData:

public class ModSaveData extends WorldSavedData{
	
	private static final String DATA_NAME = Reference.MOD_ID;
	private static ModSaveData instance;
	
	public static int diamondSacrifice;
	
    public ModSaveData() {
        super(DATA_NAME);
    }
	
    public ModSaveData(String name) {
        super(name);
    }
    
    public static ModSaveData get(World world) {
        MapStorage storage = world.getMapStorage();
        instance = (ModSaveData) storage.getOrLoadData(ModSaveData.class, DATA_NAME);

        if (instance == null) {
            instance = new ModSaveData();
            storage.setData(DATA_NAME, instance);
        }
        return instance;
    }

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

    @Override
    public NBTTagCompound writeToNBT(NBTTagCompound compound) {
        compound.setInteger("diamondSacrifice", diamondSacrifice);
        return compound;
    }

    public void setDiamondSacrificeData(int diamondSacrifice) {
        this.diamondSacrifice = diamondSacrifice;
        markDirty();
    }
     
    public int getDiamondSacrifice() {
        return diamondSacrifice;
    }
}

 

This is my Handler:

public class ServerLoader {
	
	
	@SubscribeEvent(priority = EventPriority.NORMAL)
	public static void playerConnected(PlayerEvent.PlayerLoggedInEvent event) {
		ModSaveData data = ModSaveData.get(event.player.world);
		if(data != null) {

			System.out.println("SACRIFICES " + data.getDiamondSacrifice());
		}
	}

	@SubscribeEvent(priority = EventPriority.NORMAL)
	public static void playerDisconnect(PlayerEvent.PlayerLoggedOutEvent event) {
		ModSaveData data = ModSaveData.get(event.player.world);
		if(data != null) {

			data.setDiamondSacrificeData(data.getDiamondSacrifice());
			System.out.println("SACRIFICES " + data.getDiamondSacrifice());
		}
	}
}

 

My Event for tracking Diamonds destroyed by fire:

public static void onItemDeath(EntityItemDeathEvent event){
		World world = event.getEntity().world;
		if (isDiamond(event.getEntityItem()) && event.damageSource.isFireDamage()){
			ModSaveData.get(world).setDiamondSacrificeData(ModSaveData.diamondSacrifice + 1);
			ModSaveData.get(world).isDirty();
			System.out.println(ModSaveData.diamondSacrifice);
		}
	}

 

Edited by Lea9ue
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.

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.



×
×
  • Create New...

Important Information

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