mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-06 17:59:48 +00:00
Many many changes related to inventory transactions, fixed item dropping, fixed creative menu
This commit is contained in:
@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\inventory\transaction;
|
||||
|
||||
use pocketmine\inventory\Inventory;
|
||||
use pocketmine\inventory\transaction\action\InventoryAction;
|
||||
|
||||
interface InventoryTransaction{
|
||||
|
||||
/**
|
||||
* @return float
|
||||
*/
|
||||
public function getCreationTime() : float;
|
||||
|
||||
/**
|
||||
* @return InventoryAction[]
|
||||
*/
|
||||
public function getActions() : array;
|
||||
|
||||
/**
|
||||
* @return Inventory[]
|
||||
*/
|
||||
public function getInventories() : array;
|
||||
|
||||
/**
|
||||
* @param InventoryAction $action
|
||||
*/
|
||||
public function addAction(InventoryAction $action);
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function canExecute() : bool;
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function execute() : bool;
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function hasExecuted() : bool;
|
||||
|
||||
}
|
@ -0,0 +1,173 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\inventory\transaction;
|
||||
|
||||
use pocketmine\event\inventory\InventoryTransactionEvent;
|
||||
use pocketmine\inventory\Inventory;
|
||||
use pocketmine\inventory\transaction\InventoryTransaction;
|
||||
use pocketmine\inventory\transaction\action\InventoryAction;
|
||||
use pocketmine\inventory\transaction\action\SlotChangeAction;
|
||||
use pocketmine\item\Item;
|
||||
use pocketmine\Player;
|
||||
use pocketmine\Server;
|
||||
|
||||
/**
|
||||
* This InventoryTransaction only allows doing Transaction between one / two inventories
|
||||
*/
|
||||
class SimpleInventoryTransaction implements InventoryTransaction{
|
||||
/** @var float */
|
||||
private $creationTime;
|
||||
protected $hasExecuted = false;
|
||||
/** @var Player */
|
||||
protected $source = null;
|
||||
|
||||
/** @var Inventory[] */
|
||||
protected $inventories = [];
|
||||
|
||||
/** @var InventoryAction[] */
|
||||
protected $actions = [];
|
||||
|
||||
/**
|
||||
* @param Player $source
|
||||
*/
|
||||
public function __construct(Player $source = null){
|
||||
$this->creationTime = microtime(true);
|
||||
$this->source = $source;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Player
|
||||
*/
|
||||
public function getSource() : Player{
|
||||
return $this->source;
|
||||
}
|
||||
|
||||
public function getCreationTime() : float{
|
||||
return $this->creationTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Inventory[]
|
||||
*/
|
||||
public function getInventories() : array{
|
||||
return $this->inventories;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return InventoryAction[]
|
||||
*/
|
||||
public function getActions() : array{
|
||||
return $this->actions;
|
||||
}
|
||||
|
||||
public function addAction(InventoryAction $action){
|
||||
if(isset($this->actions[spl_object_hash($action)])){
|
||||
return;
|
||||
}
|
||||
|
||||
$this->actions[spl_object_hash($action)] = $action;
|
||||
if($action instanceof SlotChangeAction){
|
||||
$action->setInventoryFrom($this->source);
|
||||
$this->inventories[spl_object_hash($action->getInventory())] = $action->getInventory();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Item[] $needItems
|
||||
* @param Item[] $haveItems
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function matchItems(array &$needItems, array &$haveItems) : bool{
|
||||
foreach($this->actions as $key => $action){
|
||||
if($action->getTargetItem()->getId() !== Item::AIR){
|
||||
$needItems[] = $action->getTargetItem();
|
||||
}
|
||||
|
||||
if(!$action->isValid($this->source)){
|
||||
return false;
|
||||
}
|
||||
|
||||
if($action->getSourceItem()->getId() !== Item::AIR){
|
||||
$haveItems[] = $action->getSourceItem();
|
||||
}
|
||||
}
|
||||
|
||||
foreach($needItems as $i => $needItem){
|
||||
foreach($haveItems as $j => $haveItem){
|
||||
if($needItem->equals($haveItem)){
|
||||
$amount = min($needItem->getCount(), $haveItem->getCount());
|
||||
$needItem->setCount($needItem->getCount() - $amount);
|
||||
$haveItem->setCount($haveItem->getCount() - $amount);
|
||||
if($haveItem->getCount() === 0){
|
||||
unset($haveItems[$j]);
|
||||
}
|
||||
if($needItem->getCount() === 0){
|
||||
unset($needItems[$i]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function canExecute() : bool{
|
||||
$haveItems = [];
|
||||
$needItems = [];
|
||||
|
||||
return $this->matchItems($needItems, $haveItems) and count($this->actions) > 0 and count($haveItems) === 0 and count($needItems) === 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function execute() : bool{
|
||||
if($this->hasExecuted() or !$this->canExecute()){
|
||||
return false;
|
||||
}
|
||||
|
||||
Server::getInstance()->getPluginManager()->callEvent($ev = new InventoryTransactionEvent($this));
|
||||
if($ev->isCancelled()){
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach($this->actions as $action){
|
||||
if($action->execute($this->source)){
|
||||
$action->onExecuteSuccess($this->source);
|
||||
}else{
|
||||
$action->onExecuteFail($this->source);
|
||||
}
|
||||
}
|
||||
|
||||
$this->hasExecuted = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function hasExecuted() : bool{
|
||||
return $this->hasExecuted;
|
||||
}
|
||||
}
|
@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\inventory\transaction\action;
|
||||
|
||||
use pocketmine\item\Item;
|
||||
use pocketmine\Player;
|
||||
|
||||
class CreativeInventoryAction extends InventoryAction{
|
||||
|
||||
/**
|
||||
* Player put an item into the creative window to destroy it.
|
||||
*/
|
||||
const TYPE_DELETE_ITEM = 0;
|
||||
/**
|
||||
* Player took an item from the creative window.
|
||||
*/
|
||||
const TYPE_CREATE_ITEM = 1;
|
||||
|
||||
protected $actionType;
|
||||
|
||||
public function __construct(Item $sourceItem, Item $targetItem, int $actionType){
|
||||
parent::__construct($sourceItem, $targetItem);
|
||||
$this->actionType = $actionType;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
($this->actionType === self::TYPE_DELETE_ITEM or Item::getCreativeItemIndex($this->sourceItem) !== -1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the type of the action.
|
||||
*/
|
||||
public function getActionType() : int{
|
||||
return $this->actionType;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
public function onExecuteSuccess(Player $source){
|
||||
|
||||
}
|
||||
|
||||
public function onExecuteFail(Player $source){
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\inventory\transaction\action;
|
||||
|
||||
use pocketmine\Player;
|
||||
|
||||
/**
|
||||
* Represents an action involving dropping an item into the world.
|
||||
*/
|
||||
class DropItemAction extends InventoryAction{
|
||||
|
||||
/**
|
||||
* Verifies that the source item of a drop-item action must be air. This is not strictly necessary, just a sanity
|
||||
* check.
|
||||
*
|
||||
* @param Player $source
|
||||
* @return bool
|
||||
*/
|
||||
public function isValid(Player $source) : bool{
|
||||
return $this->sourceItem->isNull();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
public function onExecuteSuccess(Player $source){
|
||||
|
||||
}
|
||||
|
||||
public function onExecuteFail(Player $source){
|
||||
|
||||
}
|
||||
}
|
102
src/pocketmine/inventory/transaction/action/InventoryAction.php
Normal file
102
src/pocketmine/inventory/transaction/action/InventoryAction.php
Normal file
@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\inventory\transaction\action;
|
||||
|
||||
use pocketmine\item\Item;
|
||||
use pocketmine\Player;
|
||||
|
||||
/**
|
||||
* Represents an action involving a change that applies in some way to an inventory or other item-source.
|
||||
*/
|
||||
abstract class InventoryAction{
|
||||
|
||||
/** @var float */
|
||||
private $creationTime;
|
||||
/** @var Item */
|
||||
protected $sourceItem;
|
||||
/** @var Item */
|
||||
protected $targetItem;
|
||||
|
||||
public function __construct(Item $sourceItem, Item $targetItem){
|
||||
$this->sourceItem = $sourceItem;
|
||||
$this->targetItem = $targetItem;
|
||||
|
||||
$this->creationTime = microtime(true);
|
||||
}
|
||||
|
||||
public function getCreationTime() : float{
|
||||
return $this->creationTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the item that was present before the action took place.
|
||||
* @return Item
|
||||
*/
|
||||
public function getSourceItem() : Item{
|
||||
return clone $this->sourceItem;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the item that the action attempted to replace the source item with.
|
||||
* @return Item
|
||||
*/
|
||||
public function getTargetItem() : Item{
|
||||
return clone $this->targetItem;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
||||
/**
|
||||
* Performs additional actions when this inventory-action did not complete successfully.
|
||||
*
|
||||
* @param Player $source
|
||||
*/
|
||||
abstract public function onExecuteFail(Player $source);
|
||||
|
||||
}
|
126
src/pocketmine/inventory/transaction/action/SlotChangeAction.php
Normal file
126
src/pocketmine/inventory/transaction/action/SlotChangeAction.php
Normal file
@ -0,0 +1,126 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\inventory\transaction\action;
|
||||
|
||||
use pocketmine\inventory\Inventory;
|
||||
use pocketmine\item\Item;
|
||||
use pocketmine\Player;
|
||||
|
||||
/**
|
||||
* Represents an action causing a change in an inventory slot.
|
||||
*/
|
||||
class SlotChangeAction extends InventoryAction{
|
||||
|
||||
/** @var Inventory|null */
|
||||
protected $inventory;
|
||||
/** @var int */
|
||||
private $inventorySlot;
|
||||
/** @var int */
|
||||
private $containerId;
|
||||
|
||||
/**
|
||||
* @param Item $sourceItem
|
||||
* @param Item $targetItem
|
||||
* @param int $containerId
|
||||
* @param int $inventorySlot
|
||||
*/
|
||||
public function __construct(Item $sourceItem, Item $targetItem, int $containerId, int $inventorySlot){
|
||||
parent::__construct($sourceItem, $targetItem);
|
||||
$this->inventorySlot = $inventorySlot;
|
||||
$this->containerId = $containerId;
|
||||
}
|
||||
|
||||
public function getContainerId() : int{
|
||||
return $this->containerId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the inventory involved in this action. Will return null if the action has not yet been fully initialized.
|
||||
*
|
||||
* @return Inventory|null
|
||||
*/
|
||||
public function getInventory(){
|
||||
return $this->inventory;
|
||||
}
|
||||
|
||||
public function setInventoryFrom(Player $player){
|
||||
$inventory = $player->getWindow($this->containerId);
|
||||
if($inventory === null){
|
||||
throw new \InvalidStateException("Player " . $player->getName() . " has no open container with ID " . $this->containerId);
|
||||
}
|
||||
|
||||
$this->inventory = $inventory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the slot in the inventory which this action modified.
|
||||
* @return int
|
||||
*/
|
||||
public function getSlot() : int{
|
||||
return $this->inventorySlot;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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{
|
||||
$check = $this->inventory->getItem($this->inventorySlot);
|
||||
return $check->equals($this->sourceItem) and $check->getCount() === $this->sourceItem->getCount();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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){
|
||||
$viewers = $this->inventory->getViewers();
|
||||
unset($viewers[spl_object_hash($source)]);
|
||||
$this->inventory->sendSlot($this->inventorySlot, $viewers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends the original slot contents to the source player to revert the action.
|
||||
*
|
||||
* @param Player $source
|
||||
*/
|
||||
public function onExecuteFail(Player $source){
|
||||
$this->inventory->sendSlot($this->inventorySlot, $source);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user