Skip to content

Instantly share code, notes, and snippets.

@ivon852
Last active September 10, 2021 16:19
Show Gist options
  • Select an option

  • Save ivon852/96ab52839894798a64bf66dfae16f9ef to your computer and use it in GitHub Desktop.

Select an option

Save ivon852/96ab52839894798a64bf66dfae16f9ef to your computer and use it in GitHub Desktop.
<?php
//命名空間即src/下的路徑
namespace examplePlugin;
//會用到的PocketMine類別
use pocketmine\plugin\PluginBase;
use pocketmine\event\Listener;
use pocketmine\event\player\PlayerJoinEvent;
use pocketmine\utils\TextFormat;
use pocketmine\command\Command;
use pocketmine\command\CommandSender;
use pocketmine\item\Item;
use pocketmine\Player;
class Main extends PluginBase implements Listener
{
//插件載入時
public function onLoad()
{
$this->getLogger()->info("插件載入中");
}
//插件啟用時
public function onEnable()
{
$this->getServer()->getPluginManager()->registerEvents($this, $this);
$this->getLogger()->info("插件已啟用");
}
//插件停用時
public function onDisable()
{
$this->getLogger()->info("插件已停用");
}
//玩家進入世界時
public function onJoin(PlayerJoinEvent $event)
{
$player = $event->getPlayer();
$name = $player->getName();
$this->getServer()->broadcastMessage(TextFormat::GREEN . "$name 加入了伺服器!");
}
//偵測指令
public function onCommand(CommandSender $sender, Command $cmd, string $label, array $args): bool
{
//檢查是在遊戲中輸入或是終端機
if (!$sender instanceof Player) {
$sender->sendMessage("僅限遊戲中使用。");
} else {
if ($cmd->getName() == "getsteak") {
if (!isset($args[0]) or (is_int($args[0]) and $args[0] > 0)) { //確認參數為0且為整數,否則就預設給4個
$args[0] = 4;
}
$sender->getInventory()->addItem(Item::get(364, 0, $args[0])); //給玩家牛排
$sender->sendMessage("已獲得" . $args[0] . "個牛排!");
}
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment