Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Two Plugins~different classes

Comments in 'Plugin Development' started by Marcelo234, May 23, 2016.

  1. Marcelo234
    Offline

    Marcelo234 New Member

    Joined:
    Oct 18, 2014
    Posts:
    204
    He Guys...I have two Plugins...The Plugin Test1 should call a public function from the Plugin Test2....But the public function from the Plugin Test2 isnt in the Main! So how to write this?
    Thanks Marcelo234
  2. CrazedMiner
    Offline

    CrazedMiner Notable Member Plugin Developer

    Joined:
    Jan 31, 2015
    Posts:
    348
    Plugins:
    2
    Minecraft User:
    CrazedMinerYT
    Either construct a new class instance from a class int your plugin or create an instance and store it in the main instance and create a way to access it from another plugin/class.
    PHP:
    // plugin one
    <?php
    namespace pluginone;

    use 
    pocketmine\plugin\PluginBase;

    class 
    PluginOne extends PluginBase {

        public 
    $class null;

        public function 
    onEnable() {
            
    $this->class = new MyClass();
        }

    }

    // a class within plugin one
    <?php

    namespace pluginone;

    class 
    MyClass {

        public function 
    myFunction($params) {
            echo 
    "My function was called!\nParams: {$params}\n";
        }

    }

    // second plugin
    <?php
    namespace plugintwo;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pluginone\PluginOne;

    class 
    PluginTwo extends PluginBase {

        public function 
    onEnable() {
            
    $pluginone $this->getServer()->getPluginManager()->getPlugin("PluginOne");
            if(
    $pluginone instanceof PluginOne$pluginone->class->myFunction("Params");
        }

    }
    AndrewBit and udwarf like this.
  3. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    Either that or add it as an external library.
  4. Marcelo234
    Offline

    Marcelo234 New Member

    Joined:
    Oct 18, 2014
    Posts:
    204
    Code:
    Warning: Missing argument 1 for McKaff_BowBash\Arena\Arena::__construct(), called in C:\Users\Marcelo\Desktop\mckaff_network\Genisys\plugins\McKaff_BowBash\src\McKaff_BowBash\BowBash.php on line 42 and defined in C:\Users\Marcelo\Desktop\mckaff_network\Genisys\plugins\McKaff_BowBash\src\McKaff_BowBash\Arena\Arena.php on line 79
    [18:13:01] [Server thread/CRITICAL]: TypeError: "Argument 2 passed to McKaff_BowBash\Arena\Arena::__construct() must be an instance of McKaff_BowBash\BowBash, none given, called in C:\Users\Marcelo\Desktop\mckaff_network\Genisys\plugins\McKaff_BowBash\src\McKaff_BowBash\BowBash.php on line 42" (EXCEPTION) in "/McKaff_BowBash/src/McKaff_BowBash/Arena/Arena" at line 79
    
  5. Marcelo234
    Offline

    Marcelo234 New Member

    Joined:
    Oct 18, 2014
    Posts:
    204
    PHP:
    public function __construct($idBowBash $plugin){
            
    $this->id $id;
            
    $this->plugin $plugin;
            
    $this->data $plugin->arenas[$id];
            
    $this->checkWorlds();
        }
  6. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    Recently I've been working more on static functions.
    PHP:
    class SomeClass {

    private static 
    $instance=null;

    public function 
    __construct(){
            if(
    self::$instance === nullself::$instance $this;
    }

    public static function 
    getInstance() : SomeClass { return self::$instance; }
    Now you can get the class object from anywhere in the code like this
    PHP:
    SomeClass:getInstance();
    Just make sure you've constructed that class at least once.
    Last edited: May 24, 2016
  7. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    PHP:
    public static function getInstance() : SomeClass
    //not
    public static function getInstance() : Some Class 
    :)
    Primus likes this.
  8. Tim // robske Büba
    Offline

    Tim // robske Büba Notable Member

    Joined:
    Feb 26, 2014
    Posts:
    606
    Minecraft User:
    robske_110
    If you're using static functions the static function can't access class properties!
    Last edited: Jun 6, 2016
    Legoboy0215 likes this.
  9. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    Yes, static can't access class properties but you can use them to hold objects and pass it throughout code without having the real object which holds them.
  10. AndrewBit
    Offline

    AndrewBit Notable Member

    Joined:
    Jun 18, 2015
    Posts:
    435
    Minecraft User:
    AndrewBit4
    Learn the errors, please.
    Argument 2 should be an instance of BowBash class, looks like you instanced the class with only one parameter.
    PHP:
    $classInstance = new Arena($id);
    Instead with
    PHP:
    $classInstance = new Arena($id$bowBashInstance);
  11. CrazedMiner
    Offline

    CrazedMiner Notable Member Plugin Developer

    Joined:
    Jan 31, 2015
    Posts:
    348
    Plugins:
    2
    Minecraft User:
    CrazedMinerYT
    We're not here to do everything for you, you need to have some level of programming knowledge. Make sure you 'use' the plugin class if the classes aren't in the same directory.
  12. SOFe
    Offline

    SOFe Banned

    Joined:
    May 28, 2016
    Posts:
    386
    Minecraft User:
    Herobrine
    Cases:
    • Simply acts as a library/utility function
      • Use static function
    • Interacts with the plugin
      • You need a non-static function
  13. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    It's better to use the singleton design pattern. Also, if the class contains something that is unique in each class, don't do it.
  14. Tim // robske Büba
    Offline

    Tim // robske Büba Notable Member

    Joined:
    Feb 26, 2014
    Posts:
    606
    Minecraft User:
    robske_110
    I know, sorry if it was like a question
    Primus likes this.
  15. SOFe
    Offline

    SOFe Banned

    Joined:
    May 28, 2016
    Posts:
    386
    Minecraft User:
    Herobrine
    A singleton structure, an utility class (a class where all methods and properties are static) and global variables + namespace functions are technically the same but have different syntax.
    However, a singleton object and an utility class may have faster variable loading than global variables.
  16. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    And whom are you quoting?
  17. CrazedMiner
    Offline

    CrazedMiner Notable Member Plugin Developer

    Joined:
    Jan 31, 2015
    Posts:
    348
    Plugins:
    2
    Minecraft User:
    CrazedMinerYT
    Can you stop polluting this forum with crappy, incorrect answers? I hate to be the one guy that goes and says it but it's just plain annoying.
  18. SOFe
    Offline

    SOFe Banned

    Joined:
    May 28, 2016
    Posts:
    386
    Minecraft User:
    Herobrine
    Sorry, if you think this answer is wrong, please point out what is wrong. If you can't point out the wrong part in my post, please do not accuse others for no reason. I fail to see the mistake in my post.
    Last edited: Jun 4, 2016
    Legoboy0215 and HotFireyDeath like this.
  19. SOFe
    Offline

    SOFe Banned

    Joined:
    May 28, 2016
    Posts:
    386
    Minecraft User:
    Herobrine
    And if you would like me to elaborate:

    A singleton structure is basically a class that is only supposed to have one instance per runtime, and it has a static class property that stores the supposedly-only instance of the class. Therefore, even though all the method calls on that instance are non-static, since they only manipulate that single object, there is technically (by technically, I mean "to the developer" and "in terms of the outcome") no difference between singletons and static methods and class properties other than syntax difference. Furthermore, as long as they do not duplicate other existing variables or functions, using global variables and namespace functions has similar nature.

    Otherwise, please point out the technical difference between these three:

    PHP:
    class SingletonClazz{
      public 
    $property null;
      public static 
    $instance null;
      public static function 
    getInstance() : SingletonClazz{
        return 
    self::$instance ?? (self::$instance = new self);
      }
      public function 
    method(){
        return 
    $this->property;
      }
      public function 
    methodAgain($param){
        
    $this->property $param;
      }
    }
    $v SingletonClazz::getInstance()->method();
    SingletonClazz::getInstance()->methodAgain($v);
    PHP:
    class StaticClazz{
      public static 
    $property null;
      public static function 
    method(){
        return 
    self::$property;
      }
      public static function 
    methodAgain($v){
        
    self::$property $v;
      }
    }
    $v StaticClazz::method();
    StaticClazz::methodAgain($v);
    PHP:
    $property null;
    function 
    method(){
      global 
    $property;
      return 
    $property;
    }
    function 
    methodAgain($v){
      global 
    $property;
      
    $property $v;
    }
    $v method();
    methodAgain($v);
    Please point out the technical difference between the three, apart from possible name collisions?

    And to explain my point that classes are more efficient, it is that memory related to the same class are stored under the same small index (this is natural for programming languages coded in any memory-based languages like C), like this:

    PHP:
    function method(){
      global 
    $v;
      return 
    $v;
    }
    method();
    The search processes done:
    Code:
    Search method method() from the big pool of namespace functions
    Resolve memory address of $v from the bbig pool of variables
    
    vs
    PHP:
    class StaticClazz{
      public static 
    $v;
      public static function 
    method(){
        return 
    $this->v;
    }
    StaticClazz::method();
    The search processes done:
    Code:
    Search class StaticClazz from a relatively smaller pool of classes
    Search method method() under the really small index of class methods of the class StaticClazz, where the index is already searched in the step above
    Search static class property from the really small index of static class properties of the class StaticClazz, where the index is already searched in the first step.
    
    Obviously, static class is faster. I'm not going to compare the singleton way and the global way, because I hereby humbly acknowledge you that it is out of the scope of my knowledge on how to correctly compare them, but my instinct tells me that objects are faster, and I fail to explain this instinct.

    P.S. Can you stop polluting this forum with crappy, incorrect accuses of others being wrong? I hate to be the one guy that goes and says it but it's just plain misleading. If you say that I am wrong while I am correct, other people holding the same understanding would be confused that they are wrong about what they understand while it is correct.
    Primus, Legoboy0215 and HotFireyDeath like this.
  20. CrazedMiner
    Offline

    CrazedMiner Notable Member Plugin Developer

    Joined:
    Jan 31, 2015
    Posts:
    348
    Plugins:
    2
    Minecraft User:
    CrazedMinerYT
    'Global variables'. They're called class properties.

    I'll be quite frank, a computer doesn't care about your 'instinct'.

    Wether a static function or a class based function is used is irrelevant here. They want to know how they can access functions in one plugin from another plugin.

    P.S. I'm pretty sure you meant accusations, not accuses.

    P.P.S. Pleaseeeee stop polluting this forum with crappy incorrect an irrelevant posts. Thanks! :)

Share This Page

Advertisement