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

Help with PreProccessCommandEvent.

Discussion in 'Development' started by xXSirButterXx, Jul 25, 2017.

  1. xXSirButterXx

    xXSirButterXx Witch

    Messages:
    52
    GitHub:
    xxsirbutterxx
    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.
     
  2. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    debug code?
    making sure that it actually runs?
    ALSO: consoles will not be reacted to commandpreprocess
     
  3. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    I have the strong feeling this line:
    PHP:
    if(strtolower($msg) == $cmd['CMD']){
    Does not evaluate to true. Could you show your config file?
     
    jojoe77777 likes this.
  4. xXSirButterXx

    xXSirButterXx Witch

    Messages:
    52
    GitHub:
    xxsirbutterxx
    Code:
    ---
    Commands:
    - CMD: f
      Price: 100
    Thats my config
    ...
     
  5. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
    $config = $this->getConfig()_>get("Commands");
    $cmd = $config["CMD"];

    maybe like this?
     
    jasonwynn10 likes this.
  6. xXSirButterXx

    xXSirButterXx Witch

    Messages:
    52
    GitHub:
    xxsirbutterxx
    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;
                    }
                }
            }
                  
        }
     
  7. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    the issue with that is there can only be one command
     
    Levi and Sandertv like this.
  8. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    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"
     
  9. xXSirButterXx

    xXSirButterXx Witch

    Messages:
    52
    GitHub:
    xxsirbutterxx
    How can i make it "f something"? That was the original idea
     
  10. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    explode the msg and compare the base cmd
     
  11. xXSirButterXx

    xXSirButterXx Witch

    Messages:
    52
    GitHub:
    xxsirbutterxx
    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;
                    }
                }
            }
                   
        }
     
  12. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
    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
     
  13. xXSirButterXx

    xXSirButterXx Witch

    Messages:
    52
    GitHub:
    xxsirbutterxx
    Let me rephrase, it works but only for single cmds like "cmd" not "cmd args1 args2"
     
  14. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
    add
    PHP:
     $args explode(' '$event->getMessage()); 
     
  15. Aviv

    Aviv Baby Zombie

    Messages:
    156
    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;
                    }
                }
            }
                 
        }
     
  16. Aviv

    Aviv Baby Zombie

    Messages:
    156
    If that does not work you have to debug the whole way
     
  17. InxGaming

    InxGaming Spider

    Messages:
    14
    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
     
    Last edited: Aug 1, 2017
    xXSirButterXx likes this.
  18. xXSirButterXx

    xXSirButterXx Witch

    Messages:
    52
    GitHub:
    xxsirbutterxx

    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.
     
  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.