Search in sources :

Example 1 with GlowSlime

use of net.glowstone.entity.monster.GlowSlime in project Glowstone by GlowstoneMC.

the class GlowLivingEntity method setHealth.

// //////////////////////////////////////////////////////////////////////////
// Health
@Override
public void setHealth(double health) {
    if (health < 0) {
        health = 0;
    }
    if (health > getMaxHealth()) {
        health = getMaxHealth();
    }
    this.health = health;
    metadata.set(MetadataIndex.HEALTH, (float) health);
    for (Objective objective : getServer().getScoreboardManager().getMainScoreboard().getObjectivesByCriteria(Criterias.HEALTH)) {
        objective.getScore(getName()).setScore((int) health);
    }
    if (health > 0) {
        return;
    }
    if (this.tryUseTotem()) {
        return;
    }
    // Killed
    active = false;
    Sound deathSound = getDeathSound();
    if (deathSound != null && !isSilent()) {
        world.playSound(location, deathSound, getSoundVolume(), getSoundPitch());
    }
    playEffectKnownAndSelf(EntityEffect.DEATH);
    if (this instanceof GlowPlayer) {
        GlowPlayer player = (GlowPlayer) this;
        List<ItemStack> items = null;
        boolean dropInventory = !world.getGameRuleMap().getBoolean(GameRules.KEEP_INVENTORY);
        if (dropInventory) {
            items = Arrays.stream(player.getInventory().getContents()).filter(stack -> !InventoryUtil.isEmpty(stack)).collect(Collectors.toList());
            player.getInventory().clear();
        }
        PlayerDeathEvent event = new PlayerDeathEvent(player, items, 0, player.getDisplayName() + " died.");
        EventFactory.getInstance().callEvent(event);
        server.broadcastMessage(event.getDeathMessage());
        if (dropInventory) {
            for (ItemStack item : items) {
                world.dropItemNaturally(getLocation(), item);
            }
        }
        player.setShoulderEntityRight(null);
        player.setShoulderEntityLeft(null);
        player.incrementStatistic(Statistic.DEATHS);
    } else {
        EntityDeathEvent deathEvent = new EntityDeathEvent(this, new ArrayList<>());
        if (world.getGameRuleMap().getBoolean(GameRules.DO_MOB_LOOT)) {
            LootData data = LootingManager.generate(this);
            deathEvent.getDrops().addAll(data.getItems());
            // Only drop experience when hit by a player within 5 seconds (100 game ticks)
            if (ticksLived - playerDamageTick <= 100 && data.getExperience() > 0) {
                ThreadLocalRandom random = ThreadLocalRandom.current();
                ExperienceSplitter.forEachCut(data.getExperience(), exp -> {
                    double modX = random.nextDouble() - 0.5;
                    double modZ = random.nextDouble() - 0.5;
                    Location xpLocation = new Location(world, location.getBlockX() + 0.5 + modX, location.getY(), location.getBlockZ() + 0.5 + modZ);
                    GlowExperienceOrb orb = (GlowExperienceOrb) world.spawnEntity(xpLocation, EntityType.EXPERIENCE_ORB);
                    orb.setExperience(exp);
                    orb.setSourceEntityId(this.getUniqueId());
                    if (getLastDamager() != null) {
                        orb.setTriggerEntityId(getLastDamager().getUniqueId());
                    }
                });
            }
        }
        deathEvent = EventFactory.getInstance().callEvent(deathEvent);
        for (ItemStack item : deathEvent.getDrops()) {
            world.dropItemNaturally(getLocation(), item);
        }
    }
    // to help keep code maintainable
    if (this instanceof GlowSlime) {
        GlowSlime slime = (GlowSlime) this;
        int size = slime.getSize();
        if (size > 1) {
            int count = 2 + ThreadLocalRandom.current().nextInt(3);
            SlimeSplitEvent event = EventFactory.getInstance().callEvent(new SlimeSplitEvent(slime, count));
            if (event.isCancelled() || event.getCount() <= 0) {
                return;
            }
            count = event.getCount();
            for (int i = 0; i < count; ++i) {
                Location spawnLoc = getLocation().clone();
                spawnLoc.add(ThreadLocalRandom.current().nextDouble(0.5, 3), 0, ThreadLocalRandom.current().nextDouble(0.5, 3));
                GlowSlime splitSlime = (GlowSlime) world.spawnEntity(spawnLoc, EntityType.SLIME);
                // Make the split slime the same name as the killed slime.
                if (getCustomName() != null && !getCustomName().isEmpty()) {
                    splitSlime.setCustomName(getCustomName());
                }
                splitSlime.setSize(size / 2);
            }
        }
    }
}
Also used : EntityDeathEvent(org.bukkit.event.entity.EntityDeathEvent) GlowExperienceOrb(net.glowstone.entity.objects.GlowExperienceOrb) Sound(org.bukkit.Sound) PlayerDeathEvent(org.bukkit.event.entity.PlayerDeathEvent) LootData(net.glowstone.util.loot.LootData) Objective(org.bukkit.scoreboard.Objective) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) GlowSlime(net.glowstone.entity.monster.GlowSlime) SlimeSplitEvent(org.bukkit.event.entity.SlimeSplitEvent) ItemStack(org.bukkit.inventory.ItemStack) Location(org.bukkit.Location)

Aggregations

ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)1 GlowSlime (net.glowstone.entity.monster.GlowSlime)1 GlowExperienceOrb (net.glowstone.entity.objects.GlowExperienceOrb)1 LootData (net.glowstone.util.loot.LootData)1 Location (org.bukkit.Location)1 Sound (org.bukkit.Sound)1 EntityDeathEvent (org.bukkit.event.entity.EntityDeathEvent)1 PlayerDeathEvent (org.bukkit.event.entity.PlayerDeathEvent)1 SlimeSplitEvent (org.bukkit.event.entity.SlimeSplitEvent)1 ItemStack (org.bukkit.inventory.ItemStack)1 Objective (org.bukkit.scoreboard.Objective)1