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

Is it possible to add float var in function onCommand ?

Discussion in 'Development' started by Oniii-Chan, Jan 24, 2018.

  1. Oniii-Chan

    Oniii-Chan Witch

    Messages:
    66
    GitHub:
    Vincent583
    Hi,
    I want to add float var in "public function onCommand" . Is it possible ?
    Code:
    <?php
    
    namespace CustomDamageItem;
    
    use pocketmine\plugin\PluginBase;
    use pocketmine\event\Listener;
    use pocketmine\command\CommandSender;
    use pocketmine\command\Command;
    use pocketmine\event\entity\EntityDamageEvent;
    use pocketmine\event\entity\EntityDamageByEntityEvent;
    use pocketmine\event\player\PlayerItemHeldEvent;
    use pocketmine\Player;
    use pocketmine\Server;
    use pocketmine\item\Item;
    use pocketmine\entity\Entity;
    use pocketmine\utils\TextFormat;
    
    class Main extends PluginBase implements Listener
    {
    
        public function onEnable()
        {
            $this->getServer()->getPluginManager()->registerEvents($this, $this);
        }
    
        /**
         * @param float             $customDamage
         * @return int
         **/
    
    
    
        public function onCommand($customDamage, CommandSender $sender, string $labels, Command $cmd, array $args): bool
        {
            if (!$sender instanceof Player()) {
                $sender->sendMessage(TextFormat::RED . "Cette commande doit être utiliser en jeu.");
                return true;
            }
            if (!isset($args[0])) {
                if ($sender->hasPermission("cid.command")) {
                    $sender->sendMessage(TextFormat::RED . "Usage: /cid set|reset");
                    return false;
                }
            }
            if ($args[0] === "set") {
                if ($sender->hasPermission("cid.command") && $sender->hasPermission("cid.command.set")) {
                    $sender->sendMessage("Usage: /cid set <id>");
                    return true;
                }
            }
            if ($args[0] === "reset") {
                if ($sender->hasPermission("cid.command") && $sender->hasPermission("cid.command.reset")) {
                    $sender->sendMessage("Usage: /cid reset <id>");
                }
            }
    
            if ($args[1] === "352" || $args[1] === "bone") {
                if ($sender->hasPermission("cid.command")) {
                    if ($sender->hasPermission("cid.command.set") && $sender->hasPermission("cid.command.set.id")) {
                        $sender->sendMessage("Usage: /cid set $args[1] <damage>");
                    } else {
                        if ($sender->hasPermission("cid.command.reset") && $sender->hasPermission("cid.command.reset.id")) {
                            $event->setDamage(0.25);
                        }
                    }
                }
            }
            if ($args[2] === $customDamage) {
                if (is_numeric($customDamage)) {
                    if ($sender->hasPermission("cid.command") && $sender->hasPermission("cid.command.set") && $sender->hasPermission("cid.command.id") && $sender->hasPermission("cid.command.id.damage")) {
                        $event->setDamage($customDamage);
                    }
                }
                if (!is_numeric($customDamage)) {
                    $sender->sendMessage("Les points d'attaques doivent être en valeur numérique !");
                }
            }
        }
    
    
            public
            function onItemHeld(PlayerItemHeldEvent $event)
            {
    
                if ($event->getItem()->getId() == 352) {
                    $event->getPlayer()->sendPopup("Épée en Améthyste");
                }
            }
    
            public function onEntityDamageByEntity(EntityDamageEvent $event)
            {
                if ($event instanceof EntityDamageByEntityEvent) {
                    $entity = $event->getEntity();
                    $player = $event->getDamager();
                    if ($entity instanceof Player && $player instanceof Player) {
                        $item = $player->getInventory()->getItemInHand()->getID();
                        if ($item == 352) {
                            $event->setDamage(6.0);
                        }
                    }
                }
            }
        }
        
     
  2. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    well, in php we cannot declare real datatypes on variables.
    Assuming you want to have a "float" command argument all you need is to typecast the string argument to a float
    $floatArg = (float) $args[x]
     
  3. Oniii-Chan

    Oniii-Chan Witch

    Messages:
    66
    GitHub:
    Vincent583
    Where I pût $floatArg = (float) $args[x] ?
     
  4. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    And where do you want a float
     
    DaPigGuy likes this.
  5. Oniii-Chan

    Oniii-Chan Witch

    Messages:
    66
    GitHub:
    Vincent583
    I want a float on $args[2]
     
  6. Oniii-Chan

    Oniii-Chan Witch

    Messages:
    66
    GitHub:
    Vincent583
    PHP:
    if(args[2] === $customDamage){
               if(
    is_numeric($customDamage)){
                   if(
    $sender->hasPermission("cid.command") && $sender->hasPermission("cid.command.set") && $sender->hasPermission("cid.command.id") && $sender->hasPermission("cid.command.id.damage")){
                       
    $event->setDamage($customDamage);
                   }
               }
               if(!
    is_numeric($customDamage)){
                   
    $sender->sendMessage("Les points d'attaque doivent être en valeur numérique !")
               }
           }
        
    I want to do "if args 2 is a float , check if player as some permission and set damage when user input float value"
     
  7. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    You can use is_numeric to check, and cast to float to use it.
     
    jasonwynn10 likes this.
  8. Oniii-Chan

    Oniii-Chan Witch

    Messages:
    66
    GitHub:
    Vincent583
    Can you fix my code because I don't know how to do
     
    Last edited: Jan 26, 2018
  9. Marabou

    Marabou Baby Zombie

    Messages:
    137
    GitHub:
    wiligangster
  10. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
  11. Marabou

    Marabou Baby Zombie

    Messages:
    137
    GitHub:
    wiligangster
    Hmm sorry I don't understand correctly the problem
     
  12. Kenn Fatt

    Kenn Fatt Slime

    Messages:
    82
    GitHub:
    kennfatt
    PHP:

    // inside ur code braces
    if (is_numeric($args[2) === false) {
     
    // invalid input
     
    return false;
    }

    $floatInput = (float) $args[2]; // now you can cast it as float.

     
  13. Oniii-Chan

    Oniii-Chan Witch

    Messages:
    66
    GitHub:
    Vincent583
    Thanks
     
  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.