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

Solved Coding Plugin

Discussion in 'Plugin Help' started by GeistFan, Jul 20, 2021.

  1. GeistFan

    GeistFan Slime

    Messages:
    86
    GitHub:
    GeistFan
    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;
    }
     
  2. xHearts

    xHearts Creeper

    Messages:
    2
    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.
     
  3. Primus

    Primus Zombie Pigman

    Messages:
    749
    You are wrong for the comparison operators. It's the other way around.
     
    minijaham likes this.
  4. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    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! :p
     
    Agent and Primus like this.
  5. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    PHP:
    public function onCommand(CommandSender $senderCommand $cmdstring $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!
     
    Agent and Axon like this.
  6. WEATHERCRAFTYT1

    WEATHERCRAFTYT1 Baby Zombie

    Messages:
    121
    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;
        }
     
  7. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    Also that. I was using that method because he was using a string!
     
    Agent and Primus like this.
  8. Primus

    Primus Zombie Pigman

    Messages:
    749
    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.
     
    Axon and minijaham like this.
  9. WEATHERCRAFTYT1

    WEATHERCRAFTYT1 Baby Zombie

    Messages:
    121
    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”.
     
  10. GeistFan

    GeistFan Slime

    Messages:
    86
    GitHub:
    GeistFan
    Thats what i'm think is the best
    And I know that /gamemode 0=Survival
     
    WEATHERCRAFTYT1 likes this.
  11. Primus

    Primus Zombie Pigman

    Messages:
    749
    You know.
     
    minijaham likes this.
  12. GeistFan

    GeistFan Slime

    Messages:
    86
    GitHub:
    GeistFan
    Know it says Main class not found
     

    Attached Files:

  13. GeistFan

    GeistFan Slime

    Messages:
    86
    GitHub:
    GeistFan
    I found the error
     
  14. GeistFan

    GeistFan Slime

    Messages:
    86
    GitHub:
    GeistFan
    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
     
  15. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    All errors are pretty self-explanatory!
    "cannot redeclare function onCommand()"
    You've probably wrote it twice
     
    Agent likes this.
  16. GeistFan

    GeistFan Slime

    Messages:
    86
    GitHub:
    GeistFan
    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' 
     

    Attached Files:

  17. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    Main class not found. How many times do we need to tell you how to fix this?
     
    Agent likes this.
  18. GeistFan

    GeistFan Slime

    Messages:
    86
    GitHub:
    GeistFan
    See the plugin zip its all right but anyway it shows this error
     
  19. GeistFan

    GeistFan Slime

    Messages:
    86
    GitHub:
    GeistFan
    In plugin.yml
    Main: gamemode\main
    in folder:
    /Gamemode/src/gamemode/main.php
     
  20. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    No, they are not. php is case sensitive.

    Your main file:
    PHP:
    namespace GameMode;
    ...
    class 
    Main extends...
     
    Agent likes this.
  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.