Search in sources :

Example 1 with EnchantmentValue

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) {
                }
            }
        }
    }
}
Also used : Player(org.bukkit.entity.Player) ExperienceOrb(org.bukkit.entity.ExperienceOrb) World(org.bukkit.World) EnchantmentValue(com.archyx.aureliumskills.util.mechanics.EnchantmentValue) ItemStack(org.bukkit.inventory.ItemStack) PlayerData(com.archyx.aureliumskills.data.PlayerData) Inventory(org.bukkit.inventory.Inventory) AnvilInventory(org.bukkit.inventory.AnvilInventory) Location(org.bukkit.Location) EventHandler(org.bukkit.event.EventHandler)

Aggregations

PlayerData (com.archyx.aureliumskills.data.PlayerData)1 EnchantmentValue (com.archyx.aureliumskills.util.mechanics.EnchantmentValue)1 Location (org.bukkit.Location)1 World (org.bukkit.World)1 ExperienceOrb (org.bukkit.entity.ExperienceOrb)1 Player (org.bukkit.entity.Player)1 EventHandler (org.bukkit.event.EventHandler)1 AnvilInventory (org.bukkit.inventory.AnvilInventory)1 Inventory (org.bukkit.inventory.Inventory)1 ItemStack (org.bukkit.inventory.ItemStack)1