Java Code Examples for net.minecraft.util.math.BlockPos#distanceSq()

The following examples show how to use net.minecraft.util.math.BlockPos#distanceSq() . 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 check out the related API usage on the sidebar.
Example 1
Source File: WorldMana.java    From YouTubeModdingTutorial with MIT License 6 votes vote down vote up
public float getManaInfluence(World world, BlockPos pos) {
    ChunkPos chunkPos = new ChunkPos(pos);
    float mana = 0.0f;
    for (int dx = -4 ; dx <= 4 ; dx++) {
        for (int dz = -4 ; dz <= 4 ; dz++) {
            ChunkPos cp = new ChunkPos(chunkPos.x + dx, chunkPos.z + dz);
            ManaSphere sphere = getOrCreateSphereAt(world, cp);
            if (sphere.getRadius() > 0) {
                double distanceSq = pos.distanceSq(sphere.getCenter());
                if (distanceSq < sphere.getRadius() * sphere.getRadius()) {
                    double distance = Math.sqrt(distanceSq);
                    mana += (sphere.getRadius() - distance) / sphere.getRadius();
                }
            }
        }
    }
    return mana;
}
 
Example 2
Source File: WorldMana.java    From YouTubeModdingTutorial with MIT License 6 votes vote down vote up
public float getManaStrength(World world, BlockPos pos) {
    ChunkPos chunkPos = new ChunkPos(pos);
    float mana = 0.0f;
    for (int dx = -4 ; dx <= 4 ; dx++) {
        for (int dz = -4 ; dz <= 4 ; dz++) {
            ChunkPos cp = new ChunkPos(chunkPos.x + dx, chunkPos.z + dz);
            ManaSphere sphere = getOrCreateSphereAt(world, cp);
            if (sphere.getRadius() > 0) {
                double distanceSq = pos.distanceSq(sphere.getCenter());
                if (distanceSq < sphere.getRadius() * sphere.getRadius()) {
                    double distance = Math.sqrt(distanceSq);
                    double influence = (sphere.getRadius() - distance) / sphere.getRadius();
                    mana += influence * sphere.getCurrentMana();
                }
            }
        }
    }
    return mana;
}
 
Example 3
Source File: MetaTileEntityPump.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void checkFluidBlockAt(BlockPos pumpHeadPos, BlockPos checkPos) {
    IBlockState blockHere = getWorld().getBlockState(checkPos);
    boolean shouldCheckNeighbours = isStraightInPumpRange(checkPos);

    if (blockHere.getBlock() instanceof BlockLiquid ||
        blockHere.getBlock() instanceof IFluidBlock) {
        IFluidHandler fluidHandler = FluidUtil.getFluidHandler(getWorld(), checkPos, null);
        FluidStack drainStack = fluidHandler.drain(Integer.MAX_VALUE, false);
        if (drainStack != null && drainStack.amount > 0) {
            this.fluidSourceBlocks.add(checkPos);
        }
        shouldCheckNeighbours = true;
    }

    if (shouldCheckNeighbours) {
        int maxPumpRange = getMaxPumpRange();
        for (EnumFacing facing : EnumFacing.VALUES) {
            BlockPos offsetPos = checkPos.offset(facing);
            if (offsetPos.distanceSq(pumpHeadPos) > maxPumpRange * maxPumpRange)
                continue; //do not add blocks outside bounds
            if (!fluidSourceBlocks.contains(offsetPos) &&
                !blocksToCheck.contains(offsetPos)) {
                this.blocksToCheck.add(offsetPos);
            }
        }
    }
}
 
Example 4
Source File: WorldMana.java    From YouTubeModdingTutorial with MIT License 5 votes vote down vote up
public float extractMana(World world, BlockPos pos) {
    float manaInfluence = getManaInfluence(world, pos);
    if (manaInfluence <= 0) {
        return 0;
    }
    ChunkPos chunkPos = new ChunkPos(pos);
    float extracted = 0.0f;
    for (int dx = -4 ; dx <= 4 ; dx++) {
        for (int dz = -4 ; dz <= 4 ; dz++) {
            ChunkPos cp = new ChunkPos(chunkPos.x + dx, chunkPos.z + dz);
            ManaSphere sphere = getOrCreateSphereAt(world, cp);
            if (sphere.getRadius() > 0) {
                double distanceSq = pos.distanceSq(sphere.getCenter());
                if (distanceSq < sphere.getRadius() * sphere.getRadius()) {
                    double distance = Math.sqrt(distanceSq);
                    double influence = (sphere.getRadius() - distance) / sphere.getRadius();
                    float currentMana = sphere.getCurrentMana();
                    if (influence > currentMana) {
                        influence = currentMana;
                    }
                    currentMana -= influence;
                    extracted += influence;
                    sphere.setCurrentMana(currentMana);
                    markDirty();
                }
            }
        }
    }
    return extracted;
}
 
Example 5
Source File: PositionUtils.java    From litematica with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public int compare(BlockPos pos1, BlockPos pos2)
{
    double dist1 = pos1.distanceSq(this.posReference);
    double dist2 = pos2.distanceSq(this.posReference);

    if (dist1 == dist2)
    {
        return 0;
    }

    return dist1 < dist2 == this.closestFirst ? -1 : 1;
}
 
Example 6
Source File: ModuleEffectFrost.java    From Wizardry with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public boolean run(@NotNull World world, ModuleInstanceEffect instance, @Nonnull SpellData spell, @Nonnull SpellRing spellRing) {
	Entity targetEntity = spell.getVictim(world);
	BlockPos targetPos = spell.getTargetPos();
	Entity caster = spell.getCaster(world);

	double range = spellRing.getAttributeValue(world, AttributeRegistry.AREA, spell) / 2;
	double time = spellRing.getAttributeValue(world, AttributeRegistry.DURATION, spell) * 10;

	if (!spellRing.taxCaster(world, spell, true)) return false;

	if (targetEntity != null) {
		world.playSound(null, targetEntity.getPosition(), ModSounds.FROST_FORM, SoundCategory.NEUTRAL, 1, 1);
		targetEntity.extinguish();
		if (targetEntity instanceof EntityLivingBase) {
			((EntityLivingBase) targetEntity).addPotionEffect(new PotionEffect(ModPotions.SLIPPERY, (int) time, 0, true, false));
		}
	}

	if (targetPos != null) {
		world.playSound(null, targetPos, ModSounds.FROST_FORM, SoundCategory.NEUTRAL, 1, 1);
		for (BlockPos pos : BlockPos.getAllInBox(targetPos.add(-range, -range, -range), targetPos.add(range + 1, range + 1, range + 1))) {
			double dist = pos.distanceSq(targetPos);
			if (dist > range) continue;

			for (EnumFacing facing : EnumFacing.VALUES) {
				IBlockState state = world.getBlockState(pos.offset(facing));
				if (state.getBlock() == Blocks.FIRE) {
					BlockUtils.breakBlock(world, pos.offset(facing), state, BlockUtils.makeBreaker(world, pos, caster));
				}
			}

			BlockPos up = pos.offset(EnumFacing.UP);
			if (world.getBlockState(pos).isSideSolid(world, pos, EnumFacing.UP) && world.isAirBlock(up)) {
				int layerSize = (int) (Math.max(1, Math.min(8, Math.max(1, (dist / range) * 6.0))));
				layerSize = Math.max(1, Math.min(layerSize + RandUtil.nextInt(-1, 1), 8));
				BlockUtils.placeBlock(world, up, Blocks.SNOW_LAYER.getDefaultState().withProperty(BlockSnow.LAYERS, layerSize), BlockUtils.makePlacer(world, up, caster));
			}

			if (world.getBlockState(pos).getBlock() == Blocks.WATER) {
				BlockUtils.placeBlock(world, pos, Blocks.ICE.getDefaultState(), BlockUtils.makePlacer(world, pos, caster));
			}
		}
	}
	return true;
}
 
Example 7
Source File: ItemRelayWire.java    From Valkyrien-Skies with Apache License 2.0 4 votes vote down vote up
@Override
public EnumActionResult onItemUse(EntityPlayer player, World worldIn, BlockPos pos,
    EnumHand hand,
    EnumFacing facing, float hitX, float hitY, float hitZ) {
    IBlockState clickedState = worldIn.getBlockState(pos);
    Block block = clickedState.getBlock();
    TileEntity currentTile = worldIn.getTileEntity(pos);
    ItemStack stack = player.getHeldItem(hand);

    if (currentTile instanceof IVSNodeProvider && !worldIn.isRemote) {
        ICapabilityLastRelay inst = stack
            .getCapability(ValkyrienSkiesControl.lastRelayCapability, null);
        if (inst != null) {
            if (!inst.hasLastRelay()) {
                inst.setLastRelay(pos);
                // Draw a wire in the player's hand after this
            } else {
                BlockPos lastPos = inst.getLastRelay();
                double distanceSq = lastPos.distanceSq(pos);
                TileEntity lastPosTile = worldIn.getTileEntity(lastPos);
                // System.out.println(lastPos.toString());

                if (!lastPos.equals(pos) && lastPosTile != null && currentTile != null) {
                    if (distanceSq < VSConfig.relayWireLength * VSConfig.relayWireLength) {
                        IVSNode lastPosNode = ((IVSNodeProvider) lastPosTile).getNode();
                        IVSNode currentPosNode = ((IVSNodeProvider) currentTile).getNode();
                        if (lastPosNode != null && currentPosNode != null) {
                            if (currentPosNode.isLinkedToNode(lastPosNode)) {
                                player.sendMessage(
                                    new TextComponentString("These nodes are already linked!"));
                            } else if (currentPosNode.canLinkToOtherNode(lastPosNode)) {
                                currentPosNode.makeConnection(lastPosNode);
                                stack.damageItem(1, player);
                            } else {
                                // Tell the player what they did wrong
                                player.sendMessage(new TextComponentString(TextFormatting.RED +
                                    I18n.format("message.vs_control.error_relay_wire_limit", VSConfig.networkRelayLimit)));
                            }
                            inst.setLastRelay(null);
                        }
                    } else {
                        player.sendMessage(
                            new TextComponentString(TextFormatting.RED +
                                I18n.format("message.vs_control.error_relay_wire_length")));
                        inst.setLastRelay(null);
                    }
                } else {
                    inst.setLastRelay(pos);
                }
            }
        }
    }

    if (currentTile instanceof IVSNodeProvider) {
        return EnumActionResult.SUCCESS;
    }

    return EnumActionResult.PASS;
}
 
Example 8
Source File: WorldGenSwampTreesHex.java    From TFC2 with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected TreeReturn gen(Random random, World world, Center c, IslandMap m, String wood) 
{
	tsm = TreeRegistry.instance.managerFromString(wood);
	tc = TreeRegistry.instance.treeFromString(wood);
	if(tsm == null || tc.equals(""))
	{
		TFC.log.info("Can't locate :" + wood);
		return new TreeReturn(TreeReturnEnum.None, 0);
	}

	BlockPos genPos = centerPos.add(-20+random.nextInt(41), 0, -20+random.nextInt(41));
	if(genPos.distanceSq(centerPos) > 400)//20*20
		return new TreeReturn(TreeReturnEnum.None, 0);

	genPos = genPos.add(0, map.convertHeightToMC(c.getElevation())+Global.SEALEVEL-1, 0);
	int y = world.getTopSolidOrLiquidBlock(genPos).getY();

	int growthStage = 0;
	if(m.getParams().getIslandMoisture().isGreaterThan(Moisture.LOW) && !m.getParams().hasFeature(Feature.Desert))
	{
		if(c.getMoisture().isGreaterThan(Moisture.MEDIUM) && m.getParams().getIslandMoisture().isGreaterThan(Moisture.MEDIUM))
		{
			growthStage = random.nextInt(3);
		}
		else if(c.getMoisture().isGreaterThan(Moisture.LOW))
		{
			growthStage = random.nextInt(2);
		}
	}

	TreeReturnEnum grown = TreeReturnEnum.None;
	for(;growthStage >= 0 && grown == TreeReturnEnum.None; growthStage--)
	{
		schem = tsm.getRandomSchematic(random, growthStage);
		BlockPos treePos = genPos.add(-(schem.getCenterX()-1), 0, -(schem.getCenterZ()-1));
		IBlockState s = world.getBlockState(treePos);
		if( schem != null && canGrowHere(world, treePos, schem, Math.max(growthStage, 1)))
		{
			if(buildTree(schem, tc, world, random, treePos, c))
			{
				grown = TreeReturnEnum.fromSize(growthStage);
			}
		}
	}

	return new TreeReturn(grown, schem.getBaseCount());
}
 
Example 9
Source File: WorldGenTreesHex.java    From TFC2 with GNU General Public License v3.0 4 votes vote down vote up
protected TreeReturn gen(Random random, World world, Center c, IslandMap m, String wood) 
{
	tsm = TreeRegistry.instance.managerFromString(wood);
	tc = TreeRegistry.instance.treeFromString(wood);
	if(tsm == null || tc.equals(""))
	{
		TFC.log.info("Can't locate :" + wood);
		return new TreeReturn(TreeReturnEnum.None, 0);
	}

	BlockPos genPos = centerPos.add(-20+random.nextInt(41), 0, -20+random.nextInt(41));
	if(genPos.distanceSq(centerPos) > 400)//20*20
		return new TreeReturn(TreeReturnEnum.None, 0);

	genPos = world.getTopSolidOrLiquidBlock(genPos);

	int growthStage = 0;
	if(m.getParams().getIslandMoisture().isGreaterThan(Moisture.LOW) && !m.getParams().hasFeature(Feature.Desert))
	{
		if(c.getMoisture().isGreaterThan(Moisture.MEDIUM) && m.getParams().getIslandMoisture().isGreaterThan(Moisture.MEDIUM))
		{
			growthStage = random.nextInt(3);
		}
		else if(c.getMoisture().isGreaterThan(Moisture.LOW))
		{
			growthStage = random.nextInt(2);
		}
	}

	TreeReturnEnum grown = TreeReturnEnum.None;
	for(;growthStage >= 0 && grown == TreeReturnEnum.None; growthStage--)
	{
		schem = tsm.getRandomSchematic(random, growthStage);
		if( schem != null && canGrowHere(world, genPos.down(), schem, Math.max(growthStage, 1)))
		{
			if(buildTree(schem, tc, world, random, genPos, c))
			{
				grown = TreeReturnEnum.fromSize(growthStage);
			}
		}
	}

	return new TreeReturn(grown, schem.getBaseCount());
}
 
Example 10
Source File: WorldGenCliffNoise.java    From TFC2 with GNU General Public License v3.0 4 votes vote down vote up
public double getDistance(BlockPos a, BlockPos b)
{
	return a.distanceSq(b.getX(), b.getY(), b.getZ());
}