Search in sources :

Example 86 with EntityType

use of org.bukkit.entity.EntityType in project Glowstone by GlowstoneMC.

the class CommandTarget method getMatched.

/**
 * Gets all the matched entities from the target.
 *
 * @param source the location from which the targets should be found
 * @return the entities matching the query
 */
public Entity[] getMatched(Location source) {
    if (selector == SelectorType.SENDER) {
        if (sender instanceof Entity) {
            return new Entity[] { (Entity) sender };
        } else {
            return new Entity[0];
        }
    }
    List<EntityType> types = getTypes();
    List<GameMode> gameModes = getGameModes();
    Integer count = getCount();
    int maxRadius = getMaxRange() * getMaxRange();
    int minRadius = getMinRange() * getMinRange();
    Integer minLevel = getMinLevel();
    Integer maxLevel = getMaxLevel();
    List<Entity> entities = new ArrayList<>();
    if (count == null) {
        if (selector == SelectorType.NEAREST_PLAYER || selector == SelectorType.RANDOM) {
            count = 1;
        } else {
            count = source.getWorld().getEntities().size();
        }
    }
    for (Entity entity : source.getWorld().getEntities()) {
        if (entity.getLocation().distanceSquared(source) < minRadius) {
            continue;
        }
        if (!(maxRadius == 0 || entity.getLocation().distanceSquared(source) < maxRadius)) {
            continue;
        }
        if (!types.contains(entity.getType())) {
            continue;
        }
        if (getX() != null && getX() != entity.getLocation().getBlockX()) {
            continue;
        }
        if (getY() != null && getY() != entity.getLocation().getBlockY()) {
            continue;
        }
        if (getZ() != null && getZ() != entity.getLocation().getBlockZ()) {
            continue;
        }
        if (gameModes != null && entity.getType() != EntityType.PLAYER) {
            continue;
        }
        if (gameModes != null && gameModes.contains(((Player) entity).getGameMode())) {
            continue;
        }
        if (maxLevel != null && entity.getType() != EntityType.PLAYER) {
            continue;
        }
        if (maxLevel != null && ((Player) entity).getLevel() > maxLevel) {
            continue;
        }
        if (minLevel != null && entity.getType() != EntityType.PLAYER) {
            continue;
        }
        if (minLevel != null && ((Player) entity).getLevel() < minLevel) {
            continue;
        }
        // TODO: Add more checks
        entities.add(entity);
    }
    Collections.sort(entities, new EntityDistanceComparator(count < 0, source));
    if (count > entities.size()) {
        count = entities.size();
    }
    List<Entity> matched = new ArrayList<>();
    List<Integer> used = new ArrayList<>();
    for (int i = 0; i < count; i++) {
        if (selector == SelectorType.RANDOM) {
            while (true) {
                int random = ThreadLocalRandom.current().nextInt(entities.size());
                if (!used.contains(random)) {
                    matched.add(entities.get(random));
                    used.add(random);
                    break;
                }
            }
        } else {
            matched.add(entities.get(i));
        }
    }
    return matched.toArray(new Entity[matched.size()]);
}
Also used : EntityType(org.bukkit.entity.EntityType) Entity(org.bukkit.entity.Entity) GameMode(org.bukkit.GameMode) Player(org.bukkit.entity.Player) ArrayList(java.util.ArrayList)

Example 87 with EntityType

use of org.bukkit.entity.EntityType in project Glowstone by GlowstoneMC.

the class ItemSpawn method rightClickBlock.

@Override
public void rightClickBlock(GlowPlayer player, GlowBlock against, BlockFace face, ItemStack holding, Vector clickedLoc, EquipmentSlot hand) {
    Location location = against.getLocation().add(face.getModX(), face.getModY(), face.getModZ());
    // TODO: change mob spawner when clicked by monster egg
    if (holding.hasItemMeta() && holding.getItemMeta() instanceof GlowMetaSpawn) {
        GlowMetaSpawn meta = (GlowMetaSpawn) holding.getItemMeta();
        EntityType type = meta.getSpawnedType();
        CompoundTag tag = meta.getEntityTag();
        if (type != null) {
            GlowEntity entity = against.getWorld().spawn(location.add(0.5, 0, 0.5), EntityRegistry.getEntity(type), SpawnReason.SPAWNER_EGG);
            if (tag != null) {
                EntityStorage.load(entity, tag);
            }
            if (player.getGameMode() != GameMode.CREATIVE) {
                holding.setAmount(holding.getAmount() - 1);
            }
        }
    }
}
Also used : EntityType(org.bukkit.entity.EntityType) GlowEntity(net.glowstone.entity.GlowEntity) GlowMetaSpawn(net.glowstone.inventory.GlowMetaSpawn) CompoundTag(net.glowstone.util.nbt.CompoundTag) Location(org.bukkit.Location)

Example 88 with EntityType

use of org.bukkit.entity.EntityType in project Glowstone by GlowstoneMC.

the class SummonCommand method tabComplete.

@Override
public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
    // NON-NLS
    Preconditions.checkNotNull(sender, "Sender cannot be null");
    // NON-NLS
    Preconditions.checkNotNull(args, "Arguments cannot be null");
    // NON-NLS
    Preconditions.checkNotNull(alias, "Alias cannot be null");
    if (args.length == 1) {
        String arg = args[0];
        ArrayList<String> completion = new ArrayList<>();
        for (EntityType type : EntityType.values()) {
            if (checkSummon(null, type.getName(), null) && type.getName().toLowerCase().startsWith(arg)) {
                completion.add(type.getName());
            }
        }
        EntityRegistry.getRegisteredCustomEntities().forEach((d) -> {
            if (d.getId().toLowerCase().startsWith(arg)) {
                completion.add(d.getId().toLowerCase());
            }
        });
        return completion;
    } else {
        return Collections.emptyList();
    }
}
Also used : EntityType(org.bukkit.entity.EntityType) ArrayList(java.util.ArrayList)

Example 89 with EntityType

use of org.bukkit.entity.EntityType in project Glowstone by GlowstoneMC.

the class EntityDecorator method populate.

@Override
public void populate(World world, Random random, Chunk chunk) {
    GlowServer server = (GlowServer) Bukkit.getServer();
    boolean allowAnimals = world.getAllowAnimals() && server.getAnimalsSpawnEnabled();
    boolean allowMonsters = world.getAllowMonsters() && server.getMonstersSpawnEnabled();
    if (entityTypes.length == 0) {
        return;
    }
    if (random.nextFloat() >= rarity) {
        return;
    }
    int sourceX = chunk.getX() << 4;
    int sourceZ = chunk.getZ() << 4;
    EntityType type = entityTypes[random.nextInt(entityTypes.length)];
    if ((!allowAnimals && Animals.class.isAssignableFrom(type.getEntityClass())) || !allowMonsters && Monster.class.isAssignableFrom(type.getEntityClass())) {
        return;
    }
    int centerX = sourceX + random.nextInt(16);
    int centerZ = sourceZ + random.nextInt(16);
    int count = minGroup == maxGroup ? minGroup : random.nextInt(maxGroup - minGroup) + minGroup;
    int range = 5;
    int attempts = 5;
    for (int i = 0; i < count; i++) {
        if (attempts == 0) {
            continue;
        }
        double radius = (double) range * random.nextDouble();
        double angle = random.nextDouble() * Math.PI;
        double x = radius * Math.sin(angle) + centerX;
        double z = radius * Math.cos(angle) + centerZ;
        Block block = world.getHighestBlockAt(new Location(world, x, 0, z));
        if (block.getType() == Material.WATER || block.getType() == Material.LAVA) {
            i--;
            attempts--;
            continue;
        }
        attempts = 5;
        Location location = block.getLocation().clone().add(0, 1, 0);
        location.setYaw(random.nextFloat() * 360 - 180);
        if (location.getBlock().getRelative(BlockFace.DOWN).getType() == Material.AIR) {
            location.subtract(0, 1, 0);
        }
        world.spawnEntity(location, type);
    }
}
Also used : EntityType(org.bukkit.entity.EntityType) Animals(org.bukkit.entity.Animals) Block(org.bukkit.block.Block) GlowServer(net.glowstone.GlowServer) Location(org.bukkit.Location)

Example 90 with EntityType

use of org.bukkit.entity.EntityType in project Glowstone by GlowstoneMC.

the class GlowEgg method randomHatchSpawning.

/**
 * Handle spawning entities when the egg breaks.
 *
 * @param location The location the egg breaks
 */
private void randomHatchSpawning(Location location) {
    ThreadLocalRandom random = ThreadLocalRandom.current();
    // There is a 1/8 chance for egg to hatch and spawn at least 1 entity
    boolean hatching = random.nextInt(8) == 0;
    // ...and if the egg is hatching, there is now
    // 1/32 chance to spawn 3 more entities.
    byte amount;
    if (hatching) {
        amount = (byte) ((random.nextInt(32) == 0) ? 4 : 1);
    } else {
        amount = 0;
    }
    EntityType hatchingType = EntityType.CHICKEN;
    final ProjectileSource shooter = getShooter();
    if (shooter instanceof GlowPlayer) {
        PlayerEggThrowEvent event = EventFactory.getInstance().callEvent(new PlayerEggThrowEvent((GlowPlayer) shooter, this, hatching, amount, hatchingType));
        amount = event.getNumHatches();
        hatching = event.isHatching();
        hatchingType = event.getHatchingType();
    }
    if (hatching) {
        for (int i = 0; i < amount; i++) {
            GlowEntity entity = (GlowEntity) location.getWorld().spawnEntity(location.clone().add(0, 0.3, 0), hatchingType);
            if (entity instanceof GlowAgeable) {
                ((GlowAgeable) entity).setBaby();
            }
        }
    }
}
Also used : EntityType(org.bukkit.entity.EntityType) GlowPlayer(net.glowstone.entity.GlowPlayer) PlayerEggThrowEvent(org.bukkit.event.player.PlayerEggThrowEvent) GlowEntity(net.glowstone.entity.GlowEntity) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) GlowAgeable(net.glowstone.entity.GlowAgeable) ProjectileSource(org.bukkit.projectiles.ProjectileSource)

Aggregations

EntityType (org.bukkit.entity.EntityType)109 ArrayList (java.util.ArrayList)29 ItemStack (org.bukkit.inventory.ItemStack)28 Material (org.bukkit.Material)23 Player (org.bukkit.entity.Player)23 Entity (org.bukkit.entity.Entity)19 Location (org.bukkit.Location)17 IOException (java.io.IOException)16 File (java.io.File)13 LivingEntity (org.bukkit.entity.LivingEntity)13 EventHandler (org.bukkit.event.EventHandler)12 PotionType (org.bukkit.potion.PotionType)12 HashMap (java.util.HashMap)10 ConfigurationSection (org.bukkit.configuration.ConfigurationSection)9 Block (org.bukkit.block.Block)8 YamlConfiguration (org.bukkit.configuration.file.YamlConfiguration)8 World (org.bukkit.World)7 SpawnEgg (org.bukkit.material.SpawnEgg)7 UUID (java.util.UUID)5 Biome (org.bukkit.block.Biome)5