Solved Repair items

Discussion in 'Spigot Plugin Development' started by DatsMaboi, Oct 17, 2019.

  1. Code (Java):
    public void repairHand(Player p) {
            ItemStack items = p.getItemInHand();
            if(items instanceof Damageable) {
                    items.setDurability((short)0);
                }
        }
    this does not work can someone please help me?

    i am trying to repair all items in a player's inventory.

    spigot 1.8.8
     
    #1 DatsMaboi, Oct 17, 2019
    Last edited: Oct 17, 2019
  2. Try iterating through all inventory content and set each item manually then set the content.

    Also you are trying to compare ItemStack with Repairable which is wrong. Repairable is a part of ItemMeta so you must do items.getItemMeta() instanceof Repairable but you don't have to do this at all.

    Also remove the plural -s from "items" in your for loop, you are iterating through one item at the time, not many.

    [​IMG]
     
    • Winner Winner x 1
  3. "Repairable" is the type of anything that can be repaired in the anvil.
    You're probably looking for "Damageable".
    (If you need to set durability values, since I don't think they're saved in the Repairable meta)
    You'll need to get the item meta ´ItemMeta meta = stack.getItemMeta()´
    cast it to Damageable, set your damage and then
    `stack.setItemMeta(meta)`

    (https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/inventory/meta/Damageable.html)
     
  4. i just want to repair 1 item. This is what i tried again
    Code (Java):
    public void repairHand(Player p) {
            ItemStack items = p.getItemInHand();
            if(items instanceof Damageable) {
                    items.setDurability((short)0);
                }
        }
     
  5. You're still not getting the ItemMeta from the item.
    Research the spigot api a bit, then you should figure everything eventually :)
     
  6. can someone please give me some actual meaningful help. If i could find it on spigot api i wouldn't be posting this thread.
     
  7. Strahan

    Benefactor

    What the hell more do you need? He's literally writing code for you right there, not even just explaining and letting you figure it out, which is what I'd have done.

    We aren't here to write your plugin for you. Put in a modicum of effort at least. Googling "how set item damage spigot" and restricting to the last year (typically what I do when something I thought to use is deprecated) has the answer in the first result. If you want to be a programmer, and not tear your hair out in frustration, learn how to Google.
     
    • Like Like x 1
    • Agree Agree x 1
    • Winner Winner x 1
  8. I have tried. As you can see my code.
     
  9. what do you mean by cast it to damageable
     
  10. Strahan

    Benefactor

    • Funny Funny x 2
  11. Code (Java):
    Player player = (Player) sender
    Right now I'm casting sender to player, you can do something similar for damageable.
     
  12. yes that's what i did but you can't cast damageable to itemmeta
     
  13. you cant cast damageable to itemmeta i know how to cast.
    Code (Java):
    ItemMeta meta = (Damageable)items.getItemMeta();
     
  14. Review what I wrote.
     
    • Agree Agree x 1
  15. Strahan

    Benefactor

    You are casting to a type different than you are instantiating. Do you not see a problem with that?
     
    • Optimistic Optimistic x 1
  16. The previous poster probably overlooked the "I work with 1.8.8". Damageable was only an Entity interface back in that version, not usable for ItemMeta yet. What you will have to do, is to set the durability of the item, like @kangarko suggested. (He even wrote the code for you. What an absolute unit.) All you have to do, is to not loop over the inventory, but directly use the referenced item. Insane, I know.

    btw pls do yourself a favor and update to a modern minecraft version
     
    • Winner Winner x 1
  17. oh so this
    Code (Java):
            ItemMeta meta = items.getItemMeta();
            Damageable dmg = (Damageable) meta;
     
  18. yes i tried item.setdurability but it didnt work, im guessing it's because of the damageable condition i tried to use before.
     
  19. Strahan

    Benefactor

    Ah yea, I forgot people still develop for totally outdated versions lol