Change player's inventory async?

Discussion in 'Plugin Development' started by jojodmo, Aug 4, 2014.

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

    jojodmo

    I was wondering if it was thread-safe to change a player's inventory? Would something like this work:


    Code:java
    1.  
    2. final PlayerInventory i = player.getInventory();
    3. final ItemStack myItem = new ItemStack(Material.COOKIE);
    4.  
    5. //async task stuff{
    6. i.setItem(0, myItem);
    7. //}
    8.  


    obviously I would be doing way more than just that, but you get the idea... So, would that work? If not, is there any other thread-safe way to modify a player's inventory? Thanks!
     
  2. Offline

    CraftCreeper6

    jojodmo
    That should work, yes.
     
  3. Offline

    Syd

    In 1.7. almost nothing is thread-safe. This may change in 1.8 due to further improvements and threading on Mojangs side.

    So I wouldn't recommend working with inventories outside of the main thread, especially not something like the player inventory.
     
    ZeusAllMighty11 likes this.
  4. Offline

    jojodmo

  5. Offline

    Syd

    jojodmo
    I havn't looked it up, yet, but I assume there is no syncronisation in the inventories code, so it should be non-threadsafe.
    Like in most cases it will most likely work on a local test enviroment, but without syncronisation you will sooner or later have some concurrency issues on a productive enviroment.

    You can occur several problems when working parallel on non-threadsafe enviroments. But unlike normal errors you'll maybe not end with a sudden crash, but with corrupted data. And nobody likes it when their stack diamonds is all of sudden a stack of coal. :p (well, basicly diamonds are coal, but that's another topic :S)

    So I wouldn't recommend to use it unless you're 100% sure that it's thread-safe.

    (Btw. I'm not an expert in this topic!)
     
Thread Status:
Not open for further replies.

Share This Page