Search in sources :

Example 16 with Enchantment

use of cn.nukkit.item.enchantment.Enchantment in project Nukkit by Nukkit.

the class EntityHumanType method attack.

@Override
public boolean attack(EntityDamageEvent source) {
    if (!this.isAlive()) {
        return false;
    }
    if (source.getCause() != DamageCause.VOID && source.getCause() != DamageCause.CUSTOM && source.getCause() != DamageCause.MAGIC) {
        int points = 0;
        int epf = 0;
        int toughness = 0;
        for (Item armor : inventory.getArmorContents()) {
            points += armor.getArmorPoints();
            epf += calculateEnchantmentReduction(armor, source);
            toughness += armor.getToughness();
        }
        float originalDamage = source.getDamage();
        float finalDamage = (float) (originalDamage * (1 - Math.max(points / 5, points - originalDamage / (2 + toughness / 4)) / 25) * (1 - /*0.75 */
        epf * 0.04));
        source.setDamage(finalDamage - originalDamage, DamageModifier.ARMOR);
    // source.setDamage(source.getDamage(DamageModifier.ARMOR_ENCHANTMENTS) - (originalDamage - originalDamage * (1 - epf / 25)), DamageModifier.ARMOR_ENCHANTMENTS);
    }
    if (super.attack(source)) {
        Entity damager = null;
        if (source instanceof EntityDamageByEntityEvent) {
            damager = ((EntityDamageByEntityEvent) source).getDamager();
        }
        for (int slot = 0; slot < 4; slot++) {
            Item armor = this.inventory.getArmorItem(slot);
            if (armor.hasEnchantments()) {
                if (damager != null) {
                    for (Enchantment enchantment : armor.getEnchantments()) {
                        enchantment.doPostAttack(damager, this);
                    }
                }
                Enchantment durability = armor.getEnchantment(Enchantment.ID_DURABILITY);
                if (durability != null && durability.getLevel() > 0 && (100 / (durability.getLevel() + 1)) <= new Random().nextInt(100))
                    continue;
            }
            armor.setDamage(armor.getDamage() + 1);
            if (armor.getDamage() >= armor.getMaxDurability()) {
                inventory.setArmorItem(slot, new ItemBlock(new BlockAir()));
            } else {
                inventory.setArmorItem(slot, armor, true);
            }
        }
        return true;
    } else {
        return false;
    }
}
Also used : BlockAir(cn.nukkit.block.BlockAir) Item(cn.nukkit.item.Item) EntityDamageByEntityEvent(cn.nukkit.event.entity.EntityDamageByEntityEvent) Random(java.util.Random) Enchantment(cn.nukkit.item.enchantment.Enchantment) ItemBlock(cn.nukkit.item.ItemBlock)

Example 17 with Enchantment

use of cn.nukkit.item.enchantment.Enchantment in project Nukkit by Nukkit.

the class EntityHumanType method setOnFire.

@Override
public void setOnFire(int seconds) {
    int level = 0;
    for (Item armor : this.inventory.getArmorContents()) {
        Enchantment fireProtection = armor.getEnchantment(Enchantment.ID_PROTECTION_FIRE);
        if (fireProtection != null && fireProtection.getLevel() > 0) {
            level = Math.max(level, fireProtection.getLevel());
        }
    }
    seconds = (int) (seconds * (1 - level * 0.15));
    super.setOnFire(seconds);
}
Also used : Item(cn.nukkit.item.Item) Enchantment(cn.nukkit.item.enchantment.Enchantment)

Example 18 with Enchantment

use of cn.nukkit.item.enchantment.Enchantment in project Nukkit by Nukkit.

the class Level method useBreakOn.

public Item useBreakOn(Vector3 vector, Item item, Player player, boolean createParticles) {
    if (player != null && player.getGamemode() > 1) {
        return null;
    }
    Block target = this.getBlock(vector);
    Item[] drops;
    if (item == null) {
        item = new ItemBlock(new BlockAir(), 0, 0);
    }
    if (player != null) {
        double breakTime = target.getBreakTime(item, player);
        if (player.isCreative() && breakTime > 0.15) {
            breakTime = 0.15;
        }
        if (player.hasEffect(Effect.SWIFTNESS)) {
            breakTime *= 1 - (0.2 * (player.getEffect(Effect.SWIFTNESS).getAmplifier() + 1));
        }
        if (player.hasEffect(Effect.MINING_FATIGUE)) {
            breakTime *= 1 - (0.3 * (player.getEffect(Effect.MINING_FATIGUE).getAmplifier() + 1));
        }
        Enchantment eff = item.getEnchantment(Enchantment.ID_EFFICIENCY);
        if (eff != null && eff.getLevel() > 0) {
            breakTime *= 1 - (0.3 * eff.getLevel());
        }
        breakTime -= 0.15;
        BlockBreakEvent ev = new BlockBreakEvent(player, target, item, player.isCreative(), (player.lastBreak + breakTime * 1000) > System.currentTimeMillis());
        double distance;
        if (player.isSurvival() && !target.isBreakable(item)) {
            ev.setCancelled();
        } else if (!player.isOp() && (distance = this.server.getSpawnRadius()) > -1) {
            Vector2 t = new Vector2(target.x, target.z);
            Vector2 s = new Vector2(this.getSpawnLocation().x, this.getSpawnLocation().z);
            if (!this.server.getOps().getAll().isEmpty() && t.distance(s) <= distance) {
                ev.setCancelled();
            }
        }
        this.server.getPluginManager().callEvent(ev);
        if (ev.isCancelled()) {
            return null;
        }
        if (!ev.getInstaBreak() && ev.isFastBreak()) {
            return null;
        }
        player.lastBreak = System.currentTimeMillis();
        drops = ev.getDrops();
    } else if (!target.isBreakable(item)) {
        return null;
    } else {
        drops = target.getDrops(item);
    }
    Block above = this.getBlock(new Vector3(target.x, target.y + 1, target.z));
    if (above != null) {
        if (above.getId() == Item.FIRE) {
            this.setBlock(above, new BlockAir(), true);
        }
    }
    Tag tag = item.getNamedTagEntry("CanDestroy");
    if (tag instanceof ListTag) {
        boolean canBreak = false;
        for (Tag v : ((ListTag<Tag>) tag).getAll()) {
            if (v instanceof StringTag) {
                Item entry = Item.fromString(((StringTag) v).data);
                if (entry.getId() > 0 && entry.getBlock() != null && entry.getBlock().getId() == target.getId()) {
                    canBreak = true;
                    break;
                }
            }
        }
        if (!canBreak) {
            return null;
        }
    }
    if (createParticles) {
        Map<Integer, Player> players = this.getChunkPlayers((int) target.x >> 4, (int) target.z >> 4);
        this.addParticle(new DestroyBlockParticle(target.add(0.5), target), players.values());
        if (player != null) {
            players.remove(player.getLoaderId());
        }
    }
    target.onBreak(item);
    BlockEntity blockEntity = this.getBlockEntity(target);
    if (blockEntity != null) {
        if (blockEntity instanceof InventoryHolder) {
            if (blockEntity instanceof BlockEntityChest) {
                ((BlockEntityChest) blockEntity).unpair();
            }
            for (Item chestItem : ((InventoryHolder) blockEntity).getInventory().getContents().values()) {
                this.dropItem(target, chestItem);
            }
        }
        blockEntity.close();
        this.updateComparatorOutputLevel(target);
    }
    item.useOn(target);
    if (item.isTool() && item.getDamage() >= item.getMaxDurability()) {
        item = new ItemBlock(new BlockAir(), 0, 0);
    }
    if (this.gameRules.getBoolean(GameRule.DO_TILE_DROPS)) {
        int dropExp = target.getDropExp();
        if (player != null) {
            player.addExperience(dropExp);
            if (player.isSurvival()) {
                for (int ii = 1; ii <= dropExp; ii++) {
                    this.dropExpOrb(target, 1);
                }
            }
        }
        if (player == null || player.isSurvival()) {
            for (Item drop : drops) {
                if (drop.getCount() > 0) {
                    this.dropItem(vector.add(0.5, 0.5, 0.5), drop);
                }
            }
        }
    }
    return item;
}
Also used : BlockAir(cn.nukkit.block.BlockAir) Player(cn.nukkit.Player) DestroyBlockParticle(cn.nukkit.level.particle.DestroyBlockParticle) ItemBlock(cn.nukkit.item.ItemBlock) EntityItem(cn.nukkit.entity.item.EntityItem) Item(cn.nukkit.item.Item) BlockEntityChest(cn.nukkit.blockentity.BlockEntityChest) ItemBlock(cn.nukkit.item.ItemBlock) Block(cn.nukkit.block.Block) BlockBreakEvent(cn.nukkit.event.block.BlockBreakEvent) Enchantment(cn.nukkit.item.enchantment.Enchantment) InventoryHolder(cn.nukkit.inventory.InventoryHolder) BlockEntity(cn.nukkit.blockentity.BlockEntity)

Aggregations

Enchantment (cn.nukkit.item.enchantment.Enchantment)18 Item (cn.nukkit.item.Item)13 Random (java.util.Random)6 BlockAir (cn.nukkit.block.BlockAir)3 CompoundTag (cn.nukkit.nbt.tag.CompoundTag)3 Player (cn.nukkit.Player)2 BlockEntity (cn.nukkit.blockentity.BlockEntity)2 EntityArrow (cn.nukkit.entity.projectile.EntityArrow)2 EntityDamageByEntityEvent (cn.nukkit.event.entity.EntityDamageByEntityEvent)2 ItemBlock (cn.nukkit.item.ItemBlock)2 TranslationContainer (cn.nukkit.lang.TranslationContainer)2 Type (cn.nukkit.AdventureSettings.Type)1 cn.nukkit.block (cn.nukkit.block)1 Block (cn.nukkit.block.Block)1 BlockEntityChest (cn.nukkit.blockentity.BlockEntityChest)1 BlockEntityItemFrame (cn.nukkit.blockentity.BlockEntityItemFrame)1 BlockEntitySpawnable (cn.nukkit.blockentity.BlockEntitySpawnable)1 Command (cn.nukkit.command.Command)1 CommandSender (cn.nukkit.command.CommandSender)1 CommandDataVersions (cn.nukkit.command.data.CommandDataVersions)1