Solved Set ItemStack Data Value in 1.8

Discussion in 'Spigot Plugin Development' started by ReadySetPawn, Sep 7, 2015.

  1. I've never really had to set an ItemStack data value in 1.8, but now I do and don't know how.

    This is what I have tried:
    Code (Java):
    itemStack.getData().setData(...);
    But it just doesn't work. No errors in console or IDE. It just doesn't change.
     
  2. Code (Java):

    MaterialData data = itemStack.getData();
    // do something to data, or create your own data.
    itemStack.setData(data);
     
     
    • Like Like x 1
  3. Code (Text):
    public void update(){
            MaterialData materialData = this.inventory.getItem(35).getData();

                if (this.selectedFighters.size() == 1){
                    materialData.setData((byte) 5);
                } else {
                    materialData.setData((byte) 7);
                }
            this.inventory.getItem(35).setData(materialData);
        }
    Still doesn't work =/
     
  4. Well, inventory.getItem() works in the same concept. You keep getting a new item everytime you call that. You even set the new data to it, but you never set that item back into the inventory. Its just lost in memory when the method ends.

    Here:

    Code (Text):

    public void update(){
        ItemStack item = this.inventory.getItem(35);
        MaterialData materialData = item.getData();

        if (this.selectedFighters.size() == 1) {
            materialData.setData((byte) 5);
        } else {
            materialData.setData((byte) 7);
        }

        item.setData(materialData);
        this.inventory.setItem(35, item);
    }
     
    Now, depending on what this inventory is (player inventory vs chest inventory, etc) you may need to send an update packet to the player to see the changes.
     
  5. Okay that seems to work a little. The data gets updated (I know this because I added debug code where the data value gets broadcasted). But it doesn't update (like you said). Do you know what packet I need to send in order to update the inventory?
     
  6. Remove the item and then add it back to the inventory if it's not updating, less resource intensive than calling updateInventory(). Alternatively try using ItemStack#setDurability() instead (durability=data, don't be fooled by its name).
     
  7. Code (Text):
    player.updateInventory();
    /e ninja'd
     
  8. That worked ^_^
    Thanks everybody that posted on this thread <3