public class WindowEnchantItemHandler extends MessageHandler<WindowEnchantItemMessage> {
@Override
public void handleServer(ServerSession session, WindowEnchantItemMessage message) {
Player player = session.getPlayer();
EnchantmentTableWindow window = (EnchantmentTableWindow) player.get(WindowHolder.class).getActiveWindow();
EnchantmentTableInventory inv = (EnchantmentTableInventory) window.getInventoryConverters().get(2).getInventory();
int enchantSlot = message.getEnchantment();
int enchantLevel = window.getEnchantmentLevel(enchantSlot);
Human human = player.get(Human.class);
Level level = player.get(Level.class);
if (human == null || level == null) {
return;
}
if (human.getGameMode() != GameMode.CREATIVE && level.getLevel() < enchantLevel) {
return;
}
if (!Enchantment.addRandomEnchantments(inv.get(), enchantLevel)) {
return;
}
inv.update(EnchantmentTableInventory.SLOT, inv.get());
if (human.getGameMode() != GameMode.CREATIVE) {
level.removeLevels(enchantLevel);
}
}