下面列出了net.minecraft.util.math.BlockPos#add ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private ArrayList<BlockPos> getValidBlocks(double range,
Predicate<BlockPos> validator)
{
Vec3d eyesVec = RotationUtils.getEyesPos().subtract(0.5, 0.5, 0.5);
double rangeSq = Math.pow(range + 0.5, 2);
int rangeI = (int)Math.ceil(range);
BlockPos center = new BlockPos(RotationUtils.getEyesPos());
BlockPos min = center.add(-rangeI, -rangeI, -rangeI);
BlockPos max = center.add(rangeI, rangeI, rangeI);
return BlockUtils.getAllInBox(min, max).stream()
.filter(pos -> eyesVec.squaredDistanceTo(Vec3d.of(pos)) <= rangeSq)
.filter(BlockUtils::canBeClicked).filter(validator)
.sorted(Comparator.comparingDouble(
pos -> eyesVec.squaredDistanceTo(Vec3d.of(pos))))
.collect(Collectors.toCollection(() -> new ArrayList<>()));
}
/**
* to draw the border
* @param event
*/
@SubscribeEvent
public void onRender(RenderEvent event) {
event.getBuffer().begin(GL11.GL_LINES, DefaultVertexFormats.POSITION_COLOR);
BlockPos from = new BlockPos((((int) MC.player.posX) / 512) * 512, 0, (((int) MC.player.posZ) / 512) * 512);
BlockPos to = from.add(511, 256, 511);
int color = Colors.ORANGE.toBuffer();
if(drawRegionBorder.getAsBoolean()) {
GeometryTessellator.drawCuboid(event.getBuffer(), from, to, GeometryMasks.Line.ALL, color);
}
final int chunkDistanceSetting = chunkDistance.getAsInteger() * 16;
from = from.add(chunkDistanceSetting, 0, chunkDistanceSetting);
to = to.add(-chunkDistanceSetting, 0, -chunkDistanceSetting);
color = Colors.YELLOW.toBuffer();
GeometryTessellator.drawCuboid(event.getBuffer(), from, to, GeometryMasks.Line.ALL, color);
event.getTessellator().draw();
}
/**
* Checks a line of blocks in the world from the first coordinate to triplet to the second, returning the distance
* (in blocks) before a non-air, non-leaf block is encountered and/or the end is encountered.
*/
int checkBlockLine(BlockPos posOne, BlockPos posTwo)
{
BlockPos blockpos = posTwo.add(-posOne.getX(), -posOne.getY(), -posOne.getZ());
int i = this.getGreatestDistance(blockpos);
float f = (float)blockpos.getX() / (float)i;
float f1 = (float)blockpos.getY() / (float)i;
float f2 = (float)blockpos.getZ() / (float)i;
if (i == 0)
{
return -1;
}
for (int j = 0; j <= i; ++j)
{
BlockPos blockpos1 = posOne.add(0.5F + j * f, 0.5F + j * f1, 0.5F + j * f2);
if (!this.isReplaceable(world, blockpos1))
{
return j;
}
}
return -1;
}
public static boolean isGeneratorInAir(World world, BlockPos pos) {
BlockPos startPos = pos.add(-2, 0, -2);
int waterBlockCount = 0;
for (int x = 0; x < 5; x ++) {
for (int z = 0; z < 5; z++) {
boolean isValidNeighbour = isValidNeighbour(world, startPos.add(x, 0, z));
if (isValidNeighbour) {
waterBlockCount++;
if (waterBlockCount >= EmergingTechnologyConfig.ELECTRICS_MODULE.WIND.minimumAirBlocks) {
return true;
}
}
}
}
return false;
}
@Override
public boolean generate(IWorld iWorld, ChunkGenerator<? extends ChunkGeneratorConfig> chunkGenerator, Random random, BlockPos pos, DefaultFeatureConfig defaultFeatureConfig) {
if (iWorld.getBlockState(pos.down()).getBlock() == HallowedBlocks.DECEASED_GRASS_BLOCK) {
setSpawner(iWorld, pos, EntityType.SPIDER);
for (int i = 0; i < 64; ++i) {
BlockPos pos_2 = pos.add(random.nextInt(6) - random.nextInt(6), random.nextInt(3) - random.nextInt(3), random.nextInt(6) - random.nextInt(6));
if (iWorld.isAir(pos_2) || iWorld.getBlockState(pos_2).getBlock() == HallowedBlocks.DECEASED_GRASS_BLOCK) {
iWorld.setBlockState(pos_2, Blocks.COBWEB.getDefaultState(), 2);
}
}
BlockPos chestPos = pos.add(random.nextInt(4) - random.nextInt(4), 0, random.nextInt(4) - random.nextInt(4));
iWorld.setBlockState(chestPos, StructurePiece.method_14916(iWorld, chestPos, Blocks.CHEST.getDefaultState()), 2);
LootableContainerBlockEntity.setLootTable(iWorld, random, chestPos, TheHallow.id("chests/spider_lair"));
return true;
} else {
return false;
}
}
private ArrayList<BlockPos> getBlocksByDistanceReversed(double range)
{
Vec3d eyesVec = RotationUtils.getEyesPos().subtract(0.5, 0.5, 0.5);
double rangeSq = Math.pow(range + 0.5, 2);
int rangeI = (int)Math.ceil(range);
BlockPos center = new BlockPos(RotationUtils.getEyesPos());
BlockPos min = center.add(-rangeI, -rangeI, -rangeI);
BlockPos max = center.add(rangeI, rangeI, rangeI);
return BlockUtils.getAllInBox(min, max).stream()
.filter(pos -> eyesVec.squaredDistanceTo(Vec3d.of(pos)) <= rangeSq)
.sorted(Comparator.comparingDouble(
pos -> -eyesVec.squaredDistanceTo(Vec3d.of(pos))))
.collect(Collectors.toCollection(() -> new ArrayList<>()));
}
@Override
public void scheduledTick(BlockState blockState, ServerWorld world, BlockPos blockPos, Random random) {
if (!world.isClient) {
if (!canSurvive(blockState, world, blockPos)) {
world.setBlockState(blockPos, HallowedBlocks.DECEASED_DIRT.getDefaultState());
} else {
if (world.getLightLevel(blockPos.up()) >= 9) {
BlockState defaultState = this.getDefaultState();
for (int i = 0; i < 4; ++i) {
BlockPos randomPos = blockPos.add(random.nextInt(3) - 1, random.nextInt(5) - 3, random.nextInt(3) - 1);
if (world.getBlockState(randomPos).getBlock() == HallowedBlocks.DECEASED_DIRT && canSpread(defaultState, world, randomPos)) {
world.setBlockState(randomPos, defaultState);
}
}
}
}
}
}
@Override
@SideOnly(Side.CLIENT)
public AxisAlignedBB getRenderBoundingBox() {
if (this.isPartOfAssembledMultiblock() && this.isMaster() && getRudderAxleAxisDirection()
.isPresent()) {
BlockPos minPos = this.pos;
EnumFacing axleAxis = getRudderAxleAxisDirection().get();
EnumFacing axleFacing = getRudderAxleFacingDirection().get();
Vec3i otherAxis = axleAxis.getDirectionVec().crossProduct(axleFacing.getDirectionVec());
int nexAxisX = axleAxis.getDirectionVec().getX() + axleFacing.getDirectionVec().getX();
int nexAxisY = axleAxis.getDirectionVec().getY() + axleFacing.getDirectionVec().getY();
int nexAxisZ = axleAxis.getDirectionVec().getZ() + axleFacing.getDirectionVec().getZ();
int axleLength = getRudderAxleLength().get();
int offsetX = nexAxisX * axleLength;
int offsetY = nexAxisY * axleLength;
int offsetZ = nexAxisZ * axleLength;
BlockPos maxPos = minPos.add(offsetX, offsetY, offsetZ);
int otherAxisXExpanded = otherAxis.getX() * axleLength;
int otherAxisYExpanded = otherAxis.getY() * axleLength;
int otherAxisZExpanded = otherAxis.getZ() * axleLength;
return new AxisAlignedBB(minPos, maxPos)
.grow(otherAxisXExpanded, otherAxisYExpanded, otherAxisZExpanded)
.grow(.5, .5, .5);
} else {
return super.getRenderBoundingBox();
}
}
@Override
public boolean onBlockPreBreak(ItemStack stack, BlockPos blockPos, EntityPlayer player) {
if (player.world.isRemote || player.capabilities.isCreativeMode) {
return false;
}
ToolMetaItem<?> toolMetaItem = (ToolMetaItem<?>) stack.getItem();
int damagePerBlockBreak = getToolDamagePerBlockBreak(stack);
for(int x = -5; x <= 5; x++) {
for(int z = -5; z <= 5; z++) {
for(int y = -5; y <= 5; y++) {BlockPos offsetPos = blockPos.add(x, y, z);
IBlockState blockState = player.world.getBlockState(offsetPos);
if(player.world.isBlockModifiable(player, offsetPos) && toolMetaItem.isUsable(stack, damagePerBlockBreak)) {
if(blockState.getBlock() instanceof BlockCrops) {
player.world.playEvent(2001, offsetPos, Block.getStateId(blockState));
ToolUtility.applyHarvestBehavior(offsetPos, player);
toolMetaItem.damageItem(stack, damagePerBlockBreak, false);
} else if(blockState.getMaterial() == Material.PLANTS ||
blockState.getMaterial() == Material.LEAVES ||
blockState.getMaterial() == Material.VINE) {
player.world.playEvent(2001, offsetPos, Block.getStateId(blockState));
player.world.setBlockToAir(offsetPos);
toolMetaItem.damageItem(stack, damagePerBlockBreak, false);
}
}
}
}
}
return true;
}
public static boolean isSurroundingBlocksAtLeastOneOf(BlockInfo[] blocks, BlockPos pos, World world, int radius) {
ArrayList<BlockInfo> blockList = new ArrayList<BlockInfo>(Arrays.asList(blocks));
for (int xShift = -1*radius ; xShift <= radius ; xShift++) {
for (int zShift = -1*radius ; zShift <= radius ; zShift++) {
BlockPos checkPos = pos.add(xShift, 0, zShift);
BlockInfo checkBlock = new BlockInfo(world.getBlockState(checkPos));
if (blockList.contains(checkBlock))
return true;
}
}
return false;
}
public void grow(World worldIn, Random rand, BlockPos pos, IBlockState state)
{
BlockPos blockpos = pos.up();
for (int i = 0; i < 128; ++i)
{
BlockPos blockpos1 = blockpos;
int j = 0;
while (true)
{
if (j >= i / 16)
{
if (worldIn.isAirBlock(blockpos1))
{
if (rand.nextInt(8) == 0) plantFlower(worldIn, rand, blockpos1);
}
break;
}
blockpos1 = blockpos1.add(rand.nextInt(3) - 1, (rand.nextInt(3) - 1) * rand.nextInt(3) / 2, rand.nextInt(3) - 1);
if (!(worldIn.getBlockState(blockpos1.down()).getBlock() instanceof BlockTofuBase) || worldIn.getBlockState(blockpos1).isNormalCube())
{
break;
}
++j;
}
}
}
private List<BlockPos> searchAdjBlocks(BlockPos pos) {
List<BlockPos> found = new ArrayList<BlockPos>();
for (BlockPos bp : search) {
BlockPos apos = pos.add(bp);
if (world.getBlockState(apos).getBlock() != Blocks.AIR && !toConstruct.contains(apos)) {
found.add(apos);
}
}
return found;
}
@Nullable
@Override
public BlockPos getBottomLowerLeft(World world, BlockPos pos) {
if (isBlockPart(world, pos)) {
IBlockState state = world.getBlockState(pos);
SuperchestPartIndex index = state.getValue(BlockSuperchest.FORMED);
return pos.add(-index.getDx(), -index.getDy(), -index.getDz());
} else {
return null;
}
}
protected Vec3i getRegionOffset(ISchematicRegion region, BlockPos minCorner)
{
// Get the offset from the region's block state container origin
// (the minimum corner of the region) to the enclosing area's origin/minimum corner.
BlockPos regionPos = region.getPosition();
Vec3i endRel = PositionUtils.getRelativeEndPositionFromAreaSize(region.getSize());
BlockPos regionEnd = regionPos.add(endRel);
BlockPos regionMin = fi.dy.masa.malilib.util.PositionUtils.getMinCorner(regionPos, regionEnd);
BlockPos regionOffset = regionMin.subtract(minCorner);
return regionOffset;
}
public void grow(ServerWorld worldIn, Random random, BlockPos pos, BlockState blockUnder)
{
CoralFeature coral;
int variant = random.nextInt(3);
if (variant == 0)
coral = new CoralClawFeature(DefaultFeatureConfig::deserialize);
else if (variant == 1)
coral = new CoralTreeFeature(DefaultFeatureConfig::deserialize);
else
coral = new CoralMushroomFeature(DefaultFeatureConfig::deserialize);
MaterialColor color = blockUnder.getTopMaterialColor(worldIn, pos);
BlockState proper_block = blockUnder;
for (Block block: BlockTags.CORAL_BLOCKS.values())
{
proper_block = block.getDefaultState();
if (proper_block.getTopMaterialColor(worldIn,pos) == color)
{
break;
}
}
worldIn.setBlockState(pos, Blocks.WATER.getDefaultState(), 4);
if (!((CoralFeatureInterface)coral).growSpecific(worldIn, random, pos, proper_block))
{
worldIn.setBlockState(pos, blockUnder, 3);
}
else
{
if (worldIn.random.nextInt(10)==0)
{
BlockPos randomPos = pos.add(worldIn.random.nextInt(16)-8,worldIn.random.nextInt(8),worldIn.random.nextInt(16)-8 );
if (BlockTags.CORAL_BLOCKS.contains(worldIn.getBlockState(randomPos).getBlock()))
{
worldIn.setBlockState(randomPos, Blocks.WET_SPONGE.getDefaultState(), 3);
}
}
}
}
@Override
public boolean generate(IWorld world, ChunkGenerator<? extends ChunkGeneratorConfig> chunkGenerator, Random random, BlockPos blockPos, DefaultFeatureConfig defaultFeatureConfig) {
int numCrop = 0;
for (int i = 0; i < 64; ++i) {
BlockPos randomBlockPos = blockPos.add(random.nextInt(8) - random.nextInt(8), random.nextInt(4) - random.nextInt(4), random.nextInt(8) - random.nextInt(8));
if (world.isAir(randomBlockPos) && world.getBlockState(randomBlockPos.down()).getBlock() == HallowedBlocks.DECEASED_GRASS_BLOCK) {
world.setBlockState(randomBlockPos, this.crop, 2);
++numCrop;
}
}
return numCrop > 0;
}
public static void ladder(ArgParser r, World world, BlockPos pos, EnumFacing facing) {
int height = r.getInteger("height");
IBlockState block_state = PyRegistry.getBlockVariant(r, pos, facing, (WorldServer)world);
for (int i = 0; i < height; i++) {
world.setBlockState(pos, block_state);
pos = pos.add(0, 1, 0);
}
}
@Override
public void grow(ServerWorld world, Random random, BlockPos blockPos, BlockState blockState) {
BlockPos upPos = blockPos.up();
BlockState grassBlock = HallowedBlocks.DECEASED_GRASS_BLOCK.getDefaultState();
label48:
for (int i = 0; i < 128; ++i) {
BlockPos randomPos = upPos;
for (int j = 0; j < i / 16; ++j) {
randomPos = randomPos.add(random.nextInt(3) - 1, (random.nextInt(3) - 1) * random.nextInt(3) / 2, random.nextInt(3) - 1);
if (world.getBlockState(randomPos.down()).getBlock() != this || world.getBlockState(randomPos).isFullCube(world, randomPos)) {
continue label48;
}
}
BlockState randomBlockState = world.getBlockState(randomPos);
if (randomBlockState.getBlock() == grassBlock.getBlock() && random.nextInt(10) == 0) {
((Fertilizable) grassBlock.getBlock()).grow(world, random, randomPos, randomBlockState);
}
if (randomBlockState.isAir()) {
BlockState stateToPlace;
if (random.nextInt(8) == 0) {
List<ConfiguredFeature<?, ?>> list = world.getBiomeAccess().getBiome(randomPos).getFlowerFeatures();
if (list.isEmpty()) {
continue;
}
stateToPlace = ((FlowerFeature) ((DecoratedFeatureConfig) (list.get(0)).config).feature.feature).getFlowerToPlace(random, randomPos, list.get(0).config);
} else {
stateToPlace = grassBlock;
}
if (stateToPlace.canPlaceAt(world, randomPos)) {
world.setBlockState(randomPos, stateToPlace, 3);
}
}
}
}
public void generate(World world, Random rand, BlockPos position) {
int chance = rand.nextInt(ConfigValues.manaPoolRarity);
if (chance == 0) {
if (position.getY() > 4) {
position = position.down(4);
boolean[] aboolean = new boolean[2048];
int i = rand.nextInt(4) + 4;
for (int j = 0; j < i; ++j) {
double d0 = rand.nextDouble() * 6.0D + 3.0D;
double d1 = rand.nextDouble() * 4.0D + 2.0D;
double d2 = rand.nextDouble() * 6.0D + 3.0D;
double d3 = rand.nextDouble() * (16.0D - d0 - 2.0D) + 1.0D + d0 / 2.0D;
double d4 = rand.nextDouble() * (8.0D - d1 - 4.0D) + 2.0D + d1 / 2.0D;
double d5 = rand.nextDouble() * (16.0D - d2 - 2.0D) + 1.0D + d2 / 2.0D;
for (int l = 1; l < 15; ++l) {
for (int i1 = 1; i1 < 15; ++i1) {
for (int j1 = 1; j1 < 7; ++j1) {
double d6 = ((double) l - d3) / (d0 / 2.0D);
double d7 = ((double) j1 - d4) / (d1 / 2.0D);
double d8 = ((double) i1 - d5) / (d2 / 2.0D);
double d9 = d6 * d6 + d7 * d7 + d8 * d8;
if (d9 < 1.0D) {
aboolean[(l * 16 + i1) * 8 + j1] = true;
}
}
}
}
}
for (int k1 = 0; k1 < 16; ++k1) {
for (int l2 = 0; l2 < 16; ++l2) {
for (int k = 0; k < 8; ++k) {
boolean flag = !aboolean[(k1 * 16 + l2) * 8 + k] && (k1 < 15 && aboolean[((k1 + 1) * 16 + l2) * 8 + k] || k1 > 0 && aboolean[((k1 - 1) * 16 + l2) * 8 + k] || l2 < 15 && aboolean[(k1 * 16 + l2 + 1) * 8 + k] || l2 > 0 && aboolean[(k1 * 16 + (l2 - 1)) * 8 + k] || k < 7 && aboolean[(k1 * 16 + l2) * 8 + k + 1] || k > 0 && aboolean[(k1 * 16 + l2) * 8 + (k - 1)]);
if (flag) {
Material material = world.getBlockState(position.add(k1, k, l2)).getMaterial();
if (k >= 4 && material.isLiquid()) {
return;
}
if (k < 4 && !material.isSolid() && world.getBlockState(position.add(k1, k, l2)).getBlock() != this.block) {
return;
}
}
}
}
}
for (int l1 = 0; l1 < 16; ++l1) {
for (int i3 = 0; i3 < 16; ++i3) {
for (int i4 = 0; i4 < 8; ++i4) {
if (aboolean[(l1 * 16 + i3) * 8 + i4]) {
world.setBlockState(position.add(l1, i4, i3), i4 >= 4 ? Blocks.AIR.getDefaultState() : this.block.getDefaultState(), 2);
}
}
}
}
for (int i2 = 0; i2 < 16; ++i2) {
for (int j3 = 0; j3 < 16; ++j3) {
for (int j4 = 4; j4 < 8; ++j4) {
if (aboolean[(i2 * 16 + j3) * 8 + j4]) {
BlockPos blockpos = position.add(i2, j4 - 1, j3);
if (world.getBlockState(blockpos).getBlock() == Blocks.DIRT && world.getLightFor(EnumSkyBlock.SKY, position.add(i2, j4, j3)) > 0) {
Biome biome = world.getBiome(blockpos);
if (biome.topBlock.getBlock() == Blocks.MYCELIUM) {
world.setBlockState(blockpos, Blocks.MYCELIUM.getDefaultState(), 2);
} else {
world.setBlockState(blockpos, Blocks.GRASS.getDefaultState(), 2);
if (rand.nextInt(3) == 0) {
WorldGeneratorWisdomTree tree = new WorldGeneratorWisdomTree(false);
tree.generate(world, rand, blockpos);
}
}
}
}
}
}
}
}
}
}
public BlockPos Lerp(BlockPos start, BlockPos end, float percent)
{
BlockPos b = end.add(start);
return start.add(new BlockPos(percent*b.getX(), percent*b.getY(), percent*b.getZ()));
}