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

[Solved]What's the best way for two plugins to share information?

Comments in 'Plugin Development' started by WillotheBlessed, Sep 23, 2014.

  1. WillotheBlessed
    Offline

    WillotheBlessed Active Member

    Joined:
    Sep 17, 2014
    Posts:
    69
    Minecraft User:
    will
    My issue is I want to have a cache of spawnpoints somewhere in code or a config file. What is the best way for two plugins to reference this same info?

    My first thought is as static list, but will it compile? Or perhaps they can reference the same config file? Another thought is that I could have a separate plugin for storing global information between plugins, and use a "get plugin" method to get this.

    Before going further, I wanted to send a feeler out for who else has ran into this issue and how it was solved.

    Thanks in advance!
  2. wies
    Offline

    wies Notable Member

    Joined:
    Aug 23, 2013
    Posts:
    390
    Making a variable static works in the old api but not in the current one. The best method is to make a variable public. To get the value of the variable you get the object of the other plugin with the function $this->getServer()->getPluginManager()->getPlugin($pluginName) and get the variable from that object with "->".
    For example,
    plugin 2 will display the variable $sharedData of plugin 1:
    PHP:
    <?php
    namespace Plugin1;

    use 
    pocketmine\plugin\PluginBase;

    class 
    MainClass extends PluginBase{
        public 
    $sharedData "PocketMine rocks!";
    }
    PHP:
    <?php
    namespace Plugin2;

    use 
    pocketmine\plugin\PluginBase;

    class 
    MainClass extends PluginBase{
        public function 
    onEnable(){
            
    $sharedData $this->getServer()->getPluginManager()->getPlugin("Plugin1")->sharedData;
            
    $this->getLogger()->info("The shared data of plugin1: ".$msg);
        }
    }
    You can also change the value of the public variable in another plugin:
    PHP:
    $this->getServer()->getPluginManager()->getPlugin("Plugin1")->sharedData "I <3 bacon";
    Last edited: Sep 23, 2014
    EvolSoft, LDX and WillotheBlessed like this.
  3. WillotheBlessed
    Offline

    WillotheBlessed Active Member

    Joined:
    Sep 17, 2014
    Posts:
    69
    Minecraft User:
    will
    This is what I was thinking it might look like... thanks. :D
  4. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    The most stable method is to directly pass the PluginBase object to all classes, but this only works well when you are in the same plugin.
  5. Tuff
    Offline

    Tuff Active Member

    Joined:
    Jan 25, 2014
    Posts:
    203
    Minecraft User:
    Roboroug2
    Well, you learn something new every day. I personally would have used a database or config to pass data between two plugins, (though those are not very good at storing objects :p).
    Last edited: Sep 24, 2014

Share This Page

Advertisement