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

How to get all player inventory ids

Discussion in 'Plugin Help' started by tybikman, Apr 29, 2020.

  1. tybikman

    tybikman Silverfish

    Messages:
    22
    I need to get all item IDs in inventory and delete them
    Like this:
    $inv = $ player-> getInventory ();
    $ Player-> getInventory () -> RemoveItem ($ inv);
     
    Last edited: Apr 29, 2020
  2. NutXzG

    NutXzG Baby Zombie

    Messages:
    132
    GitHub:
    NutXzG
    Ugh like this?
    PHP:
    $player $this->getServer()->getOnlinePlayer();
    $player->getInventory()->removeItem(Item::get(youritemiditemdatacount));
     
  3. tybikman

    tybikman Silverfish

    Messages:
    22
    No , i need to get all ids
     
  4. tybikman

    tybikman Silverfish

    Messages:
    22
    Like get all inventory
     
  5. NTT

    NTT Zombie

    Messages:
    311
    GitHub:
    NTT1906
    Bad code
     
  6. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    You mean $player->getInventory()->clearAll() ?
     
  7. tybikman

    tybikman Silverfish

    Messages:
    22
    I am making a gamemode plugin
    Like in gm1 one inventory and in gm0 second inventory.
     
  8. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    You will need to serialise the inventory contents if you want to save them to a file.
    PHP:
    $inventoryAsText json_encode($player->getInventory()->getContents());
    to put the items back again, you will need to deserialise it again:
    PHP:
    $inventoryArray json_decode($inventoryAsTexttrue);
    foreach (
    $inventoryArray as $slot => $itemJson) {
      
    $item Item::jsonDeserialize($itemJson);
      
    $inventoryArray[$slot] = $item;
    }
    $player->getInventory()->setContents($inventoryArray);
     
  9. tybikman

    tybikman Silverfish

    Messages:
    22
    [QUOTE = "HimbeersaftLP, сообщение: 71331, участник: 50"] Вам нужно будет сериализовать содержимое инвентаря, если вы хотите сохранить его в файл.
    PHP:
    inventoryAsText json_encode ($ player-> getInventory () -> getContents ());
    [/ 
    PHP]
    чтобы вернуть предметы обратновам нужно будет снова десериализовать их:
    [
    PHP]
    inventoryArray json_decode ($ inventoryAsTexttrue);
    foreach ($ 
    inventoryArray как slot => $ itemJson) {
      $ 
    item Item :: jsonDeserialize ($ itemJson);
      $ 
    inventoryArray [$ slot] = $ item;
    }
    Player-> getInventory () -> setContents ($ inventoryArray);
    [/ 
    PHP] [/ QUOTE]


    Thanks i will try this
     
  10. tybikman

    tybikman Silverfish

    Messages:
    22
    It can not save $inventsur in config.json , file empty
    My code
    PHP:
    if($hot == 265){
                if(
    $player->hasPermission("smarTap.don.use")){
                    if(
    $hot == 265 && $x == && $y == && $z == && $bl == 0){
                        
    $player->sendTip("§9Режимы§7:§4 Ошибка");
                    return 
    true;
                    }
                    if(
    $player->getGamemode() == 0){
                        
    $inventsur json_encode($player->getInventory()->getContents());
                        
    $player->getInventory()->clearAll();
                        
    $player->setGamemode(1);
                        
    $player->sendTip("§9Режимы§7: Вы включили креатив");
                        return 
    true;
                        }
                    if(!(
    $player->getGamemode() == 0)){
                        
    $invent json_encode($player->getInventory()->getContents());
                        
    $player->setGamemode(0);
                        
    $inventoryArray json_decode($inventsurtrue);
                        foreach (
    $inventoryArray as $slot => $itemJson) {
                        
    $item Item::jsonDeserialize($itemJson);
                        
    $inventoryArray[$slot] = $item;
                        }
                        
    $player->getInventory()->setContents($inventoryArray);
                        
    $player->sendTip("§9Режимы§7: Вы отключили креатив");
                    }
                }
     
  11. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    You aren't saving it anywhere, of course the config is empty. I'm surprised that code even ran without crashing the server.
     
  12. tybikman

    tybikman Silverfish

    Messages:
    22
    Okey , it isn't working , i tried this :/
    Pmmp can not find $inventsur
     
  13. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    Try that:
    PHP:
    if($hot == 265){
        if(
    $player->hasPermission("smarTap.don.use")){
            if(
    $hot == 265 && $x == && $y == && $z == && $bl == 0){
                
    $player->sendTip("§9Режимы§7:§4 Ошибка");
                return 
    true;
            }
            
    $inventories $this->getConfig()->get("inventories", []);
            if(
    $player->getGamemode() === Player::SURVIVAL){
                
    $inventsur json_encode($player->getInventory()->getContents());

                
    $inventories[$player->getLowerCaseName()] = $inventsur;
                
    $this->getConfig()->set("inventories"$inventories);
                
    $this->getConfig()->save();

                
    $player->getInventory()->clearAll();
                
    $player->setGamemode(1);
                
    $player->sendTip("§9Режимы§7: Вы включили креатив");
                return 
    true;
            } else {
                
    $player->setGamemode(0);

                if (isset(
    $inventories[$player->getLowerCaseName()])) {
                    
    $inventoryArray json_decode($inventories[$player->getLowerCaseName()], true);

                    foreach (
    $inventoryArray as $slot => $itemJson) {
                        
    $item Item::jsonDeserialize($itemJson);
                        
    $inventoryArray[$slot] = $item;
                    }

                    
    $player->getInventory()->setContents($inventoryArray);

                    unset(
    $inventories[$player->getLowerCaseName()]);
                    
    $this->getConfig()->set("inventories"$inventories);
                    
    $this->getConfig()->save();
                }

                
    $player->sendTip("§9Режимы§7: Вы отключили креатив");
            }
        }
    Note that I haven't tested this and I'm not responsible for the loss over player inventories if my code doesn't work.
     
  14. tybikman

    tybikman Silverfish

    Messages:
    22
    Yeee , it works , thanks so much :)
     
    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.