Skip to content
This repository has been archived by the owner on Nov 14, 2024. It is now read-only.

Commit

Permalink
save inventory to bot
Browse files Browse the repository at this point in the history
  • Loading branch information
MakStashkevich committed Dec 22, 2019
1 parent 1997a0f commit 083614e
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 4 deletions.
13 changes: 13 additions & 0 deletions client/Bot.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace client;

use client\entity\EntityHelpers;
use client\entity\inventory\PlayerInventory;
use client\level\Level;
use pocketmine\entity\Attribute;
use pocketmine\math\Vector3;
Expand Down Expand Up @@ -96,6 +97,9 @@ class Bot
/** @var array */
public $playersOnline = [];

/** @var PlayerInventory */
private $inventory;

/**
* Bot constructor.
* @param string $username
Expand All @@ -110,6 +114,15 @@ function __construct(string $username = null, string $password = null, Address $
$this->address = $address ?? new Address('0.0.0.0', 19130);
$this->skin = $skin;
$this->level = new Level();
$this->inventory = new PlayerInventory();
}

/**
* @return PlayerInventory
*/
function getInventory(): PlayerInventory
{
return $this->inventory;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions client/PocketEditionClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ protected function handleDataPacket(DataPacket $packet): void
try {
$packet->decode();
} catch (Throwable $e) {
echo "Error in decode " . $class . PHP_EOL . $e->getMessage() . PHP_EOL;
error('Error in decode ' . $class . PHP_EOL . $e->getMessage());
return;
}
if ($packet instanceof PlayStatusPacket) {
Expand All @@ -738,10 +738,10 @@ protected function handleDataPacket(DataPacket $packet): void
} elseif ($packet instanceof ContainerSetContentPacket) {
$player = $this->getPlayer();
if ($packet->targetEid === $player->getId()) {
$slots = $packet->slots;
switch ($packet->windowid) {
case ContainerIds::INVENTORY:
$slots = $packet->slots;
//todo: add slots to cache
$player->getInventory()->setAll($slots);
break;
case ContainerIds::ARMOR:
//????
Expand Down
8 changes: 7 additions & 1 deletion client/entity/inventory/InventoryType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

class InventoryType
{
/** @var int */
const DEFAULT = -1;

/** @var int */
const CHEST = 0;
const DOUBLE_CHEST = 1;
const PLAYER = 2;
Expand Down Expand Up @@ -36,7 +40,7 @@ class InventoryType
*/
public static function get($index)
{
return static::$default[$index] ?? null;
return static::$default[$index] ?? static::$default[self::DEFAULT];
}

public static function init()
Expand All @@ -46,6 +50,8 @@ public static function init()
}

static::$default = [
static::DEFAULT => new InventoryType(0, 'Default', WindowTypes::CONTAINER),

static::CHEST => new InventoryType(27, 'Chest', WindowTypes::CONTAINER),
static::DOUBLE_CHEST => new InventoryType(27 + 27, 'Double Chest', WindowTypes::CONTAINER),
static::PLAYER => new InventoryType(36 + 4, 'Player', WindowTypes::INVENTORY), //36 CONTAINER, 4 ARMOR
Expand Down
73 changes: 73 additions & 0 deletions client/entity/inventory/PlayerInventory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace client\entity\inventory;

use pocketmine\item\Item;

class PlayerInventory
{
/** @var array */
private $slots = [];

/** @var int */
private $type = InventoryType::PLAYER;

/**
* PlayerInventory constructor.
*/
function __construct()
{
$this->slots = array_fill(0, $this->getMax(), new Item(0));
}

/**
* @param array $slots
*/
function setAll(array $slots = [])
{
$max = $this->getMax();
if (count($slots) !== $max) {
error('U need give ' . $max . ' slots to setAll()');
return;
}
$this->slots = $slots;
}

/**
* @return array
*/
function getAll(): array
{
return $this->slots;
}

/**
* @param int $slot
* @return mixed
*/
function getSlot(int $slot): Item
{
$max = $this->getMax();
if ($slot > $max) {
error('U call getSlot() with $slot = ' . $slot . ', but max = ' . $max);
return new Item(0);
}
return $this->slots[$slot];
}

/**
* @return InventoryType
*/
function getType(): InventoryType
{
return InventoryType::get($this->type);
}

/**
* @return int
*/
function getMax(): int
{
return $this->getType()->getDefaultSize();
}
}

0 comments on commit 083614e

Please sign in to comment.