
API(Application Programming Interface)とは他のプラグインから
使える、アクセス修飾子がpublicなどの関数が集まったものです。
オブジェクト指向がわかっていれば簡単にできます。
使える、アクセス修飾子が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に
プレイヤーが参加(Join)したときにお金を増やすとしたら
所持金額を設定するのにset(名前,金額)
利用するときに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(名前,金額)

最新コメント