Java Code Examples for net.minecraft.server.world.ServerWorld#setBlockState()

The following examples show how to use net.minecraft.server.world.ServerWorld#setBlockState() . 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: DeceasedGrassBlock.java    From the-hallow with MIT License 6 votes vote down vote up
@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);
					}
				}
			}
		}
	}
}
 
Example 2
Source File: CavernousVineBlock.java    From Galacticraft-Rewoven with MIT License 5 votes vote down vote up
@Override
public void scheduledTick(BlockState state, ServerWorld world, BlockPos pos, Random random) {
    for (int y2 = pos.getY() - 1; y2 >= pos.getY() - 2; y2--) {

        BlockPos pos1 = new BlockPos(pos.getX(), y2, pos.getZ());
        BlockState blockState = world.getBlockState(pos1);

        if (!blockState.isAir()) {
            return;
        }
    }
    world.setBlockState(pos.down(), this.getStateFromMeta(getVineLength(world, pos)), 2);
    world.updateNeighbors(pos, state.getBlock());
}
 
Example 3
Source File: MoonBerryBushBlock.java    From Galacticraft-Rewoven with MIT License 5 votes vote down vote up
@Override
public void scheduledTick(BlockState blockState, ServerWorld world, BlockPos blockPos, Random random) {
    super.scheduledTick(blockState, world, blockPos, random);
    int age = blockState.get(AGE);
    if (age < 3 && random.nextInt(20) == 0) {
        world.setBlockState(blockPos, blockState.with(AGE, age + 1), 2);
    }
}
 
Example 4
Source File: DeceasedGrassBlock.java    From the-hallow with MIT License 5 votes vote down vote up
@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);
			}
		}
	}
}
 
Example 5
Source File: ObsidianBlock.java    From carpet-extra with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void scheduledTick(BlockState blockState_1, ServerWorld serverWorld_1, BlockPos blockPos_1, Random random_1)
{
    for (Direction dir : Direction.values())
    {
        FluidState neighbor = serverWorld_1.getFluidState(blockPos_1.offset(dir));
        if (neighbor.getFluid() != Fluids.LAVA || !neighbor.isStill())
            return;
    }
    if (random_1.nextInt(10) == 0)
    {
        serverWorld_1.setBlockState(blockPos_1, Blocks.LAVA.getDefaultState());
    }
}
 
Example 6
Source File: FarmerVillagerTask_wartFarmMixin.java    From carpet-extra with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Redirect(method = "keepRunning", at = @At(
        value = "INVOKE",
        target = "Lnet/minecraft/inventory/BasicInventory;getInvSize()I"
))
private int plantWart(BasicInventory basicInventory, ServerWorld serverWorld, VillagerEntity villagerEntity, long l)
{
    if (isFarmingCleric) // fill cancel that for loop by setting length to 0
    {
        for(int i = 0; i < basicInventory.getInvSize(); ++i)
        {
            ItemStack itemStack = basicInventory.getInvStack(i);
            boolean bl = false;
            if (!itemStack.isEmpty())
            {
                if (itemStack.getItem() == Items.NETHER_WART)
                {
                    serverWorld.setBlockState(currentTarget, Blocks.NETHER_WART.getDefaultState(), 3);
                    bl = true;
                }
            }

            if (bl)
            {
                serverWorld.playSound(null,
                        currentTarget.getX(), currentTarget.getY(), this.currentTarget.getZ(),
                        SoundEvents.ITEM_NETHER_WART_PLANT, SoundCategory.BLOCKS, 1.0F, 1.0F);
                itemStack.decrement(1);
                if (itemStack.isEmpty())
                {
                    basicInventory.setInvStack(i, ItemStack.EMPTY);
                }
                break;
            }
        }
        return 0;

    }
    return basicInventory.getInvSize();
}
 
Example 7
Source File: SaplingBlockMixin.java    From fabric-carpet with MIT License 5 votes vote down vote up
@Inject(method = "generate", at = @At(value = "INVOKE", shift = At.Shift.BEFORE,
        target = "Lnet/minecraft/block/sapling/SaplingGenerator;generate(Lnet/minecraft/world/IWorld;Lnet/minecraft/world/gen/chunk/ChunkGenerator;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/BlockState;Ljava/util/Random;)Z"),
        cancellable = true)
private void onGenerate(ServerWorld serverWorld_1, BlockPos blockPos_1, BlockState blockState_1, Random random_1, CallbackInfo ci)
{
    if(CarpetSettings.desertShrubs && serverWorld_1.getBiome(blockPos_1) == Biomes.DESERT && !BlockSaplingHelper.hasWater(serverWorld_1, blockPos_1))
    {
        serverWorld_1.setBlockState(blockPos_1, Blocks.DEAD_BUSH.getDefaultState(), 3);
        ci.cancel();
    }
}
 
Example 8
Source File: CoralBlockMixin.java    From fabric-carpet with MIT License 5 votes vote down vote up
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);
            }
        }
    }
}
 
Example 9
Source File: TombstoneBlock.java    From the-hallow with MIT License 4 votes vote down vote up
@Override
public void grow(ServerWorld world, Random random, BlockPos pos, BlockState state) {
	if (state.get(AGE) == 0 && (isPlacedOnSoil(world, pos))) {
		world.setBlockState(pos, state.with(AGE, 1));
	}
}
 
Example 10
Source File: FallingBlockMixin.java    From carpet-extra with GNU Lesser General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("ConstantConditions")
@Inject(method = "scheduledTick", at = @At("HEAD"), cancellable = true)
private void onTryStartFalling(BlockState blockState_1, ServerWorld serverWorld_1, BlockPos blockPos_1, Random random_1, CallbackInfo ci)
{
    if (CarpetExtraSettings.dragonEggBedrockBreaking && (FallingBlock)(Object)this instanceof DragonEggBlock)
    {
        if (canFallThrough(serverWorld_1.getBlockState(blockPos_1.down(1))) && blockPos_1.getY() >= 0)
        {
            if (!DragonEggBedrockBreaking.fallInstantly &&
                    serverWorld_1.getChunkManager().shouldTickChunk(new ChunkPos(blockPos_1)))
            {
                if (!serverWorld_1.isClient)
                {
                    FallingBlockEntity fallingBlockEntity_1 = new FallingBlockEntity(serverWorld_1, (double) blockPos_1.getX() + 0.5D, (double) blockPos_1.getY(), (double) blockPos_1.getZ() + 0.5D, serverWorld_1.getBlockState(blockPos_1));
                    this.configureFallingBlockEntity(fallingBlockEntity_1);
                    serverWorld_1.spawnEntity(fallingBlockEntity_1);
                }
            }
            else
            {
                if (serverWorld_1.getBlockState(blockPos_1).getBlock() == this)
                {
                    serverWorld_1.removeBlock(blockPos_1, false);
                }

                BlockPos blockPos;
                
                int minY = CarpetExtraSettings.y0DragonEggBedrockBreaking ? -1 : 0;
                
                for (blockPos = blockPos_1.down(1); canFallThrough(serverWorld_1.getBlockState(blockPos)) && blockPos.getY() > minY; blockPos = blockPos.down(1))
                {
                    ;
                }
                
                if (blockPos.getY() > minY)
                {
                    serverWorld_1.setBlockState(blockPos, this.getDefaultState());
                }
            }
        }
        ci.cancel();
    }
}