=== 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 $sender, Command $command, $currentAlias, array $args, PlayerMoveEvent $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; } }
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.
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) {# Orif(!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); }}
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.
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
Don't cancel every PlayerMoveEvent cancel only if player is tagged. See the bottom code of third post.
Got it so after i got the command working ive to do PHP: $this->freeze($player); Only that and others will start to work?
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.
weird way to do that, 1 useless character added, file size increased, server startup time increased [>1ms] PHP: if(count($args) != 0) But seriously, don't add useless and confusing stuff
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 $s, Command $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");}
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.
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.