Jump to content

Saving Data With The World (NBT)


iPanja
 Share

Recommended Posts

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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

 

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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)

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
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.

 Share



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • The mods you are using use Mixin. You are using Java 19. Java 19 breaks Mixin somehow. You must downgrade to Java 17.
    • Can you send the crash report? Crash reports are usually very helpful and most of the time say exactly what mod caused the problem. Also, you should note that throwing 226 mods together all at once is a very bad idea because if you get a crash, it is VERY hard to find which of the 226 mods caused the problem. Also shoving mode mods into it won't fix the problem, if anything it will make it worse.
    • Tenía el mismo error y lo solucioné actualizando Java, quizá te funcione.
    • Okay, so I had this modpack that I made (I have no idea what I'm doing with it, I just thought: hey, i like these (x, y, and z mods) mods, and I haven't found one already made that I like or has current mod updates, so I shoved a bunch of mods I like, and stabilizing/QoL mods to hopefully help make it work. The current count is 226 mods in one pack, and I am getting an error code 1. I had just added some other mods to hopefully stabilize it better, when this happened and now I am immediately kicked to a crash report. I frankly have no idea what went wrong, or how to fix it, so I am posting here to ask for help.       Mod List: [1.18.2] BobLib-0.3.0.jar   3dskinlayers-forge-1.5.2-mc1.18.2.jar   AI-Improvements-1.18.2-0.5.2.jar   angelring-1.18.2-2.0.2.jar   Apotheosis-1.18.2-5.7.7.jar   archarsenal-1.18.2-1.1.jar   archers_paradox-1.18.2-3.2.0.12.jar   architectury-4.11.89-forge.jar   ars_creo-1.18.2-2.1.4.jar   ars_elemental-1.18.2-0.4.9.9.jar   ars_industria-1.18.2-0.1.jar   ars_nouveau-1.18.2-2.8.0.jar   ars_scalaes-1.18.2-1.8.1-rework.jar   artifacts-1.18.2-4.2.0.jar   AttributeFix-Forge-1.18.2-14.0.2.jar   AutoRegLib-1.7-53.jar   Avaritia-1.18.2-4.0.0.4-universal.jar   badpackets-forge-0.1.3.jar   balm-3.2.2+0.jar   BasicEndOres-1.18.2-2.1.0.jar   baubley-heart-canisters-1.18.2-1.0.0.jar   bdlib-1.19.3.7-mc1.18.2.jar   BedBenefits-Forge-1.18.2-6.0.2.jar   BetterAdvancements-1.18.2-0.2.0.146.jar   BetterCompatibilityChecker-1.1.21-build.48+mc1.18.2.jar   betterinvisibility-1.18.1-1.0.4.jar   betterstats-2.2+1.18.2.jar   BetterThirdPerson-Forge-1.18.2-1.9.0.jar   bfendcities-1.0.jar   Bookshelf-Forge-1.18.2-13.2.52.jar   BotanyPots-Forge-1.18.2-8.1.25.jar   BotanyTrees-Forge-1.18.2-4.0.6.jar   bygonenether-1.2.2-1.18.2.jar   caelus-forge-1.18.1-3.0.0.2.jar   caitsmod-0.1.0-1.18.2.jar   catalogue-1.6.2-1.18.2.jar   citadel-1.11.3-1.18.2.jar   clienttweaks-forge-1.18.1-7.1.0.jar   cloth-config-6.4.90-forge.jar   Clumps-forge-1.18.2-8.0.0+17.jar   CodeChickenLib-1.18.2-4.1.3.480-universal.jar   cofh_core-1.18.2-9.2.1.39.jar   collective-1.18.2-6.43.jar   colytra-forge-1.18.1-5.2.0.4.jar   comforts-forge-1.18.2-5.0.0.5.jar   CommonCapabilities-1.18.2-2.8.2.jar   Controlling-forge-1.18.2-9.0+22.jar   convenientcurioscontainer-1.11-1.18.2.jar   cookiecore-1.18.2-4.0.1-6677a05.jar   CosmeticArmorReworked-1.18.2-v2a.jar   craftingtweaks-forge-1.18.2-14.0.3.jar   create-1.18.2-0.5.0.i.jar   CreativeCore_FORGE_v2.6.15_mc1.18.2.jar   CTM-1.18.2-1.1.5+5.jar   Cucumber-1.18.2-5.1.3.jar   curioofundying-forge-1.18-5.3.0.0.jar   curios-forge-1.18.2-5.0.7.1.jar   curious-armor-stands-1.18.1-4.0.0.jar   curiouselytra-forge-1.18.1-5.0.1.0.jar   curiouslanterns-1.18.2-1.2.0.jar   curiouslights-forge-1.3.0+1.18.2.jar   curiousshulkerboxes-forge-1.18.1-4.2.0.0.jar   CyclopsCore-1.18.2-1.16.2.jar   DarkerDepths-1.18.2-1.0.6-patch4.jar   darkness-forge-mc118-2.0.91.jar   DarkUtilities-Forge-1.18.2-10.1.6.jar   debugify-1.10.0-forge-1.18.2.jar   defaultoptions-forge-1.18.2-14.1.1.jar   Ding-1.18.2-Forge-1.4.0.jar   druidcraftrg-0.0.12.jar   DungeonCrawl-1.18.2-2.3.10.jar   DungeonsArise-1.18.2-2.1.52-release.jar   dynamiclights-1.18.6.jar   dynview-1.18.2-2.8.jar   EasyMagic-v3.3.0-1.18.2-Forge.jar   eccentrictome-1.18.2-1.9.1.jar   elenaidodge2-1.0.2.jar   elevatorid-1.18.2-1.8.4.jar   End Mod 2.1.1.jar   EnderBags-1.18.1-8.0.0.jar   enderchests-1.18-1.9.7.jar   endercrop-1.18.2-1.7.0-beta.jar   EnderStorage-1.18.2-2.9.0.182-universal.jar   endertanks-1.18-1.11.8.jar   endportalrecipe-1.18.2-5.0.jar   End's Phantasm - BETAv1.2.9.2 - 1.18.2.jar   Enhanced_Celestials-forge-1.18.2-2.0.7.jar   EnigmaticLegacy-2.25.0.jar   enlightened-end-1.18.2-v28.jar   Entity_Collision_FPS_Fix-forge-1.18.2-1.0.0.jar   entityculling-forge-mc1.18-1.5.1.jar   EquipmentCompare-1.18.2-forge-1.3.3.jar   everycomp-1.18.2-1.5.15.jar   expandability-6.0.0.jar   ExplorersCompass-1.18.2-1.3.0-forge.jar   Fairy's End v24[UnholyCursed].jar   FallingTree-1.18.2-3.5.4.jar   fancymenu_forge_2.13.3_MC_1.18.2.jar   FarmersDelight-1.18.2-1.2.0.jar   FastFurnace-1.18.2-6.0.3.jar   FastWorkbench-1.18.2-6.1.1.jar   feathers-1.0.3.jar   feature_nbt_deadlock_be_gone_forge-2.0.0+1.18.2.jar   ferritecore-4.2.2-forge.jar   findme-3.0.6-forge.jar   flywheel-forge-1.18.2-0.6.8.a.jar   forbidden_arcanus-1.18.2-2.1.1.jar   forgivingvoid-forge-1.18.1-6.0.1.jar   FpsReducer2-forge-1.18.2-2.0.jar   ftb-chunks-forge-1802.3.13-build.234.jar   ftb-library-forge-1802.3.9-build.167.jar   ftb-teams-forge-1802.2.10-build.96.jar   geckolib-forge-1.18-3.0.57.jar   goblintraders-1.8.0-1.18.2.jar   graphutil-forge-1.0.0-mc1.18.2.jar   healingcampfire-1.18.2-5.0.jar   hexerei-0.2.2.jar   homing_ender_eye-1.18.2-1.2.3.jar   Iceberg-1.18.2-forge-1.0.49.jar   inventoryessentials-forge-1.18.2-4.0.2.jar   inventorysorter-1.18-19.0.0.jar   ironchest-1.18.2-13.2.11.jar   item-filters-forge-1802.2.8-build.47.jar   ItShallNotTick-1.0.22-build.34.jar   Jade-1.18.2-forge-5.2.6.jar   jei-1.18.2-9.7.1.255.jar   jeiintegration_1.18.2-9.0.0.37.jar   JEITweaker-1.18.2-3.0.0.9.jar   JustEnoughResources-1.18.2-0.14.1.171.jar   knightcore-1.18.2-1.1.jar   konkrete_forge_1.6.0_MC_1.18-1.18.2.jar   kotlinforforge-3.9.1-all.jar   L_Enders Cataclysm-0.51-hotfix-1.18.2.jar   letmedespawn-1.18.x-1.19.x-forge-1.0.3.jar   libIPN-forge-1.18.2-2.0.2.jar   LibX-1.18.2-3.2.18.jar   Magic Shards.jar   magicfeather-1.18.2-4.2.0.jar   mahoutsukai-1.18.2-v1.34.38.jar   Mantle-1.18.2-1.9.43.jar   MaxHealthFix-Forge-1.18.2-5.0.1.jar   mcjtylib-1.18-6.0.20.jar   mna-1.7.2.10.jar   modnametooltip-1.18.1-1.18.0.jar   moredragoneggs-3.1.jar   MoreSlotSwap-v1.0.1-mc1.18.2-forge.jar   MouseTweaks-forge-mc1.18-2.21.jar   MyServerIsCompatible-1.18-1.0.jar   Nameless Trinkets-1.18.2-1.6.10.jar   naturalist-forge-1.1.1-1.18.2.jar   NaturesCompass-1.18.2-1.9.7-forge.jar   Neat-1.8-31.jar   netherportalfix-forge-1.18-9.0.0.jar   observable-2.2.3-forge.jar   OpenLoader-Forge-1.18.2-12.0.1.jar   overloadedarmorbar-1.18.2-6.jar   Patchouli-1.18.2-71.1.jar   PigPen-Forge-1.18.2-8.0.1.jar   Placebo-1.18.2-6.6.6.jar   polylib-forge-1801.0.2-build.13.jar   polymorph-forge-1.18.2-0.46.jar   potionsmaster-0.5.9-1.18.2-40.0.2.jar   Prism-1.18.2-1.0.1.jar   PuzzlesLib-v3.3.5-1.18.2-Forge.jar   ReAuth-1.18-Forge-4.0.6.jar   refinedstorage-1.10.4.jar   REIPluginCompatibilities-forge-8.0.42.jar   relics-1.18.2-0.4.1.8.jar   reliquary-1.18.2-2.0.19.1161.jar   repurposed_structures_forge-5.1.14+1.18.2.jar   rhino-forge-1802.2.1-build.255.jar   RingOfEnderchest-Forge-1.18.2-1.4.8.jar   RingsOfAscension-1.18.2-1.0.1.jar   RoughlyEnoughItems-8.3.588.jar   ScalableCatsForce-2.13.10-build-12-with-library.jar   ScalingHealth-1.18.2-6.3.0+5.jar   selene-1.18.2-1.17.9.jar   shetiphiancore-1.18-3.10.12.jar   shutupexperimentalsettings-1.0.5.jar   silent-lib-1.18.2-6.2.0.jar   simplearchivist-0.5-1.18.2.jar   simpleheartsbeta.jar   smoothboot(reloaded)-mc1.18.2-0.0.2.jar   SmoothMenu-1.18.2-1.0.0.jar   sophisticatedbackpacks-1.18.2-3.18.40.777.jar   sophisticatedcore-1.18.2-0.5.37.202.jar   sophisticatedstorage-1.18.2-0.6.16.274.jar   spark-1.9.11-forge.jar   starterkit-1.18.2-5.0.jar   structure_gel-1.18.2-2.4.7.jar   supermartijn642configlib-1.1.6-forge-mc1.18.jar   supermartijn642corelib-1.1.1a-forge-mc1.18.jar   supplementaries-1.18.2-1.5.16.jar   TConstruct-1.18.2-3.6.2.92.jar   Tips-Forge-1.18.2-5.0.11.jar   TipTheScales-forge-1.18.2-5.0.6.jar   titanium-1.18.2-3.5.7-39.jar   ToastControl-1.18.2-6.0.3.jar   ToolStats-Forge-1.18.2-9.0.3.jar   torchmaster-18.1.0.jar   trashslot1.18.2.jar   UnusualEnd1.18_V1.3.0.jar   upgradedcore-1.18.2-3.2.0.0-release.jar   upgradednetherite-1.18.2-4.2.0.6-release.jar   valhelsia_core-forge-1.18.2-0.4.0.jar   valhelsia_structures-forge-1.18.2-0.1.0.jar   valhelsia_tweaks-forge-1.18.2-0.1.0.jar   valkyrienskies-118-2.1.0-beta.10.jar   villagertools-1.18-1.0.2.jar   VisualWorkbench-v3.3.0-1.18.2-Forge.jar   voidtotem-forge-1.18.2-1.3.1.jar   waystone_towers-1.18.2-FORGE-1.0.9.jar   waystones-forge-1.18.2-10.1.0.jar   whats-that-slot-forge-1.2.3+1.18.1.jar   WitherSkeletonTweaks-1.18.2-7.1.3.jar   wmitaf-forge-mc1.18.2-2.4.0.jar   wyml-forge-1.18.2-1.1.4.jar   Xaeros_Minimap_22.17.1_Forge_1.18.2.jar   XaerosWorldMap_1.28.8_Forge_1.18.2.jar   YungsApi-1.18.2-Forge-2.2.7.jar   YungsBetterDesertTemples-1.18.2-Forge-1.3.1.jar   YungsBetterDungeons-1.18.2-Forge-2.1.0.jar   YungsBetterMineshafts-1.18.2-Forge-2.2.jar   YungsBetterOceanMonuments-1.18.2-Forge-1.0.3.jar   YungsBetterStrongholds-1.18.2-Forge-2.1.1.jar   YungsExtras-1.18.2-Forge-2.1.0.jar
    • Hi, I'm trying to make a modded Forge server so me and my friends can play together, but when I add the mods to the mods folder, I get an error. If I have the server by itself, no mods, everything runs fine. I don't know what is happening so if someone could please help that would be great. I left the crash report below.  
  • Topics

×
×
  • Create New...

Important Information

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