Search in sources :

Example 1 with GlowLivingEntity

use of net.glowstone.entity.GlowLivingEntity in project Glowstone by GlowstoneMC.

the class LookAtPlayerTask method start.

@Override
public void start(GlowLivingEntity entity) {
    target = null;
    List<Entity> nearbyEntities = entity.getNearbyEntities(RANGE, RANGE / 2, RANGE);
    double nearestSquared = Double.MAX_VALUE;
    for (Entity nearbyEntity : nearbyEntities) {
        if (nearbyEntity.getType() != EntityType.PLAYER) {
            continue;
        }
        double dist = nearbyEntity.getLocation().distanceSquared(entity.getLocation());
        if (dist < nearestSquared) {
            target = (GlowPlayer) nearbyEntity;
            nearestSquared = dist;
        }
    }
}
Also used : Entity(org.bukkit.entity.Entity) GlowLivingEntity(net.glowstone.entity.GlowLivingEntity)

Example 2 with GlowLivingEntity

use of net.glowstone.entity.GlowLivingEntity in project Glowstone by GlowstoneMC.

the class FollowPlayerTask method start.

@Override
public void start(GlowLivingEntity entity) {
    target = null;
    List<Entity> nearbyEntities = entity.getNearbyEntities(RANGE, RANGE / 2, RANGE);
    double nearestSquared = Double.MAX_VALUE;
    for (Entity nearbyEntity : nearbyEntities) {
        if (nearbyEntity.getType() != EntityType.PLAYER) {
            continue;
        }
        double dist = nearbyEntity.getLocation().distanceSquared(entity.getLocation());
        if (dist < nearestSquared) {
            target = (GlowPlayer) nearbyEntity;
            nearestSquared = dist;
        }
    }
}
Also used : Entity(org.bukkit.entity.Entity) GlowLivingEntity(net.glowstone.entity.GlowLivingEntity)

Example 3 with GlowLivingEntity

use of net.glowstone.entity.GlowLivingEntity in project Glowstone by GlowstoneMC.

the class InteractEntityHandler method handle.

@Override
public void handle(GlowSession session, InteractEntityMessage message) {
    GlowPlayer player = session.getPlayer();
    // You can't do anything when you're dead
    if (player.isDead()) {
        GlowServer.logger.info("Player " + player.getName() + " tried to interact with an entity while dead");
        return;
    }
    GlowEntity possibleTarget = player.getWorld().getEntityManager().getEntity(message.getId());
    GlowLivingEntity target = possibleTarget instanceof GlowLivingEntity ? (GlowLivingEntity) possibleTarget : null;
    if (message.getAction() == Action.ATTACK.ordinal()) {
        if (target == null) {
            if (possibleTarget != null) {
                possibleTarget.entityInteract(player, message);
            } else {
                GlowServer.logger.info("Player " + player.getName() + " tried to attack an entity that does not exist");
            }
        } else if (!target.isDead() && target.canTakeDamage(DamageCause.ENTITY_ATTACK)) {
            // Calculate damage amount
            ItemStack hand = player.getItemInHand();
            Material type = hand == null ? Material.AIR : hand.getType();
            // todo: Actual critical hit check
            float damage = AttackDamage.getMeleeDamage(type, false);
            // Set entity on fire if the item has Fire Aspect
            if (hand.getEnchantments().containsKey(Enchantment.FIRE_ASPECT)) {
                target.setFireTicks(target.getFireTicks() + hand.getEnchantments().get(Enchantment.FIRE_ASPECT) * 80);
            }
            // Apply damage. Calls the EntityDamageByEntityEvent
            target.damage(damage, player, DamageCause.ENTITY_ATTACK);
            player.incrementStatistic(Statistic.DAMAGE_DEALT, Math.round(damage));
            player.addExhaustion(0.1f);
            if (target.isDead()) {
                player.incrementStatistic(target.getType() == EntityType.PLAYER ? Statistic.PLAYER_KILLS : Statistic.MOB_KILLS);
            }
            // Apply durability loss (if applicable)
            short durabilityLoss = AttackDamage.getMeleeDurabilityLoss(type);
            if (durabilityLoss > 0 && !InventoryUtil.isEmpty(hand) && player.getGameMode() != GameMode.CREATIVE) {
                // Yes, this actually subtracts
                hand.setDurability((short) (hand.getDurability() + durabilityLoss));
            }
        }
    } else if (message.getAction() == Action.INTERACT_AT.ordinal()) {
    //todo: Handle hand variable
    // todo: Interaction with entity at a specified location (X, Y, and Z are present in the message)
    // used for adjusting specific portions of armor stands
    } else if (message.getAction() == Action.INTERACT.ordinal()) {
        //Todo: Handle hand variable
        PlayerInteractEntityEvent event = new PlayerInteractEntityEvent(player, possibleTarget);
        EventFactory.callEvent(event);
        if (!event.isCancelled()) {
            possibleTarget.entityInteract(player, message);
        }
    } else {
        GlowServer.logger.info("Player " + player.getName() + " sent unknown interact action: " + message.getAction());
    }
}
Also used : GlowPlayer(net.glowstone.entity.GlowPlayer) GlowEntity(net.glowstone.entity.GlowEntity) GlowLivingEntity(net.glowstone.entity.GlowLivingEntity) Material(org.bukkit.Material) ItemStack(org.bukkit.inventory.ItemStack) PlayerInteractEntityEvent(org.bukkit.event.player.PlayerInteractEntityEvent)

Aggregations

GlowLivingEntity (net.glowstone.entity.GlowLivingEntity)3 Entity (org.bukkit.entity.Entity)2 GlowEntity (net.glowstone.entity.GlowEntity)1 GlowPlayer (net.glowstone.entity.GlowPlayer)1 Material (org.bukkit.Material)1 PlayerInteractEntityEvent (org.bukkit.event.player.PlayerInteractEntityEvent)1 ItemStack (org.bukkit.inventory.ItemStack)1