Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[SOLVED] [1.15.2] Storing inventory contents to capability


Novârch
 Share

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



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.