Jump to content

Recommended Posts

Posted (edited)

I am trying to save information with the world. I have followed the docs to the best of my ability, along with reading similar posts on this forum, but I am still unable to get this to work. Currently, I have the Data class below which extends from WorldSavedData. It stores the data in one NBTTagCompound.

Data Class

public class ACSave extends WorldSavedData{
	private static final String IDENTIFIER = "AC_DATA";
	private NBTTagCompound data = new NBTTagCompound();
	
	public ACSave(String identifier) {
		super(identifier);
	}
	
	public ACSave() {
		super(IDENTIFIER);
	}
	
	@Override
	public void readFromNBT(NBTTagCompound nbt) {
		data = nbt;
	}
	
	@Override
	public NBTTagCompound writeToNBT(NBTTagCompound nbt) {
		nbt = data;
		return nbt;
	}
	
	public static ACSave get(World world) {
		ACSave save = (ACSave) world.getMapStorage().getOrLoadData(ACSave.class, IDENTIFIER);
		if(save == null) {
			Minecraft.getMinecraft().player.sendChatMessage("Creating a new World Configuration File");
			save = new ACSave();
			world.getMapStorage().setData(IDENTIFIER, save);
		}else {
			Minecraft.getMinecraft().player.sendChatMessage("Using a pre-existing Configuration File!");
		}
		return save;
	}
}

The follow event handler class calls upon this class. When the player loads into the world it creates a new ACSave object and calls the get() method. Whenever the player right clicks it increases the integer "Yeet" by one, displaying the new value in chat. It then marks the ACSave object as dirty, so it should be saving but it never does. Whether I wait or stop the world the data file is never created/saved. When I relaunch the world the data is all lost. On another NBT data related post, the user showed that the console ouptuted a line saying how the class was marked dirty when he exited the world, but that has never happened for me. When the world stops nothing special happens at all.

public class ACEventHandler {
	
	ACSave save;
	NBTTagCompound nbt = new NBTTagCompound();
	
	@SubscribeEvent
    public void RightClickBlock(PlayerInteractEvent.RightClickItem event) {
    	if(event.getEntity() == Minecraft.getMinecraft().player) {
    		nbt.setInteger("Yeets", nbt.getInteger("Yeets") + 1);
    		save.readFromNBT(nbt);
   			save.markDirty();
    		Minecraft.getMinecraft().player.sendChatMessage("Yeet Count: " + nbt.getInteger("Yeets"));
    	}
    }

	@SubscribeEvent
	public void EntityJoinWorldEvent(EntityJoinWorldEvent event) {
		if(event.getEntity() == Minecraft.getMinecraft().player) {
			save = ACSave.get(Minecraft.getMinecraft().world);
			nbt = save.writeToNBT(nbt);
		}
	}
}

 

I know there have been a lot of NTB related questions, and I am sorry if this is another stupid question.

 

Again, to reiterate the data is not being saved, even though I have marked it as dirty. The data file is not being created at any point, and every time I start a world the save is null so it creates a new one.

Edited by iPanja
Posted
26 minutes ago, iPanja said:

public void RightClickBlock(PlayerInteractEvent.RightClickItem event) {
    if(event.getEntity() == Minecraft.getMinecraft().player) {

 

When would this ever be false?

27 minutes ago, iPanja said:

save.readFromNBT(nbt);

You're doing this one the client, what?

28 minutes ago, iPanja said:

NBTTagCompound nbt = new NBTTagCompound();

Why are you storing your runtime information in NBT? NBT is only for writing to disk.

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.

Posted

 

14 minutes ago, Draco18s said:

When would this ever be false?

 

Originally, this code/event was triggered twice, presumably by the server and the client. After adding this statement, it only ran once when the player right clicked.

16 minutes ago, Draco18s said:

Why are you storing your runtime information in NBT? NBT is only for writing to disk.

I'm storing this variable purely for testing purposes. When I can get this to work, I will just be storing variables that have been generated along with the world that will be loaded at runtime, but will not be manipulated at runtime.

18 minutes ago, Draco18s said:

You're doing this one the client, what?

My understanding of that line is to load the new values to the NBT in the save file, which I then mark dirty on the next line hoping it will save to the disk.

 

Thank you for your response, I hope I cleared some of the things up. I'm not new to Java, not very advanced though either. A lot of my problems come from my understanding of forge, and how I should be using it instead of how I think it is supposed to work.

Posted
5 minutes ago, iPanja said:

My understanding of that line is to load the new values to the NBT in the save file, which I then mark dirty on the next line hoping it will save to the disk.

The client never ever writes anything to disk.

 

5 minutes ago, iPanja said:

Originally, this code/event was triggered twice, presumably by the server and the client. After adding this statement, it only ran once when the player right clicked.

Your code will cause the server to crash, if your code is in fact running on the server. You need to perform this check a different way.

And I'm pretty sure you want your code to run server side.

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.

Posted

Also, you probably want all your methods to be static unless you are registering your EventHandler (in preInit)

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Posted

Ok thank you Cadiboo and Draco18s. My main problem was that the client was attempting to save the information, and I have now changed that to the server side. Thanks again. I'll make it static as well.

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.

Announcements



×
×
  • Create New...

Important Information

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