So im working on a plugin that charges players for using a specific set of commands each time they use it. Its not working and i need help on what im doing wrong. PHP: public function onPCommand(PlayerCommandPreprocessEvent $e){ $player = $e->getPlayer(); $msg = $e->getMessage(); $money = EconomyAPI::getInstance()->myMoney($player); $cfg = $this->getConfig()->getAll(); foreach($cfg["Commands"] as $cmd){ if(!$player->hasPermission("cc.charge.avoid")){ if(strtolower($msg) == $cmd['CMD']){ if(EconomyAPI::getInstance()->myMoney($player) >= $cmd['Price']){ $player->sendMessage(self::PREFIX . "You have been charged " . C::YELLOW . $cmd["Price"] ."$" . C::GREEN . "For using " . $cmd["CMD"]); EconomyAPI::getInstance()->reduceMoney($player, $cmd['Price']); }else{ $player->sendMessage(C::PREFIX . "You dont have enough money for this command."); $e->setCancelled(true); } }else{ return true; } } } } I need help on making this work. I cant seem to figure out why its not working.
debug code? making sure that it actually runs? ALSO: consoles will not be reacted to commandpreprocess
I have the strong feeling this line: PHP: if(strtolower($msg) == $cmd['CMD']){ Does not evaluate to true. Could you show your config file?
Like this? PHP: public function onPCommand(PlayerCommandPreprocessEvent $e){ $player = $e->getPlayer(); $msg = $e->getMessage(); $money = EconomyAPI::getInstance()->myMoney($player); $cfg = $this->getConfig()->get("Commands"); $cmd = $cfg["CMD"]; $price = $cfg["Price"]; if(!$player->hasPermission("cc.charge.avoid")){ if(strtolower($msg) == $this->cmd){ if(EconomyAPI::getInstance()->myMoney($player) >= $this->price){ $player->sendMessage(self::PREFIX . "You have been charged " . C::YELLOW . $this->price ."$" . C::GREEN . "For using " . $this->cmd); EconomyAPI::getInstance()->reduceMoney($player, $this->price); }else{ $player->sendMessage(C::PREFIX . "You dont have enough money for this command."); $e->setCancelled(true); } }else{ return true; } } } }
i would add echo code and echo comparison like echo msg and echo this->cmd also make sure you are typing the correct thing like "/f" not "/f something"
Ive exploded the msg but it's still not working. PHP: public function onPCommand(PlayerCommandPreprocessEvent $e){ $player = $e->getPlayer(); $msg = explode(" ", $e->getMessage()); $command = substr($msg), 1); $cfg = $this->getConfig()->getAll(); foreach($cfg["Commands"] as $c){ if(!$player->hasPermission("cc.charge.avoid")){ if(strtolower($command) == $c['CMD']){ if(EconomyAPI::getInstance()->myMoney($player) >= $c['Price']){ $player->sendMessage(self::PREFIX . "You have been charged " . C::YELLOW . $c["Price"] ."$" . C::GREEN . "For using " . $c["CMD"]); EconomyAPI::getInstance()->reduceMoney($player, $c['Price']); }else{ $player->sendMessage(C::PREFIX . "You dont have enough money for this command."); $e->setCancelled(true); } }else{ return true; } } } }
PHP: public function onPCommand(PlayerCommandPreprocessEvent $e){ $player = $e->getPlayer(); $msg = explode(" ", $e->getMessage()); $command = strtolower(explode(' ', $event->getMessage())[0]); $cfg = $this->getConfig()->getAll(); foreach($cfg["Commands"] as $c){ if(!$player->hasPermission("cc.charge.avoid")){ if($command == $c['CMD']){ if(EconomyAPI::getInstance()->myMoney($player) >= $c['Price']){ $player->sendMessage(self::PREFIX . "You have been charged " . C::YELLOW . $c["Price"] ."$" . C::GREEN . "For using " . $c["CMD"]); EconomyAPI::getInstance()->reduceMoney($player, $c['Price']); }else{ $player->sendMessage(C::PREFIX . "You dont have enough money for this command."); $e->setCancelled(true); } }else{ return true; } } } } try this
PHP: public function onPCommand(PlayerCommandPreprocessEvent $e){ $player = $e->getPlayer(); $msg = $e->getMessage(); $money = EconomyAPI::getInstance()->myMoney($player); $cfg = $this->getConfig()->getAll(); foreach($cfg["Commands"] as $cmd){ if(!$player->hasPermission("cc.charge.avoid")){ if(strtolower(explode(" ", $msg)[0]) === $cmd['CMD']){ if(EconomyAPI::getInstance()->myMoney($player) >= $cmd['Price']){ $player->sendMessage(self::PREFIX . "You have been charged " . C::YELLOW . $cmd["Price"] ."$" . C::GREEN . "For using " . $cmd["CMD"]); EconomyAPI::getInstance()->reduceMoney($player, $cmd['Price']); }else{ $player->sendMessage(C::PREFIX . "You dont have enough money for this command."); $e->setCancelled(true); } }else{ return true; } } } }
Okay, I agree that this is not much of a problem but why checking like this - PHP: if(EconomyAPI::getInstance()->myMoney($player) >= $cmd['Price']) when you already got $money set to it? PHP: if($money >= $cmd['Price']) Otherwise what is the point of $money? Also what do you mean by "it's not working"? What did you expect? What actually happend? Was there an error or something? Did you put some effort on understanding why "it doesn't work"? because no offense but it looks like you didn't
Let me explain myself here. The plugin itself is supposed to charge players for using specific commands.The config is supposed to allow multiple commands. Currently it charges players for only the first command on the list and not others, but it doesnt allow specific commands like its supposed to. If you add "/me test" it wont charge the player for using "/me test" it only works if you set it as "/me" in config, but then it will charge them for any usage of /me.