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

[SOLVED] My Args issue

Discussion in 'Development' started by InspectorGadget, Nov 28, 2016.

  1. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    ===
    Hi! I am pretty much stuck in this function for ARGS. The command should be
    Command:
    /start <playername>
    Here's the code, if you could help thats great!
    PHP:
     public function execute(CommandSender $senderCommand $command$currentAlias, array $argsPlayerMoveEvent $ev){
     switch(
    strtolower($command->getName())){
     case 
    "start":
    if(
    count($args) == 0) {
     
    $sender->sendMessage("/start ");
     
    $player $sender->getServer()->getPlayer($args[0]);
     if(
    $player instanceof Player) {
     
    $ev->setCancelled(true); //For FREEZE
     
    }
     }
     return;
     }
     return 
    true;
     }
     }
     
    Last edited by a moderator: Nov 28, 2016
  2. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    It should be
    PHP:
    $player $this->getServer()->getPlayer($args[0]);
    And also
    PHP:
    if(!count($args) == 0)
    because you want to check if there is not no argument.
     
    Last edited: Nov 28, 2016
    Primus and Sandertv like this.
  3. Primus

    Primus Zombie Pigman

    Messages:
    749
    It's easy fix, basically your problem starts here
    PHP:
    # PHP:
    if(count($args) == 0) {
    # English: If arguments are empty
    And after that check you are trying to get value from empty array. This is how to fix it
    PHP:
    if(count($args) === 1) {
    # Or
    if(!empty($args)) }
    I guess you're trying to freeze player when his tagged by /command
    PHP:
    public function freeze(Player $player) {
       
    $this->freeze[$player->getName()] = true;
    }

    public function 
    onMove(PlayerMoveEvent $event) {
       if(isset(
    $this->freeze[$event->getPlayer()->getName()])) {
          
    $event->setCancelled(true);
       }
    }
     
    Last edited: Nov 28, 2016
  4. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    so will
    PHP:
    if(!empty($args)) {
    work with /start <playername>?
    Is that what i ONLY need to change?
     
  5. Primus

    Primus Zombie Pigman

    Messages:
    749
    Yes that would work. And no, that's not the only thing. I don't know your code structure but I assume you're trying to handle commands in wrong way. Also the freeze thing will work only once as new $ev is created as player moves.
     
  6. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Ok so back to the Event thing
    i cant take shortcut?
    so i have to do the normal way which defines PlayerMoveEvent
    if i do that, the event will freeze all players as soon as they move
     
  7. Primus

    Primus Zombie Pigman

    Messages:
    749
    Don't cancel every PlayerMoveEvent cancel only if player is tagged. See the bottom code of third post.
     
    HimbeersaftLP likes this.
  8. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Got it
    so after i got the command working ive to do
    PHP:
    $this->freeze($player);
    Only that and others will start to work?
     
  9. Primus

    Primus Zombie Pigman

    Messages:
    749
    Why don't you give it a run and see how it goes?
     
  10. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    I will, if anytning goes wrong ill come back. Thank you for your help! @HimbeersaftLP and @Primus
     
    HimbeersaftLP and Primus like this.
  11. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    Remember print_r() is your friend
     
  12. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Can u make a sample code with print_r? Im already exhausted with IRL work stuffs
     
  13. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Could you please just not abuse italics?
    This is wrong. You should use != instead.
    I would still prefer using isset($args[0]), as it is more straightforward in the code showing what exactly has to be used.
    How does your work IRL have anything to do with us? If you are lazy, don't make a plugin; request one instead.
    He is not telling you that you need to use it in your plugin, but if you want to show some values in your plugin for debugging, it would come handy.
     
  14. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    something like print_r($args) print_r($this->freeze) if things didnt work out how you expected
     
  15. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Ok, noted
     
  16. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    weird way to do that, 1 useless character added, file size increased, server startup time increased :p [>1ms]
    PHP:
    if(count($args) != 0)
    But seriously, don't add useless and confusing stuff
     
    HimbeersaftLP likes this.
  17. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    I don't think it even works correctly at all. The precedence of ! is higher than ==. Therefore !count($args) is resolved first. It will end up being "whether the array is empty". And since you compare this boolean against 0 with an equal (not identical) operator, 0 is resolved to false, so it becomes "whether the array is empty" equals "false". In simple words it is "if the array is not empty", which is originally what he wanted. So it works. But not correctly. For example, putting 1 or 2 at the place of 0 have no difference; they will both resolve to boolean true.
    This is so much redundant load! If you love hacking type juggling so much, $args itself already can resolve into the boolean you want. But if you to do things properly, you should properly compare the count.

    Conventionally we use isset($args[0]) instead (this is the fastest way in PHP, although the difference is still extremely negligible against using count($args) != 0), since we are going to use $args[0] later. To check for the existence of greater arguments, we can also use isset($args[1]), isset($args[2]), etc. You can even use the null coalescence operator in PHP 7 to provide default values like this:

    PHP:
    // for a command with usage /mute <username> [time = 5] [unit = minute]
    public function onCommand(CommandSender $sCommand $c$l, array $args) {
        if(!isset(
    $args[0])) return false// send usage if required argument is missing
        
    $this->mute($args[0], intval($args[1] ?? 5), $args[2] ?? "minute");
    }
     
    HimbeersaftLP and MyNET like this.
  18. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    Just to add to what @SOFe said, if you make use of like 5 arguments in a row, instead of typing isset($args[0]) && isset($args[1])... it is simply enough to type isset($args[4]), (as long as you don't use things like unset keys) so you can have a cleaner code.
     
  19. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    It is actually also convenient to use array_shift(). For example, this command:
    Code:
    /fill <block type> [:<damage>] [-hollow <thickness>] [-temporary] <log message...>
    
    Example usage:
    Code:
    /fill wool :15 -hollow 3 -temporary This is the message to be written in the log file
    /fill glass -temporary Another message
    
    There is only one required parameter: <block type>
    So I would handle it like this when manipulating a simple array:
    PHP:
    if(!isset($args[0])) return false// missing required parameter
    $blockType array_shift($args);

    if(isset(
    $args[0]) and $args[0]{0} === ":") { // if argument starts with colon, it must be damage.
        
    $damage intval(substr(array_shift($args), 1));
    } else {
        
    $damage 0;
    }

    $hollow = -1;
    if(isset(
    $args[0]) and $args[0] === "-hollow") {
        
    array_shift($args);
        
    $hollow intval(array_shift($args));
    }

    $temp false;
    if(isset(
    $args[0]) and $args[0] === "-temporary") {
        
    array_shift($args);
        
    $temp true;
    }

    $logMessage implode(" "$args); // since we have already shifted away the former arguments, we can join the whole array to reduce it back to a whole string again.
    This message is also going to be copied in the OP for clarification.
     
    Last edited: Nov 29, 2016
    HimbeersaftLP and Sandertv like this.
  20. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    Oh, sorry, stupid me was thinking that it says isset xD
     
  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.