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

Save enchantment item to config

Discussion in 'Development' started by JblusItsMe, Aug 11, 2022.

  1. JblusItsMe

    JblusItsMe Creeper

    Messages:
    3
    GitHub:
    JblusItsMe
    Hey, how to get all enchantement and set this item ? My code in crash

    PHP:
    public static function setInventory(Player $sender): void {
            
    $config self::$instance->getPlayerConfig($sender);
            try {
                if(
    $config->exists("inventory")) {
                    
    $contents = [];
                    foreach(
    $config->get("inventory") as $item) {
                        
    $contents[] = ItemFactory::getInstance()->get("$item[0]""$item[1]""$item[2]")
                            ->
    setCustomName($item[3])
                            ->
    addEnchantment($item[4]);
                    }
                    
    $sender->getInventory()->setContents($contents);
                }
                if(
    $config->exists("armor")) {
                    
    $contentsArmor = [];
                    foreach(
    $config->get("armor") as $armor) {
                        
    $contentsArmor[] = ItemFactory::getInstance()->get("$armor[0]""$armor[1]""$armor[2]")
                            ->
    setCustomName($armor[3]);
                    }
                    
    $sender->getArmorInventory()->setContents($contentsArmor);
                }
            } catch(\
    JsonException $e) {}
        }

        
    /**
         * @throws \JsonException
         */
        
    public static function saveInventory(Player $sender) {
            
    $inv = [];
            foreach(
    $sender->getInventory()->getContents() as $content) {
                
    $inv[] = [$content->getId(), $content->getMeta(), $content->getCount(), $content->getCustomName(), $content->getEnchantments()];
            }
            
    $config self::$instance->getPlayerConfig($sender);
            
    $config->set("inventory"$inv);
            
    $config->save();

            
    $armor = [];
            foreach(
    $sender->getArmorInventory()->getContents() as $content) {
                
    $armor[] = [$content->getId(), $content->getMeta(), $content->getCount(), $content->getCustomName(),$content->getEnchantments()];
            }
            
    $config->set("armor"$armor);
            
    $config->save();
        }
     
  2. iRainDropp

    iRainDropp Creeper

    Messages:
    5
    GitHub:
    iRainDropp
    What is the error, I have no idea what part of your code is crashing nor would I like to spend 20+ minutes trying to figure it out ;-; I can try to help if you post the error.
     
  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.