Pocketmine-MP非公式日本語wikiです。Pocketmine-MPに関する情報を発信しています。

APIとは?


API(Application Programming Interface)とは他のプラグインから
使える、アクセス修飾子がpublicなどの関数が集まったものです。
オブジェクト指向がわかっていれば簡単にできます。

アクセス修飾子

名前アクセス範囲
publicどのクラスからも使用可能
protectedその関数があるクラス・継承したクラス・親クラスだけ使用可能
privateその関数があるクラスなかでのみ使用可能
無い場合無い場合はどのクラスからでもアクセス可能=public

作ってみよう

今回は経済の概念を追加するプラグインを作ります。

コード


<?php
namespace main;

use pocketmine\plugin\PluginBase;
use pocketmine\Player;
use pocketmine\Server;
use pocketmine\event\Listener;
use pocketmine\utils\TextFormat;
use pocketmine\utils\Config;
use pocketmine\event\player\PlayerJoinEvent;
class SimpleCoins extends PluginBase implements Listener{
	
	public function onEnable(){
		$this->getServer()->getPluginManager()->registerEvents($this,$this);
		$this->getLogger()->info(TextFormat::YELLOW."シンプルコイン");
		if(!file_exists($this->getDataFolder())){
			mkdir($this->getDataFolder(), 0744, true);
		}
		$this->config = new Config($this->getDataFolder() . "coin.yml", Config::YAML,array());//Config
	}
	
	public function onjoin(PlayerJoinEvent $event){
		$username = $event->getPlayer()->getName();
		$this->config = new Config($this->getDataFolder() . "coin.yml", Config::YAML,array());//Config
		if($this->config->exists($username)){
		}else{
			$this->config->set($username,0);
			$this->config->save();
		}
	}
	
	public function get($username){
		$this->config = new Config($this->getDataFolder() . "coin.yml", Config::YAML,array());//Config
		if($this->config->exists($username)){
			return $this->config->get($username);
		}else{
			$this->config->set($username,0);
			$this->config->save();
			return 0;
		}
	}
	
	public function set($username,$coin){
		$this->config = new Config($this->getDataFolder() . "coin.yml", Config::YAML,array());//Config
		if($this->config->exists($username)){
			$this->config->set($username,$coin);
			$this->config->save();
		}else{
			$this->config->set($username,$coin);
			$this->config->save();
		}
	}
}

利用したい時

仮にこのプラグインの名前がMoneyだったとすると
利用するときにonEnableに
        if($this->getServer()->getPluginManager()->getPlugin("Money") != null){
            //PluginManagerからMoneyのインスタンスを取得
            $this->Money = $this->getServer()->getPluginManager()->getPlugin("Money");
            $this->getLogger()->info("Moneyを検出しました。");
        }else{
            $this->getLogger()->warning("Moneyが見つかりませんでした");
            $this->getServer()->getPluginManager()->disablePlugin($this);//このプラグインを無効化する
        }
このコードを追加する必要があります。

プレイヤーが参加(Join)したときにお金を増やすとしたら
public function onJoin(PlayerJoinEvent $event) {
    $player = $event->getPlayer();
    $Money = 100;//付与する金額
    //getで指定プレイヤーの所持金額を取得
    $Money = $this->Money->get($player->getName());
    //MoneyのAPIを使ってお金を付与
    $this->Money->set($player->getName(), $Money);//括弧内に名前とお金の量を入れる
}
所持金額を取得するのにget(名前)
所持金額を設定するのにset(名前,金額)

メニュー

トップページ
メンバー募集メンバーページ

カウンター

訪問者数

アクセス数

フリーエリア



メンバーのみ編集できます