40 public const SLOT_INPUT = 0;
41 public const SLOT_LAPIS = 1;
44 private array $options = [];
48 public function __construct(
55 $weakThis = \WeakReference::create($this);
57 onSlotChange:
static function(
Inventory $_,
int $slot) use ($weakThis) :
void{
58 if($slot === self::SLOT_INPUT && ($strongThis = $weakThis->get()) !==
null){
59 $strongThis->regenerateOptions();
62 onContentChange:
static function() use ($weakThis) :
void{
63 if(($strongThis = $weakThis->get()) !==
null){
64 $strongThis->regenerateOptions();
68 $this->inventory->getListeners()->add($this->listener);
71 public function __destruct(){
72 $this->inventory->getListeners()->remove($this->listener);
75 private function regenerateOptions() :
void{
77 $item = $this->getInput();
82 if(!$event->isCancelled() && count($event->getOptions()) > 0){
83 $this->options = array_values($event->getOptions());
84 $this->viewer->getNetworkSession()->getInvManager()?->syncEnchantingTableOptions($this->options);
88 public function getInput() :
Item{
89 return $this->inventory->getItem(self::SLOT_INPUT);
92 public function getLapis() :
Item{
93 return $this->inventory->getItem(self::SLOT_LAPIS);
96 public function getOutput(
int $optionId) : ?
Item{
97 $option = $this->getOption($optionId);
98 return $option ===
null ? null :
Helper::enchantItem($this->getInput(), $option->getEnchantments());
102 return $this->options[$optionId] ??
null;