Search in sources :

Example 1 with EntityDamageEvent

use of cn.nukkit.event.entity.EntityDamageEvent in project Nukkit by Nukkit.

the class Player method kill.

@Override
public void kill() {
    if (!this.spawned) {
        return;
    }
    boolean showMessages = this.level.getGameRules().getBoolean(GameRule.SHOW_DEATH_MESSAGE);
    String message = "death.attack.generic";
    List<String> params = new ArrayList<>();
    params.add(this.getDisplayName());
    if (showMessages) {
        EntityDamageEvent cause = this.getLastDamageCause();
        switch(cause == null ? DamageCause.CUSTOM : cause.getCause()) {
            case ENTITY_ATTACK:
                if (cause instanceof EntityDamageByEntityEvent) {
                    Entity e = ((EntityDamageByEntityEvent) cause).getDamager();
                    killer = e;
                    if (e instanceof Player) {
                        message = "death.attack.player";
                        params.add(((Player) e).getDisplayName());
                        break;
                    } else if (e instanceof EntityLiving) {
                        message = "death.attack.mob";
                        params.add(!Objects.equals(e.getNameTag(), "") ? e.getNameTag() : e.getName());
                        break;
                    } else {
                        params.add("Unknown");
                    }
                }
                break;
            case PROJECTILE:
                if (cause instanceof EntityDamageByEntityEvent) {
                    Entity e = ((EntityDamageByEntityEvent) cause).getDamager();
                    killer = e;
                    if (e instanceof Player) {
                        message = "death.attack.arrow";
                        params.add(((Player) e).getDisplayName());
                    } else if (e instanceof EntityLiving) {
                        message = "death.attack.arrow";
                        params.add(!Objects.equals(e.getNameTag(), "") ? e.getNameTag() : e.getName());
                        break;
                    } else {
                        params.add("Unknown");
                    }
                }
                break;
            case SUICIDE:
                message = "death.attack.generic";
                break;
            case VOID:
                message = "death.attack.outOfWorld";
                break;
            case FALL:
                if (cause != null) {
                    if (cause.getFinalDamage() > 2) {
                        message = "death.fell.accident.generic";
                        break;
                    }
                }
                message = "death.attack.fall";
                break;
            case SUFFOCATION:
                message = "death.attack.inWall";
                break;
            case LAVA:
                message = "death.attack.lava";
                break;
            case FIRE:
                message = "death.attack.onFire";
                break;
            case FIRE_TICK:
                message = "death.attack.inFire";
                break;
            case DROWNING:
                message = "death.attack.drown";
                break;
            case CONTACT:
                if (cause instanceof EntityDamageByBlockEvent) {
                    if (((EntityDamageByBlockEvent) cause).getDamager().getId() == Block.CACTUS) {
                        message = "death.attack.cactus";
                    }
                }
                break;
            case BLOCK_EXPLOSION:
            case ENTITY_EXPLOSION:
                if (cause instanceof EntityDamageByEntityEvent) {
                    Entity e = ((EntityDamageByEntityEvent) cause).getDamager();
                    killer = e;
                    if (e instanceof Player) {
                        message = "death.attack.explosion.player";
                        params.add(((Player) e).getDisplayName());
                    } else if (e instanceof EntityLiving) {
                        message = "death.attack.explosion.player";
                        params.add(!Objects.equals(e.getNameTag(), "") ? e.getNameTag() : e.getName());
                        break;
                    }
                } else {
                    message = "death.attack.explosion";
                }
                break;
            case MAGIC:
                message = "death.attack.magic";
                break;
            case CUSTOM:
                break;
            default:
                break;
        }
    } else {
        message = "";
        params.clear();
    }
    this.health = 0;
    this.scheduleUpdate();
    PlayerDeathEvent ev = new PlayerDeathEvent(this, this.getDrops(), new TranslationContainer(message, params.stream().toArray(String[]::new)), this.getExperienceLevel());
    ev.setKeepExperience(this.level.gameRules.getBoolean(GameRule.KEEP_INVENTORY));
    ev.setKeepInventory(ev.getKeepExperience());
    this.server.getPluginManager().callEvent(ev);
    if (!ev.getKeepInventory() && this.level.getGameRules().getBoolean(GameRule.DO_ENTITY_DROPS)) {
        for (Item item : ev.getDrops()) {
            this.level.dropItem(this, item, null, true, 40);
        }
        if (this.inventory != null) {
            this.inventory.clearAll();
        }
    }
    if (!ev.getKeepExperience() && this.level.getGameRules().getBoolean(GameRule.DO_ENTITY_DROPS)) {
        if (this.isSurvival() || this.isAdventure()) {
            int exp = ev.getExperience() * 7;
            if (exp > 100)
                exp = 100;
            int add = 1;
            for (int ii = 1; ii < exp; ii += add) {
                this.getLevel().dropExpOrb(this, add);
                add = new NukkitRandom().nextRange(1, 3);
            }
        }
        this.setExperience(0, 0);
    }
    if (showMessages && !ev.getDeathMessage().toString().isEmpty()) {
        this.server.broadcast(ev.getDeathMessage(), Server.BROADCAST_CHANNEL_USERS);
    }
    RespawnPacket pk = new RespawnPacket();
    Position pos = this.getSpawn();
    pk.x = (float) pos.x;
    pk.y = (float) pos.y;
    pk.z = (float) pos.z;
    this.dataPacket(pk);
}
Also used : BlockEntity(cn.nukkit.blockentity.BlockEntity) EntityDamageByBlockEvent(cn.nukkit.event.entity.EntityDamageByBlockEvent) EntityDamageByEntityEvent(cn.nukkit.event.entity.EntityDamageByEntityEvent) TranslationContainer(cn.nukkit.lang.TranslationContainer) EntityDamageEvent(cn.nukkit.event.entity.EntityDamageEvent)

Example 2 with EntityDamageEvent

use of cn.nukkit.event.entity.EntityDamageEvent in project Nukkit by Nukkit.

the class Potion method applyPotion.

public void applyPotion(Entity entity, double health) {
    if (!(entity instanceof EntityLiving)) {
        return;
    }
    Effect applyEffect = getEffect(this.getId(), this.isSplash());
    if (applyEffect == null) {
        return;
    }
    if (entity instanceof Player) {
        if (!((Player) entity).isSurvival() && applyEffect.isBad()) {
            return;
        }
    }
    PotionApplyEvent event = new PotionApplyEvent(this, applyEffect, entity);
    entity.getServer().getPluginManager().callEvent(event);
    if (event.isCancelled()) {
        return;
    }
    applyEffect = event.getApplyEffect();
    switch(this.getId()) {
        case INSTANT_HEALTH:
        case INSTANT_HEALTH_II:
            entity.heal(new EntityRegainHealthEvent(entity, (float) (health * (double) (4 << (applyEffect.getAmplifier() + 1))), EntityRegainHealthEvent.CAUSE_EATING));
            break;
        case HARMING:
        case HARMING_II:
            entity.attack(new EntityDamageEvent(entity, DamageCause.MAGIC, (float) (health * (double) (6 << (applyEffect.getAmplifier() + 1)))));
            break;
        default:
            int duration = (int) ((isSplash() ? health : 1) * (double) applyEffect.getDuration() + 0.5);
            applyEffect.setDuration(duration);
            entity.addEffect(applyEffect);
    }
}
Also used : Player(cn.nukkit.Player) EntityLiving(cn.nukkit.entity.EntityLiving) EntityRegainHealthEvent(cn.nukkit.event.entity.EntityRegainHealthEvent) PotionApplyEvent(cn.nukkit.event.potion.PotionApplyEvent) EntityDamageEvent(cn.nukkit.event.entity.EntityDamageEvent)

Example 3 with EntityDamageEvent

use of cn.nukkit.event.entity.EntityDamageEvent in project Nukkit by Nukkit.

the class PlayerFood method update.

public void update(int tickDiff) {
    if (!this.getPlayer().isFoodEnabled())
        return;
    if (this.getPlayer().isAlive()) {
        int diff = Server.getInstance().getDifficulty();
        if (this.getLevel() > 17) {
            this.foodTickTimer += tickDiff;
            if (this.foodTickTimer >= 80) {
                if (this.getPlayer().getHealth() < this.getPlayer().getMaxHealth()) {
                    EntityRegainHealthEvent ev = new EntityRegainHealthEvent(this.getPlayer(), 1, EntityRegainHealthEvent.CAUSE_EATING);
                    this.getPlayer().heal(ev);
                // this.updateFoodExpLevel(3);
                }
                this.foodTickTimer = 0;
            }
        } else if (this.getLevel() == 0) {
            this.foodTickTimer += tickDiff;
            if (this.foodTickTimer >= 80) {
                EntityDamageEvent ev = new EntityDamageEvent(this.getPlayer(), DamageCause.VOID, 1);
                float now = this.getPlayer().getHealth();
                if (diff == 1) {
                    if (now > 10)
                        this.getPlayer().attack(ev);
                } else if (diff == 2) {
                    if (now > 1)
                        this.getPlayer().attack(ev);
                } else {
                    this.getPlayer().attack(ev);
                }
                this.foodTickTimer = 0;
            }
        }
        if (this.getPlayer().hasEffect(Effect.HUNGER)) {
            this.updateFoodExpLevel(0.025);
        }
    }
}
Also used : EntityRegainHealthEvent(cn.nukkit.event.entity.EntityRegainHealthEvent) EntityDamageEvent(cn.nukkit.event.entity.EntityDamageEvent)

Example 4 with EntityDamageEvent

use of cn.nukkit.event.entity.EntityDamageEvent in project Nukkit by Nukkit.

the class KillCommand method execute.

@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
    if (!this.testPermission(sender)) {
        return true;
    }
    if (args.length >= 2) {
        sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));
        return false;
    }
    if (args.length == 1) {
        if (!sender.hasPermission("nukkit.command.kill.other")) {
            sender.sendMessage(new TranslationContainer(TextFormat.RED + "%commands.generic.permission"));
            return true;
        }
        Player player = sender.getServer().getPlayer(args[0]);
        if (player != null) {
            EntityDamageEvent ev = new EntityDamageEvent(player, DamageCause.SUICIDE, 1000);
            sender.getServer().getPluginManager().callEvent(ev);
            if (ev.isCancelled()) {
                return true;
            }
            player.setLastDamageCause(ev);
            player.setHealth(0);
            Command.broadcastCommandMessage(sender, new TranslationContainer("commands.kill.successful", player.getName()));
        } else {
            sender.sendMessage(new TranslationContainer(TextFormat.RED + "%commands.generic.player.notFound"));
        }
        return true;
    }
    if (sender instanceof Player) {
        if (!sender.hasPermission("nukkit.command.kill.self")) {
            sender.sendMessage(new TranslationContainer(TextFormat.RED + "%commands.generic.permission"));
            return true;
        }
        EntityDamageEvent ev = new EntityDamageEvent((Player) sender, DamageCause.SUICIDE, 1000);
        sender.getServer().getPluginManager().callEvent(ev);
        if (ev.isCancelled()) {
            return true;
        }
        ((Player) sender).setLastDamageCause(ev);
        ((Player) sender).setHealth(0);
        sender.sendMessage(new TranslationContainer("commands.kill.successful", sender.getName()));
    } else {
        sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));
        return false;
    }
    return true;
}
Also used : Player(cn.nukkit.Player) TranslationContainer(cn.nukkit.lang.TranslationContainer) EntityDamageEvent(cn.nukkit.event.entity.EntityDamageEvent)

Example 5 with EntityDamageEvent

use of cn.nukkit.event.entity.EntityDamageEvent 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

EntityDamageEvent (cn.nukkit.event.entity.EntityDamageEvent)6 Player (cn.nukkit.Player)2 EntityDamageByBlockEvent (cn.nukkit.event.entity.EntityDamageByBlockEvent)2 EntityDamageByEntityEvent (cn.nukkit.event.entity.EntityDamageByEntityEvent)2 EntityRegainHealthEvent (cn.nukkit.event.entity.EntityRegainHealthEvent)2 Item (cn.nukkit.item.Item)2 TranslationContainer (cn.nukkit.lang.TranslationContainer)2 Block (cn.nukkit.block.Block)1 BlockAir (cn.nukkit.block.BlockAir)1 BlockTNT (cn.nukkit.block.BlockTNT)1 BlockEntity (cn.nukkit.blockentity.BlockEntity)1 Entity (cn.nukkit.entity.Entity)1 EntityHumanType (cn.nukkit.entity.EntityHumanType)1 EntityLiving (cn.nukkit.entity.EntityLiving)1 BlockUpdateEvent (cn.nukkit.event.block.BlockUpdateEvent)1 EntityExplodeEvent (cn.nukkit.event.entity.EntityExplodeEvent)1 PotionApplyEvent (cn.nukkit.event.potion.PotionApplyEvent)1 ItemBlock (cn.nukkit.item.ItemBlock)1 HugeExplodeSeedParticle (cn.nukkit.level.particle.HugeExplodeSeedParticle)1 ExplodePacket (cn.nukkit.network.protocol.ExplodePacket)1