I need help making and fixing my plugin. I'm trying to add a config to it. Code: PHP: <?phpnamespace pmmp\SenderPE;use pocketmine\command\Command;use pocketmine\command\CommandSender;use pocketmine\command\CommandExecutor;use pocketmine\event\Listener;use pocketmine\Player;use pocketmine\plugin\PluginBase;use pocketmine\Server;use pocketmine\utils\TextFormat;use pocketmine\utils\Config;use pocketmine\permission\ServerOperator;use pocketmine\event\player\PlayerChatEvent;class Main extends PluginBase implements Listener { public function onEnable() { $this->yml = new Config($this->getDataFolder()."config.yml", Config::YAML); $this->getServer()->getPluginManager()->registerEvents($this, $this); } public function onDisable() { $this->getServer()->getPluginManager()->registerEvents($this, $this); } public function onCommand(CommandSender $sender, Command $command, $label, array $args){ switch($command->getName()){ case "sendnews": if($sender->hasPermission("senderpe.news")){ if(isset($args[0])) { $msg = $this->yml->get("Broadcast"); $sender->sendMessage("Sent the message " . $msg); $sender->getLevel()->getServer()->broadcastMessage("§3§l[News]§r§3 " . implode(" ", $args)); return true; } else { $sender->sendMessage("§cYou cannot send an empty message!"); } } else { $sender->sendMessage("§cYou need OP to do this!"); return true; } } }} Error: Code: Unhandled exception executing command 'sendnews Test' in sendnews: Call to a member function get() on null Error: "Call to a member function get() on null" (EXCEPTION) in "/SenderPE/src/pmmp/SenderPE/Main" at line 25
First this is no use The plugin has already disabled why would you register events? Second You have to define what $this->yml is PHP: public $yml; Also, did you create your config.yml ?
That means the config file wasn't saved. Just add PHP: $this->saveDefaultConfig(); before anything inside the onEnable() scope.
Is this all correct? PHP: <?phpnamespace pmmp\SenderPE;use pocketmine\command\Command;use pocketmine\command\CommandSender;use pocketmine\command\CommandExecutor;use pocketmine\event\Listener;use pocketmine\Player;use pocketmine\plugin\PluginBase;use pocketmine\Server;use pocketmine\utils\TextFormat;use pocketmine\utils\Config;use pocketmine\permission\ServerOperator;use pocketmine\event\player\PlayerChatEvent;class Main extends PluginBase implements Listener { public function onEnable() { $this->saveDefaultConfig(); public $yml; $this->yml = new Config($this->getDataFolder()."config.yml", Config::YAML); $this->getServer()->getPluginManager()->registerEvents($this, $this); } public function onCommand(CommandSender $sender, Command $command, $label, array $args){ switch($command->getName()){ case "sendnews": if($sender->hasPermission("senderpe.news")){ if(isset($args[0])) { $msg = $this->yml->get("Broadcast"); $sender->sendMessage("Sent the message " . $msg); $sender->getLevel()->getServer()->broadcastMessage("§3§l[News]§r§3 " . implode(" ", $args)); return true; } else { $sender->sendMessage("§cYou cannot send an empty message!"); } } else { $sender->sendMessage("§cYou need OP to do this!"); return true; } } }}
You put $yml here : PHP: public $yml;public function onEnable() { $this->saveDefaultConfig(); $this->yml = new Config($this->getDataFolder()."config.yml", Config::YAML); $this->getServer()->getPluginManager()->registerEvents($this, $this); } PHP: $sender->getServer()->broadcastMessage("§3§l[News]§r§3 " . implode(" ", $args)); and uhmm why do you have to check $args[0] isset? if you don't check what $args[0] is , player can send like "/sendnews oangoawngaw" and the rest of the code can be run
Because I don't want people to send an empty [News] message. If args[0] isn't set (that means blank message), it will say you can't send an empty message.
If you have PHP: $this->saveDefaultConfig(); Then remove the PHP: $this->yml = new Config($this->getDataFolder()."config.yml", Config::YAML); And instead or $this->yml->get() use $this->getConfig()->get()
I new tried coding a LuckyDrop plugin, but it didn't work: PHP: <?phpnamespace pmmp\SenderPE;use pocketmine\utils\Config;use pocketmine\PluginBase;use pocketmine\Server;use pocketmine\utils\TextFormat;use pocketmine\event\Listener;use pocketmine\event\block\BlockBreakEvent;use pocketmine\block\Block;use pocketmine\item\Item;use pocketmine\inventory\Inventory;use pocketmine\Player;class Main extends PluginBase implements Listener { public function onEnable() { $this->getServer()->getPluginManager()->registerEvents($this, $this); $this->saveDefaultConfig(); } public function onBreak(BlockBreakEvent $event) { $which = $this->getConfig()->get("blockbreak"); if($event->getBlock()->getId() === $which) { $block = array($this->getConfig()->get("block")); $bs = $this->getConfig()->get("meta"); $amount = $this->getConfig()->get("dropnumber"); $drops = array(); $drops[] = new Item($block, $bs, $amount); $event->setDrops($drops); } }} My config.yml: Code: # LuckyDrop by WinterBuild7074 - Config # Block to break - ID blockbreak: "1" # Blocks/items to drop - ID block: - 265 - 313 # Block/item meta - Example: IDs like '5:2', meta is '2'. # Set to '0' for no block state/meta meta: "0" # How much it should drop dropnumber: "1"
Did you get any errors in the console? Plus, if you already have the SenderPE plugin to your server, you should rename this one because you will get the message: Could not load plugin 'SenderPE'. Plugin exists!
If that isn't the error, what is? PHP: <?phpnamespace pmmp\SenderPE;use pocketmine\utils\Config;use pocketmine\PluginBase;use pocketmine\Server;use pocketmine\utils\TextFormat;use pocketmine\event\Listener;use pocketmine\event\block\BlockBreakEvent;use pocketmine\block\Block;use pocketmine\item\Item;use pocketmine\inventory\Inventory;use pocketmine\Player;class Main extends PluginBase implements Listener { public function onEnable() { $this->getServer()->getPluginManager()->registerEvents($this, $this); $this->saveDefaultConfig(); } public function onBreak(BlockBreakEvent $event) { $which = $this->getConfig()->get("blockbreak"); if($event->getBlock()->getId() == $which) { $block = $this->getConfig()->get("block",[]); //it is already returning an array $bs = $this->getConfig()->get("meta",0); // add a default value $amount = $this->getConfig()->get("dropnumber",1); // add a default value $drops = array(); foreach($block as $blockB) { $drops[] = new Item($blockB, $bs, $amount); // the first parameter is an integer not an array } $event->setDrops($drops); } }} Code: # LuckyDrop by WinterBuild7074 - Config # Block to break - ID blockbreak: 1 # Blocks/items to drop - ID block: - 265 - 313 # Block/item meta - Example: IDs like '5:2', meta is '2'. # Set to '0' for no block state/meta meta: '0' # How much it should drop dropnumber: 1 SO MANY FACEPALMS