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

Saving item to config.

Discussion in 'Development' started by Muqsit, Dec 19, 2016.

  1. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    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.
     
    Last edited: Dec 19, 2016
  2. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    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.
     
  3. Wii54

    Wii54 Silverfish

    Messages:
    16
    GitHub:
    wii54
    Did u make directory using @mkdir?
     
  4. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Hint: the function name is mkdir(), not @mkdir(). It is sometimes considered a bad practice to use the @ operator.
     
    Muqsit likes this.
  5. xBeastMode

    xBeastMode Shog Chips

    Messages:
    0
    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)
     
  6. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    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.
     
  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.