Update inventory after interact

Discussion in 'Plugin Development' started by shadyre, May 29, 2011.

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

    shadyre

    Hi, for my plugin i need update my inventory after an interaction with a sign, that "updateInventory()" is deprecated ...

    Do you can help me ?
     
    Locercus likes this.
  2. Offline

    Jayjay110

    So do I... +1 To this post!
     
  3. Offline

    shadyre

    I have test :

    Code:
    Player player = event.getPlayer();
    
    // first test, i write a second getInventory() ... but nothing
    player.getInventory();
    
    // second test
    player.wait();
    // nothing too ...
    but only player.updateInventory() work ...
     
  4. Offline

    NathanWolf

    I had/have this same issue with Wand. I've found I can use bukkitsched to (semi-reliably) postpone the update a bit, then it seems to work. (You have to actually modify the inventory, though- add/remove and set all seem to work.)
     
  5. Offline

    shadyre

    Oh ok, i test that thank you

    I have test with that :
    Code:
    public void updatePlayerInventory(Player player) {
            player.getInventory().addItem(new ItemStack(Material.WOOD, 1));
            player.getInventory().removeItem(new ItemStack(Material.WOOD, 1));
        }
    And in my code, after interact :
    Code:
    updatePlayerInventory(player);
    But already nothing ...

    That doesn't work too ... :

    (lines[1].toUpperCase() it's the line of a sign)
    player.getInventory().getItem(Material.getMaterial(lines[1].toUpperCase()).getId());

    I have a start of possible solution :
    Code:
    ItemStack[] item = player.getInventory().getContents();
                                    for(int i=0; i<35; i++) {
                                        if(item[i] != null) {
                                            player.sendMessage("" + item[i].getAmount() + " " + item[i].getType());
                                        }
                                    }
    The message return information of REAL item in your inventory, but for now i don't know how to reload the player inventory ...

    ( Sorry for my english i'm french :) )

    Code:
    ItemStack[] item = player.getInventory().getContents();
    player.getInventory().clear();
    for(int i=0; i<35; i++) {
        if(item[i] != null) {
            player.getInventory().setItem(i, new ItemStack(item[i].getType(), item[i].getAmount()));
        }
    }
    player.getInventory().setContents(item);
    Doesn't work for now ... Any idea ?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 16, 2016
  6. Offline

    DreadKyller

    first off, if you want the inventory to stay the same, when the item==null, you want to add null to the itemstack list. Also you really should not even need the for all the items in the list to add to the inventory if you are setting the contents to the ItemStack[].

    EDIT!

    I know your problem, what's happening:

    player.getContents is returning a ItemStack[] in which you are adding an item to the list, but you are not adding it to the inventory, only the copy of the inventory.

    PHP:
    player.setContents(item)


    should fix your problem
     
  7. Offline

    shadyre

    player.setContents(item), doesn't work ecause seContents() for player doesn't exist :s and yea i know with getContents() i get the content ... i want make a save of the inventory befor destroy it and after with player.getInventory().setContents(item); i want copy the new exact inventory :)

    But it don't work for now :(

    here :
    Code:
    for(int i=0; i<35; i++) {
        if(item[i] != null) {
            player.getInventory().setItem(i, new ItemStack(item[i].getType(), item[i].getAmount()));
        }
    }
    
    I don't want copy or make a null item ... if is null why make or copy it ?
    I just want list existant item, list them and finally add them in my inventory

    Nobody can update inventory without updateInventory() ?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 16, 2016
  8. Offline

    Jayjay110

    Wait wat are u aimin to do?
     
  9. Offline

    shadyre

    aimin ? :s sorry i don't understand :(
     
  10. Offline

    Jayjay110

    lol my bad, Aiming....:p
     
  11. Offline

    shadyre

    Oh ^^ if i understand right,
    i wanna update my inventory after an interaction with a sign ( shop sign ), selling buying are good, but the method updateInventory() is deprecated, and i want update it without this method ...

    All of my test doesn't work ... :(

    How to update inventory without updateInventory() ... :(
     
  12. Offline

    Jayjay110

    ok weres your code ill fix it for u if I can understand it properley
     
  13. Offline

    shadyre

    My last code :

    Code:
    ItemStack[] items = player.getInventory().getContents();
    player.getInventory().clear();
    for(int i=0; i<35; i++) {
        if(items[i] != null) {
            // debug message
            player.sendMessage(items[i].getType() + " " + items[i].getAmount());
            player.getInventory().setItem(i, new ItemStack(items[i].getType(), items[i].getAmount()));
        }
    }
    player.getInventory().setContents(items);
    if you need more information ask me :) and thnak for your help !
     
    Jayjay110 likes this.
  14. Offline

    DreadKyller

    here is one thing you could do, instead of using the setItem you could just use addItem:

    PHP:
    ItemStack[] items player.getInventory().getContents();
    player.getInventory().clear();
    for(
    int i=0i<35i++) {
        if(
    items[i] != null) {
            
    // debug message
            
    player.sendMessage(items[i].getType() + " " items[i].getAmount());
            
    player.getInventory().addItem(items[i]);
        }else{
            
    player.getInventory().addItem(null);
        }
    }
    Also, just as future, as seeing the items array is an itemstack array, you don't need to make a new ItemStack out of it's material and it's value, just use the ItemStack...
     
  15. Offline

    shadyre

    Ok, i have use an itemstack for replace old items as the same position in the inventory ! :) i test that

    EDIT :
    just replace player.getInventory().addItem(null);
    by player.getInventory().addItem(new ItemStack(null));

    Because add item need ItemStack value ...
     
  16. Offline

    DreadKyller

    Oo yeah, sorry. but anyways, tell if it works
     
  17. Offline

    shadyre

    I'm right, thank for your help, but he doesn't work, that crash all of inventory content and return error.
     
  18. Offline

    DreadKyller

    Oo, yeah, because actually, it should be just null, not ItemStack(null) because if you look at the items array you have, you'll find a lot of "null" if the inventory returned null from the check, then you should use it, look at this:

    PHP:
    ItemStack[] items player.getInventory().getContents();
    player.getInventory().clear();
    for(
    int i=0i<35i++) {
        if(
    items[i] != null) {
            
    // debug message
            
    player.sendMessage(items[i].getType().toString() + " " items[i].getAmount());
            
    player.getInventory().addItem(items[i]);
        }else{
            
    player.getInventory().addItem(items[i]);
        }
    the reason I seperate the null and others, even though both are added from the list, is because a null value will return an error if you try to send the player a message of null.getType() or null.getAmount()
     
    shadyre likes this.
  19. Offline

    shadyre

    Return an error too, because you can't add null item ... :(

    EDIT : it's for that if use : if(items != null) { ... }
     
  20. Offline

    DreadKyller

    lol, well that should be
    PHP:
    items[i]
    I'm trying to explain something here:

    There are items in your inventory:

    Items that are taken up are the ItemStack
    the blank slots have null as the value
    Blank places in armor are filled with air

    I've tested this before and I know you can add a null object as an item list and it will just make that slot empty, only I thought of one problem, even after it adds the empty slot it will then just add an item right over it because the addItem() looks for the first item slot that is of value null and adds it there, so adding mull will not work. Hmm, I really don;t get what's wrong, I've never had this problem, and why do you even need to update the inventory? doesn't it automatically do that?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 16, 2016
  21. Offline

    shadyre

    no, inventory update work only with this method : updateInventory(), if you don't use that, all new item stacked isn't showed ( icons or number ).

    I need made a new method for update the player inventory because updateInventory() method is deprecated ...
    And if they delete this method in the futur, in prevent of that i want made a new valid method
     
  22. Offline

    DreadKyller

    ... whatever, then this is something I have no idea what in the fk is with it. When I do anything to the inventory it updates automatically, what are you doing that makes your inventory need to be updated? I was asking a question not telling a statement
     
  23. Offline

    shadyre

    Code:
    if(player.getItemInHand().getAmount() > buyPrice) player.getItemInHand().setAmount(player.getItemInHand().getAmount() - buyPrice);
    else player.getInventory().setItemInHand(null);
    
    player.getInventory().addItem(new ItemStack(Material.getMaterial(lines[1].toUpperCase()), buyValue));
    This is my code in result of an interaction with an shop sign for adding item in the inventory and update item in hand.
     
  24. Offline

    DreadKyller

    yeah, and that does not have any inventory.updateInventory(), and that should work
     
  25. Offline

    shadyre

    yes because i havn't wrote it, i don't want use a deprecated method ...

    On the bottom of my code i have :
    // player.updateInventory();

    And you can't use inventory.updateInventory() if is same as player.getInventory().updateInventory();
    This method doesn't exist ... and i have the latest build of bukkit API
     
  26. Offline

    ZephyrSigmar

    Hmm,i just made a plugin which needs updateinventory,and its works fine,but maybe i didnt understood your problem.
    Here is my code:
    Code:
    Player p = event.getPlayer();
    Inventory i = p.getInventory();
    int metarial = event.getBlock().getTypeId();
    i.addItem(new ItemStack(metarial, 1));
    p.sendMessage("[Create]You get 1 " + metarial);
    p.updateInventory();
     
  27. Offline

    shadyre

    Yeah but you use p.updateInventory(); :) and that's deprecated :p
     
  28. Offline

    ZephyrSigmar

    But its still works,then why it's problem if i use this? :confused:
     
  29. Offline

    shadyre

    it's not a problem, and yes it work, but because this method was deprecated and if they deleted it some plugin don't work, it's for that i prefer made a new method for update inventory for prevent a possible supretion of updateInventory() ...

    But use it it's not a problem :)

    i have found that : http://leaky.bukkit.org/issues/313
    it's not the inventory directly update, it's the UI ... i search on the official source for found a solution

    EDIT :
    This is the official source file of updateInventory(); : http://javadoc.lukegb.com/Bukkit/d7/d7b/CraftPlayer_8java_source.html

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 16, 2016
  30. Offline

    chillindude911

    JUST USE @SUPPRESSWARNING!

    :) TeeHee
     
Thread Status:
Not open for further replies.

Share This Page