Search in sources :

Example 1 with Damageable

use of org.bukkit.inventory.meta.Damageable in project EliteMobs by MagmaGuy.

the class PlayerDamagedByEliteMobHandler method eliteMobDamageHandler.

/**
 * EliteMobs -> player damage handler.
 *
 * @param event
 */
@EventHandler(priority = EventPriority.HIGHEST)
public void eliteMobDamageHandler(PlayerDamagedByEliteMobEvent event) {
    if (!event.getEliteMobEntity().isValid()) {
        event.setCancelled(true);
        return;
    }
    if (event.getEntityDamageByEntityEvent().isCancelled() && !bypass)
        return;
    // From this point on, the damage event is fully altered by Elite Mobs
    Player player = (Player) event.getEntity();
    if (ElitePlayerInventory.playerInventories.get(player.getUniqueId()) == null)
        return;
    if (player.isBlocking()) {
        if (player.getInventory().getItemInOffHand().getType().equals(Material.SHIELD)) {
            ItemMeta itemMeta = player.getInventory().getItemInOffHand().getItemMeta();
            Damageable damageable = (Damageable) itemMeta;
            if (player.getInventory().getItemInOffHand().getItemMeta().hasEnchant(Enchantment.DURABILITY))
                if (player.getInventory().getItemInOffHand().getItemMeta().getEnchantLevel(Enchantment.DURABILITY) / 20D > ThreadLocalRandom.current().nextDouble())
                    damageable.setDamage(damageable.getDamage() + 5);
            player.getInventory().getItemInOffHand().setItemMeta(itemMeta);
            if (Material.SHIELD.getMaxDurability() < damageable.getDamage())
                player.getInventory().setItemInOffHand(null);
        }
        if (event.getEntityDamageByEntityEvent().getDamager() instanceof Projectile)
            event.getEntityDamageByEntityEvent().getDamager().remove();
        return;
    }
    // if the damage source is custom , the damage is final
    if (bypass) {
        bypass = false;
        // Deal with the player getting killed
        if (player.getHealth() - event.getEntityDamageByEntityEvent().getDamage() <= 0)
            PlayerDeathMessageByEliteMob.addDeadPlayer(player, PlayerDeathMessageByEliteMob.initializeDeathMessage(player, event.getEliteMobEntity().getLivingEntity()));
        return;
    }
    // if the projectile deals custom damage
    if (event.getProjectile() != null && ProjectileTagger.projectileHasCustomDamage(event.getProjectile())) {
        double damage = ProjectileTagger.getProjectileCustomDamage(event.getProjectile());
        if (damage < 0)
            return;
        // Set the final damage value
        event.getEntityDamageByEntityEvent().setDamage(EntityDamageEvent.DamageModifier.BASE, damage);
        // Deal with the player getting killed
        if (player.getHealth() - event.getEntityDamageByEntityEvent().getDamage() <= 0)
            PlayerDeathMessageByEliteMob.addDeadPlayer(player, PlayerDeathMessageByEliteMob.initializeDeathMessage(player, event.getEliteMobEntity().getLivingEntity()));
        return;
    }
    // Determine tiers
    double eliteTier = MobTierCalculator.findMobTier(event.getEliteMobEntity());
    double playerTier = ElitePlayerInventory.playerInventories.get(player.getUniqueId()).getArmorTier(true);
    double newDamage = eliteToPlayerDamageFormula(eliteTier, playerTier, player, event.getEliteMobEntity(), event.getEntityDamageByEntityEvent());
    // nullify vanilla reductions
    for (EntityDamageEvent.DamageModifier modifier : EntityDamageByEntityEvent.DamageModifier.values()) if (event.getEntityDamageByEntityEvent().isApplicable(modifier))
        event.getEntityDamageByEntityEvent().setDamage(modifier, 0);
    // Set the final damage value
    event.getEntityDamageByEntityEvent().setDamage(EntityDamageEvent.DamageModifier.BASE, newDamage);
    // Deal with the player getting killed
    if (player.getHealth() - event.getEntityDamageByEntityEvent().getDamage() <= 0)
        PlayerDeathMessageByEliteMob.addDeadPlayer(player, PlayerDeathMessageByEliteMob.initializeDeathMessage(player, event.getEliteMobEntity().getLivingEntity()));
}
Also used : Player(org.bukkit.entity.Player) Damageable(org.bukkit.inventory.meta.Damageable) ItemMeta(org.bukkit.inventory.meta.ItemMeta) Projectile(org.bukkit.entity.Projectile) EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent) EventHandler(org.bukkit.event.EventHandler)

Example 2 with Damageable

use of org.bukkit.inventory.meta.Damageable in project EliteMobs by MagmaGuy.

the class RepairMenu method calculateOutput.

private static void calculateOutput(Inventory repairInventory) {
    if (repairInventory.getItem(RepairMenuConfig.eliteScrapInputSlot) == null || repairInventory.getItem(RepairMenuConfig.eliteItemInputSlot) == null) {
        repairInventory.setItem(RepairMenuConfig.outputSlot, null);
        return;
    }
    int scrapLevel = ItemTagger.getEnchantment(repairInventory.getItem(RepairMenuConfig.eliteScrapInputSlot).getItemMeta(), "EliteScrap");
    ItemStack outputItem = repairInventory.getItem(RepairMenuConfig.eliteItemInputSlot).clone();
    int itemLevel = ItemTierFinder.findBattleTier(outputItem);
    int baselineRepair = 300;
    int levelDifferenceModifier = (scrapLevel - itemLevel) * 10;
    int newDamage = baselineRepair + levelDifferenceModifier;
    Damageable damageable = (Damageable) outputItem.getItemMeta();
    int damage = Math.min(damageable.getDamage() - newDamage, damageable.getDamage());
    damageable.setDamage(damage);
    outputItem.setItemMeta(damageable);
    repairInventory.setItem(outputSlot, outputItem);
}
Also used : Damageable(org.bukkit.inventory.meta.Damageable) ItemStack(org.bukkit.inventory.ItemStack)

Example 3 with Damageable

use of org.bukkit.inventory.meta.Damageable in project EliteMobs by MagmaGuy.

the class AlternativeDurabilityLoss method onPlayerDeath.

@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerDeath(PlayerDeathEvent event) {
    ArrayList<ItemStack> itemsList = new ArrayList<>(Arrays.asList(event.getEntity().getInventory().getArmorContents()));
    itemsList.add(event.getEntity().getInventory().getItemInMainHand());
    itemsList.add(event.getEntity().getInventory().getItemInOffHand());
    for (ItemStack itemStack : itemsList) if (itemStack != null && itemStack.getType().getMaxDurability() != 0 && EliteMobsItemDetector.isEliteMobsItem(itemStack) && itemStack.getItemMeta() instanceof Damageable) {
        Damageable damageable = (Damageable) itemStack.getItemMeta();
        int maxDurability = itemStack.getType().getMaxDurability();
        int durabilityLoss = (int) (maxDurability * durabilityLoss(itemStack));
        int currentDurability = damageable.getDamage();
        int newDurability = currentDurability + durabilityLoss;
        damageable.setDamage(newDurability);
        itemStack.setItemMeta(damageable);
        if (newDurability >= maxDurability)
            if (ItemSettingsConfig.isPreventEliteItemsFromBreaking()) {
                damageable.setDamage(maxDurability - 1);
                itemStack.setItemMeta(damageable);
            } else
                itemStack.setAmount(0);
    }
}
Also used : Damageable(org.bukkit.inventory.meta.Damageable) ArrayList(java.util.ArrayList) ItemStack(org.bukkit.inventory.ItemStack) EventHandler(org.bukkit.event.EventHandler)

Example 4 with Damageable

use of org.bukkit.inventory.meta.Damageable in project Prism-Bukkit by prism.

the class MaterialState method setItemDamage.

/**
 * Sets an item as damaged by the amount given as the second param.
 *
 * @param stack  ItemStack
 * @param damage Integer
 */
public static void setItemDamage(ItemStack stack, int damage) {
    ItemMeta meta = Bukkit.getItemFactory().getItemMeta(stack.getType());
    if (meta instanceof Damageable) {
        Damageable d = (Damageable) meta;
        d.setDamage(damage);
        stack.setItemMeta(meta);
    }
}
Also used : Damageable(org.bukkit.inventory.meta.Damageable) ItemMeta(org.bukkit.inventory.meta.ItemMeta)

Example 5 with Damageable

use of org.bukkit.inventory.meta.Damageable in project Denizen-For-Bukkit by DenizenScript.

the class ItemTag method setDurability.

public void setDurability(short value) {
    ItemMeta meta = getItemMeta();
    if (meta instanceof Damageable) {
        ((Damageable) meta).setDamage(value);
        setItemMeta(meta);
    }
}
Also used : Damageable(org.bukkit.inventory.meta.Damageable) ItemMeta(org.bukkit.inventory.meta.ItemMeta)

Aggregations

Damageable (org.bukkit.inventory.meta.Damageable)5 ItemMeta (org.bukkit.inventory.meta.ItemMeta)3 EventHandler (org.bukkit.event.EventHandler)2 ItemStack (org.bukkit.inventory.ItemStack)2 ArrayList (java.util.ArrayList)1 Player (org.bukkit.entity.Player)1 Projectile (org.bukkit.entity.Projectile)1 EntityDamageEvent (org.bukkit.event.entity.EntityDamageEvent)1