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

Solved Need help making a plugin with a config

Discussion in 'Plugin Help' started by WinterBuild7074, Apr 12, 2017.

  1. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    I need help making and fixing my plugin. I'm trying to add a config to it.
    Code:
    PHP:
    <?php

    namespace 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 $senderCommand $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
    
     
  2. XdmingXD

    XdmingXD Baby Zombie

    Messages:
    158
    GitHub:
    xdmingxd
    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 ?
     
  3. xBeastMode

    xBeastMode Shog Chips

    Messages:
    0
    That means the config file wasn't saved. Just add
    PHP:
    $this->saveDefaultConfig();
    before anything inside the onEnable() scope.
     
    XdmingXD likes this.
  4. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    Is this all correct?
    PHP:
    <?php

    namespace 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 $senderCommand $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;
                }
            }
        }
    }
     
  5. XdmingXD

    XdmingXD Baby Zombie

    Messages:
    158
    GitHub:
    xdmingxd
    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
     
  6. xBeastMode

    xBeastMode Shog Chips

    Messages:
    0
    Nah, visibilities can never go inside a function scope or outside of an object.
     
  7. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    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.
     
  8. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    Now it says: "Call to a member function get() on null".
     
  9. XdmingXD

    XdmingXD Baby Zombie

    Messages:
    158
    GitHub:
    xdmingxd
    You didn't set anything in your config.yml = =
     
  10. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    My config.yml looks like this:
    Code:
    # SenderPE Config
    
    Broadcast: "TestingIt123"
    
     
  11. TheDiamondYT

    TheDiamondYT Zombie

    Messages:
    298
    GitHub:
    TheDiamondYT1
    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()
     
  12. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    I new tried coding a LuckyDrop plugin, but it didn't work:
    PHP:
    <?php
    namespace 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"
     
  13. MalakasPlayzMC

    MalakasPlayzMC Spider Jockey

    Messages:
    37
    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!
     
  14. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    No, that is not the error.
    But I think there's a problem with the code.
     
  15. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    If that isn't the error, what is?
    PHP:
    <?php
    namespace 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 :facepalm:
     
  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.