下面列出了怎么用net.minecraft.util.math.AxisAlignedBB的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public boolean shouldExecute() {
if (targetChance > 0 && taskOwner.getRNG().nextInt(targetChance) != 0) {
return false;
} else {
double horizDist = getTargetDistance();
double vertDist = getVerticalDistance();
AxisAlignedBB bb = taskOwner.getEntityBoundingBox().expand(horizDist, vertDist, horizDist);
List<T> list = taskOwner.getEntityWorld().<T> getEntitiesWithinAABB(targetClass, bb,
Predicates.<T> and(targetEntitySelector, EntitySelectors.NOT_SPECTATING));
Collections.sort(list, sorter);
if (list.isEmpty()) {
return false;
} else {
this.targetEntity = list.get(0);
return true;
}
}
}
/**
* Checks if a block is full sized based off of block bounds. This
* is not a perfect check as mods may have a full size. However,
* have a 3D model that doesn't look a full block in size. There
* is no way around this other than to make a black list check.
*
* @param block - block to compare
* @return true if full block
*/
public static boolean isFullBlock(IBlockAccess world, BlockPos pos, IBlockState state)
{
AxisAlignedBB bb = state.getCollisionBoundingBox((World) world, pos);
if(bb == null)
return false;
int minX = (int) (bb.minX * 100);
int minY = (int) (bb.minY * 100);
int minZ = (int) (bb.minZ * 100);
int maxX = (int) (bb.maxX * 100);
int maxY = (int) (bb.maxY * 100);
int maxZ = (int) (bb.maxZ * 100);
return minX == 0 && minY == 0 && minZ == 0 && maxX == 100 && maxY == 100 && maxZ == 100;
}
private static boolean traceToSelectionBoxCorner(SelectionBox box, Corner corner, Vec3d start, Vec3d end)
{
BlockPos pos = (corner == Corner.CORNER_1) ? box.getPos1() : (corner == Corner.CORNER_2) ? box.getPos2() : null;
if (pos != null)
{
AxisAlignedBB bb = PositionUtils.createAABBForPosition(pos);
RayTraceResult hit = bb.calculateIntercept(start, end);
if (hit != null)
{
double dist = hit.hitVec.distanceTo(start);
if (closestCornerDistance < 0 || dist < closestCornerDistance)
{
closestCornerDistance = dist;
closestCorner = new RayTraceWrapper(box, corner, hit.hitVec);
}
return true;
}
}
return false;
}
@Nullable
public BlockPos getBottomBlock(@Nonnull Entity entity) {
boolean isSpectator = (entity instanceof EntityPlayer && ((EntityPlayer) entity).isSpectator());
if (isSpectator) return null;
AxisAlignedBB bb = entity.getEntityBoundingBox();
int mX = MathHelper.floor(bb.minX);
int mY = MathHelper.floor(bb.minY);
int mZ = MathHelper.floor(bb.minZ);
for (int y2 = mY; y2 < bb.maxY; y2++) {
for (int x2 = mX; x2 < bb.maxX; x2++) {
for (int z2 = mZ; z2 < bb.maxZ; z2++) {
BlockPos tmp = new BlockPos(x2, y2, z2);
if (!entity.world.isAirBlock(tmp)) return tmp;
}
}
}
return null;
}
/**
* Keeps the Ship in the world border
*/
private void forceShipIntoWorldBorder() {
WorldBorder border = parent.world().getWorldBorder();
AxisAlignedBB shipBB = parent.getShipBoundingBox();
if (shipBB.maxX > border.maxX()) {
parent.getWrapperEntity().posX += border.maxX() - shipBB.maxX;
}
if (shipBB.minX < border.minX()) {
parent.getWrapperEntity().posX += border.minX() - shipBB.minX;
}
if (shipBB.maxZ > border.maxZ()) {
parent.getWrapperEntity().posZ += border.maxZ() - shipBB.maxZ;
}
if (shipBB.minZ < border.minZ()) {
parent.getWrapperEntity().posZ += border.minZ() - shipBB.minZ;
}
}
private void renderPositions(EntityPlayer clientPlayer, BlockPosEU posStart, BlockPosEU posEnd, int color, float partialTicks)
{
GlStateManager.glLineWidth(2.0f);
for (int a = 0; a < 3; a++)
{
List<BlockPosEU> column = this.positions.get(a);
if (column != null)
{
final int size = column.size();
for (int i = 0; i < size; i++)
{
BlockPosEU pos = column.get(i);
//if (pos.equals(posStart) == false && (posEnd == null || posEnd.equals(pos) == false))
{
AxisAlignedBB aabb = BuildersWandRenderer.createAABB(pos.getX(), pos.getY(), pos.getZ(), 0, partialTicks, clientPlayer);
RenderGlobal.drawSelectionBoundingBox(aabb, ((color >>> 16) & 0xFF) / 255f, ((color >>> 8) & 0xFF) / 255f, (color & 0xFF) / 255f, 1.0f);
}
}
}
}
}
@SubscribeEvent
public void harvestDrops(HarvestDropsEvent event) {
if (!ToroQuestConfiguration.cropsAffectRep) {
return;
}
if (isCrop(event.getState().getBlock())) {
BlockPos pos = event.getPos();
AxisAlignedBB bb = new AxisAlignedBB(pos);
List<EntityPlayer> players = event.getWorld().getEntitiesWithinAABB(EntityPlayer.class, bb);
if (players != null && players.size() > 0) {
for (EntityPlayer player : players) {
adjustPlayerRep(player, pos.getX() / 16, pos.getZ() / 16, -getFarmRepAmount(event.getWorld().rand));
}
}
}
}
@Override
public boolean onItemActive(ItemStack stack, World worldIn, Entity entityIn, int slot, boolean selected) {
if (worldIn.getTotalWorldTime() % 100 == 0) {
entityIn.playSound(GTSounds.SONAR, 1.0F, 1.0F);
AxisAlignedBB area = new AxisAlignedBB(entityIn.getPosition()).grow(16);
List<Entity> list = entityIn.world.getEntitiesInAABBexcluding(entityIn, area, null);
if (!list.isEmpty()) {
for (Entity thing : list) {
if (thing instanceof EntityLiving) {
((EntityLivingBase) thing).addPotionEffect(new PotionEffect(MobEffects.GLOWING, 80, 0, false, false));
}
if (thing instanceof EntityPlayer) {
((EntityPlayer) thing).addPotionEffect(new PotionEffect(MobEffects.GLOWING, 80, 0, false, false));
}
}
}
return true;
}
return false;
}
/**
* Pushes entities away from target within an area, code adapted from
* EE/ProjectE.
*/
public static void repelEntitiesInAABBFromPoint(World world, AxisAlignedBB boundingbox, double x, double y,
double z) {
List<Entity> list = world.getEntitiesWithinAABB(Entity.class, boundingbox);
if (list.isEmpty()) {
return;
}
for (Entity entity : list) {
if ((entity instanceof EntityLiving) || (entity instanceof IProjectile)) {
if (entity instanceof EntityArrow && ((EntityArrow) entity).onGround) {
continue;
}
if (entity instanceof EntityArmorStand) {
continue;
}
Vec3d p = new Vec3d(x, y, z);
Vec3d t = new Vec3d(entity.posX, entity.posY, entity.posZ);
double distance = p.distanceTo(t) + 0.1D;
Vec3d r = new Vec3d(t.x - p.x, t.y - p.y, t.z - p.z);
entity.motionX += r.x / 1.5D / distance;
entity.motionY += r.y / 1.5D / distance;
entity.motionZ += r.z / 1.5D / distance;
}
}
}
@Override
public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos)
{
switch (state.getValue(FACING)) {
case EAST:
return AABB_E;
case NORTH:
return AABB_N;
case SOUTH:
return AABB_S;
case WEST:
return AABB_W;
default:
break;
}
return AABB_N;
}
private boolean handleLikelyCollision(BlockPos inWorldPos, BlockPos inLocalPos,
IBlockState inWorldState,
IBlockState inLocalState) {
// System.out.println("Handling a likely collision");
AxisAlignedBB inLocalBB = new AxisAlignedBB(inLocalPos.getX(), inLocalPos.getY(),
inLocalPos.getZ(),
inLocalPos.getX() + 1, inLocalPos.getY() + 1, inLocalPos.getZ() + 1);
AxisAlignedBB inGlobalBB = new AxisAlignedBB(inWorldPos.getX(), inWorldPos.getY(),
inWorldPos.getZ(),
inWorldPos.getX() + 1, inWorldPos.getY() + 1, inWorldPos.getZ() + 1);
// This changes the box bounding box to the real bounding box, not sure if this
// is better or worse for this mod
// List<AxisAlignedBB> colBB = worldObj.getCollisionBoxes(inLocalBB);
// inLocalBB = colBB.get(0);
Polygon shipInWorld = new Polygon(inLocalBB,
parent.getShipTransformationManager().getCurrentPhysicsTransform(),
TransformType.SUBSPACE_TO_GLOBAL);
Polygon worldPoly = new Polygon(inGlobalBB);
PhysPolygonCollider collider = new PhysPolygonCollider(shipInWorld, worldPoly,
parent.getShipTransformationManager().normals);
if (!collider.seperated) {
return handleActualCollision(collider, inWorldPos, inLocalPos, inWorldState,
inLocalState);
}
return false;
}
/**
* Returns the index of the BB in the given list that the given vectors are currently pointing at.
* @return the list index of the pointed box, or null of no hit was detected
*/
@Nullable
public static <T> T getPointedBox(Vec3d eyesVec, Vec3d lookVec, double reach, Map<T, AxisAlignedBB> boxMap)
{
Vec3d lookEndVec = eyesVec.add(lookVec.x * reach, lookVec.y * reach, lookVec.z * reach);
double distance = reach;
T key = null;
for (Map.Entry<T, AxisAlignedBB> entry : boxMap.entrySet())
{
AxisAlignedBB bb = entry.getValue();
RayTraceResult rayTrace = bb.calculateIntercept(eyesVec, lookEndVec);
if (bb.contains(eyesVec))
{
if (distance >= 0.0D)
{
distance = 0.0D;
key = entry.getKey();
}
}
else if (rayTrace != null)
{
double distanceTmp = eyesVec.distanceTo(rayTrace.hitVec);
if (distanceTmp < distance)
{
distance = distanceTmp;
key = entry.getKey();
}
}
}
return key;
}
public static void drawOutlinedBox(AxisAlignedBB aabb)
{
Tessellator tessellator = Tessellator.getInstance();
VertexBuffer buffer = tessellator.getBuffer();
buffer.begin(GL11.GL_LINES, DefaultVertexFormats.POSITION);
buffer.pos(aabb.minX, aabb.minY, aabb.minZ).endVertex();
buffer.pos(aabb.minX, aabb.maxY, aabb.minZ).endVertex();
buffer.pos(aabb.maxX, aabb.minY, aabb.minZ).endVertex();
buffer.pos(aabb.maxX, aabb.maxY, aabb.minZ).endVertex();
buffer.pos(aabb.maxX, aabb.minY, aabb.maxZ).endVertex();
buffer.pos(aabb.maxX, aabb.maxY, aabb.maxZ).endVertex();
buffer.pos(aabb.minX, aabb.minY, aabb.maxZ).endVertex();
buffer.pos(aabb.minX, aabb.maxY, aabb.maxZ).endVertex();
buffer.pos(aabb.minX, aabb.minY, aabb.minZ).endVertex();
buffer.pos(aabb.maxX, aabb.minY, aabb.minZ).endVertex();
buffer.pos(aabb.minX, aabb.minY, aabb.minZ).endVertex();
buffer.pos(aabb.minX, aabb.minY, aabb.maxZ).endVertex();
buffer.pos(aabb.minX, aabb.minY, aabb.maxZ).endVertex();
buffer.pos(aabb.maxX, aabb.minY, aabb.maxZ).endVertex();
buffer.pos(aabb.maxX, aabb.minY, aabb.maxZ).endVertex();
buffer.pos(aabb.maxX, aabb.minY, aabb.minZ).endVertex();
buffer.pos(aabb.minX, aabb.maxY, aabb.minZ).endVertex();
buffer.pos(aabb.maxX, aabb.maxY, aabb.minZ).endVertex();
buffer.pos(aabb.maxX, aabb.maxY, aabb.minZ).endVertex();
buffer.pos(aabb.maxX, aabb.maxY, aabb.maxZ).endVertex();
buffer.pos(aabb.minX, aabb.maxY, aabb.minZ).endVertex();
buffer.pos(aabb.minX, aabb.maxY, aabb.maxZ).endVertex();
buffer.pos(aabb.maxX, aabb.maxY, aabb.maxZ).endVertex();
buffer.pos(aabb.minX, aabb.maxY, aabb.maxZ).endVertex();
tessellator.draw();
}
public EntityLivingBase getLivingEntityNearbyByUUID(UUID uuid, double bbRadius)
{
double r = bbRadius;
AxisAlignedBB bb = new AxisAlignedBB(this.posX - r, this.posY - r, this.posZ - r, this.posX + r, this.posY + r, this.posZ + r);
List<EntityLivingBase> list = this.getEntityWorld().getEntitiesWithinAABB(EntityLivingBase.class, bb);
return EntityUtils.findEntityByUUID(list, uuid);
}
@Test
public void test_fromTo_with_offset()
{
AxisAlignedBB bb = gson.fromJson("{ \"from\": [1,2,3], \"to\": [4,5.5,6], \"offset\": [1,2,3] }", AxisAlignedBB.class);
assertEquals(2, bb.minX, 0.001);
assertEquals(4, bb.minY, 0.001);
assertEquals(6, bb.minZ, 0.001);
assertEquals(5, bb.maxX, 0.001);
assertEquals(7.5, bb.maxY, 0.001);
assertEquals(9, bb.maxZ, 0.001);
}
public void init() {
int layers = this.block.getLayers(this.meta);
this.quads = this.createList(7);
this.setParticalTexture(this.block.getParticleTexture(this.meta));
boolean color = this.block instanceof IGTColorBlock;
EnumFacing blockFacing = EnumFacing.NORTH;
ModelRotation rotation = ModelRotation.X0_Y0;
for (int i = 0; i < layers; ++i) {
AxisAlignedBB box = this.block.getRenderBox(this.meta, i);
EnumFacing[] facings;
int facingLength;
int j;
EnumFacing side;
ModelRotation sideRotation;
BlockPartFace face;
TextureAtlasSprite sprite;
facings = EnumFacing.VALUES;
facingLength = facings.length;
for (j = 0; j < facingLength; ++j) {
side = facings[j];
sideRotation = this.getRotation(blockFacing, side, rotation);
face = this.createBlockFace(side, i, color);
sprite = this.block.getLayerTexture(this.meta, side, i);
if (sprite != null) {
this.quads[side.getIndex()].add(this.getBakery().makeBakedQuad(this.getMinBox(side, box), this.getMaxBox(side, box), face, sprite, side, sideRotation, (BlockPartRotation) null, false, true));
}
}
}
}
@Test
public void test_default()
{
Map<String, AxisAlignedBB> map = gson.fromJson("{ \"default\": \"default\" }", new TypeToken<Map<String, AxisAlignedBB>>() {}.getType());
assertTrue(map.containsKey("default"));
assertSame(BlockMixin.DEFAULT_AABB_MARKER, map.get("default"));
}
public static ArrayList<BlockPos> getBlockPosInAABB(AxisAlignedBB aabb)
{
ArrayList<BlockPos> out = new ArrayList<BlockPos>();
for(int x = (int) aabb.minX; x < aabb.maxX; x++)
{
for(int y = (int) aabb.minY; y < aabb.maxY; y++)
{
for(int z = (int) aabb.minZ; z < aabb.maxZ; z++)
{
out.add(new BlockPos(x, y, z));
}
}
}
return out;
}
public static void renderAreaOutline(BlockPos pos1, BlockPos pos2, float lineWidth,
Color4f colorX, Color4f colorY, Color4f colorZ, Entity renderViewEntity, float partialTicks)
{
GlStateManager.glLineWidth(lineWidth);
AxisAlignedBB aabb = createEnclosingAABB(pos1, pos2, renderViewEntity, partialTicks);
drawBoundingBoxEdges(aabb, colorX, colorY, colorZ);
}
@Nullable
@Override
public AxisAlignedBB getCollisionBoundingBox(IBlockState state, IBlockAccess worldIn, BlockPos pos)
{
if (getContent().collisionBounds == null)
return null;
AxisAlignedBB bounds = getContent().collisionBounds.get(getSubtype(state)).orElse(null);
if (bounds == DEFAULT_AABB_MARKER)
return super.getCollisionBoundingBox(state, worldIn, pos);
else
return bounds;
}
ChunkData(SPacketChunkData packet) {
pos = new ChunkPos(packet.getChunkX(), packet.getChunkZ());
bbox =
new AxisAlignedBB(pos.getXStart(), 0, pos.getZStart(), pos.getXEnd(), 255, pos.getZEnd());
isFullChunk = packet.isFullChunk();
update(packet);
}
@Override
public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess world, BlockPos pos) {
TileEntity tile = world.getTileEntity(pos);
if (!(tile instanceof GTTileBaseSuperconductorCable)) {
return new AxisAlignedBB(0.25D, 0.25D, 0.25D, 0.75D, 0.75D, 0.75D);
} else {
GTTileBaseSuperconductorCable cable = (GTTileBaseSuperconductorCable) tile;
double thickness = this.size / 32.0D;
double minX = 0.5D - thickness;
double minY = 0.5D - thickness;
double minZ = 0.5D - thickness;
double maxX = 0.5D + thickness;
double maxY = 0.5D + thickness;
double maxZ = 0.5D + thickness;
if (cable.connection.contains(EnumFacing.WEST)) {
minX = 0.0D;
}
if (cable.connection.contains(EnumFacing.DOWN)) {
minY = 0.0D;
}
if (cable.connection.contains(EnumFacing.NORTH)) {
minZ = 0.0D;
}
if (cable.connection.contains(EnumFacing.EAST)) {
maxX = 1.0D;
}
if (cable.connection.contains(EnumFacing.UP)) {
maxY = 1.0D;
}
if (cable.connection.contains(EnumFacing.SOUTH)) {
maxZ = 1.0D;
}
return new AxisAlignedBB(minX, minY, minZ, maxX, maxY, maxZ);
}
}
public static void drawSolidBox(AxisAlignedBB bb)
{
GL11.glVertex3d(bb.minX, bb.minY, bb.minZ);
GL11.glVertex3d(bb.maxX, bb.minY, bb.minZ);
GL11.glVertex3d(bb.maxX, bb.minY, bb.maxZ);
GL11.glVertex3d(bb.minX, bb.minY, bb.maxZ);
GL11.glVertex3d(bb.minX, bb.maxY, bb.minZ);
GL11.glVertex3d(bb.minX, bb.maxY, bb.maxZ);
GL11.glVertex3d(bb.maxX, bb.maxY, bb.maxZ);
GL11.glVertex3d(bb.maxX, bb.maxY, bb.minZ);
GL11.glVertex3d(bb.minX, bb.minY, bb.minZ);
GL11.glVertex3d(bb.minX, bb.maxY, bb.minZ);
GL11.glVertex3d(bb.maxX, bb.maxY, bb.minZ);
GL11.glVertex3d(bb.maxX, bb.minY, bb.minZ);
GL11.glVertex3d(bb.maxX, bb.minY, bb.minZ);
GL11.glVertex3d(bb.maxX, bb.maxY, bb.minZ);
GL11.glVertex3d(bb.maxX, bb.maxY, bb.maxZ);
GL11.glVertex3d(bb.maxX, bb.minY, bb.maxZ);
GL11.glVertex3d(bb.minX, bb.minY, bb.maxZ);
GL11.glVertex3d(bb.maxX, bb.minY, bb.maxZ);
GL11.glVertex3d(bb.maxX, bb.maxY, bb.maxZ);
GL11.glVertex3d(bb.minX, bb.maxY, bb.maxZ);
GL11.glVertex3d(bb.minX, bb.minY, bb.minZ);
GL11.glVertex3d(bb.minX, bb.minY, bb.maxZ);
GL11.glVertex3d(bb.minX, bb.maxY, bb.maxZ);
GL11.glVertex3d(bb.minX, bb.maxY, bb.minZ);
}
@Override
public void readFromNBT(NBTTagCompound nbt) {
super.readFromNBT(nbt);
stats.readFromNBT(nbt);
prevProgress = progress = nbt.getInteger("scanTime");
totalProgress = nbt.getInteger("scanTotalBlocks");
status = ErrorCodes.values()[nbt.getInteger("status")];
building = nbt.getBoolean("building");
if(nbt.hasKey("bb")) {
NBTTagCompound tag = nbt.getCompoundTag("bb");
bbCache = new AxisAlignedBB(tag.getDouble("minX"),
tag.getDouble("minY"), tag.getDouble("minZ"),
tag.getDouble("maxX"), tag.getDouble("maxY"), tag.getDouble("maxZ"));
}
blockPos.clear();
if(nbt.hasKey("infrastructureLocations")) {
int array[] = nbt.getIntArray("infrastructureLocations");
for(int counter = 0; counter < array.length; counter += 3) {
blockPos.add(new HashedBlockPosition(array[counter], array[counter+1], array[counter+2]));
}
}
}
@Override
public void scanRocket(World world, BlockPos pos2, AxisAlignedBB bb) {
int actualMinX = (int)bb.maxX,
actualMinY = (int)bb.maxY,
actualMinZ = (int)bb.maxZ,
actualMaxX = (int)bb.minX,
actualMaxY = (int)bb.minY,
actualMaxZ = (int)bb.minZ;
for(int xCurr = (int)bb.minX; xCurr <= bb.maxX; xCurr++) {
for(int zCurr = (int)bb.minZ; zCurr <= bb.maxZ; zCurr++) {
for(int yCurr = (int)bb.minY; yCurr<= bb.maxY; yCurr++) {
BlockPos posCurr = new BlockPos(xCurr, yCurr, zCurr);
if(!world.isAirBlock(posCurr)) {
if(xCurr < actualMinX)
actualMinX = xCurr;
if(yCurr < actualMinY)
actualMinY = yCurr;
if(zCurr < actualMinZ)
actualMinZ = zCurr;
if(xCurr > actualMaxX)
actualMaxX = xCurr;
if(yCurr > actualMaxY)
actualMaxY = yCurr;
if(zCurr > actualMaxZ)
actualMaxZ = zCurr;
}
}
}
}
status = ErrorCodes.SUCCESS_STATION;
}
@Override
public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos)
{
state = this.getActualState(state, source, pos);
int size = state.getValue(SIZE_PROPERTY);
if(size== 0)
return AABB_0;
else if(size == 1)
return AABB_1;
else if(size == 2)
return AABB_2;
return NULL_AABB;
}
@Override
public RayTraceResult collisionRayTrace(IBlockState blockState, World worldIn, BlockPos pos, Vec3d start, Vec3d end)
{
List<RayTraceResult> list = Lists.<RayTraceResult>newArrayList();
for (AxisAlignedBB axisalignedbb : getCollisionBoxList((TileSmallVessel)worldIn.getTileEntity(pos)))
{
list.add(this.rayTrace(pos, start, end, axisalignedbb));
}
RayTraceResult raytraceresult1 = null;
double d1 = 0.0D;
for (RayTraceResult raytraceresult : list)
{
if (raytraceresult != null)
{
double d0 = raytraceresult.hitVec.squareDistanceTo(end);
if (d0 > d1)
{
raytraceresult1 = raytraceresult;
d1 = d0;
}
}
}
return raytraceresult1;
}
@Override
public Iterator<ShipHolder> getShipsIntersectingWith(AxisAlignedBB playerBB) {
List<ShipHolder> ships = new ArrayList<>();
for (ShipHolder shipHolder : ships) {
if (shipHolder.getShip() != null) {
// TODO: Finish me
}
}
return ships.iterator();
}
@Override
public void setPosition(double x, double y, double z) {
posX = x;
posY = y;
posZ = z;
//Correct misalignment of bounding box
double hw = width / 2.0F;
double hd = hw * 2.25;
float f1 = height;
setEntityBoundingBox(new AxisAlignedBB(
x - hw, y, z - hd,
x + hw, y + f1, z + hd));
}
private boolean valid (BlockPos pos) {
// There are no entities to block placement,
if (!mc.world.checkNoEntityCollision(new AxisAlignedBB(pos))) {
return false;
}
// Check if the block is replaceable
return mc.world.getBlockState(pos).getBlock().isReplaceable(mc.world, pos);
}