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 $sender, Command $cmd, string $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", 40, 100, 40); $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", 40, 100, 40); $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; }
The reason I have $sender->sendMessage("".$one->getId()); is to check what it stores $one as in-game.
Add another class property just like you did with $this->vanish and store the item there. (also please don't hardcode item ids)
I haven't tested this, but it should work: (I added comments to the lines I changed) PHP: public function onCommand(CommandSender $sender, Command $cmd, string $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", 40, 100, 40); $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", 40, 100, 40); $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; }
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