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

Solved Command Issue?

Discussion in 'Development' started by Astro, Nov 20, 2018.

  1. Astro

    Astro Slime

    Messages:
    78
    GitHub:
    RealAstro
    This command is not working in-game for some reason, and no error is shown
    PHP:
    class EVCommand extends PluginCommand {

        public function 
    __construct($nameMain $plugin) {
            
    parent::__construct($name$plugin);
            
    $this->setPermission("infernalcore.admin.endervault");
            
    $this->setDescription("Give a player an Ender Vault");
        }

        public function 
    execute(CommandSender $senderstring $commandLabel, array $args) {
            
    $target Server::getInstance()->getPlayer(args[1]);
            if (
    $sender->hasPermission("infernalcore.admin.endervault")) {
                
    $sender->sendMessage("§8[§5!§8]§e Usage: /endervault give (Player)");
                if (
    $target == null) {
                    
    $sender->sendMessage("§8[§5!§8]§c " $target " Cannot be found or is Offline!");
                } else {
                    switch (
    $args[0]) {
                        case 
    "give":
                            if (empty(
    $args[1])) {
                                
    $sender->sendMessage("§8[§5!§8]§c That's not the correct use of the command /endervault, please run /endervault for the correct usage!");
                            } else {
                                
    $ev Item::get(12011);
                                
    $ev->setCustomName(C::RESET C::LIGHT_PURPLE "Ender Vault " C::RESET C::GRAY "(Right-Click)");
                                
    $ev->setNamedTagEntry(new ListTag("ench"));
                                
    $ev->setLore([
                                    
    "§r§5*** §r§dEnder Vault: §7November§5 ***§r",
                                    
    "§r§7Limited Edition - Obtained in §dNovember",
                                    
    "",
                                    
    "§r§dCommon Rewards:",
                                    
    " §r§7- Random Key",
                                    
    " §r§7- Random Command Voucher ",
                                    
    " §r§7- Tier §d§k5§r§7 Money Pouch",
                                    
    " §r§7- Spawner Pouch",
                                    
    " §r§7- Warlock Kit",
                                    
    "",
                                    
    "§r§dDivine Rewards:",
                                    
    " §r§7- Divine Sword",
                                    
    " §r§7- Divine Axe",
                                    
    " §r§7- Divine Tools",
                                    
    " §r§7- Divine Armor",
                                    
    " §r§7- §d§kNecromancer§r §7Rank",
                                    
    " §r§8  -Rank is 5% more common compared",
                                    
    " §r§8  to the chance in Crates!",
                                    
    "",
                                    
    "§r§d(!) Click to open the Ender Vault!"
                                
    ]);
                                
    $target->getInventory()->addItem($ev);
                                
    $target->sendMessage("§8[§5!§8] §eYou have received an Ender Vault!");
                            }
                        break;
                    }
                }
            } else {
                
    $sender->sendMessage("§8[§5!§8]§c You do not have permission to execute this command!");
            }
        }
    }
     
  2. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    Probably because you haven't registered it correctly. Check your plugin.yml and make sure you're setting the command's executor in your plugins main file.
     
  3. Astro

    Astro Slime

    Messages:
    78
    GitHub:
    RealAstro
    PHP:
        public function onEnable() {
            
    $this->getLogger()->info("InfernalCore is now enabled!");
            
    $this->getLogger()->info("Created by RealAstro!");

            
    /** Events **/
            
    $this->getServer()->getPluginManager()->registerEvents(new EVEvents($this), $this);

            
    /** Commands **/
            
    $this->getServer()->getCommandMap()->register("endervault", new EVCommand("endervault"$this));
        }

        public function 
    onDisable() {
            
    $this->getLogger()->info("InfernalCore is now disabled!");
        }
    }
    command is registered in main.php
    and this is my plugin.yml
    Code:
    name: InfernalCore
    main: InfernalCore\Main
    version: 1.0
    api: 3.2.2
    author: ImAstroPvP
    commands:
      endervault:
        description: gives u an endervault
        permission: infernalcore.admin.endervault
     
  4. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    Change
    PHP:
    $this->getServer()->getCommandMap()->register("endervault", new EVCommand("endervault"$this));
    to
    PHP:
    $this->getCommand("endervault")->setExecutor(new EVCommand("endervault"$this));
     
  5. Astro

    Astro Slime

    Messages:
    78
    GitHub:
    RealAstro
    after using

    $this->getCommand("endervault")->setExecutor(new EVCommand("endervault", $this));

    i started getting errors..
     
  6. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    Good, you should now be able to identify what you did wrong ;)

    You've used the 'magic' plugin.yml command registration to route your command to your plugins main classes onCommand method AND registered a new command to the command map directly. You need to choose one method to register the command (for obvious reasons) but the errors you're getting now are most likely related to you calling and overwriting methods in your command class that you shouldn't be.

    Here's an example of how to register commands without the use of black magic from plugin.yml, and here's an example of using the magic of plugin.yml to register commands.

    Personally, I prefer the plugin.yml magic as there's less code I need to worry about and maintain but there are a lot of cases where registering commands without plugin.yml is a better option. At the end of the day you can make either method work and it really comes down to what you prefer :)
     
    Astro likes this.
  7. Astro

    Astro Slime

    Messages:
    78
    GitHub:
    RealAstro
    Thanks! That explanation helped me a lot, now i'm slowly fixing it, some issues but not that big of a deal, thanks again!
     
    xXNiceAssassinlo YT 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.