Weird inventar behavior

Discussion in 'Plugin Development' started by AmShaegar, Feb 28, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    AmShaegar

    Basically, I want to save someone's quick bar and replace it by some wool. Later, I want to restore the quick bar.
    Global:
    Code:
    private ItemStack[] restoreInventory;
    Save:
    Code:
    restoreInventory = new ItemStack[9];
    for(int i = 0; i < 9; i++) {
        restoreInventory[i] = player.getInventory().getItem(i);
        player.getInventory().clear(i);
        player.getInventory().setItem(i, new Wool(color).toItemStack(1));
    }
    player.updateInventory();
    Restore:
    Code:
    for(int i = 0; i < 9; i++) {
        player.getInventory().clear(i);
        player.getInventory().setItem(i, restoreInventory[i]);
    }
    player.updateInventory();
    If I do this once, everything is fine. If I do this more than once, the player can get his saved sword by using wool to attack someone. If you selected the slot where the sword was before, it will be restored. Why?

    [​IMG]
     
  2. Offline

    ZachBora

    I think you have to clone the item stacks or else it saves a reference to it.
     
  3. Offline

    juampi_92

    i don't understand your problem

    The player has one sword, and then you take it all and give him some wool? but when you restore it, it only gives him the sword and doesnt take away the rest of the wool?
     
  4. Offline

    AmShaegar

    Nope. The player has some tools. They are replaced by wool. But when you attack someone with the slot of the sword activated, the wool disappears and the sword get restored.

    I will try to clone the ItemStacks. Thank you.

    Edit:
    ZachBora : Thanks for the attempt but cloning does not solve the issue. Neither cloning on save nor on restore.
     
  5. Offline

    ZachBora

    AmShaegar I remember people complaining about such issues and the reply was to add a player.updateInventory(); after each and every inventory manipulations... I know it sounds weird but you could try that.
     
  6. Offline

    AmShaegar

    Well... that sounds really weird. Kinda more weird than the initial problem... I will try.

    Edit: @ZachBora Sorry, did not work as well. :-(

    I feel like pushing this before I am going to bed is a good idea. I still have no idea why this happens.

    Steps to reproduce the bug:
    • put a sword in slot 0
    • issue the inventory saving. slots 0 to 8 are saved and replaced by wool.
    • attack somone with item 1 in hand.
    • restore inventory and remove wool.
    • save inventory again.
    • attack someone with item 1 in hand.
    Result: The sword gets restored as seen in the screenshot above. Nothing else is touched.

    I could only watch this for the sword and bow and is not limited to slot 0.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
Thread Status:
Not open for further replies.

Share This Page