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

Solved Transforming ingots to blocks

Discussion in 'Development' started by Khaled, Apr 1, 2020.

  1. Khaled

    Khaled Slime

    Messages:
    81
    GitHub:
    xXKHaLeD098Xx
    Hello, so i was creating a plugin where if there are ingots in a chest it transformes them into blocks and this is my code:
    PHP:

    public function matchItem(Item $neededItemChestInventory $inv) {
            
    $items $inv->getContents();
            foreach(
    $items as $key => $item) {
                if(
    $item->equals($neededItem)) {
                    return 
    $key;
                }
                return 
    false;
            }
        }

    foreach (
    $contents as $item){ // on playerinteractevent
                                    
    $uses $nbt->getInt("usesCondense"); // dont mind this
                                    
    if ($item->getId() === Item::IRON_INGOT){
                                        if(
    $item->getCount() >= 9){
                                            
    $ev->setCancelled();
                                            
    $condensable true// dont mind this
                                            
    $transform floor($item->getCount() / 9);
                                            
    $remove $transform 9;
                                            
    $slot $this->matchItem($item$inv);
                                            for(
    $i 0$i $transform$i++){
                                                
    $p->getInventory()->addItem(Item::get(Item::IRON_BLOCK));
                                                
    $inv->setItem($slotItem::get($item->getId(), 0$item->getCount() - $remove)); // here's the problem
                                            
    }
                                        }
                                    }
                                }
    however it works and gives me the right count of iron blocks too but it doesn't substract the count of all the ingots from the $remove variable but it subtracts only 1 slot (and i want it to be in all the slots which have iron ingots) i hope u understand to help me.
     
  2. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    Khaled likes this.
  3. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    I would suggest counting all the items and removing them before calculating and adding the blocks
    PHP:
    $count 0;
    foreach (
    $contents as $slot => $item){
        if (
    $item->getId() === Item::IRON_INGOT){
            
    $count += $item->getCount();
            
    $inventory->clear($slot);
        }
    }
    $blocks floor($count/9);
    $inventory->addItem(Item::get(Item::IRON_BLOCK,0,$blocks));
    $remain $count 9;
    if(
    $remain 0)
    $inventory->addItem(Item::get(Item::IRON_INGOT,0,$remain));
     
    Khaled likes this.
  4. Khaled

    Khaled Slime

    Messages:
    81
    GitHub:
    xXKHaLeD098Xx
    Thank you very much guys! it works
     
    HimbeersaftLP 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.