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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • that happens every time I enter a new dimension.
    • This is the last line before the crash: [ebwizardry]: Synchronising spell emitters for PixelTraveler But I have no idea what this means
    • What in particular? I barely used that mod this time around, and it's never been a problem in the past.
    • Im trying to build my mod using shade since i use the luaj library however i keep getting this error Reason: Task ':reobfJar' uses this output of task ':shadowJar' without declaring an explicit or implicit dependency. This can lead to incorrect results being produced, depending on what order the tasks are executed. So i try adding reobfJar.dependsOn shadowJar  Could not get unknown property 'reobfJar' for object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler. my gradle file plugins { id 'eclipse' id 'idea' id 'maven-publish' id 'net.minecraftforge.gradle' version '[6.0,6.2)' id 'com.github.johnrengelman.shadow' version '7.1.2' id 'org.spongepowered.mixin' version '0.7.+' } apply plugin: 'net.minecraftforge.gradle' apply plugin: 'org.spongepowered.mixin' apply plugin: 'com.github.johnrengelman.shadow' version = mod_version group = mod_group_id base { archivesName = mod_id } // Mojang ships Java 17 to end users in 1.18+, so your mod should target Java 17. java.toolchain.languageVersion = JavaLanguageVersion.of(17) //jarJar.enable() println "Java: ${System.getProperty 'java.version'}, JVM: ${System.getProperty 'java.vm.version'} (${System.getProperty 'java.vendor'}), Arch: ${System.getProperty 'os.arch'}" minecraft { mappings channel: mapping_channel, version: mapping_version copyIdeResources = true runs { configureEach { workingDirectory project.file('run') property 'forge.logging.markers', 'REGISTRIES' property 'forge.logging.console.level', 'debug' arg "-mixin.config=derp.mixin.json" mods { "${mod_id}" { source sourceSets.main } } } client { // Comma-separated list of namespaces to load gametests from. Empty = all namespaces. property 'forge.enabledGameTestNamespaces', mod_id } server { property 'forge.enabledGameTestNamespaces', mod_id args '--nogui' } gameTestServer { property 'forge.enabledGameTestNamespaces', mod_id } data { workingDirectory project.file('run-data') args '--mod', mod_id, '--all', '--output', file('src/generated/resources/'), '--existing', file('src/main/resources/') } } } sourceSets.main.resources { srcDir 'src/generated/resources' } repositories { flatDir { dirs './libs' } maven { url = "https://jitpack.io" } } configurations { shade implementation.extendsFrom shade } dependencies { minecraft "net.minecraftforge:forge:${minecraft_version}-${forge_version}" implementation 'org.luaj:luaj-jse-3.0.2' implementation fg.deobf("com.github.Virtuoel:Pehkui:${pehkui_version}") annotationProcessor 'org.spongepowered:mixin:0.8.5:processor' minecraftLibrary 'luaj:luaj-jse:3.0.2' shade 'luaj:luaj-jse:3.0.2' } // Example for how to get properties into the manifest for reading at runtime. tasks.named('jar', Jar).configure { manifest { attributes([ 'Specification-Title' : mod_id, 'Specification-Vendor' : mod_authors, 'Specification-Version' : '1', // We are version 1 of ourselves 'Implementation-Title' : project.name, 'Implementation-Version' : project.jar.archiveVersion, 'Implementation-Vendor' : mod_authors, 'Implementation-Timestamp': new Date().format("yyyy-MM-dd'T'HH:mm:ssZ"), "TweakClass" : "org.spongepowered.asm.launch.MixinTweaker", "TweakOrder" : 0, "MixinConfigs" : "derp.mixin.json" ]) } rename 'mixin.refmap.json', 'derp.mixin-refmap.json' } shadowJar { archiveClassifier = '' configurations = [project.configurations.shade] finalizedBy 'reobfShadowJar' } assemble.dependsOn shadowJar reobf { re shadowJar {} } publishing { publications { mavenJava(MavenPublication) { artifact jar } } repositories { maven { url "file://${project.projectDir}/mcmodsrepo" } } } my entire project:https://github.com/kevin051606/DERP-Mod/tree/Derp-1.0-1.20
  • Topics

×
×
  • Create New...

Important Information

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