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

Call To Function Get() On String?

Discussion in 'Development' started by Junkdude, Dec 4, 2016.

  1. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    Loader.php
    PHP:
    <?php

    namespace MCMMOPE;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\utils\TextFormat;
    use 
    MCMMOPE\Mining;
    use 
    pocketmine\event\entity\EntitySpawnEvent;
    use 
    pocketmine\command\CommandExecutor;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\utils\Config;
    class 
    Loader extends PluginBase implements Listener,CommandExecutor{

        public function 
    dataPath()
        {
          return 
    $this->getDataFolder();
        }


        public function 
    onEnable(){
            
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
            
    ##SOON$this->getServer()->getPluginManager()->registerEvents(new Commands($this), $this);
            
    $this->getServer()->getPluginManager()->registerEvents(new Mining($this), $this);
            
    $this->getLogger()->info(TextFormat::GREEN."MCMMOPE Enabled!");
            
    $this->getCommand("activate_mining")->setExecutor(new Mining($this));
            @
    mkdir($this->getDataFolder());
            
    $this->saveResource("mininglevel.yml");
            
    $this->saveResource("miningexperiance.yml");
            
    $this->mininglevel = new Config($this->getDataFolder() . "mininglevel.yml"Config::YAML);
            
    $this->miningexperiance = new Config($this->getDataFolder() . "miningexperiance.yml"Config::YAML);
            
    $this->miningexperiance->save();
            
    $this->mininglevel->save();

                
            
            
        }
     
        public function 
    onDisable(){
            
    $this->getLogger()->info(TextFormat::RED."MCMMOEPE Disabled!");
        }
    }
    Mining.php:
    PHP:
    <?php

    namespace MCMMOPE;

    use 
    pocketmine\Player;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\utils\TextFormat as c;
    use 
    pocketmine\Server;
    use 
    pocketmine\event\Cancellable;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\block\BlockBreakEvent;
    use 
    pocketmine\utils\Config;
    use 
    pocketmine\command\CommandExecutor;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\command\Command;
    use 
    MCMMOPE\Loader;
    use 
    pocketmine\entity\Effect;

    class 
    Mining extends PluginBase implements Listener{

        public function 
    __construct(Loader $plugin){
              
    $this->plugin $plugin;
        }

        public function 
    dataPath()
        {
          return 
    $this->getDataFolder();
        }

        public function 
    onBreak(BlockBreakEvent $event){
          
    $p $event->getPlayer();
          
    $name $p->getName();
          
    $test $p->getInventory()->getItemInHand();
          if (
    $test->getCustomName() === "§eZa Roc\n§rTorture 25 Percent\nExplosive 25 Percent") {
            
    $event->getBlock()->setNameTag("TEST");
          }
            
    $this->mininglevel $this->loader->getDataFolder() . "mininglevel.yml";
            
    $this->miningexperiance $this->loader->getDataFolder() . "miningexperiance.yml";
              
    $level $this->mininglevel->get($name);
              
    $exp $this->miningexperiance->get($name);
              if(
    $event->getBlock()->GetId() == 1){
              
    $this->mininglevel->set($name,$exp+30);
              
    $this->mininglevel->Save();
              if(
    $exp 1000){
              
    $p->sendMessage("Mining Skill Is Now Level 1");
            }if(
    $exp 2000 ){
              
    $p->sendMessage("Mining Skill Is Now Level 2");
            }
    }
    }

        
    public function 
    onCommand(CommandSender $senderCommand $cmd$label, array $args){

      if(
    strtolower($cmd->getName()) === "activate_mining"){
      
    $dataFile $this->loader->getDataFolder() . strtolower($sender->getName());
      
    $name $sender;
      if(
    is_file($dataFile)) {
        
    $data yaml_parse_file($dataFile);
        
    $lastTime $data["last-execute-command"][$cmd->getName()];
      } else {
        
    $lastTime 0;
      }
      if(
    time() - $lastTime 5) {
        
    $sender->sendMessage("You execute too frequently!");
        return 
    true;
      }else if(
    time() - $lastTime 5) {
        
    $sender->SendMessage("Skill Activated");
        
    ##if($name->)
        ##$name->addEffect(Effect::getEffect(20)->setDuration($duration)->setAmplifier(1));
     
    }
    $data["last-execute-command"][$cmd->getName()] = time();
      
    yaml_emit_file($dataFile$data);
      
    // handle command
      
    return true;
    }
    }
    }
     
  2. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    You'd be better off saving/creating the mining.yml resource in the onEnable function like @LilCrispy2o9 said and then create a Config instance in your listener and assign it to a class property. That way you don't create a new Config object each time your event handler is called.
     
  3. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    Gonna be honest, can you make that simpler to understand? I have no clue what thats supposed to mean
     
  4. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    How do I assign it to a class property
     
  5. LilCrispy2o9

    LilCrispy2o9 Spider Jockey

    Messages:
    43
    GitHub:
    lilcrispy2o9
    In Mining.php add
    PHP:
    public $mininglevel;
    then in your __construct function add
    PHP:
    $this->mininglevel = new Config($this->plugin->getDataFolder() . "mininglevel.yml");
    There are also quit a few other erros in your code, you shoudl go and look on github to get a better understanding of PHP and other things.
     
  6. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    ik there are error,s this is just a "roadmap" type deal
     
  7. LilCrispy2o9

    LilCrispy2o9 Spider Jockey

    Messages:
    43
    GitHub:
    lilcrispy2o9
    If you fix them, it should work perfectly.
     
  8. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    *Sigh*
    I'm getting the same error again. I modified my code so it generated everything onJoin but it still gives me the error "
    [18:29:40] [Server thread/CRITICAL]: "Could not pass event 'pocketmine\event\block\BlockBreakEvent' to 'MCMMOPE v': Call to a member function get() on string on MCMMOPE\Skills\Mining
    [18:29:40] [Server thread/CRITICAL]: Error: "Call to a member function get() on string" (EXCEPTION) in "/plugins/AMCMMOPE/src/MCMMOPE/Skills/Mining" at line 43"

    Mining:
    PHP:
    <?php

    namespace MCMMOPE\Skills;

    use 
    pocketmine\Player;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\utils\TextFormat as c;
    use 
    pocketmine\Server;
    use 
    pocketmine\event\Cancellable;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\block\BlockBreakEvent;
    use 
    pocketmine\utils\Config;
    use 
    pocketmine\command\CommandExecutor;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\command\Command;
    use 
    MCMMOPE\Loader;
    use 
    pocketmine\entity\Effect;

    class 
    Mining extends PluginBase implements Listener{

    public function 
    dataPath()
        {
          return 
    $this->getDataFolder();
        }

        public function 
    __construct(Loader $plugin){
              
    $this->plugin $plugin;
              
    ##$this->mininglevel = new Config($this->plugin->getDataFolder() . "mininglevel.yml");
              ##$this->miningexperiance= new Config($this->plugin->getDataFolder() . "miningexperiance.yml");

        
    }

        

        public function 
    onBreak(BlockBreakEvent $event){
          
    $player $event->getPlayer();
          
    $file = ($this->getDataFolder()."Players/".$player->getName().".yml"); 
          
    $data $file->get("Kills""KillsEXP""AcrobaticsLevel""AcrobaticsEXP""ExcavationLevel""ExcavationEXP""MiningLevel""MiningEXP");
              if(
    $event->getBlock()->GetId() == 1){
                if(
    file_exists($PlayerFile)){
                  if(
    $file->exists($data)){
                
    $p->sendmessage("TIS LIT");
                    
    $data->set("MiningEXP", +1);
                    
    $this->PlayerFile->save();
                }
          }
        }
      }

            private function 
    mininghaste(Player $player){
            
    $file $this->getDataFolder()."Players/".$player->getName().".yml"
            
    $data $file->get("MiningEXP");
                
    $player->sendMessage("Skill Activated!");
            }


    public function 
    onCommand(CommandSender $senderCommand $cmd$label, array $args){

      if(
    strtolower($cmd->getName()) === "activate_mining"){
      
    $dataFile $this->getDataFolder() . strtolower($sender->getName());
      
    $name $sender;
      if(
    is_file($dataFile)) {
        
    $data yaml_parse_file($dataFile);
        
    $lastTime $data["last-execute-command"][$cmd->getName()];
      } else {
        
    $lastTime 0;
      }
      if(
    time() - $lastTime 5) {
        
    $sender->sendMessage("You execute too frequently!");
        return 
    true;
      }else if(
    time() - $lastTime 5) {
        
    $this->mininghaste($sender);
        
    ##if($name->)
        ##$name->addEffect(Effect::getEffect(20)->setDuration($duration)->setAmplifier(1));
     
    }
    $data["last-execute-command"][$cmd->getName()] = time();
      
    yaml_emit_file($dataFile$data);
      
    // handle command
      
    return true;
    }
    }
    }
    Loader:
    PHP:
    <?php

    namespace MCMMOPE;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\utils\TextFormat as TF;
    use 
    MCMMOPE\Skills\Mining;
    use 
    pocketmine\event\entity\EntitySpawnEvent;
    use 
    pocketmine\command\CommandExecutor;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\utils\Config;
    use 
    pocketmine\event\player\PlayerJoinEvent;
    class 
    Loader extends PluginBase implements Listener,CommandExecutor{

        public function 
    dataPath()
        {
          return 
    $this->getDataFolder();
        }


        public function 
    onEnable(){
            
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
            
    ##SOON$this->getServer()->getPluginManager()->registerEvents(new Commands($this), $this);
            
    $this->getServer()->getPluginManager()->registerEvents(new Mining($this), $this);
            
    $this->getLogger()->info(TF::GREEN."MCMMOPE Enabled!");
            
    $this->getCommand("activate_mining")->setExecutor(new Mining($this));
            
    ##@mkdir($this->getDataFolder());
            ##$this->saveResource("mininglevel.yml");
         ##   $this->saveResource("miningexperiance.yml");
      ##      $this->mininglevel = new Config($this->getDataFolder() . "mininglevel.yml", Config::YAML);
      ##      $this->miningexperiance = new Config($this->getDataFolder() . "miningexperiance.yml", Config::YAML);
     ##       $this->miningexperiance->save();
     ##       $this->mininglevel->save();

                
            
            
        
    }
     
        public function 
    onDisable(){
            
    $this->getLogger()->info(TF::RED."MCMMOEPE Disabled!");
        }

        public function 
    onJoin(PlayerJoinEvent $event){
            
    $player $event->getPlayer();
            
    $file $this->getDataFolder()."Players/".$player->getName().".yml"
            
    $player->sendmessage("TEST");
            if(!
    file_exists($file)){
                
    $this->PlayerFile = new Config($this->getDataFolder()."Players/".$player->getName().".yml"Config::YAML);
            
    $this->PlayerFile->set("KillsLevel"0);
            
    $this->PlayerFile->set("KillsEXP"0);     
            
    $this->PlayerFile->set("AcrobaticsLevel"0);
            
    $this->PlayerFile->set("AcrobaticsEXP"0);
            
    $this->PlayerFile->set("MiningLevel"0);
            
    $this->PlayerFile->set("MiningEXP"0);
            
    $this->PlayerFile->set("ExcavationLevel"0);
            
    $this->PlayerFile->set("ExcavationEXP"0);
            
    $this->PlayerFile->save();
            
    $this->getLogger()->info(TF::GREEN."Your new ".TF::BOLDTF::GRAY."MMO".TF::RESETTF::GREEN." account has been created!");
          }
                
                
    $this->PlayerFile = new Config($this->getDataFolder()."Players/".$player->getName().".yml"Config::YAML);
                
    $data $this->PlayerFile->get("Kills""KillsEXP""AcrobaticsLevel""AcrobaticsEXP""ExcavationLevel""ExcavationEXP""MiningLevel""MiningEXP");
            if(!
    $this->PlayerFile->exists($data)){
                    
    $this->PlayerFile->set("KillsLevel"0);
                    
    $this->PlayerFile->set("KillsEXP"0);     
                    
    $this->PlayerFile->set("AcrobaticsLevel"0);
                    
    $this->PlayerFile->set("AcrobaticsEXP"0);
                    
    $this->PlayerFile->set("ExcavationLevel"0);
                    
    $this->PlayerFile->set("ExcavationEXP"0);
                    
    $this->PlayerFile->set("MiningLevel"0);
                    
    $this->PlayerFile->set("MiningEXP"0);
                    
    $this->PlayerFile->save();
                }
        }
    }
     
  9. KnownUnown

    KnownUnown Spider Jockey Poggit Reviewer

    Messages:
    47
    GitHub:
    knownunown
    PHP:
    $file = ($this->getDataFolder()."Players/".$player->getName().".yml");
    $data $file->get("Kills""KillsEXP""AcrobaticsLevel""AcrobaticsEXP""ExcavationLevel""ExcavationEXP""MiningLevel""MiningEXP");
    While an attempt was made, this is not the way you read a YML config. $file is a string, which the error implies. Trying to call get() on it would be useless, because it is a string, not a config object. This may be of some use.
     
    Primus likes this.
  10. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    So what exactly do I change? I don't understand the Config.php link to be honest
     
  11. KnownUnown

    KnownUnown Spider Jockey Poggit Reviewer

    Messages:
    47
    GitHub:
    knownunown
    PocketMine utilizes Object Oriented PHP, correct? You are trying to call a function get() (which you are actually doing incorrectly, but we can talk about that later) to get data from a YML file. Strings do not provide this method, but PocketMine provides a utility class called Config to assist you with parsing these files. To use it and it's methods (including get()), all you have to do is to construct a new instance of the Config object with the things it needs. The line I linked you to shows you what it needs. You can do this from here.
     
    Primus likes this.
  12. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    Ohhh, so where do I construct it?
     
  13. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    LilCrispy2o9 likes this.
  14. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
  15. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Read again until you understand it all ;)
     
  16. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    Im still confused, I re-read it and still lost.
     
  17. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
  18. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
  19. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Help on what?
     
  20. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    The whole issue.
     
  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.