Hello Guys, i want to code an plugin with Gamemode function (/gm1, /gm0) But my code doesn't function. public function onCommand(CommandSender $sender, Command $cmd, string $label, array $args):bool { if($cmd->getName() === "gm0") { if($sender->hasPermission("gamemode.cmd")) { $sender->setGameMode("survival"); } }return true; }
Okay, so for starters if you're going to use 2 commands in this plugin I recommend using the switch() function instead of doing if(bla, bla). Secondly, don't use (===) use (==) instead you can learn why by looking and understanding here: https://www.geeksforgeeks.org/how-d...ty-triple-equals-comparison-operators-differ/ Thirdly, the reason why your code failed is because you didn't use the right permission (ga memode.cmd -> pocketmine.command.gamemode) and if you did use the right one the plugin would crash anyways because when you use: setGamemode() it expects for you to put an integer as it's first argument (a number) in this case you put a string value as it's first argument. Also here's a tip: Check if the sender is an actual player.
I prefer using if statements because it makes almost no performance difference compared to switch case, and mainly because it is more readable for me. Also, === and == in this case, wouldn't make much of a difference. Command->getName() returns string. Permission doesn't matter since the permission that the person wrote is for the COMMAND he made, not the original command /gamemode! + I like using the edit button(primus), but I guess I was too lazy to move my crosshair to the smaller button!
PHP: public function onCommand(CommandSender $sender, Command $cmd, string $label, array $args):bool { if($cmd->getName() === "gm0") { if($sender->hasPermission("gamemode.cmd")) { $sender->setGameMode(/pocketmine/Server::getGamemodeFromString("survival")); // You can do this! } }return true; } Here's your fixed version!
or if you want to make life easier and not write all of it then just do this. Code: public function onCommand(CommandSender $sender, Command $cmd, string $label, array $args):bool { if($cmd->getName() === "gm0") { if($sender->hasPermission("gamemode.cmd")) { $sender->setGamemode(0); // The 0 Means Survival. 2 Means Adventure and 1 Means Creative and 3 Means Spectator } }return true; }
Unnamed number constants, also known as magic numbers. Your code must be self-explanatory. A person looking at that code in three years will have no idea what the heck zero represents. Also, not so likely, but those ids can change and your hardcoded zero will make your plugin outdated. Although the @minijaham version is a tiny bit longer, it's much more understandable for a person who knows nothing about Minecraft.
this has been always a thing in Minecraft. You can even do /gamemode 0 and still set you to survival. They are not really “magic numbers”.
Now it crash with this error: Code: Error: Cannot redeclare GameMode\Main::onCommand() File: plugins/GameMode/src/gamemode/main Line: 29 Type: E_COMPILE_ERROR Here is line 29: public function onCommand(CommandSender $sender, Command $cmd, string $label, array $args):bool
All errors are pretty self-explanatory! "cannot redeclare function onCommand()" You've probably wrote it twice
Now it says this again: Code: [12:27:49] [Server thread/INFO]: Loading GameMode vBeta 1.0 [12:27:49] [Server thread/ERROR]: Main class for plugin GameMode not found [12:27:49] [Server thread/CRITICAL]: Could not load plugin 'GameMode'
No, they are not. php is case sensitive. Your main file: PHP: namespace GameMode;...class Main extends...