Set Damageable and Repairable ItemMetas onto ItemStack

Discussion in 'Plugin Development' started by XantiuM, Sep 16, 2019.

  1. Offline

    XantiuM

    I am trying to set new damage and repair cost values onto an ItemStack using Damageable and Repairables casted as ItemStacks, set using the setItemMeta method.

    My current relevant code is:
    Code:
    Damageable newElytraDamage = (Damageable) slot0.getItemMeta();
    newElytraDamage.setDamage(newDamage);
    
    Repairable newElytraRepairable = (Repairable) slot0.getItemMeta();
    newElytraRepairable.setRepairCost((elytraRepairable.getRepairCost() * 2) + 1);
    
    ItemStack newElytraStack = new ItemStack(Material.ELYTRA, 1);
    newElytraStack.setItemMeta((ItemMeta) newElytraDamage);
    newElytraStack.setItemMeta((ItemMeta) newElytraRepairable);
    What this appears to do is overwrite the new damage item meta with the repairable item meta. I want it to do both. How can I achieve this?
     
  2. Online

    timtower Moderator Moderator

    @XantiuM Get it for Damageable, change it, set it.
    Repeat for Repairable
     
  3. Offline

    XantiuM

    @timtower Is this what you mean? I'm confused about the wording of your answer. It still doesn't appear to work, appearing to set a prior-work related NBT but not changing the damage value.
    Code:
    ItemStack newElytraStackInit = new ItemStack(Material.ELYTRA, 1);
    newElytraStackInit.setItemMeta((ItemMeta) newElytraDamage);
    
    ItemStack newElytraStack = new ItemStack(newElytraStackInit);
    newElytraStack.setItemMeta((ItemMeta) newElytraRepairable);
     
  4. Online

    timtower Moderator Moderator

    @XantiuM
    Code:
    Damageable newElytraDamage = (Damageable) slot0.getItemMeta();
    newElytraDamage.setDamage(newDamage);
    newElytraStack.setItemMeta((ItemMeta) newElytraDamage);
    
    Repairable newElytraRepairable = (Repairable) slot0.getItemMeta();
    newElytraRepairable.setRepairCost((elytraRepairable.getRepairCost() * 2) + 1);
    
    ItemStack newElytraStack = new ItemStack(Material.ELYTRA, 1);
    newElytraStack.setItemMeta((ItemMeta) newElytraRepairable);
     
  5. Offline

    XantiuM

    @timtower This seems like the same thing but you moved a newElytraStack method above where it is initialized (second to last line). Could you explain what you are trying to do? Or would more context help?
     
  6. Online

    timtower Moderator Moderator

    @XantiuM Wait, why do you make a new ItemStack? I totally missed that part.
    Code:
    Damageable newElytraDamage = (Damageable) slot0.getItemMeta();
    newElytraDamage.setDamage(newDamage);
    slot0.setItemMeta((ItemMeta) newElytraDamage);
    
    Repairable newElytraRepairable = (Repairable) slot0.getItemMeta();
    newElytraRepairable.setRepairCost((elytraRepairable.getRepairCost() * 2) + 1);
    slot0.setItemMeta((ItemMeta) newElytraRepairable);
     
  7. Offline

    XantiuM

    @timtower I guess more context would help. I am trying to create a new anvil recipe, the old method of repairing elytra (elytra + leather). slot0 is an ItemStack for inventory slot 0 in the anvil (leftmost box), so that shouldn't be edited. I am making a new ItemStack (newElytraStack) to be later given as the final anvil output. This is why I need to increase the repair cost and lessen damage.
     
  8. Online

    timtower Moderator Moderator

    @XantiuM Then you make the new ItemStack as first line. Probably a copy of slot0, then you modify the item meta from that one.
     
  9. Offline

    XantiuM

    @timtower Okay, I redid the code I wrote around that, creating the newElytraStack ItemStack from a copy of slot0. I fail to see how this will solve my issue as I still have an overwrite going on:
    Code:
    newElytraStack.setItemMeta((ItemMeta) newElytraDamage);
    newElytraStack.setItemMeta((ItemMeta) newElytraRepairable);
    
    Is there another method I am not aware about that will let me do what I wish to accomplish (write both damage and prior-work information, perhaps an ItemMeta[])? I've searched a fair bit with no avail.

    Edit: This code below looks like it may be working, I'll report back:
    Code:
    ItemStack newElytraStackInit = new ItemStack(slot0);
    
    Damageable newElytraDamage = (Damageable) newElytraStackInit.getItemMeta();
    newElytraDamage.setDamage(newDamage);
    
    newElytraStackInit.setItemMeta((ItemMeta) newElytraDamage);
    ItemStack newElytraStack = new ItemStack(newElytraStackInit);
    
    Repairable newElytraRepairable = (Repairable) newElytraStack.getItemMeta();
    newElytraRepairable.setRepairCost((elytraRepairable.getRepairCost() * 2) + 1);
    
    newElytraStack.setItemMeta((ItemMeta) newElytraRepairable);
    
     
    Last edited: Sep 16, 2019
  10. Online

    timtower Moderator Moderator

    @XantiuM Both values comtain the full meta.
    It doesn't know what values to overwrite so it overwrites all of them, every time you set the meta.
     
  11. Offline

    XantiuM

    @timtower The code from my edit above works (I think)! A little messy, but what can you do. I've been stuck on this for probably 2 hours. Thanks for your help!
     
    timtower likes this.

Share This Page