Config to another class

Discussion in 'Development' started by Azonuno, Oct 21, 2021.

  1. Azonuno

    Azonuno Spider

    Messages:
    12
    i need the config below to go 5to the secound class for some reason it wont register

    public function onJoin(PlayerJoinEvent $event) {
    $player = $event->getPlayer();
    $name = $player->getName();
    $config = new Config($this->getDataFolder() . "$name.yml", Config::YAML);
    $soul = $config->get(strtolower($name)); // get players config
    if($soul == "") { //if no souls set to 0
    $config->set(strtolower($name), 0);
    $config->save();

    }
    }
    public function getPlayerConfig(string $name) : Config {
    $getPlayerConfig = new Config($this->getDataFolder()."$name.yml", Config::YAML);
    }


    other class
    public function __Constructor(Main $Plugin, playerConfig $getPlayerConfig){
    $this->Main = $Plugin;
    $this->playerConfig = $getPlayerConfig;
    $this->playerConfig = $this->Main->getPlayerConfig();

    }
    public function onBreak(BlockBreakEvent $event) {
    $player = $event->getPlayer();
    $name = $player->getName();
    $config = $this->getPlayerConfig();
    $soul = $config->get(strtolower($name));
    $player->sendMessage("2");
    $config->set(strtolower($name), $soul + 1);
    $player->sendMessage("1");
    $config->save();



    error
    Error: "Call to undefined method CE\Items\Weapons\Sword\SoulGripperEnchant::getPlayerConfig()" (EXCEPTION) in "plugins/DCE/src/CE/Items/Weapons/Sword/SoulGripperEnchant" at line 28
     
  2. Tobias14

    Tobias14 Spider

    Messages:
    6
    GitHub:
    tobias-2006
    You can't do
    PHP:
    $config $this->getPlayerConfig();
    ,
    because getPlayerConfig() is not defined in that class.

    So you have to write
    PHP:
    $config $this->Main->getPlayerConfig();
     
    Last edited: Oct 25, 2021
  3. Primus

    Primus Zombie Pigman

    Messages:
    707
    The method is defined in the class, it just doesn't return the config object that was constructed in it.
     
  4. Tobias14

    Tobias14 Spider

    Messages:
    6
    GitHub:
    tobias-2006
    I know he forgot the return statement. However, I focused on the error which is at the end of the message. He has 2 classes, one main class and the second one. In the main class the method is defined, in the second class it is not. ;)
     
    Last edited: Oct 26, 2021
    Primus likes this.
  5. Primus

    Primus Zombie Pigman

    Messages:
    707
    You seem to be correct!
     

Share This Page

  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.