I'm aiming to save $item's id, damage, count and tags to a json config (hoping json is the right one to use here, yaml has problems with "\n" in item names). Here's me code: PHP: public function saveItemToConfig(Item $item) {$toJson = $item->jsonSerialize();$cfg = new Config($this->getDataFolder().'config.json', Config::JSON);$cfg->set('item', $toJson);$cfg->save();}public function giveItemFromConfig(Player $player) {$cfg = new Config($this->getDataFolder().'config.json', Config::JSON);$json = $cfg->get('item', $toJson);$item = Item::get($json["id"], $json["damage"], $json["count"], $json["nbt"]);$player->getInventory()->addItem($item);} This is what I execute... PHP: $item = $player->getInventory()->getItemInHand();$this->saveItemToConfig($item);//later...$this->giveItemFromConfig($player); The code did work, but at times, when the item name had 3+ line breaks, it would just clear the 'item' value from $cfg, and return a null error when I'd execute $this->giveItemFromConfig($player); Any help? EDIT: YAML is bad. You can get yaml_emit no memory errors with it.
Okay, weird. Better. Config::SERIALIZED does the trick. You can directly save $item and then $player->getInventory()->addItem($configdata). No nbtSerialize, jsonSerialise etc. Simple and straight. And works without these errors. Smooth, I'd say.
Hint: the function name is mkdir(), not @mkdir(). It is sometimes considered a bad practice to use the @ operator.
Try replacing "\n" with something else temporarily using str_replace() when you save the item. For example: str_replace("[BREAK]", "\n", $toJson) When you give the item back then replace it back. Example: str_replace("\n", "[BREAK]", $item_name)
Of course. Well, thanks, but I've already managed to fix it using serialized config... This might be helpful to the future visitors... CODE: PHP: public function saveItemToConfig(Item $item) {$cfg = new Config($this->getDataFolder().'config.sl', Config::SERIALIZED);$cfg->set('item', $item);$cfg->save();}public function giveItemFromConfig(Player $player) {$cfg = new Config($this->getDataFolder().'config.sl', Config::SERIALIZED);$item = $cfg->get('item');$player->getInventory()->addItem($item);} Executing.. PHP: $item = $player->getInventory()->getItemInHand();$this->saveItemToConfig($item);$this->giveItemFromConfig($player); PHP's serializing works perfectly for saving items.