Skip to content

Instantly share code, notes, and snippets.

@Muqsit
Created January 25, 2018 18:22
Show Gist options
  • Save Muqsit/47d07f791bd45a3bc4cf4e5fbc9ccc10 to your computer and use it in GitHub Desktop.
Save Muqsit/47d07f791bd45a3bc4cf4e5fbc9ccc10 to your computer and use it in GitHub Desktop.
<?php
namespace villagertrade;
use pocketmine\entity\Villager;
use pocketmine\inventory\BaseInventory;
use pocketmine\item\Item;
use pocketmine\nbt\NetworkLittleEndianNBTStream;
use pocketmine\nbt\tag\{ByteTag, CompoundTag, IntTag, ListTag};
use pocketmine\network\mcpe\protocol\UpdateTradePacket;
use pocketmine\Player;
class VillagerInventory extends BaseInventory {
/** @var Villager */
private $villager;
public function __construct(Villager $villager)
{
$this->villager = $villager;
parent::__construct();
}
public function getName() : string
{
return "Villager Inventory";
}
public function getDefaultSize() : int
{
return 2;
}
public function getVillager() : Villager
{
return $this->villager;
}
public function onOpen(Player $player) : void
{
parent::onOpen($player);
$pk = new UpdateTradePacket();
$pk->windowId = $player->getWindowId($this);
$pk->varint1 = 0;//could be career?
$pk->varint2 = 0;//could be careerlevel?
$pk->isWilling = false;
$pk->traderEid = $this->villager->getId();
$pk->playerEid = $player->getId();
$pk->displayName = $this->getName();
$writer = new \pocketmine\nbt\NetworkLittleEndianNBTStream();
$writer->setData(
new CompoundTag("Offers", [
new ListTag("Recipes", [
new CompoundTag("", [
new ByteTag("rewardExp", 1),
new IntTag("maxUses", mt_rand(2, 12)),
new IntTag("uses", 0),
Item::get(Item::DIAMOND)->nbtSerialize(-1, "buy"),
Item::get(Item::PAPER)->nbtSerialize(-1, "sell")
])
])
])
);
$pk->offers = $writer->write();
$player->dataPacket($pk);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment