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

API for own plugin

Comments in 'Plugin Development' started by AppPhil, Oct 25, 2015.

  1. AppPhil
    Offline

    AppPhil Active Member

    Joined:
    Oct 3, 2015
    Posts:
    54
  2. PocketKiller
    Offline

    PocketKiller Notable Member

    Joined:
    Jul 20, 2015
    Posts:
    741
    if you know PHP, you can.
    PHP:
    public function functionNameOfAPI(){
    echo 
    "my first API function is ran!";
    }
    and when doing
    PHP:
    $plugin $this->getServer()->getPluginManager()->getPlugin("PluginName");
    $plugin->functionNameOfAPI();
    will result on doing that echo in the function.
    Last edited: Oct 26, 2015
  3. AppPhil
    Offline

    AppPhil Active Member

    Joined:
    Oct 3, 2015
    Posts:
    54
    But can I create objects in this way, too?
  4. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    Yes.
    AppPhil likes this.
  5. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    This code will cause - Undefined property Main::$functionNameOfAPI
    AppPhil likes this.
  6. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    PHP:
    $plugin->functionNameOfAPI();
    AppPhil likes this.
  7. PocketKiller
    Offline

    PocketKiller Notable Member

    Joined:
    Jul 20, 2015
    Posts:
    741
    A Typo :/
    Primus and AppPhil like this.
  8. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    Huge one. $this->getKing() is a function, but $this->getKing is a variable.
  9. HotFireyDeath
    Offline

    HotFireyDeath Notable Member Plugin Developer

    Joined:
    Mar 19, 2015
    Posts:
    328
    Plugins:
    1
    Minecraft User:
    HotFireyDeath
    The way I do API uses functions too, but builds a different class in your plugin.

    Your API:
    PHP:
    <?php

    namespace SomePluginR;

    use 
    pocketmine\plugin\PluginBase;

    class 
    PluginAPI{
      public function 
    taskone($player){
         
    $player->sendMessage("hi");
      }
    }

    class 
    SomePluginR extends PluginBase{
    }
    How others might use it (supposedly):
    PHP:
    <?php

    namespace SomeOtherPlugin;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\event\player\PlayerJoinEvent;
    use 
    SomePluginR\SomePluginR;

    class 
    SomeOtherPlugin extends PluginBase{
      
    //suppose I registered $events($this,$this)... 

      
    public function onJoin(PlayerJoinEvent $event){
        
    $player $event->getPlayer();
        
    $mss = new PluginAPI();
        
    $mss->taskone($player);
        return 
    True;
      }
    }
  10. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Actually, if you have good OOP coding practices (at least I think it is good), you already have the API without noticing it.
    Take a look at the LegionPE plugin http://lgpe.co/tsrc - anyone can easily add a plugin to interact with the plugin using its public getters, setters and functions, although obviously I don't expect any other plugins to interact with it. This is because when you have written the code for a few months, you would completely forget about it. Only an API-style internal structure can allow you to know how to do what thing conveniently without re-reading the code. (Of course, you still need to do so if you are debugging, but the productivity is still much faster)

Share This Page

Advertisement