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.

[1.16.5] Save players inventories (serialization & deserialization)

Featured Replies

Posted

Hello,

I'd like to serialize and deserialize players' inventories in order to have 1 inventory per dimension (Overworld, nether, end, my_custom_dimension).

I currently have the PlayerEvent.PlayerChangedDimensionEvent event set-up and the custom dimension, I don't know how could I serialize & deserialize the inventories.

I've already tried Gson without any success, it kept on crashing.

Thank you in advance for the help :)

    @SubscribeEvent
    public static void onPlayerChangeDimension(PlayerEvent.PlayerChangedDimensionEvent event) {
        PlayerEntity p = event.getPlayer();

        RegistryKey<World> from = event.getFrom();
        RegistryKey<World> to = event.getTo();

        // Save the player's inventory (from)
        // . . .

        // Retrieve the player's inventory (to)
        // . . .

    }

 

  • Author

Could you please send me some examples. I'm having a rough time trying to get it working.

I don't know why but I don't have: PlayerInventory#save(ListNBT)  nor PlayerInventory#load(ListNBT)

But instead: #write(CompoundNBT)  and #read(CompoundNBT)

  • Author

I’m using MCP mappings.

build.gradle file —> channel: snapshot | version: 20201028-1.16.3

  • Author

I'm now facing an issue when serializing and deserializing, I better know how NBT works but when using Gson I can only serialize to a file which looks like this: 

{"mainInventory":[{"tagMap":{"id":{"data":"minecraft:air"},"Count":{"data":1}}},{"tagMap":{"id":{"data":"minecraft:air"},"Count":{"data":1}}},{"tagMap":{"id":{"data":"minecraft:air"},"Count":{"data":1}}},{"tagMap":{"id":{"data":"minecraft:air"},"Count":{"data":1}}},{"tagMap":{"id":{"data":"minecraft:air"},"Count":{"data":1}}},{"tagMap":{"id":{"data":"minecraft:air"},"Count":{"data":1}}},{"tagMap":{"id":{"data":"minecraft:air"},"Count":{"data":1}}},{"tagMap":{"id":{"data":"minecraft:air"},"Count":{"data":1}}},{"tagMap":{"id":{"data":"minecraft:air"},"Count":{"data":1}}},{"tagMap":{"id":{"data":"minecraft:air"},"Count":{"data":1}}},{"tagMap":{"id":{"data":"minecraft:air"},"Count":{"data":1}}},{"tagMap":{"id":{"data":"minecraft:air"},"Count":{"data":1}}},{"tagMap":{"id":{"data":"minecraft:air"},"Count":{"data":1}}},{"tagMap":{"id":{"data":"minecraft:air"},"Count":{"data":1}}},{"tagMap":{"id":{"data":"minecraft:air"},"Count":{"data":1}}},{"tagMap":{"id":{"data":"minecraft:air"},"Count":{"data":1}}},{"tagMap":{"id":{"data":"minecraft:air"},"Count":{"data":1}}},{"tagMap":{"id":{"data":"minecraft:air"},"Count":{"data":1}}},{"tagMap":{"id":{"data":"minecraft:air"},"Count":{"data":1}}},{"tagMap":{"id":{"data":"minecraft:air"},"Count":{"data":1}}},{"tagMap":{"id":{"data":"minecraft:air"},"Count":{"data":1}}},{"tagMap":{"id":{"data":"minecraft:air"},"Count":{"data":1}}},{"tagMap":{"id":{"data":"minecraft:air"},"Count":{"data":1}}},{"tagMap":{"id":{"data":"minecraft:air"},"Count":{"data":1}}},{"tagMap":{"id":{"data":"minecraft:air"},"Count":{"data":1}}},{"tagMap":{"id":{"data":"minecraft:air"},"Count":{"data":1}}},{"tagMap":{"id":{"data":"minecraft:air"},"Count":{"data":1}}},{"tagMap":{"id":{"data":"minecraft:air"},"Count":{"data":1}}},{"tagMap":{"id":{"data":"minecraft:air"},"Count":{"data":1}}},{"tagMap":{"id":{"data":"minecraft:air"},"Count":{"data":1}}},{"tagMap":{"id":{"data":"minecraft:air"},"Count":{"data":1}}},{"tagMap":{"id":{"data":"minecraft:air"},"Count":{"data":1}}},{"tagMap":{"id":{"data":"minecraft:air"},"Count":{"data":1}}},{"tagMap":{"id":{"data":"minecraft:air"},"Count":{"data":1}}},{"tagMap":{"id":{"data":"minecraft:air"},"Count":{"data":1}}},{"tagMap":{"id":{"data":"minecraft:air"},"Count":{"data":1}}}],"armorSlots":[{"tagMap":{"id":{"data":"minecraft:air"},"Count":{"data":1}}},{"tagMap":{"id":{"data":"minecraft:air"},"Count":{"data":1}}},{"tagMap":{"id":{"data":"minecraft:air"},"Count":{"data":1}}},{"tagMap":{"id":{"data":"minecraft:air"},"Count":{"data":1}}}]}

But when it comes to deserialization, Gson doesn't work...

I won't upload the full log but important bits I cannot understand:

[11:32:13.645] [Server thread/ERROR] [EventBus/EVENTBUS]: Exception caught during firing event: Unable to invoke no-args constructor for interface net.minecraft.nbt.INBT. Register an InstanceCreator with Gson for this type may fix this problem.
	Index: 1
	Listeners:
		0: NORMAL
		1: ASM: class com.example.examplemod.Event.PlayerEvents onPlayerChangeDimension(Lnet/minecraftforge/event/entity/player/PlayerEvent$PlayerChangedDimensionEvent;)V
java.lang.RuntimeException: Unable to invoke no-args constructor for interface net.minecraft.nbt.INBT. Register an InstanceCreator with Gson for this type may fix this problem.
Caused by: java.lang.UnsupportedOperationException: Interface can't be instantiated! Interface name: net.minecraft.nbt.INBT

Thank you in advance, and sorry for the long waiting time.

  • Author

I must serialize the NBTList into a String and then write it into a file.

How could I use NBT to save a file to a specified path?

I'd like to save the entire player class (position, inventory, effects, et cetera) for each dimensions.

Edited by RS785

you could re-invent the wheel and do exactly what you said.... or you could use world saved data (link). using it makes a dat file in saves/YourWorldName/data directory.

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.