mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-10-18 12:04:46 +00:00
Merge branch 'stable' into next-minor
This commit is contained in:
@@ -46,7 +46,10 @@ abstract class BaseInventory implements Inventory{
|
||||
protected $name;
|
||||
/** @var string */
|
||||
protected $title;
|
||||
/** @var \SplFixedArray|(Item|null)[] */
|
||||
/**
|
||||
* @var \SplFixedArray|(Item|null)[]
|
||||
* @phpstan-var \SplFixedArray<Item|null>
|
||||
*/
|
||||
protected $slots;
|
||||
/** @var Player[] */
|
||||
protected $viewers = [];
|
||||
@@ -55,8 +58,6 @@ abstract class BaseInventory implements Inventory{
|
||||
|
||||
/**
|
||||
* @param Item[] $items
|
||||
* @param int $size
|
||||
* @param string $title
|
||||
*/
|
||||
public function __construct(array $items = [], int $size = null, string $title = null){
|
||||
$this->slots = new \SplFixedArray($size ?? $this->getDefaultSize());
|
||||
@@ -73,7 +74,6 @@ abstract class BaseInventory implements Inventory{
|
||||
|
||||
/**
|
||||
* Returns the size of the inventory.
|
||||
* @return int
|
||||
*/
|
||||
public function getSize() : int{
|
||||
return $this->slots->getSize();
|
||||
@@ -83,7 +83,7 @@ abstract class BaseInventory implements Inventory{
|
||||
* Sets the new size of the inventory.
|
||||
* WARNING: If the size is smaller, any items past the new size will be lost.
|
||||
*
|
||||
* @param int $size
|
||||
* @return void
|
||||
*/
|
||||
public function setSize(int $size){
|
||||
$this->slots->setSize($size);
|
||||
@@ -100,8 +100,6 @@ abstract class BaseInventory implements Inventory{
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $includeEmpty
|
||||
*
|
||||
* @return Item[]
|
||||
*/
|
||||
public function getContents(bool $includeEmpty = false) : array{
|
||||
@@ -121,7 +119,6 @@ abstract class BaseInventory implements Inventory{
|
||||
|
||||
/**
|
||||
* @param Item[] $items
|
||||
* @param bool $send
|
||||
*/
|
||||
public function setContents(array $items, bool $send = true) : void{
|
||||
if(count($items) > $this->getSize()){
|
||||
@@ -148,9 +145,6 @@ abstract class BaseInventory implements Inventory{
|
||||
/**
|
||||
* Drops the contents of the inventory into the specified Level at the specified position and clears the inventory
|
||||
* contents.
|
||||
*
|
||||
* @param Level $level
|
||||
* @param Vector3 $position
|
||||
*/
|
||||
public function dropContents(Level $level, Vector3 $position) : void{
|
||||
foreach($this->getContents() as $item){
|
||||
@@ -437,7 +431,6 @@ abstract class BaseInventory implements Inventory{
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Player|Player[] $target
|
||||
*/
|
||||
@@ -460,7 +453,6 @@ abstract class BaseInventory implements Inventory{
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $index
|
||||
* @param Player|Player[] $target
|
||||
*/
|
||||
public function sendSlot(int $index, $target) : void{
|
||||
|
@@ -23,6 +23,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\inventory;
|
||||
|
||||
use pocketmine\level\Position;
|
||||
use pocketmine\network\mcpe\protocol\BlockEventPacket;
|
||||
use pocketmine\network\mcpe\protocol\LevelSoundEventPacket;
|
||||
use pocketmine\network\mcpe\protocol\types\WindowTypes;
|
||||
@@ -35,9 +36,6 @@ class ChestInventory extends ContainerInventory{
|
||||
/** @var Chest */
|
||||
protected $holder;
|
||||
|
||||
/**
|
||||
* @param Chest $tile
|
||||
*/
|
||||
public function __construct(Chest $tile){
|
||||
parent::__construct($tile);
|
||||
}
|
||||
@@ -56,7 +54,7 @@ class ChestInventory extends ContainerInventory{
|
||||
|
||||
/**
|
||||
* This override is here for documentation and code completion purposes only.
|
||||
* @return Chest
|
||||
* @return Chest|Position
|
||||
*/
|
||||
public function getHolder(){
|
||||
return $this->holder;
|
||||
|
@@ -50,7 +50,7 @@ abstract class ContainerInventory extends BaseInventory{
|
||||
|
||||
if($holder instanceof Entity){
|
||||
$pk->entityUniqueId = $holder->getId();
|
||||
}elseif($holder instanceof Vector3){
|
||||
}else{
|
||||
$pk->x = $holder->getFloorX();
|
||||
$pk->y = $holder->getFloorY();
|
||||
$pk->z = $holder->getFloorZ();
|
||||
@@ -70,7 +70,6 @@ abstract class ContainerInventory extends BaseInventory{
|
||||
|
||||
/**
|
||||
* Returns the Minecraft PE inventory type used to show the inventory window to clients.
|
||||
* @return int
|
||||
*/
|
||||
abstract public function getNetworkType() : int;
|
||||
|
||||
|
@@ -129,11 +129,6 @@ class CraftingGrid extends BaseInventory{
|
||||
|
||||
/**
|
||||
* Returns the item at offset x,y, offset by where the starts of the recipe rectangle are.
|
||||
*
|
||||
* @param int $x
|
||||
* @param int $y
|
||||
*
|
||||
* @return Item
|
||||
*/
|
||||
public function getIngredient(int $x, int $y) : Item{
|
||||
if($this->startX !== null and $this->startY !== null){
|
||||
@@ -145,8 +140,6 @@ class CraftingGrid extends BaseInventory{
|
||||
|
||||
/**
|
||||
* Returns the width of the recipe we're trying to craft, based on items currently in the grid.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getRecipeWidth() : int{
|
||||
return $this->xLen ?? 0;
|
||||
@@ -154,7 +147,6 @@ class CraftingGrid extends BaseInventory{
|
||||
|
||||
/**
|
||||
* Returns the height of the recipe we're trying to craft, based on items currently in the grid.
|
||||
* @return int
|
||||
*/
|
||||
public function getRecipeHeight() : int{
|
||||
return $this->yLen ?? 0;
|
||||
|
@@ -128,8 +128,6 @@ class CraftingManager{
|
||||
|
||||
/**
|
||||
* Returns a pre-compressed CraftingDataPacket for sending to players. Rebuilds the cache if it is not found.
|
||||
*
|
||||
* @return BatchPacket
|
||||
*/
|
||||
public function getCraftingDataPacket() : BatchPacket{
|
||||
if($this->craftingDataCache === null){
|
||||
@@ -142,14 +140,11 @@ class CraftingManager{
|
||||
/**
|
||||
* Function used to arrange Shapeless Recipe ingredient lists into a consistent order.
|
||||
*
|
||||
* @param Item $i1
|
||||
* @param Item $i2
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public static function sort(Item $i1, Item $i2){
|
||||
//Use spaceship operator to compare each property, then try the next one if they are equivalent.
|
||||
($retval = $i1->getId() <=> $i2->getId()) === 0 && ($retval = $i1->getDamage() <=> $i2->getDamage()) === 0 && ($retval = $i1->getCount() <=> $i2->getCount());
|
||||
($retval = $i1->getId() <=> $i2->getId()) === 0 && ($retval = $i1->getDamage() <=> $i2->getDamage()) === 0 && ($retval = $i1->getCount() <=> $i2->getCount()) === 0;
|
||||
|
||||
return $retval;
|
||||
}
|
||||
@@ -178,6 +173,9 @@ class CraftingManager{
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Item[] $outputs
|
||||
*/
|
||||
private static function hashOutputs(array $outputs) : string{
|
||||
$outputs = self::pack($outputs);
|
||||
usort($outputs, [self::class, "sort"]);
|
||||
@@ -210,27 +208,18 @@ class CraftingManager{
|
||||
return $this->furnaceRecipes;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ShapedRecipe $recipe
|
||||
*/
|
||||
public function registerShapedRecipe(ShapedRecipe $recipe) : void{
|
||||
$this->shapedRecipes[self::hashOutputs($recipe->getResults())][] = $recipe;
|
||||
|
||||
$this->craftingDataCache = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ShapelessRecipe $recipe
|
||||
*/
|
||||
public function registerShapelessRecipe(ShapelessRecipe $recipe) : void{
|
||||
$this->shapelessRecipes[self::hashOutputs($recipe->getResults())][] = $recipe;
|
||||
|
||||
$this->craftingDataCache = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param FurnaceRecipe $recipe
|
||||
*/
|
||||
public function registerFurnaceRecipe(FurnaceRecipe $recipe) : void{
|
||||
$input = $recipe->getInput();
|
||||
$this->furnaceRecipes[$input->getId() . ":" . ($input->hasAnyDamageValue() ? "?" : $input->getDamage())] = $recipe;
|
||||
@@ -238,10 +227,7 @@ class CraftingManager{
|
||||
}
|
||||
|
||||
/**
|
||||
* @param CraftingGrid $grid
|
||||
* @param Item[] $outputs
|
||||
*
|
||||
* @return CraftingRecipe|null
|
||||
*/
|
||||
public function matchRecipe(CraftingGrid $grid, array $outputs) : ?CraftingRecipe{
|
||||
//TODO: try to match special recipes before anything else (first they need to be implemented!)
|
||||
@@ -271,6 +257,7 @@ class CraftingManager{
|
||||
* @param Item[] $outputs
|
||||
*
|
||||
* @return CraftingRecipe[]|\Generator
|
||||
* @phpstan-return \Generator<int, CraftingRecipe, void, void>
|
||||
*/
|
||||
public function matchRecipeByOutputs(array $outputs) : \Generator{
|
||||
//TODO: try to match special recipes before anything else (first they need to be implemented!)
|
||||
@@ -290,19 +277,12 @@ class CraftingManager{
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Item $input
|
||||
*
|
||||
* @return FurnaceRecipe|null
|
||||
*/
|
||||
public function matchFurnaceRecipe(Item $input) : ?FurnaceRecipe{
|
||||
return $this->furnaceRecipes[$input->getId() . ":" . $input->getDamage()] ?? $this->furnaceRecipes[$input->getId() . ":?"] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*
|
||||
* @param Recipe $recipe
|
||||
*/
|
||||
public function registerRecipe(Recipe $recipe) : void{
|
||||
$recipe->registerToCraftingManager($this);
|
||||
|
@@ -36,18 +36,12 @@ interface CraftingRecipe extends Recipe{
|
||||
/**
|
||||
* Returns a list of results this recipe will produce when the inputs in the given crafting grid are consumed.
|
||||
*
|
||||
* @param CraftingGrid $grid
|
||||
*
|
||||
* @return Item[]
|
||||
*/
|
||||
public function getResultsFor(CraftingGrid $grid) : array;
|
||||
|
||||
/**
|
||||
* Returns whether the given crafting grid meets the requirements to craft this recipe.
|
||||
*
|
||||
* @param CraftingGrid $grid
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function matchesCraftingGrid(CraftingGrid $grid) : bool;
|
||||
}
|
||||
|
@@ -24,6 +24,7 @@ declare(strict_types=1);
|
||||
namespace pocketmine\inventory;
|
||||
|
||||
use pocketmine\item\Item;
|
||||
use pocketmine\level\Position;
|
||||
use pocketmine\Player;
|
||||
use pocketmine\tile\Chest;
|
||||
use function array_merge;
|
||||
@@ -56,7 +57,7 @@ class DoubleChestInventory extends ChestInventory implements InventoryHolder{
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Chest
|
||||
* @return Chest|Position
|
||||
*/
|
||||
public function getHolder(){
|
||||
return $this->left->getHolder();
|
||||
@@ -88,7 +89,6 @@ class DoubleChestInventory extends ChestInventory implements InventoryHolder{
|
||||
|
||||
/**
|
||||
* @param Item[] $items
|
||||
* @param bool $send
|
||||
*/
|
||||
public function setContents(array $items, bool $send = true) : void{
|
||||
$size = $this->getSize();
|
||||
@@ -128,20 +128,17 @@ class DoubleChestInventory extends ChestInventory implements InventoryHolder{
|
||||
parent::onClose($who);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ChestInventory
|
||||
*/
|
||||
public function getLeftSide() : ChestInventory{
|
||||
return $this->left;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ChestInventory
|
||||
*/
|
||||
public function getRightSide() : ChestInventory{
|
||||
return $this->right;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function invalidate(){
|
||||
$this->left = null;
|
||||
$this->right = null;
|
||||
|
@@ -52,7 +52,7 @@ class EnderChestInventory extends ChestInventory{
|
||||
/**
|
||||
* Set the holder's position to that of a tile
|
||||
*
|
||||
* @param EnderChest $enderChest
|
||||
* @return void
|
||||
*/
|
||||
public function setHolderPosition(EnderChest $enderChest){
|
||||
$this->holder->setComponents($enderChest->getFloorX(), $enderChest->getFloorY(), $enderChest->getFloorZ());
|
||||
|
@@ -55,50 +55,26 @@ class FurnaceInventory extends ContainerInventory{
|
||||
return $this->holder;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Item
|
||||
*/
|
||||
public function getResult() : Item{
|
||||
return $this->getItem(2);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Item
|
||||
*/
|
||||
public function getFuel() : Item{
|
||||
return $this->getItem(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Item
|
||||
*/
|
||||
public function getSmelting() : Item{
|
||||
return $this->getItem(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Item $item
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function setResult(Item $item) : bool{
|
||||
return $this->setItem(2, $item);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Item $item
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function setFuel(Item $item) : bool{
|
||||
return $this->setItem(1, $item);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Item $item
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function setSmelting(Item $item) : bool{
|
||||
return $this->setItem(0, $item);
|
||||
}
|
||||
|
@@ -33,32 +33,22 @@ class FurnaceRecipe implements Recipe{
|
||||
/** @var Item */
|
||||
private $ingredient;
|
||||
|
||||
/**
|
||||
* @param Item $result
|
||||
* @param Item $ingredient
|
||||
*/
|
||||
public function __construct(Item $result, Item $ingredient){
|
||||
$this->output = clone $result;
|
||||
$this->ingredient = clone $ingredient;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Item $item
|
||||
* @return void
|
||||
*/
|
||||
public function setInput(Item $item){
|
||||
$this->ingredient = clone $item;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Item
|
||||
*/
|
||||
public function getInput() : Item{
|
||||
return clone $this->ingredient;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Item
|
||||
*/
|
||||
public function getResult() : Item{
|
||||
return clone $this->output;
|
||||
}
|
||||
|
@@ -34,47 +34,21 @@ use pocketmine\Player;
|
||||
interface Inventory{
|
||||
public const MAX_STACK = 64;
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getSize() : int;
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getMaxStackSize() : int;
|
||||
|
||||
/**
|
||||
* @param int $size
|
||||
*/
|
||||
public function setMaxStackSize(int $size) : void;
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName() : string;
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getTitle() : string;
|
||||
|
||||
/**
|
||||
* @param int $index
|
||||
*
|
||||
* @return Item
|
||||
*/
|
||||
public function getItem(int $index) : Item;
|
||||
|
||||
/**
|
||||
* Puts an Item in a slot.
|
||||
* If a plugin refuses the update or $index is invalid, it'll return false
|
||||
*
|
||||
* @param int $index
|
||||
* @param Item $item
|
||||
* @param bool $send
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function setItem(int $index, Item $item, bool $send = true) : bool;
|
||||
|
||||
@@ -92,10 +66,6 @@ interface Inventory{
|
||||
|
||||
/**
|
||||
* Checks if a given Item can be added to the inventory
|
||||
*
|
||||
* @param Item $item
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function canAddItem(Item $item) : bool;
|
||||
|
||||
@@ -110,24 +80,18 @@ interface Inventory{
|
||||
public function removeItem(Item ...$slots) : array;
|
||||
|
||||
/**
|
||||
* @param bool $includeEmpty
|
||||
*
|
||||
* @return Item[]
|
||||
*/
|
||||
public function getContents(bool $includeEmpty = false) : array;
|
||||
|
||||
/**
|
||||
* @param Item[] $items
|
||||
* @param bool $send
|
||||
*/
|
||||
public function setContents(array $items, bool $send = true) : void;
|
||||
|
||||
/**
|
||||
* Drops the contents of the inventory into the specified Level at the specified position and clears the inventory
|
||||
* contents.
|
||||
*
|
||||
* @param Level $level
|
||||
* @param Vector3 $position
|
||||
*/
|
||||
public function dropContents(Level $level, Vector3 $position) : void;
|
||||
|
||||
@@ -137,7 +101,6 @@ interface Inventory{
|
||||
public function sendContents($target) : void;
|
||||
|
||||
/**
|
||||
* @param int $index
|
||||
* @param Player|Player[] $target
|
||||
*/
|
||||
public function sendSlot(int $index, $target) : void;
|
||||
@@ -145,10 +108,6 @@ interface Inventory{
|
||||
/**
|
||||
* Checks if the inventory contains any Item with the same material data.
|
||||
* It will check id, amount, and metadata (if not null)
|
||||
*
|
||||
* @param Item $item
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function contains(Item $item) : bool;
|
||||
|
||||
@@ -156,8 +115,6 @@ interface Inventory{
|
||||
* Will return all the Items that has the same id and metadata (if not null).
|
||||
* Won't check amount
|
||||
*
|
||||
* @param Item $item
|
||||
*
|
||||
* @return Item[]
|
||||
*/
|
||||
public function all(Item $item) : array;
|
||||
@@ -167,51 +124,31 @@ interface Inventory{
|
||||
* and count >= to the count of the specified item stack.
|
||||
*
|
||||
* If $exact is true, only items with equal ID, damage, NBT and count will match.
|
||||
*
|
||||
* @param Item $item
|
||||
* @param bool $exact
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function first(Item $item, bool $exact = false) : int;
|
||||
|
||||
/**
|
||||
* Returns the first empty slot, or -1 if not found
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function firstEmpty() : int;
|
||||
|
||||
/**
|
||||
* Returns whether the given slot is empty.
|
||||
*
|
||||
* @param int $index
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isSlotEmpty(int $index) : bool;
|
||||
|
||||
/**
|
||||
* Will remove all the Items that has the same id and metadata (if not null)
|
||||
*
|
||||
* @param Item $item
|
||||
*/
|
||||
public function remove(Item $item) : void;
|
||||
|
||||
/**
|
||||
* Will clear a specific slot
|
||||
*
|
||||
* @param int $index
|
||||
* @param bool $send
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function clear(int $index, bool $send = true) : bool;
|
||||
|
||||
/**
|
||||
* Clears all the slots
|
||||
*
|
||||
* @param bool $send
|
||||
*/
|
||||
public function clearAll(bool $send = true) : void;
|
||||
|
||||
@@ -231,50 +168,25 @@ interface Inventory{
|
||||
*/
|
||||
public function getViewers() : array;
|
||||
|
||||
/**
|
||||
* @param Player $who
|
||||
*/
|
||||
public function onOpen(Player $who) : void;
|
||||
|
||||
/**
|
||||
* Tries to open the inventory to a player
|
||||
*
|
||||
* @param Player $who
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function open(Player $who) : bool;
|
||||
|
||||
public function close(Player $who) : void;
|
||||
|
||||
/**
|
||||
* @param Player $who
|
||||
*/
|
||||
public function onClose(Player $who) : void;
|
||||
|
||||
/**
|
||||
* @param int $index
|
||||
* @param Item $before
|
||||
* @param bool $send
|
||||
*/
|
||||
public function onSlotChange(int $index, Item $before, bool $send) : void;
|
||||
|
||||
/**
|
||||
* Returns whether the specified slot exists in the inventory.
|
||||
*
|
||||
* @param int $slot
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function slotExists(int $slot) : bool;
|
||||
|
||||
/**
|
||||
* @return null|InventoryEventProcessor
|
||||
*/
|
||||
public function getEventProcessor() : ?InventoryEventProcessor;
|
||||
|
||||
/**
|
||||
* @param null|InventoryEventProcessor $eventProcessor
|
||||
*/
|
||||
public function setEventProcessor(?InventoryEventProcessor $eventProcessor) : void;
|
||||
}
|
||||
|
@@ -37,11 +37,6 @@ interface InventoryEventProcessor{
|
||||
* Called prior to a slot in the given inventory changing. This is called by inventories that this listener is
|
||||
* attached to.
|
||||
*
|
||||
* @param Inventory $inventory
|
||||
* @param int $slot
|
||||
* @param Item $oldItem
|
||||
* @param Item $newItem
|
||||
*
|
||||
* @return Item|null that should be used in place of $newItem, or null if the slot change should not proceed.
|
||||
*/
|
||||
public function onSlotChange(Inventory $inventory, int $slot, Item $oldItem, Item $newItem) : ?Item;
|
||||
|
@@ -39,6 +39,7 @@ class MultiRecipe{
|
||||
public const TYPE_FIREWORKS = "00000000-0000-0000-0000-000000000002";
|
||||
public const TYPE_MAP_LOCKING_CARTOGRAPHY = "602234E4-CAC1-4353-8BB7-B1EBFF70024B";
|
||||
|
||||
/** @var UUID */
|
||||
private $uuid;
|
||||
|
||||
public function __construct(UUID $uuid){
|
||||
|
@@ -41,9 +41,6 @@ class PlayerInventory extends BaseInventory{
|
||||
/** @var int */
|
||||
protected $itemInHandIndex = 0;
|
||||
|
||||
/**
|
||||
* @param Human $player
|
||||
*/
|
||||
public function __construct(Human $player){
|
||||
$this->holder = $player;
|
||||
parent::__construct();
|
||||
@@ -93,11 +90,9 @@ class PlayerInventory extends BaseInventory{
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $slot
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
private function throwIfNotHotbarSlot(int $slot){
|
||||
private function throwIfNotHotbarSlot(int $slot) : void{
|
||||
if(!$this->isHotbarSlot($slot)){
|
||||
throw new \InvalidArgumentException("$slot is not a valid hotbar slot index (expected 0 - " . ($this->getHotbarSize() - 1) . ")");
|
||||
}
|
||||
@@ -106,10 +101,6 @@ class PlayerInventory extends BaseInventory{
|
||||
/**
|
||||
* Returns the item in the specified hotbar slot.
|
||||
*
|
||||
* @param int $hotbarSlot
|
||||
*
|
||||
* @return Item
|
||||
*
|
||||
* @throws \InvalidArgumentException if the hotbar slot index is out of range
|
||||
*/
|
||||
public function getHotbarSlotItem(int $hotbarSlot) : Item{
|
||||
@@ -119,7 +110,6 @@ class PlayerInventory extends BaseInventory{
|
||||
|
||||
/**
|
||||
* Returns the hotbar slot number the holder is currently holding.
|
||||
* @return int
|
||||
*/
|
||||
public function getHeldItemIndex() : int{
|
||||
return $this->itemInHandIndex;
|
||||
@@ -132,6 +122,7 @@ class PlayerInventory extends BaseInventory{
|
||||
* @param bool $send Whether to send updates back to the inventory holder. This should usually be true for plugin calls.
|
||||
* It should only be false to prevent feedback loops of equipment packets between client and server.
|
||||
*
|
||||
* @return void
|
||||
* @throws \InvalidArgumentException if the hotbar slot is out of range
|
||||
*/
|
||||
public function setHeldItemIndex(int $hotbarSlot, bool $send = true){
|
||||
@@ -148,8 +139,6 @@ class PlayerInventory extends BaseInventory{
|
||||
|
||||
/**
|
||||
* Returns the currently-held item.
|
||||
*
|
||||
* @return Item
|
||||
*/
|
||||
public function getItemInHand() : Item{
|
||||
return $this->getHotbarSlotItem($this->itemInHandIndex);
|
||||
@@ -157,10 +146,6 @@ class PlayerInventory extends BaseInventory{
|
||||
|
||||
/**
|
||||
* Sets the item in the currently-held slot to the specified item.
|
||||
*
|
||||
* @param Item $item
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function setItemInHand(Item $item) : bool{
|
||||
return $this->setItem($this->getHeldItemIndex(), $item);
|
||||
@@ -170,6 +155,8 @@ class PlayerInventory extends BaseInventory{
|
||||
* Sends the currently-held item to specified targets.
|
||||
*
|
||||
* @param Player|Player[] $target
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function sendHeldItem($target){
|
||||
$item = $this->getItemInHand();
|
||||
@@ -195,12 +182,14 @@ class PlayerInventory extends BaseInventory{
|
||||
|
||||
/**
|
||||
* Returns the number of slots in the hotbar.
|
||||
* @return int
|
||||
*/
|
||||
public function getHotbarSize() : int{
|
||||
return 9;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function sendCreativeContents(){
|
||||
//TODO: this mess shouldn't be in here
|
||||
$holder = $this->getHolder();
|
||||
|
@@ -110,8 +110,6 @@ class ShapedRecipe implements CraftingRecipe{
|
||||
}
|
||||
|
||||
/**
|
||||
* @param CraftingGrid $grid
|
||||
*
|
||||
* @return Item[]
|
||||
*/
|
||||
public function getResultsFor(CraftingGrid $grid) : array{
|
||||
@@ -119,9 +117,6 @@ class ShapedRecipe implements CraftingRecipe{
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @param Item $item
|
||||
*
|
||||
* @return $this
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
@@ -168,12 +163,6 @@ class ShapedRecipe implements CraftingRecipe{
|
||||
return $ingredients;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $x
|
||||
* @param int $y
|
||||
*
|
||||
* @return Item
|
||||
*/
|
||||
public function getIngredient(int $x, int $y) : Item{
|
||||
$exists = $this->ingredientList[$this->shape[$y]{$x}] ?? null;
|
||||
return $exists !== null ? clone $exists : ItemFactory::get(Item::AIR, 0, 0);
|
||||
@@ -196,12 +185,6 @@ class ShapedRecipe implements CraftingRecipe{
|
||||
$manager->registerShapedRecipe($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param CraftingGrid $grid
|
||||
* @param bool $reverse
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function matchInputMap(CraftingGrid $grid, bool $reverse) : bool{
|
||||
for($y = 0; $y < $this->height; ++$y){
|
||||
for($x = 0; $x < $this->width; ++$x){
|
||||
@@ -217,11 +200,6 @@ class ShapedRecipe implements CraftingRecipe{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param CraftingGrid $grid
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function matchesCraftingGrid(CraftingGrid $grid) : bool{
|
||||
if($this->width !== $grid->getRecipeWidth() or $this->height !== $grid->getRecipeHeight()){
|
||||
return false;
|
||||
|
@@ -46,6 +46,9 @@ class ShapelessRecipe implements CraftingRecipe{
|
||||
$this->results = array_map(function(Item $item) : Item{ return clone $item; }, $results);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Item[]
|
||||
*/
|
||||
public function getResults() : array{
|
||||
return array_map(function(Item $item) : Item{ return clone $item; }, $this->results);
|
||||
}
|
||||
@@ -55,10 +58,6 @@ class ShapelessRecipe implements CraftingRecipe{
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Item $item
|
||||
*
|
||||
* @return ShapelessRecipe
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function addIngredient(Item $item) : ShapelessRecipe{
|
||||
@@ -74,8 +73,6 @@ class ShapelessRecipe implements CraftingRecipe{
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Item $item
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function removeIngredient(Item $item){
|
||||
@@ -99,9 +96,6 @@ class ShapelessRecipe implements CraftingRecipe{
|
||||
return array_map(function(Item $item) : Item{ return clone $item; }, $this->ingredients);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getIngredientCount() : int{
|
||||
$count = 0;
|
||||
foreach($this->ingredients as $ingredient){
|
||||
@@ -120,11 +114,6 @@ class ShapelessRecipe implements CraftingRecipe{
|
||||
$manager->registerShapelessRecipe($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param CraftingGrid $grid
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function matchesCraftingGrid(CraftingGrid $grid) : bool{
|
||||
//don't pack the ingredients - shapeless recipes require that each ingredient be in a separate slot
|
||||
$input = $grid->getContents();
|
||||
@@ -140,6 +129,6 @@ class ShapelessRecipe implements CraftingRecipe{
|
||||
return false; //failed to match the needed item to a given item
|
||||
}
|
||||
|
||||
return empty($input); //crafting grid should be empty apart from the given ingredient stacks
|
||||
return count($input) === 0; //crafting grid should be empty apart from the given ingredient stacks
|
||||
}
|
||||
}
|
||||
|
@@ -61,21 +61,18 @@ class CraftingTransaction extends InventoryTransaction{
|
||||
/**
|
||||
* @param Item[] $txItems
|
||||
* @param Item[] $recipeItems
|
||||
* @param bool $wildcards
|
||||
* @param int $iterations
|
||||
*
|
||||
* @return int
|
||||
* @throws TransactionValidationException
|
||||
*/
|
||||
protected function matchRecipeItems(array $txItems, array $recipeItems, bool $wildcards, int $iterations = 0) : int{
|
||||
if(empty($recipeItems)){
|
||||
if(count($recipeItems) === 0){
|
||||
throw new TransactionValidationException("No recipe items given");
|
||||
}
|
||||
if(empty($txItems)){
|
||||
if(count($txItems) === 0){
|
||||
throw new TransactionValidationException("No transaction items given");
|
||||
}
|
||||
|
||||
while(!empty($recipeItems)){
|
||||
while(count($recipeItems) > 0){
|
||||
/** @var Item $recipeItem */
|
||||
$recipeItem = array_pop($recipeItems);
|
||||
$needCount = $recipeItem->getCount();
|
||||
@@ -114,7 +111,7 @@ class CraftingTransaction extends InventoryTransaction{
|
||||
if($iterations < 1){
|
||||
throw new TransactionValidationException("Tried to craft zero times");
|
||||
}
|
||||
if(!empty($txItems)){
|
||||
if(count($txItems) > 0){
|
||||
//all items should be destroyed in this process
|
||||
throw new TransactionValidationException("Expected 0 ingredients left over, have " . count($txItems));
|
||||
}
|
||||
|
@@ -53,6 +53,7 @@ use function spl_object_hash;
|
||||
* @see InventoryAction
|
||||
*/
|
||||
class InventoryTransaction{
|
||||
/** @var bool */
|
||||
protected $hasExecuted = false;
|
||||
/** @var Player */
|
||||
protected $source;
|
||||
@@ -64,7 +65,6 @@ class InventoryTransaction{
|
||||
protected $actions = [];
|
||||
|
||||
/**
|
||||
* @param Player $source
|
||||
* @param InventoryAction[] $actions
|
||||
*/
|
||||
public function __construct(Player $source, array $actions = []){
|
||||
@@ -74,9 +74,6 @@ class InventoryTransaction{
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Player
|
||||
*/
|
||||
public function getSource() : Player{
|
||||
return $this->source;
|
||||
}
|
||||
@@ -100,9 +97,6 @@ class InventoryTransaction{
|
||||
return $this->actions;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param InventoryAction $action
|
||||
*/
|
||||
public function addAction(InventoryAction $action) : void{
|
||||
if(!isset($this->actions[$hash = spl_object_hash($action)])){
|
||||
$this->actions[$hash] = $action;
|
||||
@@ -128,8 +122,6 @@ class InventoryTransaction{
|
||||
/**
|
||||
* @internal This method should not be used by plugins, it's used to add tracked inventories for InventoryActions
|
||||
* involving inventories.
|
||||
*
|
||||
* @param Inventory $inventory
|
||||
*/
|
||||
public function addInventory(Inventory $inventory) : void{
|
||||
if(!isset($this->inventories[$hash = spl_object_hash($inventory)])){
|
||||
@@ -231,19 +223,16 @@ class InventoryTransaction{
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Item $needOrigin
|
||||
* @param SlotChangeAction[] $possibleActions
|
||||
*
|
||||
* @return null|Item
|
||||
*/
|
||||
protected function findResultItem(Item $needOrigin, array $possibleActions) : ?Item{
|
||||
assert(!empty($possibleActions));
|
||||
assert(count($possibleActions) > 0);
|
||||
|
||||
foreach($possibleActions as $i => $action){
|
||||
if($action->getSourceItem()->equalsExact($needOrigin)){
|
||||
$newList = $possibleActions;
|
||||
unset($newList[$i]);
|
||||
if(empty($newList)){
|
||||
if(count($newList) === 0){
|
||||
return $action->getTargetItem();
|
||||
}
|
||||
$result = $this->findResultItem($action->getTargetItem(), $newList);
|
||||
@@ -293,7 +282,6 @@ class InventoryTransaction{
|
||||
|
||||
/**
|
||||
* Executes the group of actions, returning whether the transaction executed successfully or not.
|
||||
* @return bool
|
||||
*
|
||||
* @throws TransactionValidationException
|
||||
*/
|
||||
@@ -332,9 +320,6 @@ class InventoryTransaction{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function hasExecuted() : bool{
|
||||
return $this->hasExecuted;
|
||||
}
|
||||
|
@@ -37,6 +37,7 @@ class CreativeInventoryAction extends InventoryAction{
|
||||
*/
|
||||
public const TYPE_CREATE_ITEM = 1;
|
||||
|
||||
/** @var int */
|
||||
protected $actionType;
|
||||
|
||||
public function __construct(Item $sourceItem, Item $targetItem, int $actionType){
|
||||
@@ -46,10 +47,6 @@ class CreativeInventoryAction extends InventoryAction{
|
||||
|
||||
/**
|
||||
* Checks that the player is in creative, and (if creating an item) that the item exists in the creative inventory.
|
||||
*
|
||||
* @param Player $source
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isValid(Player $source) : bool{
|
||||
return $source->isCreative(true) and
|
||||
@@ -65,10 +62,6 @@ class CreativeInventoryAction extends InventoryAction{
|
||||
|
||||
/**
|
||||
* No need to do anything extra here: this type just provides a place for items to disappear or appear from.
|
||||
*
|
||||
* @param Player $source
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function execute(Player $source) : bool{
|
||||
return true;
|
||||
|
@@ -53,10 +53,6 @@ class DropItemAction extends InventoryAction{
|
||||
|
||||
/**
|
||||
* Drops the target item in front of the player.
|
||||
*
|
||||
* @param Player $source
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function execute(Player $source) : bool{
|
||||
return $source->dropItem($this->targetItem);
|
||||
|
@@ -43,7 +43,6 @@ abstract class InventoryAction{
|
||||
|
||||
/**
|
||||
* Returns the item that was present before the action took place.
|
||||
* @return Item
|
||||
*/
|
||||
public function getSourceItem() : Item{
|
||||
return clone $this->sourceItem;
|
||||
@@ -51,7 +50,6 @@ abstract class InventoryAction{
|
||||
|
||||
/**
|
||||
* Returns the item that the action attempted to replace the source item with.
|
||||
* @return Item
|
||||
*/
|
||||
public function getTargetItem() : Item{
|
||||
return clone $this->targetItem;
|
||||
@@ -59,17 +57,11 @@ abstract class InventoryAction{
|
||||
|
||||
/**
|
||||
* Returns whether this action is currently valid. This should perform any necessary sanity checks.
|
||||
*
|
||||
* @param Player $source
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
abstract public function isValid(Player $source) : bool;
|
||||
|
||||
/**
|
||||
* Called when the action is added to the specified InventoryTransaction.
|
||||
*
|
||||
* @param InventoryTransaction $transaction
|
||||
*/
|
||||
public function onAddToTransaction(InventoryTransaction $transaction) : void{
|
||||
|
||||
@@ -78,10 +70,6 @@ abstract class InventoryAction{
|
||||
/**
|
||||
* Called by inventory transactions before any actions are processed. If this returns false, the transaction will
|
||||
* be cancelled.
|
||||
*
|
||||
* @param Player $source
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function onPreExecute(Player $source) : bool{
|
||||
return true;
|
||||
@@ -91,24 +79,16 @@ abstract class InventoryAction{
|
||||
* Performs actions needed to complete the inventory-action server-side. Returns if it was successful. Will return
|
||||
* false if plugins cancelled events. This will only be called if the transaction which it is part of is considered
|
||||
* valid.
|
||||
*
|
||||
* @param Player $source
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
abstract public function execute(Player $source) : bool;
|
||||
|
||||
/**
|
||||
* Performs additional actions when this inventory-action completed successfully.
|
||||
*
|
||||
* @param Player $source
|
||||
*/
|
||||
abstract public function onExecuteSuccess(Player $source) : void;
|
||||
|
||||
/**
|
||||
* Performs additional actions when this inventory-action did not complete successfully.
|
||||
*
|
||||
* @param Player $source
|
||||
*/
|
||||
abstract public function onExecuteFail(Player $source) : void;
|
||||
|
||||
|
@@ -39,12 +39,6 @@ class SlotChangeAction extends InventoryAction{
|
||||
/** @var int */
|
||||
private $inventorySlot;
|
||||
|
||||
/**
|
||||
* @param Inventory $inventory
|
||||
* @param int $inventorySlot
|
||||
* @param Item $sourceItem
|
||||
* @param Item $targetItem
|
||||
*/
|
||||
public function __construct(Inventory $inventory, int $inventorySlot, Item $sourceItem, Item $targetItem){
|
||||
parent::__construct($sourceItem, $targetItem);
|
||||
$this->inventory = $inventory;
|
||||
@@ -53,8 +47,6 @@ class SlotChangeAction extends InventoryAction{
|
||||
|
||||
/**
|
||||
* Returns the inventory involved in this action.
|
||||
*
|
||||
* @return Inventory
|
||||
*/
|
||||
public function getInventory() : Inventory{
|
||||
return $this->inventory;
|
||||
@@ -62,7 +54,6 @@ class SlotChangeAction extends InventoryAction{
|
||||
|
||||
/**
|
||||
* Returns the slot in the inventory which this action modified.
|
||||
* @return int
|
||||
*/
|
||||
public function getSlot() : int{
|
||||
return $this->inventorySlot;
|
||||
@@ -70,10 +61,6 @@ class SlotChangeAction extends InventoryAction{
|
||||
|
||||
/**
|
||||
* Checks if the item in the inventory at the specified slot is the same as this action's source item.
|
||||
*
|
||||
* @param Player $source
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isValid(Player $source) : bool{
|
||||
return (
|
||||
@@ -84,9 +71,6 @@ class SlotChangeAction extends InventoryAction{
|
||||
|
||||
/**
|
||||
* Adds this action's target inventory to the transaction's inventory list.
|
||||
*
|
||||
* @param InventoryTransaction $transaction
|
||||
*
|
||||
*/
|
||||
public function onAddToTransaction(InventoryTransaction $transaction) : void{
|
||||
$transaction->addInventory($this->inventory);
|
||||
@@ -94,10 +78,6 @@ class SlotChangeAction extends InventoryAction{
|
||||
|
||||
/**
|
||||
* Sets the item into the target inventory.
|
||||
*
|
||||
* @param Player $source
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function execute(Player $source) : bool{
|
||||
return $this->inventory->setItem($this->inventorySlot, $this->targetItem, false);
|
||||
@@ -105,8 +85,6 @@ class SlotChangeAction extends InventoryAction{
|
||||
|
||||
/**
|
||||
* Sends slot changes to other viewers of the inventory. This will not send any change back to the source Player.
|
||||
*
|
||||
* @param Player $source
|
||||
*/
|
||||
public function onExecuteSuccess(Player $source) : void{
|
||||
$viewers = $this->inventory->getViewers();
|
||||
@@ -116,8 +94,6 @@ class SlotChangeAction extends InventoryAction{
|
||||
|
||||
/**
|
||||
* Sends the original slot contents to the source player to revert the action.
|
||||
*
|
||||
* @param Player $source
|
||||
*/
|
||||
public function onExecuteFail(Player $source) : void{
|
||||
$this->inventory->sendSlot($this->inventorySlot, $source);
|
||||
|
Reference in New Issue
Block a user