public function onSlotChange($index, $before) { parent::onSlotChange($index, $before); if ($index == 0) { $item = $this->getItem(0); if ($item->getId() == Item::AIR) { $this->entries = null; } elseif ($before->getId() == Item::AIR and !$item->hasEnchantments()) { //before enchant if ($this->entries == null) { $enchantAbility = Enchantment::getEnchantAbility($item); $this->entries = []; for ($i = 0; $i < 3; $i++) { $result = []; $level = $this->levels[$i]; $k = $level + mt_rand(0, round(round($enchantAbility / 4) * 2)) + 1; $bonus = ($this->randomFloat() + $this->randomFloat() - 1) * 0.15 + 1; $modifiedLevel = $k * (1 + $bonus) + 0.5; $possible = EnchantmentLevelTable::getPossibleEnchantments($item, $modifiedLevel); $weights = []; $total = 0; for ($j = 0; $j < count($possible); $j++) { $id = $possible[$j]->getId(); $weight = Enchantment::getEnchantWeight($id); $weights[$j] = $weight; $total += $weight; } $v = mt_rand(1, $total + 1); $sum = 0; for ($key = 0; $key < count($weights); ++$key) { $sum += $weights[$key]; if ($sum >= $v) { $key++; break; } } $key--; $enchantment = $possible[$key]; $result[] = $enchantment; unset($possible[$key]); //Extra enchantment while (count($possible) > 0) { $modifiedLevel = round($modifiedLevel / 2); $v = mt_rand(0, 51); if ($v <= $modifiedLevel + 1) { $possible = $this->removeConflictEnchantment($enchantment, $possible); $weights = []; $total = 0; for ($j = 0; $j < count($possible); $j++) { $id = $possible[$j]->getId(); $weight = Enchantment::getEnchantWeight($id); $weights[$j] = $weight; $total += $weight; } $v = mt_rand(1, $total + 1); $sum = 0; for ($key = 0; $key < count($weights); ++$key) { $sum += $weights[$key]; if ($sum >= $v) { $key++; break; } } $key--; $enchantment = $possible[$key]; $result[] = $enchantment; unset($possible[$key]); } else { break; } } $this->entries[$i] = new EnchantmentEntry($result, $level, Enchantment::generateName()); } $this->sendEnchantmentList(); } } } }