Jump to content

[SOLVED] [1.15.2] Storing inventory contents to capability


Novârch

Recommended Posts

I've been trying to make a "time revert" effect lately, where you would set a checkpoint at one time and then be able to go back to it. This of course requires lots of things to be saved (broken blocks, placed blocks, entity positions, game time, etc.), things like broken and placed blocks have been easy, but I'm having trouble with player inventories. The half-baked system I thought up was giving all the ItemStacks in a player's inventory a special variable in a capability and then to delete all items that don't have that variable at a certain value, this works poorly though, items don't revert durability and stackable items can be destroyed if manually stacked. Is there a way I can save every player's inventory contents into a capability to be able to revert to that? I'd really appreciate some ideas.

It's sad how much time mods spend saying "x is no longer supported on this forum. Please update to a modern version of Minecraft to receive support".

Link to comment
Share on other sites

What I can think of is to use ItemStack.write() to write the stack into the compound, and then use CompoundNBT.put() to save the compound that contains the info of the itemstack. In this way every single compound will have an itemstack, store in that given compound.

This is what I do to save a list of BlockPos

private void writeBlockPosWithDirectionList(CompoundNBT compound, String key, HashSet<Connection> targetList, boolean direction)
{
    CompoundNBT subCompound = new CompoundNBT();
    ArrayList<Integer> x = new ArrayList<>();
    ArrayList<Integer> y = new ArrayList<>();
    ArrayList<Integer> z = new ArrayList<>();
    ArrayList<Integer> d = new ArrayList<>();
    targetList.forEach((connection) -> {
        BlockPos blockPos = connection.blockPos;
        x.add(blockPos.getX());
        y.add(blockPos.getY());
        z.add(blockPos.getZ());
        if (direction)
            d.add(connection.direction.getIndex());
    });
    subCompound.putIntArray("x", x);
    subCompound.putIntArray("y", y);
    subCompound.putIntArray("z", z);
    if (direction)
        subCompound.putIntArray("d", d);
    compound.put(key, subCompound);
}

read

private HashSet<Connection> readBlockPosWithDirectionList(CompoundNBT compound, String key)
{
    HashSet<Connection> connections = new HashSet<>();
    CompoundNBT subCompound = compound.getCompound(key);
    int[] x = subCompound.getIntArray("x");
    int[] y = subCompound.getIntArray("y");
    int[] z = subCompound.getIntArray("z");
    int[] d = subCompound.getIntArray("d");
    for (int i=0;i<subCompound.getIntArray("x").length;i++)
        connections.add(new Connection(new BlockPos(x[i], y[i], z[i]), Direction.byIndex(d[i])));
    return connections;
}
Edited by poopoodice
Link to comment
Share on other sites

8 hours ago, poopoodice said:

What I can think of is to use ItemStack.write() to write the stack into the compound, and then use CompoundNBT.put()

Thank you! I've gone ahead and used that and setup a capability, I also took a look at the read write methods inside PlayerInventory. The issue now is that for some reason the inventory in the capability is syncing with the player's inventory, so it isn't reverting like it should. Can you see anything I missed here that could cause that, the issue seems pretty much impossible.

 

Edit: Forgot to include a link to the class, here.

Edit 2: It seems that it removes new items but doesn't bring back old ones? Weird.

Edit 3: It also seems the items added to the player's inventory are ghost items and disappear when dropped. I'm stupid and didn't check World#isRemote.

Edited by Novârch

It's sad how much time mods spend saying "x is no longer supported on this forum. Please update to a modern version of Minecraft to receive support".

Link to comment
Share on other sites

I'm pretty sure you are interacting directly with the player inventory

https://github.com/Novarch129/JoJo-s-Bizarre-Survival/blob/1.15.x/src/main/java/io/github/novarch129/jojomod/capability/StandPlayerEffects.java#L26

Instead you should copy the inventory of the player in order to save it, for example ArrayList<ItemStack>, and use ItemStack.copy

Edited by poopoodice
Link to comment
Share on other sites

11 hours ago, poopoodice said:

Instead you should copy the inventory of the player in order to save it, for example ArrayList<ItemStack>, and use ItemStack.copy

I'm doing that now, I've updated the repo with the new code so you can just use the same links. The same issue still occurs though, if I throw an item it will not re-appear in my inventory, but if I manually delete it using creative mode it will.

 

Edit: I have confirmed the issue is linked to tossing the item, if I toss the item no matter what I do the item will be erased. I don't understand why the list in the capability updates.

Edited by Novârch

It's sad how much time mods spend saying "x is no longer supported on this forum. Please update to a modern version of Minecraft to receive support".

Link to comment
Share on other sites

3 hours ago, Novârch said:

I'm doing that now, I've updated the repo with the new code so you can just use the same links. The same issue still occurs though, if I throw an item it will not re-appear in my inventory, but if I manually delete it using creative mode it will.

 

Edit: I have confirmed the issue is linked to tossing the item, if I toss the item no matter what I do the item will be erased. I don't understand why the list in the capability updates.

When you toss an item the itemstack is modified, and since you only pass a reference of the player itemstacks to your capability, those itemstacks will also shrink. Copy the stacks with .copy() in your detonate method when you save the itemstacks to create new identical itemstacks instead. However this also means that you can now duplicate items by saving, tossing items, and revert, since you will get your items back, and still have the old items on the ground.

 

P.S. Interesting mod :)

  • Like 1
Link to comment
Share on other sites

4 hours ago, vemerion said:

When you toss an item the itemstack is modified, and since you only pass a reference of the player itemstacks to your capability, those itemstacks will also shrink. Copy the stacks with .copy()

That's exactly what I was looking for! Thank you!

  • Like 1

It's sad how much time mods spend saying "x is no longer supported on this forum. Please update to a modern version of Minecraft to receive support".

Link to comment
Share on other sites

  • Novârch changed the title to [SOLVED] [1.15.2] Storing inventory contents to capability

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

    • https://pastebin.com/VwpAW6PX My game crashes upon launch when trying to implement the Oculus mod to this mod compilation, above is the crash report, I do not know where to begin to attempt to fix this issue and require assistance.
    • https://youtube.com/shorts/gqLTSMymgUg?si=5QOeSvA4TTs-bL46
    • CubeHaven is a SMP server with unique features that can't be found on the majority of other servers! Java: MC.CUBEHAVEN.NET Bedrock: MC.CUBEHAVEN.NET:19132 3 different stores: - CubeHaven Store: Our store to purchase using real money. - Bitcoin Store: Store for Bitcoin. Bitcoin can be earned from playing the server. Giving options for players if they want to spend real money or grind to obtain exclusive packages. - Black Market: A hidden store for trading that operates outside our traditional stores, like custom enchantments, exclusive items and more. Some of our features include: Rank Up: Progress through different ranks to unlock new privileges and perks. 📈 Skills: RPG-style skill system that enhances your gaming experience! 🎮 Leaderboards: Compete and shine! Top players are rewarded weekly! 🏆 Random Teleporter: Travel instantly across different worlds with a click! 🌐 Custom World Generation: Beautifully generated world. 🌍 Dungeons: Explore challenging and rewarding dungeons filled with treasures and monsters. 🏰 Kits: Unlock ranks and gain access to various kits. 🛠️ Fishing Tournament: Compete in a friendly fishing tournament! 🎣 Chat Games: Enjoy games right within the chat! 🎲 Minions: Get some help from your loyal minions. 👥 Piñata Party: Enjoy a festive party with Piñatas! 🎉 Quests: Over 1000 quests that you can complete! 📜 Bounty Hunter: Set a bounty on a player's head. 💰 Tags: Displayed on nametags, in the tab list, and in chat. 🏷️ Coinflip: Bet with other players on coin toss outcomes, victory, or defeat! 🟢 Invisible & Glowing Frames: Hide your frames for a cleaner look or apply a glow to it for a beautiful look. 🔲✨[ Player Warp: Set your own warp points for other players to teleport to. 🌟 Display Shop: Create your own shop and sell to other players! 🛒 Item Skins: Customize your items with unique skins. 🎨 Pets: Your cute loyal companion to follow you wherever you go! 🐾 Cosmetics: Enhance the look of your character with beautiful cosmetics! 💄 XP-Bottle: Store your exp safely in a bottle for later use! 🍶 Chest & Inventory Sorting: Keep your items neatly sorted in your inventory or chest! 📦 Glowing: Stand out from other players with a colorful glow! ✨ Player Particles: Over 100 unique particle effects to show off. 🎇 Portable Inventories: Over virtual inventories with ease. 🧳 And a lot more! Become part of our growing community today! Discord: https://cubehaven.net/discord Java: MC.CUBEHAVEN.NET Bedrock: MC.CUBEHAVEN.NET:19132
    • # Problematic frame: # C [libopenal.so+0x9fb4d] It is always the same issue - this refers to the Linux OS - so your system may prevent Java from working   I am not familiar with Linux - check for similar/related issues  
  • Topics

×
×
  • Create New...

Important Information

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