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

Tutorial Some standard changes in the Zekkou Cake rewrite

Comments in 'Resources' started by PEMapModder, Apr 7, 2014.

  1. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Well, this is not really a table. It is too ugly to be a table.

    https://github.com/PEMapModder/API-Diff/tree/Again/

    In this repository, I pushed two commits, the first one using the old API, the second one using the new API.

    You can go to here to read a colourful version of this comparison. But the plain text format is here. I generated it through $ git diff.

    PHP:
    <!-- Edited by PEMapModder on 7/4/2014 GMT+8 17:26: Better readability-->

    <?php

    // description

    -/*
    -__PocketMine Plugin__
    -class=AnOldApiPlugin
    -name=OldApi
    -author=PEMapModder
    -version=1.0
    -apiversion=12
    -*/
    -
    -
    // information
    +namespace pemapmodder\exampleplugin// A warning will be generated if no namespace is used
    +// No description required. Do it in plugin.yml

    // no namespaces needed
    +// namespaces are used
    +use pocketmine\Server;
    +use 
    pocketmine\command\Command;
    +use 
    pocketmine\command\CommandExecutor;
    +use 
    pocketmine\command\CommandSender;
    +use 
    pocketmine\command\PluginCommand;
    +use 
    pocketmine\event\Event;
    +use 
    pocketmine\event\EventExecutor;
    +use 
    pocketmine\event\EventPriority;
    +use 
    pocketmine\event\Listener;
    +use 
    pocketmine\plugin\Plugin;
    +use 
    pocketmine\plugin\PluginBase;
    +use 
    pocketmine\utils\TextFormat;
    +use 
    pocketmine\scheduler\PluginTask;

    // main plugin class

    -class AnOldApiPlugin implements Plugin{
    +class 
    AnNewApiPlugin extends PluginBase implements Listener{
    +    
    // not necessary that you extend PluginBase, but it is more convenient. however it is necessary that you implement the Plugin interface. (PluginBase is subclass of it)
    +    // Listener interface is empty. You can leet anything implement it.
    +    // interface CommandExecutor is onCommand(CommandSender, Command, mixed, array);
    +
        
    // start
    -    public function __construct(ServerAPI $api$server false){
    -        
    $this->api $api;
    +    public function 
    onLoad(){
    +        
    // $this->server is already done by PluginBase on initialization
        
    }
        
    // initialize
    -    public function init(){
    +    public function 
    onEnable(){
            
    // events
    -        $this->api->addHandler("player.block.touch", array($this"eventHandler"), 10);
    +        
    $this->getServer()->getPluginManager()->registerEvent("pocketmine\\event\\BlockBreakEvent"$thisEventPriority::HIGH, new MyEventExecutor(array($this"eventHandler")), $thisfalse);
    +        
    $this->getServer()->getPluginManager()->registerEvent("pocketmine\\event\\BlockPlaceEvent"$thisEventPriority::HIGH, new MyEventExecutor(array($this"eventHandler")), $thisfalse);
            
    // command /echo
    -        $this->api->console->register("echo""echos a command to console", array($this"commandHandler"));
    +        
    $command = new PluginCommand("echo"$this);
    +        
    $command->setDescription("echos a command to console");
    -        
    $this->api->console->alias("ec""echo");
    -        
    $this->api->console->alias("eh""echo");
    +        
    $command->setAliases(array("ec""eh")); // just some examples...
    +        $command->register($this->getServer()->getCommandMap()); // register to that particular map. idk if there are any other command maps :D
    +        // You could have done this with plugin.yml
            // schedule
    -        $this->api->schedule(1200, array($this"delayedFunction"), array("item 0""item 1"), false);
    -        
    $this->api->schedule(700, array($this"repeatedFunction"), array("item 2""item 3"), true);
    +        
    $this->getServer()->getScheduler()->scheduleDelayedTask(
    +                new 
    MyCallbackPluginTask(array($this"delayedFunction"), array("item 0""item 1"), $this), 1200);
    +        
    $this->getServer()->getScheduler()->scheduleRepeatedTask(
    +                new 
    MyCallbackPluginTask(array($this"repeatedFunction"), array("item 2""item 3"), $this), 700);
        }
        
    // finalize
    -    public function __destruct(){
    +    public function 
    onDisable(){
        }
        
    // handle commands
    -    public function commandHandler($cmd$params$issuer$alias){
    -        switch(
    $cmd){
    +    public function 
    onCommand(CommandSender $issuerCommand $cmd$label, array $args){
    +        switch(
    $cmd->getName()){
                case 
    "echo":
    -                
    console(FORMAT_AQUA."$issuer has used /$cmd.");
    -                return 
    "You have used /$cmd.";
    +                
    console(TextFormat::AQUA.$issuer->getName()." has used /".$cmd->getName().".");
    +                
    $issuer->sendMessage("You have used /"$cmd->getName().".");
            }
        }
        
    // handle events
    -    public function eventHandler($data$event){
    -        switch(
    $event){
    -            case 
    "player.block.touch":
    -                if(
    $data["player"]->username !== "PEMapModder")
    -                    return 
    false;
    +    public function 
    eventHandler(Event $event){
    +        switch(
    get_class($event)){
    +            case 
    "BlockBreakEvent":
    +            case 
    "BlockPlaceEvent":
    +                if(
    $event->getPlayer()->getName() !== "PEMapModder"// just an example, ok?
    +                    $event->setCancelled(false);
                    break;
            }
        }
    @@ -
    63,+79,34 @@ class AnOldApiPlugin implements Plugin{

    class 
    AnotherRandomClass{
        public function 
    __construct(){
    -        
    $this->server ServerAPI::request();
    +        
    $this->server Server::getInstance();
    +    }
    +}
    +
    +
    // convenient callback plugin task class
    +
    +class 
    MyCallbackPluginTask extends PluginTask{
    +    public function 
    __construct(callable $callback$data = array(), Plugin $p$callWithDataAsArray false){
    +        
    parent::__construct($p);
    +        
    $this->cb $callback;
    +        
    $this->data $data;
    +        
    $this->cwdaa $callWithDataAsArray;
    +    }
    +    public function 
    onRun($tick){
    +        if(
    $this->cwdaa)
    +            
    call_user_func_array($this->cb$data);
    +        else 
    call_user_func($this->cb$data$tick);
    +    }
    +}
    +
    +
    // convenient callback event executor class
    +
    +class 
    MyEventExecutor implements EventExecutor{
    +    public function 
    __construct(callable $callback){
    +        
    $this->cb $callback;
    +    }
    +    public function 
    execute(Listener $lEvent $event){ // we don't use the Listener class if we use this
    +        call_user_func($this->cb$event);
        }
    }


    Hope this helped!
    If you want to, just click the like button!
    Last edited by a moderator: May 1, 2014
  2. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Anything you want me to add? Post here.
    Last edited: Apr 7, 2014
    Spurs_Man321 likes this.
  3. Dinokiller
    Offline

    Dinokiller Notable Member

    Joined:
    Sep 22, 2013
    Posts:
    376
    Minecraft User:
    Dinokiller_
    When shochicp said all the plugins would be broken I didn't believe him, now I do. xD
    Anyway, this looks far better then the current API.
    PEMapModder likes this.
  4. Dinokiller
    Offline

    Dinokiller Notable Member

    Joined:
    Sep 22, 2013
    Posts:
    376
    Minecraft User:
    Dinokiller_
    Where do you actually get the development build of 1.4?
  5. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
  6. Dinokiller
    Offline

    Dinokiller Notable Member

    Joined:
    Sep 22, 2013
    Posts:
    376
    Minecraft User:
    Dinokiller_
    And where do you get the PHP binaries with all the required extensions?
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    I didn't. I didn't even test it. I just looked at the code part and the examples xd
  8. Dinokiller
    Offline

    Dinokiller Notable Member

    Joined:
    Sep 22, 2013
    Posts:
    376
    Minecraft User:
    Dinokiller_
  9. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Btw it is not necessary to make so many complicated classes issues (complicated for some). Commands can be reigstered in plugin.yml and events can just be used in the style:
    PHP:
    public function onEnable(){
        
    $this->getServer()->getPluginManager()->registerEvents($this$this);
    }
    // you must use the doc comment /** */ for this.
    /**
     * @param BlockPlaceEvent $evt
     * @ignoreCancelled false
     * @priority HIGH
     */
    public function event handler(BlockPlaceEvent $evt){
    }
    PocketMine checks this through \ReflectionClass. You can get more information of the definitions and requirements [URL="http://hk1.php.net/manual/en/class.reflectionclass.php]here[/url].
  10. Dinokiller
    Offline

    Dinokiller Notable Member

    Joined:
    Sep 22, 2013
    Posts:
    376
    Minecraft User:
    Dinokiller_
    I really want to get RandomTP working for 1.4 but I can't get PocketMine 1.4 to actually run.
  11. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    :D
  12. Spurs_Man321
    Offline

    Spurs_Man321 Active Member

    Joined:
    Apr 7, 2014
    Posts:
    52
    Minecraft User:
    spursman321
  13. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    When you ignore someone you don't receive alerts from their posts.
  14. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Fixed an error in namespaces ("pocketmine\\event\\XxEvent" instead of "XxEvent").

    P.S. The forum is very quiet these days...
  15. Spurs_Man321
    Offline

    Spurs_Man321 Active Member

    Joined:
    Apr 7, 2014
    Posts:
    52
    Minecraft User:
    spursman321
    I figured that in my first few days
  16. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    I need to look further into the permission things though.
    64FF00 likes this.
  17. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    I'll add something about permissions soon.
    64FF00 likes this.
  18. Darunia18
    Offline

    Darunia18 Staff Member Sectional Moderator

    Joined:
    Aug 23, 2013
    Posts:
    755
    Plugins:
    2
    Minecraft User:
    Darunia18
    Something I'd like to point out with the new API is that it seems like it'll be easier to port Bukkit plugins now, and will actually be "porting" instead of rewriting it completely XD This actually helps a lot for some projects of mine :DDD
    64FF00 and PEMapModder like this.
  19. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    OK I'll look at the Bukkit plugins format when I have time. See if I can get another comparison for that ;)
    64FF00 likes this.
  20. Darunia18
    Offline

    Darunia18 Staff Member Sectional Moderator

    Joined:
    Aug 23, 2013
    Posts:
    755
    Plugins:
    2
    Minecraft User:
    Darunia18
    Shoghi has said that he is trying to get PocketMine to be more like Bukkit, I'm guessing to encourage developers and server owners of Bukkit to use PocketMine as well.
    PEMapModder likes this.

Share This Page

Advertisement