Search in sources :

Example 6 with GlowEntity

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

the class GlowWorld method getNearbyEntities.

/**
 * Returns a list of entities within a bounding box centered around a Location.
 *
 * <p>Some implementations may impose artificial restrictions on the size of the search bounding
 * box.
 *
 * @param location The center of the bounding box
 * @param x        1/2 the size of the box along x axis
 * @param y        1/2 the size of the box along y axis
 * @param z        1/2 the size of the box along z axis
 * @return the collection of entities near location. This will always be a non-null collection.
 */
@Override
public Collection<Entity> getNearbyEntities(Location location, double x, double y, double z) {
    Vector minCorner = new Vector(location.getX() - x, location.getY() - y, location.getZ() - z);
    Vector maxCorner = new Vector(location.getX() + x, location.getY() + y, location.getZ() + z);
    // TODO: test
    BoundingBox searchBox = BoundingBox.fromCorners(minCorner, maxCorner);
    GlowEntity except = null;
    return entityManager.getEntitiesInside(searchBox, except);
}
Also used : BoundingBox(net.glowstone.entity.physics.BoundingBox) GlowEntity(net.glowstone.entity.GlowEntity) Vector(org.bukkit.util.Vector)

Example 7 with GlowEntity

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

the class Explosion method damageEntities.

// ///////////////////////////////////////
// Damage entities
private void damageEntities() {
    float power = this.power;
    this.power *= 2;
    Collection<Entity> entities = getNearbyEntities();
    for (Entity entity : entities) {
        if (!(entity instanceof GlowEntity)) {
            continue;
        }
        // refine area to sphere, instead of box
        if (distanceToSquared(entity) > power * power) {
            continue;
        }
        double exposure = world.rayTrace(location, (GlowEntity) entity);
        double impact = (1 - (distanceTo(entity) / power / 2)) * exposure;
        double damage = (impact * impact + impact) * 8 * power + 1;
        int epf = getProtectionFactor(entity);
        double reduction = calculateEnchantedReduction(epf);
        damage = damage * reduction;
        exposure -= exposure * epf * 0.15;
        DamageCause damageCause;
        if (source == null || source.getType() == EntityType.PRIMED_TNT) {
            damageCause = DamageCause.BLOCK_EXPLOSION;
        } else {
            damageCause = DamageCause.ENTITY_EXPLOSION;
        }
        ((GlowEntity) entity).damage(damage, source, damageCause);
        if (entity instanceof GlowPlayer && ((GlowPlayer) entity).isFlying()) {
            continue;
        }
        if (entity instanceof LivingEntity) {
            LivingEntity livingEntity = (LivingEntity) entity;
            Vector rayLength = RayUtil.getVelocityRay(distanceToHead(livingEntity));
            rayLength.multiply(exposure);
            Vector currentVelocity = entity.getVelocity();
            currentVelocity.add(rayLength);
            entity.setVelocity(currentVelocity);
        }
    }
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) Entity(org.bukkit.entity.Entity) GlowEntity(net.glowstone.entity.GlowEntity) LivingEntity(org.bukkit.entity.LivingEntity) GlowPlayer(net.glowstone.entity.GlowPlayer) GlowEntity(net.glowstone.entity.GlowEntity) DamageCause(org.bukkit.event.entity.EntityDamageEvent.DamageCause) BlockVector(org.bukkit.util.BlockVector) Vector(org.bukkit.util.Vector)

Example 8 with GlowEntity

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

the class TestForCommand method execute.

@Override
public boolean execute(CommandSender sender, String label, String[] args, CommandMessages commandMessages) {
    if (!testPermission(sender, commandMessages.getPermissionMessage())) {
        return true;
    }
    if (args.length < 1) {
        sendUsageMessage(sender, commandMessages);
        return false;
    }
    String name = args[0];
    Entity[] entities;
    if (name.startsWith("@")) {
        CommandTarget target = new CommandTarget(sender, name);
        entities = target.getMatched(CommandUtils.getLocation(sender));
        if (entities.length == 0) {
            commandMessages.getGeneric(GenericMessage.NO_MATCHES).sendInColor(ChatColor.RED, sender, name);
            return false;
        }
    } else {
        // TODO: Select custom-named non-player entities?
        GlowPlayer player = (GlowPlayer) Bukkit.getPlayerExact(args[0]);
        if (player == null) {
            commandMessages.getGeneric(GenericMessage.NO_SUCH_PLAYER).sendInColor(ChatColor.RED, sender, name);
            return false;
        } else {
            entities = new Entity[] { player };
        }
    }
    LocalizedStringImpl foundMessage = new LocalizedStringImpl("testfor.found", commandMessages.getResourceBundle());
    if (args.length >= 2) {
        String data = String.join(" ", Arrays.copyOfRange(args, 1, args.length));
        CompoundTag tag;
        try {
            tag = Mojangson.parseCompound(data);
        } catch (MojangsonParseException e) {
            commandMessages.getGeneric(GenericMessage.INVALID_JSON).sendInColor(ChatColor.RED, sender, e.getMessage());
            return false;
        }
        LocalizedStringImpl wrongDataMessage = new LocalizedStringImpl("testfor.wrong-data", commandMessages.getResourceBundle());
        for (Entity entity : entities) {
            if (entity instanceof GlowEntity) {
                CompoundTag entityTag = new CompoundTag();
                EntityStorage.save((GlowEntity) entity, entityTag);
                if (tag.matches(entityTag)) {
                    foundMessage.send(sender, CommandUtils.getName(entity));
                } else {
                    wrongDataMessage.sendInColor(ChatColor.RED, sender, CommandUtils.getName(entity));
                }
            }
        }
    } else {
        for (Entity entity : entities) {
            foundMessage.send(sender, CommandUtils.getName(entity));
        }
    }
    // matching entities.
    return true;
}
Also used : Entity(org.bukkit.entity.Entity) GlowEntity(net.glowstone.entity.GlowEntity) CommandTarget(net.glowstone.command.CommandTarget) GlowPlayer(net.glowstone.entity.GlowPlayer) LocalizedStringImpl(net.glowstone.i18n.LocalizedStringImpl) MojangsonParseException(net.glowstone.util.mojangson.ex.MojangsonParseException) GlowEntity(net.glowstone.entity.GlowEntity) CompoundTag(net.glowstone.util.nbt.CompoundTag)

Example 9 with GlowEntity

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

the class SummonCommand method execute.

@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
    if (!testPermission(sender))
        return true;
    if (sender instanceof ConsoleCommandSender) {
        sender.sendMessage("This command can only be executed by a player or via command blocks.");
        return true;
    }
    Location location = null;
    if (sender instanceof Player) {
        location = ((Player) sender).getLocation().clone();
    } else if (sender instanceof BlockCommandSender) {
        location = ((BlockCommandSender) sender).getBlock().getLocation().clone();
    }
    if (args.length == 0) {
        sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage);
        return false;
    }
    if (args.length >= 4) {
        location = CommandUtils.getLocation(location, args[1], args[2], args[3]);
    }
    if (location == null) {
        return false;
    }
    location.setYaw(0.0f);
    location.setPitch(0.0f);
    CompoundTag tag = null;
    if (args.length >= 5) {
        String data = String.join(" ", new ArrayList<>(Arrays.asList(args)).subList(4, args.length));
        try {
            tag = Mojangson.parseCompound(data);
        } catch (MojangsonParseException e) {
            sender.sendMessage(ChatColor.RED + "Invalid Data Tag: " + e.getMessage());
        }
    }
    String entityName = args[0];
    if (!checkSummon(sender, entityName)) {
        return true;
    }
    GlowEntity entity;
    if (EntityType.fromName(entityName) != null) {
        entity = (GlowEntity) location.getWorld().spawnEntity(location, EntityType.fromName(entityName));
    } else {
        Class<? extends GlowEntity> clazz = EntityRegistry.getCustomEntityDescriptor(entityName).getEntityClass();
        entity = ((GlowWorld) location.getWorld()).spawn(location, clazz, CreatureSpawnEvent.SpawnReason.CUSTOM);
    }
    if (tag != null) {
        EntityStorage.load(entity, tag);
    }
    sender.sendMessage("Object successfully summoned.");
    return true;
}
Also used : Player(org.bukkit.entity.Player) ArrayList(java.util.ArrayList) MojangsonParseException(net.glowstone.util.mojangson.ex.MojangsonParseException) GlowEntity(net.glowstone.entity.GlowEntity) ConsoleCommandSender(org.bukkit.command.ConsoleCommandSender) CompoundTag(net.glowstone.util.nbt.CompoundTag) Location(org.bukkit.Location) BlockCommandSender(org.bukkit.command.BlockCommandSender)

Example 10 with GlowEntity

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

the class GlowChunk method unload.

@Deprecated
public boolean unload(boolean save, boolean safe) {
    safe = false;
    if (!isLoaded()) {
        return true;
    }
    if (safe && world.isChunkInUse(x, z)) {
        return false;
    }
    if (save && !world.getChunkManager().performSave(this)) {
        return false;
    }
    EventFactory.getInstance().callEvent(new ChunkUnloadEvent(this));
    sections = null;
    biomes = null;
    heightMap = null;
    blockEntities.clear();
    if (save) {
        for (GlowEntity entity : entities) {
            entity.remove();
        }
        entities.clear();
    }
    return true;
}
Also used : ChunkUnloadEvent(org.bukkit.event.world.ChunkUnloadEvent) GlowEntity(net.glowstone.entity.GlowEntity)

Aggregations

GlowEntity (net.glowstone.entity.GlowEntity)19 Location (org.bukkit.Location)9 GlowPlayer (net.glowstone.entity.GlowPlayer)8 CompoundTag (net.glowstone.util.nbt.CompoundTag)8 Vector (org.bukkit.util.Vector)7 ArrayList (java.util.ArrayList)6 Entity (org.bukkit.entity.Entity)4 EntityType (org.bukkit.entity.EntityType)4 LivingEntity (org.bukkit.entity.LivingEntity)3 Message (com.flowpowered.network.Message)2 IOException (java.io.IOException)2 Collections (java.util.Collections)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)2 EventFactory (net.glowstone.EventFactory)2 BoundingBox (net.glowstone.entity.physics.BoundingBox)2 MojangsonParseException (net.glowstone.util.mojangson.ex.MojangsonParseException)2 ConsoleCommandSender (org.bukkit.command.ConsoleCommandSender)2 ItemStack (org.bukkit.inventory.ItemStack)2