use of com.archyx.aureliumskills.util.mechanics.EnchantmentValue in project AureliumSkills by Archy-X.
the class ForgingAbilities method disenchanter.
@EventHandler(priority = EventPriority.MONITOR)
public void disenchanter(InventoryClickEvent event) {
if (event.isCancelled())
return;
if (blockDisabled(Ability.DISENCHANTER))
return;
// This ability requires at least 1.14
if (!VersionUtils.isAtLeastVersion(14))
return;
if (event.getWhoClicked() instanceof Player) {
Player player = (Player) event.getWhoClicked();
if (blockAbility(player))
return;
Inventory inventory = event.getClickedInventory();
if (inventory == null)
return;
ClickType click = event.getClick();
// Only allow right and left clicks if inventory full
if (click != ClickType.LEFT && click != ClickType.RIGHT && ItemUtils.isInventoryFull(player))
return;
// Make sure the click was successful
if (event.getResult() != Event.Result.ALLOW)
return;
// Make sure cursor is empty
if (player.getItemOnCursor().getType() != Material.AIR)
return;
if (event.getClickedInventory().getType() == InventoryType.GRINDSTONE) {
if (event.getSlotType() == InventoryType.SlotType.RESULT) {
PlayerData playerData = plugin.getPlayerManager().getPlayerData(player);
if (playerData == null)
return;
if (playerData.getAbilityLevel(Ability.DISENCHANTER) == 0)
return;
Location location = inventory.getLocation();
if (location == null)
return;
ItemStack first = inventory.getItem(0);
ItemStack second = inventory.getItem(1);
if (first != null && second != null) {
// If two items, make sure items are the same type
if (first.getType() != second.getType()) {
return;
}
}
Set<EnchantmentValue> enchants = new HashSet<>();
// Add enchants to disenchant
checkEnchants(first, enchants);
checkEnchants(second, enchants);
if (enchants.size() == 0)
return;
// Calculate the sum
try {
int sum = 0;
for (EnchantmentValue value : enchants) {
String enchantName = value.getEnchantment().getKey().getKey().toUpperCase(Locale.ENGLISH);
if (containsEnchant(enchantName)) {
sum += GrindstoneEnchant.valueOf(enchantName).getLevel(value.getLevel());
}
}
// Get the average experience that would drop
int average = (sum + (int) Math.ceil(((double) sum) / 2)) / 2;
int added = (int) Math.round(average * (getValue(Ability.DISENCHANTER, playerData) / 100));
World world = location.getWorld();
if (world != null) {
world.spawn(location, ExperienceOrb.class).setExperience(added);
}
} catch (IllegalArgumentException ignored) {
}
}
}
}
}
Aggregations