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

2 Files Connect

Discussion in 'Plugin Help' started by Robin, May 16, 2020.

  1. Robin

    Robin Spider

    Messages:
    11
    GitHub:
    Blackbarbar
    hey i need help.

    i am coding a Shop UI Plugin and now i have a big problem.

    My main file called Main.php and my shop file called Shop.php

    How can i connect these 2 files?

    In the Main file i had write the UI Basics (openShopForm)


    In the Shop File i had write the Shop form, but this form don't open.

    Thanks for help

    Good Day ;)
     
  2. ethaniccc

    ethaniccc Baby Zombie

    Messages:
    189
    GitHub:
    ethaniccc
    You have to call it as an object and execute the function in the object.
    You also have to construct the object sufficiently.

    Here is an example of what I'm talking about:
    Let's say we have two classes: Class A, and Class B.
    In class A, I want to call a function from class B.
    In class A I would do something like this:
    PHP:
    class extends PluginBase{

    public function 
    openMenu(){
        
    $menu = new ShopUI($this);
        
    $menu->openMenu($player);
    }

    }
    and Class B would look something like this:
    PHP:
    class B{

    private 
    $plugin;

    public function 
    __construct(PluginMainFile $plugin){
        
    $this->plugin $plugin
    }

    public function 
    openMenu(Player $player){
        
    // actions
    }

    }
    I suck at explaining, but you should search up on Google "PHP OOP" for more understanding.
     
  3. Robin

    Robin Spider

    Messages:
    11
    GitHub:
    Blackbarbar
    Thank you very much :)
     
  4. Robin

    Robin Spider

    Messages:
    11
    GitHub:
    Blackbarbar
    Also I Must Write the Plugin so:

    Main.php

    class Main extends PluginBase implements Listener{

    public function openMyForm($player)

    case 0;
    $this->openShopForm($player);


    Shop.php

    private $plugin;

    public function __construct(PluginMainFile $plugin){
    $this->plugin = $plugin
    }


    public function openShopForm($player)

    Actions

    is it right or isn’t it?
     
  5. ethaniccc

    ethaniccc Baby Zombie

    Messages:
    189
    GitHub:
    ethaniccc
    Not really
    You see, the function "openShopForm" is in the Shop class and not in the Main class. Because "$this" refers to the current class/object, that code wouldn't work.
    Also, in Shop.php, in the function __construct, the "PluginMainFile" is supposed to be the name of the class that is constructing it.
    On top of that. I'm pretty sure you can't use "case" without using switch first
    Code:
    PHP:
    <?php

    class Main extends PluginBase implements Listener{

        public function 
    openMyForm(Player $player){
            
    switvh($data){ // ? what is being switched here
                
    case 0:
                    
    $shop = new ShopUI($this);
                    
    $shop->openShopForm($player);
                break;
            }
        }

    }

    class 
    ShopUI{

        private 
    $plugin;

        public function 
    __construct(Main $plugin){
            
    $this->plugin $plugin;
        }

        public function 
    openShopForm(Player $player){
            
    // do your actions
        
    }

    }
     
  6. Robin

    Robin Spider

    Messages:
    11
    GitHub:
    Blackbarbar
    [14:41:10] [Server thread/CRITICAL]: TypeError: "Argument 1 passed to pocketmine\plugin\PluginBase::__construct() must implement interface pocketmine\plugin\PluginLoader, instance of black\Main given, called in phar:///home/minecraft/plugins/WordShop_1.3.4.phar/src/black/Main.php on line 110" (EXCEPTION) in "src/pocketmine/plugin/PluginBase" at line 77


    help plss
     
  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.