Search in sources :

Example 6 with ItemBlock

use of cn.nukkit.item.ItemBlock in project Nukkit by Nukkit.

the class PlayerInventory method clear.

@Override
public boolean clear(int index, boolean send) {
    if (this.slots.containsKey(index)) {
        Item item = new ItemBlock(new BlockAir(), null, 0);
        Item old = this.slots.get(index);
        if (index >= this.getSize() && index < this.size) {
            EntityArmorChangeEvent ev = new EntityArmorChangeEvent(this.getHolder(), old, item, index);
            Server.getInstance().getPluginManager().callEvent(ev);
            if (ev.isCancelled()) {
                if (index >= this.size) {
                    this.sendArmorSlot(index, this.getViewers());
                } else {
                    this.sendSlot(index, this.getViewers());
                }
                return false;
            }
            item = ev.getNewItem();
        } else {
            EntityInventoryChangeEvent ev = new EntityInventoryChangeEvent(this.getHolder(), old, item, index);
            Server.getInstance().getPluginManager().callEvent(ev);
            if (ev.isCancelled()) {
                if (index >= this.size) {
                    this.sendArmorSlot(index, this.getViewers());
                } else {
                    this.sendSlot(index, this.getViewers());
                }
                return false;
            }
            item = ev.getNewItem();
        }
        if (item.getId() != Item.AIR) {
            this.slots.put(index, item.clone());
        } else {
            this.slots.remove(index);
        }
        this.onSlotChange(index, old, send);
    }
    return true;
}
Also used : BlockAir(cn.nukkit.block.BlockAir) Item(cn.nukkit.item.Item) EntityArmorChangeEvent(cn.nukkit.event.entity.EntityArmorChangeEvent) EntityInventoryChangeEvent(cn.nukkit.event.entity.EntityInventoryChangeEvent) ItemBlock(cn.nukkit.item.ItemBlock)

Example 7 with ItemBlock

use of cn.nukkit.item.ItemBlock in project Nukkit by Nukkit.

the class PlayerInventory method setArmorContents.

public void setArmorContents(Item[] items) {
    if (items.length < 4) {
        Item[] newItems = new Item[4];
        System.arraycopy(items, 0, newItems, 0, items.length);
        items = newItems;
    }
    for (int i = 0; i < 4; ++i) {
        if (items[i] == null) {
            items[i] = new ItemBlock(new BlockAir(), null, 0);
        }
        if (items[i].getId() == Item.AIR) {
            this.clear(this.getSize() + i);
        } else {
            this.setItem(this.getSize() + i, items[i]);
        }
    }
}
Also used : BlockAir(cn.nukkit.block.BlockAir) Item(cn.nukkit.item.Item) ItemBlock(cn.nukkit.item.ItemBlock)

Example 8 with ItemBlock

use of cn.nukkit.item.ItemBlock in project Nukkit by Nukkit.

the class BlockEntityFurnace method checkFuel.

protected void checkFuel(Item fuel) {
    FurnaceBurnEvent ev = new FurnaceBurnEvent(this, fuel, fuel.getFuelTime() == null ? 0 : fuel.getFuelTime());
    if (ev.isCancelled()) {
        return;
    }
    maxTime = ev.getBurnTime();
    burnTime = ev.getBurnTime();
    burnDuration = 0;
    if (this.getBlock().getId() == Item.FURNACE) {
        this.getLevel().setBlock(this, new BlockFurnaceBurning(this.getBlock().getDamage()), true);
    }
    if (burnTime > 0 && ev.isBurning()) {
        fuel.setCount(fuel.getCount() - 1);
        if (fuel.getCount() == 0) {
            if (fuel.getId() == Item.BUCKET && fuel.getDamage() == 10) {
                fuel.setDamage(0);
                fuel.setCount(1);
            } else {
                fuel = new ItemBlock(new BlockAir(), 0, 0);
            }
        }
        this.inventory.setFuel(fuel);
    }
}
Also used : BlockAir(cn.nukkit.block.BlockAir) FurnaceBurnEvent(cn.nukkit.event.inventory.FurnaceBurnEvent) BlockFurnaceBurning(cn.nukkit.block.BlockFurnaceBurning) ItemBlock(cn.nukkit.item.ItemBlock)

Example 9 with ItemBlock

use of cn.nukkit.item.ItemBlock 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 10 with ItemBlock

use of cn.nukkit.item.ItemBlock in project Nukkit by Nukkit.

the class Explosion method explodeB.

public boolean explodeB() {
    HashMap<BlockVector3, Boolean> updateBlocks = new HashMap<>();
    List<Vector3> send = new ArrayList<>();
    Vector3 source = (new Vector3(this.source.x, this.source.y, this.source.z)).floor();
    double yield = (1d / this.size) * 100d;
    if (this.what instanceof Entity) {
        EntityExplodeEvent ev = new EntityExplodeEvent((Entity) this.what, this.source, this.affectedBlocks, yield);
        this.level.getServer().getPluginManager().callEvent(ev);
        if (ev.isCancelled()) {
            return false;
        } else {
            yield = ev.getYield();
            this.affectedBlocks = ev.getBlockList();
        }
    }
    double explosionSize = this.size * 2d;
    double minX = NukkitMath.floorDouble(this.source.x - explosionSize - 1);
    double maxX = NukkitMath.ceilDouble(this.source.x + explosionSize + 1);
    double minY = NukkitMath.floorDouble(this.source.y - explosionSize - 1);
    double maxY = NukkitMath.ceilDouble(this.source.y + explosionSize + 1);
    double minZ = NukkitMath.floorDouble(this.source.z - explosionSize - 1);
    double maxZ = NukkitMath.ceilDouble(this.source.z + explosionSize + 1);
    AxisAlignedBB explosionBB = new SimpleAxisAlignedBB(minX, minY, minZ, maxX, maxY, maxZ);
    Entity[] list = this.level.getNearbyEntities(explosionBB, this.what instanceof Entity ? (Entity) this.what : null);
    for (Entity entity : list) {
        double distance = entity.distance(this.source) / explosionSize;
        if (distance <= 1) {
            Vector3 motion = entity.subtract(this.source).normalize();
            int exposure = 1;
            double impact = (1 - distance) * exposure;
            int damage = (int) (((impact * impact + impact) / 2) * 8 * explosionSize + 1);
            if (this.what instanceof Entity) {
                entity.attack(new EntityDamageByEntityEvent((Entity) this.what, entity, DamageCause.ENTITY_EXPLOSION, damage));
            } else if (this.what instanceof Block) {
                entity.attack(new EntityDamageByBlockEvent((Block) this.what, entity, DamageCause.BLOCK_EXPLOSION, damage));
            } else {
                entity.attack(new EntityDamageEvent(entity, DamageCause.BLOCK_EXPLOSION, damage));
            }
            entity.setMotion(motion.multiply(impact));
        }
    }
    ItemBlock air = new ItemBlock(new BlockAir());
    // Iterator iter = this.affectedBlocks.entrySet().iterator();
    for (Block block : this.affectedBlocks) {
        // Block block = (Block) ((HashMap.Entry) iter.next()).getValue();
        if (block.getId() == Block.TNT) {
            ((BlockTNT) block).prime(new NukkitRandom().nextRange(10, 30));
        } else if (Math.random() * 100 < yield) {
            for (Item drop : block.getDrops(air)) {
                this.level.dropItem(block.add(0.5, 0.5, 0.5), drop);
            }
        }
        this.level.setBlockIdAt((int) block.x, (int) block.y, (int) block.z, 0);
        Vector3 pos = new Vector3(block.x, block.y, block.z);
        for (BlockFace side : BlockFace.values()) {
            Vector3 sideBlock = pos.getSide(side);
            BlockVector3 index = Level.blockHash((int) sideBlock.x, (int) sideBlock.y, (int) sideBlock.z);
            if (!this.affectedBlocks.contains(sideBlock) && !updateBlocks.containsKey(index)) {
                BlockUpdateEvent ev = new BlockUpdateEvent(this.level.getBlock(sideBlock));
                this.level.getServer().getPluginManager().callEvent(ev);
                if (!ev.isCancelled()) {
                    ev.getBlock().onUpdate(Level.BLOCK_UPDATE_NORMAL);
                }
                updateBlocks.put(index, true);
            }
        }
        send.add(new Vector3(block.x - source.x, block.y - source.y, block.z - source.z));
    }
    ExplodePacket pk = new ExplodePacket();
    pk.x = (float) this.source.x;
    pk.y = (float) this.source.y;
    pk.z = (float) this.source.z;
    pk.radius = (float) this.size;
    pk.records = send.stream().toArray(Vector3[]::new);
    this.level.addChunkPacket((int) source.x >> 4, (int) source.z >> 4, pk);
    this.level.addParticle(new HugeExplodeSeedParticle(this.source));
    this.level.addSound(new Vector3(this.source.x, this.source.y, this.source.z), Sound.RANDOM_EXPLODE);
    return true;
}
Also used : Entity(cn.nukkit.entity.Entity) EntityExplodeEvent(cn.nukkit.event.entity.EntityExplodeEvent) HashMap(java.util.HashMap) EntityDamageByBlockEvent(cn.nukkit.event.entity.EntityDamageByBlockEvent) ArrayList(java.util.ArrayList) ExplodePacket(cn.nukkit.network.protocol.ExplodePacket) Item(cn.nukkit.item.Item) HugeExplodeSeedParticle(cn.nukkit.level.particle.HugeExplodeSeedParticle) EntityDamageEvent(cn.nukkit.event.entity.EntityDamageEvent) BlockAir(cn.nukkit.block.BlockAir) ItemBlock(cn.nukkit.item.ItemBlock) BlockTNT(cn.nukkit.block.BlockTNT) EntityDamageByEntityEvent(cn.nukkit.event.entity.EntityDamageByEntityEvent) BlockUpdateEvent(cn.nukkit.event.block.BlockUpdateEvent) ItemBlock(cn.nukkit.item.ItemBlock) Block(cn.nukkit.block.Block)

Aggregations

ItemBlock (cn.nukkit.item.ItemBlock)12 BlockAir (cn.nukkit.block.BlockAir)11 Item (cn.nukkit.item.Item)9 Player (cn.nukkit.Player)3 Block (cn.nukkit.block.Block)3 BlockEntity (cn.nukkit.blockentity.BlockEntity)3 Entity (cn.nukkit.entity.Entity)3 EntityItem (cn.nukkit.entity.item.EntityItem)2 EntityDamageByEntityEvent (cn.nukkit.event.entity.EntityDamageByEntityEvent)2 EntityInventoryChangeEvent (cn.nukkit.event.entity.EntityInventoryChangeEvent)2 Enchantment (cn.nukkit.item.enchantment.Enchantment)2 BlockFurnace (cn.nukkit.block.BlockFurnace)1 BlockFurnaceBurning (cn.nukkit.block.BlockFurnaceBurning)1 BlockTNT (cn.nukkit.block.BlockTNT)1 BlockEntityChest (cn.nukkit.blockentity.BlockEntityChest)1 BlockEntityItemFrame (cn.nukkit.blockentity.BlockEntityItemFrame)1 EntityArrow (cn.nukkit.entity.projectile.EntityArrow)1 BlockBreakEvent (cn.nukkit.event.block.BlockBreakEvent)1 BlockPlaceEvent (cn.nukkit.event.block.BlockPlaceEvent)1 BlockUpdateEvent (cn.nukkit.event.block.BlockUpdateEvent)1