1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Solved Setting item durability

Discussion in 'Development' started by SavionLegendZzz, Dec 11, 2018.

  1. SavionLegendZzz

    SavionLegendZzz Slime

    Messages:
    75
    GitHub:
    savionlegends
    Im trying to set an items durability but it seems it wont fully go to its max durability and remove the green bar. Here is the code there is no errors either
    PHP:
    $handItem $player->getInventory()->getItemInHand();
    if(
    $handItem instanceof Armor && $handItem instanceof Durable){
                                        if(
    $handItem->getDamage() >= $handItem->getMaxDurability()){
                                            
    $player->sendMessage(TextFormat::RED."This item is already at full durability!");
                                            return;
                                        }
                                        if(
    $player->getInventory()->contains(Item::get(Item::IRON_INGOT))){
                                            
    $handItem->setDamage($handItem->getDamage() - 50);
                                            
    $player->getInventory()->sendSlot($player->getInventory()->getHeldItemIndex(), $player);
                                            
    $player->getInventory()->sendContents($player);
                                            if(
    $handItem->hasEnchantments()){
                                                
    $handItem->removeEnchantments();
                                            }
                                            
    $player->getInventory()->removeItem(Item::get(Item::IRON_INGOT0$player->getInventory()->getItemInHand()->getDefensePoints()));
                                            
    $mcmmoPlayer->addXP($skill15);
                                            
    $player->sendMessage(TextFormat::YELLOW."Repaired!");
    Edit: not sure why it formatted like that
     
  2. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    You need to set the updated item instance back to the player's inventory after modifying the item.
    PHP:
    $inventory->setItemInHand($handItem);
    You can get rid of the sendSlot() and sendContents() since setItemInHand() sends slot updates as well.
     
    SavionLegendZzz and DaPigGuy like this.
  3. SavionLegendZzz

    SavionLegendZzz Slime

    Messages:
    75
    GitHub:
    savionlegends
    Thank you i got it now. Here is the code if anyone else needs it:
    PHP:
    if($handItem instanceof Armor && $handItem instanceof Durable){
                                        if(
    $handItem->getDamage() + $handItem->getMaxDurability() === $handItem->getMaxDurability()){
                                            
    $player->sendMessage(TextFormat::RED.$handItem->getName()." is already at full durability!");
                                            return;
                                        }
                                        if(
    $player->getInventory()->contains(Item::get(Item::IRON_INGOT))){
                                            if(
    $handItem->hasEnchantments()){
                                                
    $handItem->removeEnchantments();
                                            }
                                            
    $handItem->setDamage(($handItem->getDamage() - 50));
                                            if(
    $handItem->getDamage() >= $handItem->getMaxDurability()){
                                                
    $player->getInventory()->setItemInHand(Item::get($handItem->getId()));
                                            }else{
                                                
    $player->getInventory()->setItemInHand($handItem);
                                            }
                                            
    $player->getInventory()->removeItem(Item::get(Item::IRON_INGOT0$player->getInventory()->getItemInHand()->getDefensePoints()));
                                            
    $mcmmoPlayer->addXP($skill15);
                                            
    $player->sendMessage(TextFormat::YELLOW."Repaired!");
                                        }else{
                                            
    $player->sendMessage(TextFormat::RED."You need ".$player->getInventory()->getItemInHand()->getDefensePoints()." iron ingots to repair this armor!");
                                        }
    Edit: STILL dont know why it is formatting this way
     
    SleepSpace9 and Muqsit like this.
  4. dktapps

    dktapps Administrator Staff Member PMMP Team

    Messages:
    774
    GitHub:
    dktapps
    `Armor` extends `Durable`, so you don't need the extra `instanceof Durable` check.
     
    Muqsit likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.