Skip to content

DaisukeDaisukeTeam/Gacha

Repository files navigation


Virion library for Gacha

プラグイン開発者向けのガチャ用ライブラリ

環境

PHP 8.0
PocketMine-MP 4.0.0

インストール

  1. VirionToolspluginsの中に配置します
  2. サーバーを再起動します
  3. plugin_data/VirionTools/buildsにダウンロードしたGacha.pharを配置します
  4. コマンドラインより、$ iv Gacha [導入したいプラグインの名前]を実行します

サンプル

[サンプルコード]
https://github.com/Rark7040/GachaExample

合計の確率が100%に満たないもしくはそれより多い場合、
全ての排出アイテムに対し、比率的に均等に確率が調整されます

$gacha = new Gacha(
	'TestGacha',
	'example',
	new GachaMessages(),
	new RandomItemTable(
		PMGachaItem::create(Rarity::create('N', 60), VanillaItems::COAL()),
		PMGachaItem::create(Rarity::create('R', 20), VanillaItems::IRON_INGOT()),
		PMGachaItem::create(Rarity::create('SR', 8), VanillaItems::GOLD_INGOT()),
		PMGachaItem::create(Rarity::create('SSR', 3), VanillaItems::DIAMOND()),
		PMGachaItem::create(Rarity::create('UR', 0.6), VanillaItems::EMERALD()),
	),
	new ExampleTicket()
);

$gacha->roll($player, 1);
Other Code
<?php
declare(strict_types=1);

namespace rarkhopper\gacha_example;

use pocketmine\item\Item;
use pocketmine\player\Player;
use pocketmine\world\sound\PopSound;
use rarkhopper\gacha\IGachaItem;
use rarkhopper\gacha\IRarity;
use rarkhopper\gacha\Rarity;

class PMGachaItem implements IGachaItem{
	protected Rarity $rarity;
	protected Item $item;

	public static function create(Rarity $rarity, Item $item):static{
		$instance = new static;
		$instance->rarity = $rarity;
		$instance->item = $item;
		return $instance;
	}

	public function getRarity():IRarity{
		return $this->rarity;
	}

	public function giveItem(Player $player):void{
		$player->sendMessage('['.$this->getRarity()->getName().'] '.$this->item->getName());
		$player->getInventory()->addItem(clone $this->item);
		$world = $player->getWorld();
		$world->addSound($player->getPosition(), new PopSound());
	}
}
<?php
declare(strict_types=1);

namespace rarkhopper\gacha_example;

use pocketmine\item\VanillaItems;
use pocketmine\player\Player;
use rarkhopper\gacha\ITicket;

class ExampleTicket implements ITicket{
	public function has(Player $player, int $count):bool{
		return $player->getInventory()->contains(VanillaItems::GOLD_NUGGET()->setCount($count));
	}

	public function consume(Player $player, int $count):void{
		$player->getInventory()->removeItem(VanillaItems::GOLD_NUGGET()->setCount($count));
	}
}