GenisysPro  for Minecraft PE/Windows 10 v1.1.x
Feature-rich server software for Minecraft PE and Windows 10 Edition
BaseTransaction Class Reference

Public Member Functions

 __construct ($inventory, $slot, Item $targetItem, $achievements=[], $transactionType=Transaction::TYPE_NORMAL)
 
 getCreationTime ()
 
 getInventory ()
 
 getSlot ()
 
 getTargetItem ()
 
 getSourceItem ()
 
 setTargetItem (Item $item)
 
 getFailures ()
 
 addFailure ()
 
 succeeded ()
 
 setSuccess ($value=true)
 
 getTransactionType ()
 
 getAchievements ()
 
 hasAchievements ()
 
 addAchievement (string $achievementName)
 
 sendSlotUpdate (Player $source)
 
 getChange ()
 
 execute (Player $source)
 

Protected Attributes

 $inventory
 
 $slot
 
 $targetItem
 
 $creationTime
 
 $transactionType = Transaction::TYPE_NORMAL
 
 $failures = 0
 
 $wasSuccessful = false
 
 $achievements = []
 

Additional Inherited Members

- Data Fields inherited from Transaction
const TYPE_NORMAL = 0
 
const TYPE_DROP_ITEM = 1
 

Constructor & Destructor Documentation

◆ __construct()

__construct (   $inventory,
  $slot,
Item  $targetItem,
  $achievements = [],
  $transactionType = Transaction::TYPE_NORMAL 
)
Parameters
Inventory$inventory
int$slot
Item$targetItem
string[]$achievements
int$transactionType
52  {
53  $this->inventory = $inventory;
54  $this->slot = (int) $slot;
55  $this->targetItem = clone $targetItem;
56  $this->creationTime = microtime(true);
57  $this->transactionType = $transactionType;
58  $this->achievements = $achievements;
59  }

Member Function Documentation

◆ addAchievement()

addAchievement ( string  $achievementName)
Parameters
string$achievementName
152  {
153  $this->achievements[] = $achievementName;
154  }

◆ addFailure()

addFailure ( )
110  {
111  $this->failures++;
112  }

◆ execute()

execute ( Player  $source)
Parameters
Player$source
Returns
bool

Handles transaction execution. Returns whether transaction was successful or not.

Implements Transaction.

232  : bool{
233  if($this->getInventory()->processSlotChange($this)){ //This means that the transaction should be handled the normal way
234  if(!$source->getServer()->allowInventoryCheats and !$source->isCreative()){
235  $change = $this->getChange();
236 
237  if($change === null){ //No changes to make, ignore this transaction
238  return true;
239  }
240 
241  /* Verify that we have the required items */
242  if($change["out"] instanceof Item){
243  if(!$this->getInventory()->slotContains($this->getSlot(), $change["out"])){
244  return false;
245  }
246  }
247  if($change["in"] instanceof Item){
248  if(!$source->getFloatingInventory()->contains($change["in"])){
249  return false;
250  }
251  }
252 
253  /* All checks passed, make changes to floating inventory
254  * This will not be reached unless all requirements are met */
255  if($change["out"] instanceof Item){
256  $source->getFloatingInventory()->addItem($change["out"]);
257  }
258  if($change["in"] instanceof Item){
259  $source->getFloatingInventory()->removeItem($change["in"]);
260  }
261  }
262  $this->getInventory()->setItem($this->getSlot(), $this->getTargetItem(), false);
263  }
264 
265  /* Process transaction achievements, like getting iron from a furnace */
266  foreach($this->achievements as $achievement){
267  $source->awardAchievement($achievement);
268  }
269 
270  return true;
271  }

◆ getAchievements()

getAchievements ( )
Returns
array|string|[]
138  {
139  return $this->achievements;
140  }

◆ getChange()

getChange ( )

Returns the change in inventory resulting from this transaction

Returns
array ("in" => items added to the inventory, "out" => items removed from the inventory) ]
183  {
184  $sourceItem = $this->getInventory()->getItem($this->slot);
185 
186  if($sourceItem->equals($this->targetItem, true, true, true)){
187  //This should never happen, somehow a change happened where nothing changed
188  return null;
189 
190  }elseif($sourceItem->equals($this->targetItem)){ //Same item, change of count
191  $item = clone $sourceItem;
192  $countDiff = $this->targetItem->getCount() - $sourceItem->getCount();
193  $item->setCount(abs($countDiff));
194 
195  if($countDiff < 0){ //Count decreased
196  return ["in" => null,
197  "out" => $item];
198  }elseif($countDiff > 0){ //Count increased
199  return [
200  "in" => $item,
201  "out" => null];
202  }else{
203  //Should be impossible (identical items and no count change)
204  //This should be caught by the first condition even if it was possible
205  return null;
206  }
207  }elseif($sourceItem->getId() !== Item::AIR and $this->targetItem->getId() === Item::AIR){
208  //Slot emptied (item removed)
209  return ["in" => null,
210  "out" => clone $sourceItem];
211 
212  }elseif($sourceItem->getId() === Item::AIR and $this->targetItem->getId() !== Item::AIR){
213  //Slot filled (item added)
214  return ["in" => $this->getTargetItem(),
215  "out" => null];
216 
217  }else{
218  //Some other slot change - an item swap (tool damage changes will be ignored as they are processed server-side before any change is sent by the client
219  return ["in" => $this->getTargetItem(),
220  "out" => clone $sourceItem];
221  }
222  }

◆ getCreationTime()

getCreationTime ( )
Returns
float|mixed

Implements Transaction.

64  {
65  return $this->creationTime;
66  }

◆ getFailures()

getFailures ( )
Returns
int
106  {
107  return $this->failures;
108  }

◆ getInventory()

getInventory ( )
Returns
Inventory

Implements Transaction.

71  {
72  return $this->inventory;
73  }

◆ getSlot()

getSlot ( )
Returns
int

Implements Transaction.

78  {
79  return $this->slot;
80  }

◆ getSourceItem()

getSourceItem ( )
Returns
Item

Implements Transaction.

92  {
93  return clone $this->inventory->getItem($this->slot);
94  }

◆ getTargetItem()

getTargetItem ( )
Returns
Item

Implements Transaction.

85  {
86  return clone $this->targetItem;
87  }

◆ getTransactionType()

getTransactionType ( )
Returns
int
131  {
132  return $this->transactionType;
133  }

◆ hasAchievements()

hasAchievements ( )
Returns
bool
145  {
146  return count($this->achievements) !== 0;
147  }

◆ sendSlotUpdate()

sendSlotUpdate ( Player  $source)
Parameters
Player$sourceSends a slot update to inventory viewers For successful transactions, update non-source viewers (source does not need updating) For failed transactions, update the source (non-source viewers will see nothing anyway)
163  {
164  if($this->getInventory() instanceof TemporaryInventory){
165  return;
166  }
167 
168  if($this->wasSuccessful){
169  $targets = $this->getInventory()->getViewers();
170  unset($targets[spl_object_hash($source)]);
171  }else{
172  $targets = [$source];
173  }
174  $this->inventory->sendSlot($this->slot, $targets);
175  }

◆ setSuccess()

setSuccess (   $value = true)
Parameters
bool$value
124  {
125  $this->wasSuccessful = $value;
126  }

◆ setTargetItem()

setTargetItem ( Item  $item)
Parameters
Item$item
99  {
100  $this->targetItem = clone $item;
101  }

◆ succeeded()

succeeded ( )
Returns
bool
117  {
118  return $this->wasSuccessful;
119  }

Field Documentation

◆ $achievements

$achievements = []
protected

◆ $creationTime

$creationTime
protected

◆ $failures

$failures = 0
protected

◆ $inventory

$inventory
protected

◆ $slot

$slot
protected

◆ $targetItem

$targetItem
protected

◆ $transactionType

$transactionType = Transaction::TYPE_NORMAL
protected

◆ $wasSuccessful

$wasSuccessful = false
protected

The documentation for this class was generated from the following file: