PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
SimpleTransactionGroup.php
1 <?php
2 
3 /*
4  *
5  * ____ _ _ __ __ _ __ __ ____
6  * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7  * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8  * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9  * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * @author PocketMine Team
17  * @link http://www.pocketmine.net/
18  *
19  *
20 */
21 
22 namespace pocketmine\inventory;
23 
28 
33  private $creationTime;
34  protected $hasExecuted = false;
36  protected $source = null;
37 
39  protected $inventories = [];
40 
42  protected $transactions = [];
43 
47  public function __construct(Player $source = null){
48  $this->creationTime = microtime(true);
49  $this->source = $source;
50  }
51 
55  public function getSource(){
56  return $this->source;
57  }
58 
59  public function getCreationTime(){
60  return $this->creationTime;
61  }
62 
63  public function getInventories(){
64  return $this->inventories;
65  }
66 
67  public function getTransactions(){
68  return $this->transactions;
69  }
70 
71  public function addTransaction(Transaction $transaction){
72  if(isset($this->transactions[spl_object_hash($transaction)])){
73  return;
74  }
75  foreach($this->transactions as $hash => $tx){
76  if($tx->getInventory() === $transaction->getInventory() and $tx->getSlot() === $transaction->getSlot()){
77  if($transaction->getCreationTime() >= $tx->getCreationTime()){
78  unset($this->transactions[$hash]);
79  }else{
80  return;
81  }
82  }
83  }
84  $this->transactions[spl_object_hash($transaction)] = $transaction;
85  $this->inventories[spl_object_hash($transaction->getInventory())] = $transaction->getInventory();
86  }
87 
94  protected function matchItems(array &$needItems, array &$haveItems){
95  foreach($this->transactions as $key => $ts){
96  if($ts->getTargetItem()->getId() !== Item::AIR){
97  $needItems[] = $ts->getTargetItem();
98  }
99  $checkSourceItem = $ts->getInventory()->getItem($ts->getSlot());
100  $sourceItem = $ts->getSourceItem();
101  if(!$checkSourceItem->equals($sourceItem, true) or $sourceItem->getCount() !== $checkSourceItem->getCount()){
102  return false;
103  }
104  if($sourceItem->getId() !== Item::AIR){
105  $haveItems[] = $sourceItem;
106  }
107  }
108 
109  foreach($needItems as $i => $needItem){
110  foreach($haveItems as $j => $haveItem){
111  if($needItem->equals($haveItem, true)){
112  $amount = min($needItem->getCount(), $haveItem->getCount());
113  $needItem->setCount($needItem->getCount() - $amount);
114  $haveItem->setCount($haveItem->getCount() - $amount);
115  if($haveItem->getCount() === 0){
116  unset($haveItems[$j]);
117  }
118  if($needItem->getCount() === 0){
119  unset($needItems[$i]);
120  break;
121  }
122  }
123  }
124  }
125 
126  return true;
127  }
128 
129  public function canExecute(){
130  $haveItems = [];
131  $needItems = [];
132 
133  return $this->matchItems($haveItems, $needItems) and count($haveItems) === 0 and count($needItems) === 0 and count($this->transactions) > 0;
134  }
135 
136  public function execute(){
137  if($this->hasExecuted() or !$this->canExecute()){
138  return false;
139  }
140 
141  Server::getInstance()->getPluginManager()->callEvent($ev = new InventoryTransactionEvent($this));
142  if($ev->isCancelled()){
143  foreach($this->inventories as $inventory){
144  if($inventory instanceof PlayerInventory){
145  $inventory->sendArmorContents($this->getSource());
146  }
147  $inventory->sendContents($this->getSource());
148  }
149 
150  return false;
151  }
152 
153  foreach($this->transactions as $transaction){
154  $transaction->getInventory()->setItem($transaction->getSlot(), $transaction->getTargetItem(), $this->getSource());
155  }
156 
157  $this->hasExecuted = true;
158 
159  return true;
160  }
161 
162  public function hasExecuted(){
163  return $this->hasExecuted;
164  }
165 }
matchItems(array &$needItems, array &$haveItems)
static getInstance()
Definition: Server.php:1444