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

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

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.