PocketMine-MP 5.33.2 git-1133d49c924b4358c79d44eeb97dcbf56cb4d1eb
Loading...
Searching...
No Matches
EnchantingTableInventoryWindow.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
22declare(strict_types=1);
23
24namespace pocketmine\block\inventory\window;
25
36use function array_values;
37use function count;
38
40 public const SLOT_INPUT = 0;
41 public const SLOT_LAPIS = 1;
42
44 private array $options = [];
45
46 private InventoryListener $listener;
47
48 public function __construct(
49 Player $viewer,
50 Position $holder
51 ){
52 parent::__construct($viewer, new SimpleInventory(2), $holder);
53
55 $weakThis = \WeakReference::create($this);
56 $this->listener = new CallbackInventoryListener(
57 onSlotChange: static function(Inventory $_, int $slot) use ($weakThis) : void{ //remaining params unneeded
58 if($slot === self::SLOT_INPUT && ($strongThis = $weakThis->get()) !== null){
59 $strongThis->regenerateOptions();
60 }
61 },
62 onContentChange: static function() use ($weakThis) : void{
63 if(($strongThis = $weakThis->get()) !== null){
64 $strongThis->regenerateOptions();
65 }
66 }
67 );
68 $this->inventory->getListeners()->add($this->listener);
69 }
70
71 public function __destruct(){
72 $this->inventory->getListeners()->remove($this->listener);
73 }
74
75 private function regenerateOptions() : void{
76 $this->options = [];
77 $item = $this->getInput();
78 $options = Helper::generateOptions($this->holder, $item, $this->viewer->getEnchantmentSeed());
79
80 $event = new PlayerEnchantingOptionsRequestEvent($this->viewer, $this, $options);
81 $event->call();
82 if(!$event->isCancelled() && count($event->getOptions()) > 0){
83 $this->options = array_values($event->getOptions());
84 $this->viewer->getNetworkSession()->getInvManager()?->syncEnchantingTableOptions($this->options);
85 }
86 }
87
88 public function getInput() : Item{
89 return $this->inventory->getItem(self::SLOT_INPUT);
90 }
91
92 public function getLapis() : Item{
93 return $this->inventory->getItem(self::SLOT_LAPIS);
94 }
95
96 public function getOutput(int $optionId) : ?Item{
97 $option = $this->getOption($optionId);
98 return $option === null ? null : Helper::enchantItem($this->getInput(), $option->getEnchantments());
99 }
100
101 public function getOption(int $optionId) : ?EnchantingOption{
102 return $this->options[$optionId] ?? null;
103 }
104}
static generateOptions(Position $tablePos, Item $input, int $seed)
static enchantItem(Item $item, array $enchantments)