hi, im working on a plugin that spawn chest with items in random location at spawn, when i start the server, it doesn't output any errors but when i use /chest, it said unknow command PHP: <?phpnamespace envoys;use pocketmine\utils\TextFormat;use pocketmine\Player;use pocketmine\inventory\Inventory;use pocketmine\block\Block;use pocketmine\level\Level;use pocketmine\math\Vector3;use pocketmine\block\Chest;use pocketmine\plugin\PluginBase;use pocketmine\event\Listener;use envoys\Envoys;use pocketmine\command\Command;use pocketmine\command\CommandSender;use pocketmine\command\CommandExecutor;use pocketmine\event\inventory\InventoryOpenEvent;use pocketmine\event\inventory\InventoryCloseEvent;use pocketmine\Server;use pocketmine\utils\Config;class ChestSpawn extends PluginBase implements Listener{ public function onEnable() { @mkdir($this->getDataFolder()); $this->config = (new Config($this->getDataFolder() . "config.yml", Config::YAML, array( "apply-for-world" => "FACTIONS", "items" => array("264") ))); $this->getServer()->getPluginManager()->registerEvents($this, $this); $this->getLogger()->info("All has loaded"); } public function onDisable() { $this->getLogger()->info("Stopped"); $this->config->save(); } public function onCommand(CommandSender $s, Command $command, $label, array $args) { $world = $this->config->get("apply-for-world"); $x = mt_rand(1673,1770); $z = mt_rand(85,-20); $cmd = strtolower($command->getName()); switch ($cmd) { case "chest": if($sender->hasPermission("envoys.chest")) { for($y = 11; $y == 11; $y++) { $b1_low = $world->getBlock(new Vector3($x-1, $y, $z));//block low left $b2_low = $world->getBlock(new Vector3($x, $y, $z+1));//block low right $b1_high = $world->getBlock(new Vector3($x+1, $y+1, $z));//block high left $b2_high = $world->getBlock(new Vector3($x, $y+1, $z));//block high top $b3_high = $world->getBlock(new Vector3($x, $y+1, $z+1)); //block high right $currect = $world->getBlock(new Vector3($x, $y, $z)); //currect block $setcurrectblock = new Vector3($x, $y, $z); if(($b1_low instanceof Soild) and ($b2_low instanceof Soild) and ($b1_high instanceof Soild) and ($b2_high instanceof Soild) and ($b3_high instanceof Soild) and ($currect instanceof Soild)) continute; $world->setBlock($setcurrectblock, Block::get(54,0)); $chest = $world->getTile($setcurrectblock); $slot = mt_rand(0,27); foreach($this->config->get("items") as $item) { $chest->getInventory->setItem($slot, $item); } $s->sendMessage(TextFormat::GREEN ."New Chest spawn at ". $setcurrectblock); $this->getServer()->broadcastMessage(TextFormat::BLUE ."New Chest Spawn at Lobby, go look for it!"); } } else { $sender->sendMessage(TextFormat::RED ."You don't have permission to use this command"); } } } public function onChestOpen(InventoryOpenEvent $ev) { $player = $ev->getPlayer(); $name = $player->getName(); $inv = $ev->getInventory(); $chestinv = $chest->getInventory(); if($inv == $chestinv) { $this->getServer()->broadcastMessage(TextFormat::GREEN ."Congrat!, ". $name ." has found a LootChest!"); $this->getServer()->broadcastMessage("Chest Coords: ". $setcurrectblock); } } public function onChestClose(InventoryOpenEvent $ev, Level $level) { $player = $ev->getPlayer(); $inv = $ev->getInventory(); $chestinv = $chest->getInventory(); if($inv == $chestinv) { $level->setBlock($setcurrectblock, Block::get(Block::AIR)); } } }
You should change switch ($cmd) { to PHP: switch(strtolower($command->getName())){ or PHP: switch($command->getName()){
0 : Make your code more readable, please 1 : CommandSender $s but, instead, $sender used 2 : for($y = 11; $y == 11; $y++), why is this for loop even there? ... If you only want to do this for 1 value of $y, just define it outside -> $y = 11; 3: $cmd is defined ($cmd = strtolower($command->getName())) so there is nothing to edit in the switch statement Survingo 4 : $command->getName() returns the command name based on the plugin.yml, not the player input -> if the command name is lower-case in the plugin.yml then strtolower($command->getName()) is useless. 5: you do not need to use switch($var) statements for just 1-2 conditions. it is used for many conditions -> instead, use if($cmd == "chest") 6: it is Solid, not Soild / spelling mistake 7: it is continue not contitute / spelling mistake 8: when a match is found, and the job is done, it's time for a break. where is the break statement after a case? 9: $world->setBlock(params...) is wrong, $world = $this->config->get("apply-for-world") which returns a string. If you want to get the level object by that string (name) then do this -> $level = $this->getServer()->getLevelByName($world) and then $level->setBlock(params...) 10: foreach(Item[] as Item){} $this->config->get("items") doesn't return array of item objects, but array of item ids -> to get the items do this : PHP: foreach($this->config->get("items") as $item_id){ $item = Item::get($item_id) $chest->getInventory()->setItem($slot,$item)} 11: use pocketmine\block\Solid;
Ok, now the plugin seems to work after i edit like what you said and remove onChestOpen and onChestClose function, so it couldn't announce who found that chest and remove it after it closed, can you teach me how to do that?
Idk but I thinm its wrong change $z to mt_rand (-20, 85) because I think that the lowest value must be first