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

Solved Backpack Plugin with NBT Tags?

Discussion in 'Plugin Help' started by timistwild, Mar 23, 2021.

  1. timistwild

    timistwild Spider Jockey

    Messages:
    33
    Hi!
    Im currently working on a backpack plugin. For the backpack design i use the InvMenu Plugin developed by Muqsit(Thank you for that if you read it :) ) Here is the link: https://github.com/Muqsit/InvMenu
    Now i want to safe the Items which are set by the player with NBT Tags. I already started with:
    PHP:
    $menu->setInventoryCloseListener(function (Player $playerInventory $inventory) {

                for (
    $slots 0$slots <= 40$slots++) {
                    
    $pName $player->getName();
                    
    $item $inventory->getItem($slots);
                    
    $nbt $item->getNamedTag() ?? new CompoundTag("", []);
                    
    $nbt->inv = new StringTag($pName,"items");
                    
    $item->setNamedTag($nbt);
                }
    (I hope this is right) My Problem is now that when the player opens the Backpack the Items need to load(of course) but where can i "get" the Items from to even check if they have the right tag? Thanks for your help :)
     
  2. timistwild

    timistwild Spider Jockey

    Messages:
    33
    Is it a dumb question?
     
    TestDevelopment likes this.
  3. TestDevelopment

    TestDevelopment Spider Jockey

    Messages:
    41
    GitHub:
    non
    Try using a list tag and then using int or sttinh tags for every slot you want to be saved
     
  4. TestDevelopment

    TestDevelopment Spider Jockey

    Messages:
    41
    GitHub:
    non
    Probably not :)
     
  5. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
  6. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    Take a look at Item::jsonSerialize/Item::jsonDeserialize or Item::nbtSerialize/Item::nbtDeserialize depending on how you are saving the data
     
    minijaham and Primus like this.
  7. timistwild

    timistwild Spider Jockey

    Messages:
    33
    Alright i tried this:
    (This is where i save the items)
    PHP:
    $menu->setInventoryCloseListener(function (Player $playerInventory $inventory) {

                for (
    $slots 0$slots <= 40$slots++) {
                    
    $pName $player->getName();
                    
    $item $inventory->getItem($slots);
                    
    $value $item->nbtSerialize();
                    
    $this->saveItemToConfig($value);
                }

            });
    And this is where i try to give the Items
    PHP:
    for ($slots 1$slots <= 40$slots++) {
                
    $items $this->giveItemFromConfig($player);
                
    $cook $items->nbtDeserialize();
                
    $inv->setItem($slots,$items);


            }
    And these are the functions:
    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');
            return 
    $item;
        }
    And this is the failure:
    Code:
     [Server thread/CRITICAL]: Error: "Call to a member function nbtDeserialize() on bool" (EXCEPTION) in "plugins/Backpack/src/Tim/Backpack/Main" at line 100
    I do not really unterstand the nbtDeserialize function. Someone could help me?
     
  8. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
  9. timistwild

    timistwild Spider Jockey

    Messages:
    33
    Thank you, the link was very helpful! :)
     
  10. Primus

    Primus Zombie Pigman

    Messages:
    749
    Stop abusing configs
     
    minijaham 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.