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

How use Server::generateLevel with custom class?

Discussion in 'Plugin Help' started by alexwolf_dev, Feb 3, 2017.

  1. alexwolf_dev

    alexwolf_dev Spider Jockey

    Messages:
    43
    GitHub:
    Frago9876543210
    I create class test extends Generator, but gives an error message:

    Code:
    [Server thread/CRITICAL]: Error: "Class name must be a valid object or a string" (EXCEPTION) in "/src/pocketmine/Server" at line 1109
    
    PHP:
    <?php

    namespace custom_gen;

    use 
    pocketmine\level\generator\Generator;
    use 
    pocketmine\plugin\PluginBase;

    class 
    main extends PluginBase
    {
        public function 
    onLoad()
        {
            
    Generator::addGenerator(test::class, test::NAME);
            
    $this->getServer()->generateLevel('test'null, \custom_gen\test::class, []); //this not works
        
    }
    }
     
  2. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    Why would it work if test::class is not an object? If you have made a test class, add a use statement for it.
     
  3. alexwolf_dev

    alexwolf_dev Spider Jockey

    Messages:
    43
    GitHub:
    Frago9876543210
    Example please)
     
  4. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Wouldn't you need
    Also?
     
  5. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    use this
    PHP:
    $this->getServer()->generateLevel('test'null'test');
     
    Last edited: Feb 7, 2017
  6. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
  7. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    This should work:
    PHP:
    <?php
    namespace custom_gen;

    use 
    pocketmine\level\generator\Generator;
    use 
    pocketmine\plugin\PluginBase;

    class 
    main extends PluginBase {
        public function 
    onLoad() {
            
    Generator::addGenerator(test::class, "Test");
            
    $this->getServer()->generateLevel("Test"nulltest::class);
        }
    }
     
  8. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    Well that makes no difference
    You need to pass a string not a class as the generator https://github.com/pmmp/PocketMine-MP/blob/master/src/pocketmine/Server.php#L1003
    http://php.net/manual/en/function.class-exists.php
     
  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.