EpicEnchants/src/main/java/com/craftaro/epicenchants/listeners/item/BlackScrollListener.java

51 lines
2.1 KiB
Java
Raw Normal View History

package com.craftaro.epicenchants.listeners.item;
2019-02-19 14:23:20 +01:00
import com.craftaro.core.third_party.de.tr7zw.nbtapi.NBTCompound;
import com.craftaro.core.third_party.de.tr7zw.nbtapi.NBTItem;
import com.craftaro.epicenchants.objects.Enchant;
import com.craftaro.epicenchants.EpicEnchants;
import com.craftaro.epicenchants.utils.single.RomanNumber;
2019-02-19 14:23:20 +01:00
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;
import static com.craftaro.epicenchants.utils.single.GeneralUtils.getRandomElement;
2019-02-19 14:23:20 +01:00
public class BlackScrollListener extends ItemListener {
public BlackScrollListener(EpicEnchants instance) {
super(instance);
}
@Override
void onApply(InventoryClickEvent event, NBTItem cursor, NBTItem current) {
if (!cursor.hasTag("black-scroll") || !cursor.getBoolean("black-scroll")) {
2019-02-19 14:23:20 +01:00
return;
}
event.setCancelled(true);
2019-08-05 04:24:48 +02:00
NBTCompound compound = current.getCompound("enchants");
2019-02-19 14:23:20 +01:00
if (compound == null || compound.getKeys().isEmpty()) {
this.instance.getLocale().getMessage("blackscroll.noenchants")
2019-08-04 23:50:07 +02:00
.sendPrefixedMessage(event.getWhoClicked());
2019-02-19 14:23:20 +01:00
return;
}
String id = getRandomElement(compound.getKeys());
2022-03-18 20:35:14 +01:00
int level = compound.getInteger(id);
Enchant enchant = this.instance.getEnchantManager().getValueUnsafe(id);
ItemStack toSet = this.instance.getEnchantUtils().removeEnchant(event.getCurrentItem(), enchant);
2019-02-19 14:23:20 +01:00
2022-03-18 20:35:14 +01:00
event.getWhoClicked().getInventory().addItem(enchant.getBook().get(enchant, level, cursor.getInteger("success-rate"), 100));
2019-02-19 14:23:20 +01:00
event.setCurrentItem(toSet);
this.instance.getLocale().getMessage("blackscroll.success")
2019-08-04 23:50:07 +02:00
.processPlaceholder("enchant", enchant.getIdentifier())
.processPlaceholder("group_color", enchant.getGroup().getColor())
.processPlaceholder("group_name", enchant.getGroup().getName())
.processPlaceholder("level", RomanNumber.toRoman(level))
.sendPrefixedMessage(event.getWhoClicked());
2019-04-08 14:49:08 +02:00
2019-02-19 14:23:20 +01:00
useItem(event);
}
}