Help with plug-in I'm making a plug in that runs a command in the console what I did $command = "lagg clear"; $this->getServer()->dispatchCommand (new ConsoleCommandSender(), $command); It says this Call to a member function dispatchCommand() on null
Can you post the entire code on github instead to give us context? The error is telling you that $this->getServer() is null, which means that whatever '$this' happens to be, it doesn't extend a class that has the getServer() method.
why are you using __construct() in the plugin's main class? It can be used, but it is being used improperly in your plugin. remove the function from your plugin and it should be fixed.
I remove the __construct() from the plug in i tested it and it said this Error: "Call to a member function dispatchCommand() on null"
Try this edited version of your plugin: PHP: <?phpnamespace GolemCrate\Dew\Commands;use pocketmine\Player;use pocketmine\Server;use pocketmine\item\Item;use pocketmine\plugin\PluginBase;use pocketmine\utils\TextFormat;use pocketmine\level\Level;use pocketmine\math\Vector3;use pocketmine\inventory\Inventory;use pocketmine\item\enchantment\Enchantment;use pocketmine\command\Command;use pocketmine\command\CommandSender;use pocketmine\command\ConsoleCommandSender;class crate extends PluginBase { // no need to implement CommandExecutor because PluginBase already does// removed constructor function public function onCommand(CommandSender $sender, Command $command, $label, array $args){ $cmd = strtolower($command->getName()); switch($cmd){ case "gcrate": if($sender instanceof Player){ $inv = $sender->getInventory(); $it = Item::get(131,0,1); $it->setCustomName("§bGkey §7don't put in chest or throw"); if($inv->contains($it)){ $level = $sender->getLevel(); $player = $sender->getName(); $inv->removeItem($it); $result = rand(1,3); switch($result){ case 1: $this->getServer()->dispatchCommand(new ConsoleCommandSender(), "lagg clear"); // moved string from variable to function $sender->sendMessage("§b------------"); $sender->sendMessage("§bGcrate > §aYou won 500 Money!"); $sender->sendMessage("§b------------"); break; case 2: $i = Item::get(19,0,1); $i->setCustomName("500 Money"); $e = Item::get(19,0,1); $e->setCustomName("500 Money."); $en = Enchantment::getEnchantment(1); $en->setLevel(4); $i->addEnchantment($en); $e->addEnchantment($en); $inv->addItem($i); $inv->addItem($e); $sender->sendMessage("§b------------"); $sender->sendMessage("§bGcrate > §aYou won 1000 Money!"); $sender->sendMessage("§b------------"); break; case 3: $i = Item::get(276,0,1); $i->setCustomName("§bDiam Sword"); $inv->addItem($i); $sender->sendMessage("§bGcrate > §aYou won Diamond Sword"); break; } }else{ $sender->sendMessage("§e[Gcrate] §fyou need a Gkey!"); } }else{ $sender->sendMessage("§cRun this command on Game!"); } } } }
There's no errors saying in the console while loading the plug in. I tested it again and it said the same error
Show more information, like this: Paste your code into pastebin and give the link to it. Show the error, either an image or text. What did you expect to happen? What really happened?
PHP: <?phpnamespace pmmp\Test;use pocketmine\plugin\PluginBase;use pocketmine\command\Command;use pocketmine\command\CommandSender;use pocketmine\command\ConsoleCommandSender;class Main extends PluginBase { public function onCommand(CommandSender $sender, Command $command, $label, array $args){ switch($command->getName()){ case "gcrate": $this->getServer()->dispatchCommand(new ConsoleCommandSender(), "say hi"); return true; } }} This code just works. Can't make it simpler then this.
They have provided all of the above information. @Amon28 Could you paste a copy of your plugin.yml? I have another idea.
If you are calling the constructor in the class extending PluginBase, then.. PHP: public function __construct(){ parent::__construct(); //your code...} But the constructor in your plugin's class extending PluginBase is useless.
That plugin will not work for PMMP with that plugin.yml You are using a spoon! We do not give support for spoon plugins on these forums.
It references a class "GolemCrate\Dew\Main" as your main class, that class should extend PluginBase. None of your other classes (ie "GolemCrate\Dew\Commands\crate") should extend PluginBase as they are not plugins, just classes in your plugin.
I assumed so, I think they are having PM load the main class and then trying to call new crate($this) or something and assuming crate will just act as a plugin with a working getServer method. If that is not the case, I have no idea what is going wrong.