Search in sources :

Example 1 with BoundingBox

use of com.elmakers.mine.bukkit.utility.BoundingBox in project MagicPlugin by elBukkit.

the class MagicCommandExecutor method showEntityInfo.

private void showEntityInfo(CommandSender sender, Entity entity, String label, NumberFormat formatter) {
    BoundingBox hitbox = HitboxUtils.getHitbox(entity);
    Vector size = hitbox.size();
    String message = ChatColor.BLACK + label + ": " + ChatColor.AQUA + formatter.format(size.getX()) + ChatColor.DARK_GRAY + "x" + ChatColor.AQUA + formatter.format(size.getY()) + ChatColor.DARK_GRAY + "x" + ChatColor.AQUA + formatter.format(size.getZ());
    if (entity instanceof LivingEntity) {
        LivingEntity li = (LivingEntity) entity;
        message += ChatColor.DARK_GRAY + ", " + ChatColor.GREEN + ((int) li.getMaxHealth()) + "hp";
    }
    sender.sendMessage(message);
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) BoundingBox(com.elmakers.mine.bukkit.utility.BoundingBox) BlockVector(org.bukkit.util.BlockVector) Vector(org.bukkit.util.Vector)

Example 2 with BoundingBox

use of com.elmakers.mine.bukkit.utility.BoundingBox in project MagicPlugin by elBukkit.

the class BaseProjectileAction method step.

@Override
public SpellResult step(CastContext context) {
    if (tracking == null || tracking.size() == 0) {
        tracking = null;
        return SpellResult.CAST;
    }
    if (System.currentTimeMillis() > expiration) {
        for (Entity entity : tracking) {
            entity.removeMetadata("track", context.getPlugin());
            entity.removeMetadata("damaged", context.getPlugin());
            entity.removeMetadata("hit", context.getPlugin());
            entity.remove();
        }
        context.getMage().sendDebugMessage(ChatColor.DARK_GRAY + "Projectiles expired", 4);
        tracking = null;
        return SpellResult.NO_TARGET;
    }
    for (Entity entity : tracking) {
        if (!entity.isValid() || entity.hasMetadata("hit")) {
            tracking.remove(entity);
            Plugin plugin = context.getPlugin();
            Entity targetEntity = null;
            Block targetBlock = null;
            Location targetLocation = entity.getLocation();
            List<MetadataValue> metadata = entity.getMetadata("hit");
            for (MetadataValue value : metadata) {
                if (value.getOwningPlugin().equals(plugin)) {
                    Object o = value.value();
                    if (o != null && o instanceof WeakReference) {
                        WeakReference<?> reference = (WeakReference<?>) o;
                        o = reference.get();
                        if (o != null && o instanceof Entity) {
                            targetEntity = (Entity) o;
                            targetLocation = targetEntity.getLocation();
                        }
                        break;
                    } else if (o != null && o instanceof Block) {
                        targetBlock = (Block) o;
                        break;
                    }
                }
            }
            entity.removeMetadata("track", plugin);
            if (targetEntity == null) {
                context.getMage().sendDebugMessage(ChatColor.GRAY + "Projectile missed", 4);
            } else {
                context.getMage().sendDebugMessage(ChatColor.GREEN + "Projectile hit " + ChatColor.GOLD + targetEntity.getType());
            }
            entity.removeMetadata("hit", plugin);
            Location sourceLocation = entity.getLocation();
            // So.. projectile X direction is backwards. See: https://hub.spigotmc.org/jira/browse/SPIGOT-3867
            // The direction of the entity is adjusted to account for the orientation of the models.
            // I'm using the fix suggested by md_5, which is to use the velocity rather than orientation.
            // This feels cleaner than inverting the x-component of direction, just in case Mojang ever
            // fixes this issue.
            Vector direction = entity.getVelocity().normalize();
            sourceLocation.setDirection(direction);
            if (targetBlock != null) {
                // If we have a target block, try to approximate the hit location but ensure that it's on the edge of the block.
                // This makes this appear similar to how raycast targeting would work
                context.getMage().sendDebugMessage(ChatColor.GREEN + "Projectile at " + TextUtils.printLocation(entity.getLocation()) + ChatColor.GREEN + " hit block at " + TextUtils.printBlock(targetBlock) + " facing " + TextUtils.printVector(sourceLocation.getDirection()), 13);
                // Set previous location, this is mainly so the Teleport action works right.
                context.setPreviousBlock(sourceLocation.getBlock());
                targetLocation = targetBlock.getLocation();
                // raycast from entity through block
                Vector startPoint = sourceLocation.toVector();
                Vector endPoint = startPoint.clone().add(direction.clone().normalize().multiply(2));
                BoundingBox hitbox = new BoundingBox(targetLocation.toVector(), 0.001, 0.998, 0.001, 0.998, 0.001, 0.998);
                Vector hit = hitbox.getIntersection(startPoint, endPoint);
                if (hit != null) {
                    targetLocation.setX(hit.getX());
                    targetLocation.setY(hit.getY());
                    targetLocation.setZ(hit.getZ());
                }
            } else {
                context.getMage().sendDebugMessage(ChatColor.GRAY + "Projectile hit at " + TextUtils.printLocation(entity.getLocation()) + " facing " + TextUtils.printVector(sourceLocation.getDirection()), 132);
            }
            createActionContext(context, context.getMage().getEntity(), sourceLocation, targetEntity, targetLocation);
            actionContext.playEffects("hit");
            SpellResult result = startActions();
            if (targetEntity != null) {
                result = result.min(SpellResult.CAST);
            } else {
                result = result.min(SpellResult.NO_TARGET);
            }
            context.addResult(result);
            return result;
        }
    }
    return SpellResult.PENDING;
}
Also used : Entity(org.bukkit.entity.Entity) MetadataValue(org.bukkit.metadata.MetadataValue) WeakReference(java.lang.ref.WeakReference) BoundingBox(com.elmakers.mine.bukkit.utility.BoundingBox) Block(org.bukkit.block.Block) SpellResult(com.elmakers.mine.bukkit.api.spell.SpellResult) Vector(org.bukkit.util.Vector) Plugin(org.bukkit.plugin.Plugin) Location(org.bukkit.Location)

Aggregations

BoundingBox (com.elmakers.mine.bukkit.utility.BoundingBox)2 Vector (org.bukkit.util.Vector)2 SpellResult (com.elmakers.mine.bukkit.api.spell.SpellResult)1 WeakReference (java.lang.ref.WeakReference)1 Location (org.bukkit.Location)1 Block (org.bukkit.block.Block)1 Entity (org.bukkit.entity.Entity)1 LivingEntity (org.bukkit.entity.LivingEntity)1 MetadataValue (org.bukkit.metadata.MetadataValue)1 Plugin (org.bukkit.plugin.Plugin)1 BlockVector (org.bukkit.util.BlockVector)1