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

How do I use the stored variable in the else function?

Discussion in 'Development' started by NickteeChunky, Oct 6, 2019.

  1. NickteeChunky

    NickteeChunky Witch

    Messages:
    64
    GitHub:
    nickteechunky
    So I'm making a whole staff mode system and I'm trying to make it so when a person runs the command it stores their inventory's contents and replaces it with new ones. Although when they disable it their contents are brought back.

    Here, how do I define $one in the } else { function. In the if function $one is stored as the item you had in your first slot then it enables vanish itself and replaces that item. When vanish is disabled how do I bring $one back because it's either undefined or equals the item I currently have when vanish is enabled.

    PHP:
        public function onCommand(CommandSender $senderCommand $cmdstring $label, array $args): bool{
            
    $name $sender->getName();
            if(
    $cmd->getName() == "vanish"){
                if(
    $sender instanceof Player){
                    if(
    $sender->hasPermission("staffmode.access")){
                        if(
    $this->vanish[$name] == false){
                            
    $one $sender->getInventory()->getItem(0);
                            
    $sender->sendMessage("".$one->getId());
                            
    $this->vanish[$name] = true;
                            
    $sender->sendMessage(self::PREFIX.C::GREEN."§eYou're now vanished. Players shall not be able to see you, don't abuse!");
                            
    $sender->addEffect(new EffectInstance(Effect::getEffect(Effect::NIGHT_VISION), (99999999*20), (1), (false)));
                            
    $sender->getPlayer()->addTitle("§eVanish Mode""§a§lENABLED"4010040);
                            
    $this->getServer()->broadcastMessage(C::GREEN "§cParty is over people! §e$name §chas left the game.");
                            
    $oi Item::get(345);
                            
    $oi->setDamage(1);
                            
    $oi->setCount(1);
                            
    $one $sender->getInventory()->getItem(0);
                            
    $sender->getInventory()->setItem(0$oi);
                        } else {
                            
    $this->vanish[$name] = false;
                            
    $oi Item::get(345);
                            
    $oi->setDamage(1);
                            
    $oi->setCount(1);
                            if(
    $sender->getInventory()->getItem(0) == $oi){
                            
    $sender->getInventory()->setItem(0$one);
                            }
                            foreach(
    $this->getServer()->getOnlinePlayers() as $players){
                                
    $players->showPlayer($sender);
                            }
                            
    $sender->sendMessage(self::PREFIX C::RED "§eYou are no longer vanished. Players shall be able to see you now.");
                            
    $sender->removeEffect(Effect::NIGHT_VISION);
                            
    $sender->getPlayer()->addTitle("§eVanish Mode""§c§lDISABLED"4010040);
                            
    $name $sender->getName();
                            
    $this->getServer()->broadcastMessage(C::RED "§aIs it a bird? Is it a plane? Nope! It's §l§e".$name."§a!");
                        }
                    }
                }
            }
            return 
    true;
        }
     
  2. NickteeChunky

    NickteeChunky Witch

    Messages:
    64
    GitHub:
    nickteechunky
    The reason I have $sender->sendMessage("".$one->getId()); is to check what it stores $one as in-game.
     
  3. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    Add another class property just like you did with $this->vanish and store the item there. (also please don't hardcode item ids)
     
  4. NickteeChunky

    NickteeChunky Witch

    Messages:
    64
    GitHub:
    nickteechunky
    How? May you please give me an example?
     
  5. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    I haven't tested this, but it should work: (I added comments to the lines I changed)
    PHP:
        public function onCommand(CommandSender $senderCommand $cmdstring $label, array $args): bool{
            
    $name $sender->getName();
            if(
    $cmd->getName() === "vanish"){ // Use === instead of ==
                
    if($sender instanceof Player){
                    if(
    $sender->hasPermission("staffmode.access")){
                        if(
    $this->vanish[$name] === false){ // Use === instead of ==
                            
    $one $sender->getInventory()->getItem(0);
                            
    $sender->sendMessage("".$one->getId());
                            
    $this->vanish[$name] = $one// Save into class property
                            
    $sender->sendMessage(self::PREFIX.C::GREEN."§eYou're now vanished. Players shall not be able to see you, don't abuse!");
                            
    $sender->addEffect(new EffectInstance(Effect::getEffect(Effect::NIGHT_VISION), (99999999*20), (1), (false)));
                            
    $sender->getPlayer()->addTitle("§eVanish Mode""§a§lENABLED"4010040);
                            
    $this->getServer()->broadcastMessage(C::GREEN "§cParty is over people! §e$name §chas left the game.");
                            
    $oi Item::get(Item::COMPASS); // Use constant instead of hardcoded item ID
                            
    $oi->setDamage(1);
                            
    $oi->setCount(1);
                            
    $sender->getInventory()->setItem(0$oi);
                        } else {
                            
    $one $this->vanish[$name]; // Retrieve item from class property
                            
    $oi Item::get(Item::COMPASS);
                            
    $oi->setDamage(1);
                            
    $oi->setCount(1);
                            if(
    $sender->getInventory()->getItem(0)->equalsExact($oi)){ // use Item->equalsExact for item comparison
                                
    $sender->getInventory()->setItem(0$one);
                            }
                            foreach(
    $this->getServer()->getOnlinePlayers() as $players){
                                
    $players->showPlayer($sender);
                            }
                            
    $sender->sendMessage(self::PREFIX C::RED "§eYou are no longer vanished. Players shall be able to see you now.");
                            
    $sender->removeEffect(Effect::NIGHT_VISION);
                            
    $sender->getPlayer()->addTitle("§eVanish Mode""§c§lDISABLED"4010040);
                            
    $name $sender->getName();
                            
    $this->getServer()->broadcastMessage(C::RED "§aIs it a bird? Is it a plane? Nope! It's §l§e".$name."§a!");
                            
    $this->vanish[$name] = false// Make it false again
                        
    }
                    }
                }
            }
            return 
    true;
        }
     
    NickteeChunky likes this.
  6. NickteeChunky

    NickteeChunky Witch

    Messages:
    64
    GitHub:
    nickteechunky
    Thank you so much for your detailed description to every line and effort into helping me. You've helped me further understand how class properties work and how to use them. It is working now, you're a live savior. <3
     
    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.