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

Public Member Functions

 __construct (Position $pos)
 
 getHolder ()
 
 getResultSlotIndex ()
 
 onOpen (Player $who)
 
 onClose (Player $who)
 
 checkEnts (array $ent1, array $ent2)
 
 onEnchant (Player $who, Item $before, Item $after)
 
 countBookshelf ()
 
 removeConflictEnchantment (Enchantment $enchantment, array $enchantments)
 
- Public Member Functions inherited from TemporaryInventory
 getResultSlotIndex ()
 
 onClose (Player $who)
 
- Public Member Functions inherited from ContainerInventory
 onOpen (Player $who)
 
 onClose (Player $who)
 
- Public Member Functions inherited from BaseInventory
 __construct (InventoryHolder $holder, InventoryType $type, array $items=[], $overrideSize=null, $overrideTitle=null)
 
 __destruct ()
 
 getSize ()
 
 getHotbarSize ()
 
 setSize ($size)
 
 getMaxStackSize ()
 
 getName ()
 
 getTitle ()
 
 getItem ($index)
 
 getContents ()
 
 setContents (array $items, $send=true)
 
 setItem ($index, Item $item, $send=true)
 
 contains (Item $item)
 
 slotContains ($slot, Item $item, $matchCount=false)
 
 all (Item $item)
 
 remove (Item $item, $send=true)
 
 first (Item $item)
 
 firstEmpty ()
 
 firstOccupied ()
 
 canAddItem (Item $item)
 
 clear ($index, $send=true)
 
 clearAll ($send=true)
 
 getViewers ()
 
 getHolder ()
 
 setMaxStackSize ($size)
 
 open (Player $who)
 
 close (Player $who)
 
 onOpen (Player $who)
 
 onClose (Player $who)
 
 onSlotChange ($index, $before, $send)
 
 processSlotChange (Transaction $transaction)
 
 sendContents ($target)
 
 sendSlot ($index, $target)
 
 getType ()
 
- Public Member Functions inherited from Inventory
 setItem ($index, Item $item)
 
 addItem (... $slots)
 
 removeItem (... $slots)
 
 setContents (array $items)
 
 remove (Item $item)
 
 clear ($index)
 
 clearAll ()
 

Additional Inherited Members

- Data Fields inherited from Inventory
const MAX_STACK = 64
 
- Protected Attributes inherited from BaseInventory
 $type
 
 $maxStackSize = Inventory::MAX_STACK
 
 $size
 
 $name
 
 $title
 
 $slots = []
 
 $viewers = []
 
 $holder
 

Constructor & Destructor Documentation

◆ __construct()

__construct ( Position  $pos)

EnchantInventory constructor.

Parameters
Position$pos
47  {
48  parent::__construct(new FakeBlockMenu($this, $pos), InventoryType::get(InventoryType::ENCHANT_TABLE));
49  }

Member Function Documentation

◆ checkEnts()

checkEnts ( array  $ent1,
array  $ent2 
)
Parameters
Enchantment[]$ent1
Enchantment[]$ent2
Returns
bool
211  {
212  foreach($ent1 as $enchantment){
213  $hasResult = false;
214  foreach($ent2 as $enchantment1){
215  if($enchantment->equals($enchantment1)){
216  $hasResult = true;
217  continue;
218  }
219  }
220  if(!$hasResult){
221  return false;
222  }
223  }
224  return true;
225  }

◆ countBookshelf()

countBookshelf ( )
Returns
int
261  : int{
262  if($this->getHolder()->getLevel()->getServer()->countBookshelf){
263  $count = 0;
264  $pos = $this->getHolder();
265  $offsets = [[2, 0], [-2, 0], [0, 2], [0, -2], [2, 1], [2, -1], [-2, 1], [-2, 1], [1, 2], [-1, 2], [1, -2], [-1, -2]];
266  for($i = 0; $i < 3; $i++){
267  foreach($offsets as $offset){
268  if($pos->getLevel()->getBlockIdAt($pos->x + $offset[0], $pos->y + $i, $pos->z + $offset[1]) == Block::BOOKSHELF){
269  $count++;
270  }
271  if($count >= 15){
272  break 2;
273  }
274  }
275  }
276  return $count;
277  }else{
278  return mt_rand(0, 15);
279  }
280  }

◆ getHolder()

getHolder ( )
Returns
EnchantTable

Implements Inventory.

54  {
55  return $this->holder;
56  }

◆ getResultSlotIndex()

getResultSlotIndex ( )
Returns
int
61  {
62  return -1; //enchanting tables don't have result slots, they modify the item in the target slot instead
63  }

◆ onClose()

onClose ( Player  $who)
Parameters
Player$who

Implements Inventory.

189  {
190  parent::onClose($who);
191 
192  $level = $this->getHolder()->getLevel();
193  for($i = 0; $i < 2; ++$i){
194  if($level instanceof Level) $level->dropItem($this->getHolder()->add(0.5, 0.5, 0.5), $this->getItem($i));
195  $this->clear($i);
196  }
197 
198  if(count($this->getViewers()) === 0){
199  $this->levels = null;
200  $this->entries = null;
201  $this->bookshelfAmount = 0;
202  }
203  }

◆ onEnchant()

onEnchant ( Player  $who,
Item  $before,
Item  $after 
)
Parameters
Player$who
Item$before
Item$after
232  {
233  $result = ($before->getId() === Item::BOOK) ? new EnchantedBook() : $before;
234  if(!$before->hasEnchantments() and $after->hasEnchantments() and $after->getId() == $result->getId() and
235  $this->levels != null and $this->entries != null
236  ){
237  $enchantments = $after->getEnchantments();
238  for($i = 0; $i < 3; $i++){
239  if($this->checkEnts($enchantments, $this->entries[$i]->getEnchantments())){
240  $lapis = $this->getItem(1);
241  $level = $who->getXpLevel();
242  $cost = $this->entries[$i]->getCost();
243  if($lapis->getId() == Item::DYE and $lapis->getDamage() == Dye::BLUE and $lapis->getCount() > $i and $level >= $cost){
244  foreach($enchantments as $enchantment){
245  $result->addEnchantment($enchantment);
246  }
247  $this->setItem(0, $result);
248  $lapis->setCount($lapis->getCount() - $i - 1);
249  $this->setItem(1, $lapis);
250  $who->takeXpLevel($i + 1);
251  break;
252  }
253  }
254  }
255  }
256  }

◆ onOpen()

onOpen ( Player  $who)
Parameters
Player$who

Implements Inventory.

68  {
69  parent::onOpen($who);
70  if($this->levels == null){
71  $this->bookshelfAmount = $this->countBookshelf();
72 
73  if($this->bookshelfAmount < 0){
74  $this->bookshelfAmount = 0;
75  }
76 
77  if($this->bookshelfAmount > 15){
78  $this->bookshelfAmount = 15;
79  }
80 
81  $base = mt_rand(1, 8) + ($this->bookshelfAmount / 2) + mt_rand(0, $this->bookshelfAmount);
82  $this->levels = [
83  0 => max($base / 3, 1),
84  1 => (($base * 2) / 3 + 1),
85  2 => max($base, $this->bookshelfAmount * 2)
86  ];
87  }
88  }

◆ removeConflictEnchantment()

removeConflictEnchantment ( Enchantment  $enchantment,
array  $enchantments 
)
Parameters
Enchantment$enchantment
Enchantment[]$enchantments
Returns
Enchantment[]
288  {
289  if(count($enchantments) > 0){
290  foreach($enchantments as $e){
291  $id = $e->getId();
292  if($id == $enchantment->getId()){
293  unset($enchantments[$id]);
294  continue;
295  }
296 
297  if($id >= 0 and $id <= 4 and $enchantment->getId() >= 0 and $enchantment->getId() <= 4){
298  //Protection
299  unset($enchantments[$id]);
300  continue;
301  }
302 
303  if($id >= 9 and $id <= 14 and $enchantment->getId() >= 9 and $enchantment->getId() <= 14){
304  //Weapon
305  unset($enchantments[$id]);
306  continue;
307  }
308 
309  if(($id === Enchantment::TYPE_MINING_SILK_TOUCH and $enchantment->getId() === Enchantment::TYPE_MINING_FORTUNE) or ($id === Enchantment::TYPE_MINING_FORTUNE and $enchantment->getId() === Enchantment::TYPE_MINING_SILK_TOUCH)){
310  //Protection
311  unset($enchantments[$id]);
312  continue;
313  }
314  }
315  }
316  $result = [];
317  if(count($enchantments) > 0){
318  foreach($enchantments as $enchantment){
319  $result[] = $enchantment;
320  }
321  }
322  return $result;
323  }

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