Search in sources :

Example 1 with LootData

use of net.glowstone.util.loot.LootData in project Glowstone by GlowstoneMC.

the class GlowLivingEntity method setHealth.

@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) {
        active = false;
        Sound deathSound = getDeathSound();
        if (deathSound != null && !isSilent()) {
            world.playSound(location, deathSound, getSoundVolume(), getSoundPitch());
        }
        playEffect(EntityEffect.DEATH);
        if (this instanceof GlowPlayer) {
            GlowPlayer player = (GlowPlayer) this;
            ItemStack mainHand = player.getInventory().getItemInMainHand();
            ItemStack offHand = player.getInventory().getItemInOffHand();
            if (!InventoryUtil.isEmpty(mainHand) && mainHand.getType() == Material.TOTEM) {
                player.getInventory().setItemInMainHand(InventoryUtil.createEmptyStack());
                player.setHealth(1.0);
                active = true;
                return;
            } else if (!InventoryUtil.isEmpty(offHand) && offHand.getType() == Material.TOTEM) {
                player.getInventory().setItemInOffHand(InventoryUtil.createEmptyStack());
                player.setHealth(1.0);
                active = true;
                return;
            }
            List<ItemStack> items = new ArrayList<>();
            if (!world.getGameRuleMap().getBoolean("keepInventory")) {
                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.callEvent(event);
            server.broadcastMessage(event.getDeathMessage());
            for (ItemStack item : items) {
                world.dropItemNaturally(getLocation(), item);
            }
            player.incrementStatistic(Statistic.DEATHS);
        } else {
            EntityDeathEvent deathEvent = new EntityDeathEvent(this, new ArrayList<>());
            if (world.getGameRuleMap().getBoolean("doMobLoot")) {
                LootData data = LootingManager.generate(this);
                Collections.addAll(deathEvent.getDrops(), data.getItems());
            // todo: drop experience
            }
            deathEvent = EventFactory.callEvent(deathEvent);
            for (ItemStack item : deathEvent.getDrops()) {
                world.dropItemNaturally(getLocation(), item);
            }
        }
    }
}
Also used : Objective(org.bukkit.scoreboard.Objective) LootData(net.glowstone.util.loot.LootData) ItemStack(org.bukkit.inventory.ItemStack)

Aggregations

LootData (net.glowstone.util.loot.LootData)1 ItemStack (org.bukkit.inventory.ItemStack)1 Objective (org.bukkit.scoreboard.Objective)1