Java Code Examples for net.minecraft.world.World#getEntitiesInAABBexcluding()

The following examples show how to use net.minecraft.world.World#getEntitiesInAABBexcluding() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: YouTubeModdingTutorial   File: RayTraceTools.java    License: MIT License 5 votes vote down vote up
public static void rayTrace(Beam beam, Function<Entity, Boolean> consumer) {
    // Based on EntityRender.getMouseOver(float partialTicks) which we can't use because that's client only
    Vec3d start = beam.getStart();
    Vec3d lookVec = beam.getLookVec();
    Vec3d end = beam.getEnd();
    double dist = beam.getDist();
    World world = beam.getWorld();
    EntityPlayer player = beam.getPlayer();
    List<Entity> targets = world.getEntitiesInAABBexcluding(player, player.getEntityBoundingBox().expand(lookVec.x * dist, lookVec.y * dist, lookVec.z * dist).grow(1.0D, 1.0D, 1.0D),
            Predicates.and(EntitySelectors.NOT_SPECTATING, ent -> ent != null && ent.canBeCollidedWith()));
    List<Pair<Entity, Double>> hitTargets = new ArrayList<>();
    for (Entity target : targets) {
        AxisAlignedBB targetBB = target.getEntityBoundingBox().grow(target.getCollisionBorderSize());
        if (targetBB.contains(start)) {
            hitTargets.add(Pair.of(target, 0.0));
        } else {
            RayTraceResult targetResult = targetBB.calculateIntercept(start, end);
            if (targetResult != null) {
                double d3 = start.distanceTo(targetResult.hitVec);
                if (d3 < dist) {
                    hitTargets.add(Pair.of(target, d3));
                }
            }
        }
    }
    hitTargets.sort(Comparator.comparing(Pair::getRight));
    hitTargets.stream().filter(pair -> consumer.apply(pair.getLeft())).findFirst();
}
 
Example 2
public static List<Entity> getEntitiesWithinSubRegion(World world, BlockPos origin, BlockPos regionPos, BlockPos regionSize,
        SchematicPlacement schematicPlacement, SubRegionPlacement placement)
{
    // These are the untransformed relative positions
    BlockPos regionPosRelTransformed = PositionUtils.getTransformedBlockPos(regionPos, schematicPlacement.getMirror(), schematicPlacement.getRotation());
    BlockPos posEndAbs = PositionUtils.getTransformedPlacementPosition(regionSize.add(-1, -1, -1), schematicPlacement, placement).add(regionPosRelTransformed).add(origin);
    BlockPos regionPosAbs = regionPosRelTransformed.add(origin);
    AxisAlignedBB bb = PositionUtils.createEnclosingAABB(regionPosAbs, posEndAbs);

    return world.getEntitiesInAABBexcluding(null, bb, null);
}
 
Example 3
private static void takeEntitiesFromWorld(LitematicaSchematic schematic, World world, List<SelectionBox> boxes, BlockPos origin)
{
    for (SelectionBox box : boxes)
    {
        String regionName = box.getName();
        ISchematicRegion region = schematic.getSchematicRegion(regionName);
        List<EntityInfo> schematicEntityList = region != null ? region.getEntityList() : null;

        if (schematicEntityList == null)
        {
            InfoUtils.showGuiOrInGameMessage(MessageType.ERROR, "litematica.message.error.schematic_save.missing_entity_list", box.getName());
            continue;
        }

        AxisAlignedBB bb = PositionUtils.createEnclosingAABB(box.getPos1(), box.getPos2());
        BlockPos regionPosAbs = box.getPos1();
        List<EntityInfo> list = new ArrayList<>();
        List<Entity> entities = world.getEntitiesInAABBexcluding(null, bb, null);

        for (Entity entity : entities)
        {
            NBTTagCompound tag = new NBTTagCompound();

            if (entity.writeToNBTOptional(tag))
            {
                Vec3d posVec = new Vec3d(entity.posX - regionPosAbs.getX(), entity.posY - regionPosAbs.getY(), entity.posZ - regionPosAbs.getZ());
                NBTUtils.writeVec3dToListTag(posVec, tag);
                list.add(new EntityInfo(posVec, tag));
            }
        }

        schematicEntityList.addAll(list);
    }
}
 
Example 4
Source Project: AdvancedRocketry   File: ItemBasicLaserGun.java    License: MIT License 5 votes vote down vote up
public RayTraceResult rayTraceEntity(World world, Entity entity) {

		Vec3d vec3d = new Vec3d(entity.posX, entity.posY + entity.getEyeHeight(), entity.posZ);
		Vec3d vec3d1 = entity.getLook(0);
		Vec3d vec3d2 = vec3d.addVector(vec3d1.x * reachDistance, vec3d1.y * reachDistance, vec3d1.z * reachDistance);


		List<Entity> list = world.getEntitiesInAABBexcluding(entity, entity.getEntityBoundingBox().grow(vec3d1.x * reachDistance, vec3d1.y * reachDistance, vec3d1.z * reachDistance).expand(1.0D, 1.0D, 1.0D), Predicates.and(EntitySelectors.NOT_SPECTATING, new Predicate<Entity>()
				{
			public boolean apply(@Nullable Entity p_apply_1_)
			{
				return p_apply_1_ != null && p_apply_1_.canBeCollidedWith();
			}
				}));

		for (int j = 0; j < list.size(); ++j)
		{
			Entity entity1 = (Entity)list.get(j);
			AxisAlignedBB axisalignedbb = entity1.getEntityBoundingBox().grow((double)entity1.getCollisionBorderSize());
			RayTraceResult raytraceresult = axisalignedbb.calculateIntercept(vec3d, vec3d2);

			if (axisalignedbb.contains(vec3d))
			{
			}
			else if (raytraceresult != null)
			{
				raytraceresult.entityHit = entity1;
				return raytraceresult;
			}
		}

		return null;
	}
 
Example 5
public static void takeEntitiesFromWorldWithinChunk(LitematicaSchematic schematic, World world, int chunkX, int chunkZ,
        ImmutableMap<String, IntBoundingBox> volumes, ImmutableMap<String, SelectionBox> boxes, Set<UUID> existingEntities, BlockPos origin)
{
    for (Map.Entry<String, IntBoundingBox> entry : volumes.entrySet())
    {
        String regionName = entry.getKey();
        Box box = boxes.get(regionName);
        ISchematicRegion region = schematic.getSchematicRegion(regionName);
        List<EntityInfo> schematicEntityList = region != null ? region.getEntityList() : null;

        if (box == null || schematicEntityList == null)
        {
            InfoUtils.showGuiOrInGameMessage(MessageType.ERROR, "litematica.message.error.schematic_save.missing_entity_list", regionName);
            continue;
        }

        AxisAlignedBB bb = PositionUtils.createAABBFrom(entry.getValue());
        List<Entity> entities = world.getEntitiesInAABBexcluding(null, bb, null);
        BlockPos regionPosAbs = box.getPos1();

        for (Entity entity : entities)
        {
            UUID uuid = entity.getUniqueID();
            /*
            if (entity.posX >= bb.minX && entity.posX < bb.maxX &&
                entity.posY >= bb.minY && entity.posY < bb.maxY &&
                entity.posZ >= bb.minZ && entity.posZ < bb.maxZ)
            */
            if (existingEntities.contains(uuid) == false)
            {
                NBTTagCompound tag = new NBTTagCompound();

                if (entity.writeToNBTOptional(tag))
                {
                    Vec3d posVec = new Vec3d(entity.posX - regionPosAbs.getX(), entity.posY - regionPosAbs.getY(), entity.posZ - regionPosAbs.getZ());
                    NBTUtils.writeVec3dToListTag(posVec, tag);
                    schematicEntityList.add(new EntityInfo(posVec, tag));
                    existingEntities.add(uuid);
                }
            }
        }
    }
}