Java Code Examples for net.minecraft.block.BlockState#get()

The following examples show how to use net.minecraft.block.BlockState#get() . 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: IForgeBlock.java    From patchwork-api with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Re-colors this block in the world.
 *
 * @param state   The current state
 * @param world   The world
 * @param pos     Block position
 * @param facing  ??? (this method has no usages)
 * @param color   Color to recolor to.
 * @return if the block was affected
 */
@SuppressWarnings("unchecked")
default boolean recolorBlock(BlockState state, IWorld world, BlockPos pos, Direction facing, DyeColor color) {
	for (Property<?> prop : state.getProperties()) {
		if (prop.getName().equals("color") && prop.getType() == DyeColor.class) {
			DyeColor current = (DyeColor) state.get(prop);

			if (current != color && prop.getValues().contains(color)) {
				world.setBlockState(pos, state.with(((Property<DyeColor>) prop), color), 3);
				return true;
			}
		}
	}

	return false;
}
 
Example 2
Source File: PumpkinPieBlock.java    From the-hallow with MIT License 6 votes vote down vote up
private ActionResult tryEat(IWorld iWorld, BlockPos pos, BlockState state, PlayerEntity player) {
	if (!player.canConsume(false)) {
		return ActionResult.PASS;
	}
	float saturation = 0.1F;
	TrinketComponent trinketPlayer = TrinketsApi.getTrinketComponent(player);
	ItemStack mainHandStack = trinketPlayer.getStack("hand:ring");
	ItemStack offHandStack = trinketPlayer.getStack("offhand:ring");
	if (mainHandStack.getItem().equals(HallowedItems.PUMPKIN_RING) || offHandStack.getItem().equals(HallowedItems.PUMPKIN_RING)) {
		saturation = 0.3F;
	}
	player.getHungerManager().add(2, saturation);
	int bites = state.get(BITES);
	if (bites > 1) {
		iWorld.setBlockState(pos, state.with(BITES, bites - 1), 3);
	} else {
		iWorld.removeBlock(pos, false);
	}
	return ActionResult.SUCCESS;
}
 
Example 3
Source File: HopperMinecartEntity_transferItemsOutFeatureMixin.java    From carpet-extra with GNU Lesser General Public License v3.0 6 votes vote down vote up
private Vec3d getBlockBelowCartOffset(){
    BlockState blockState_1 = this.world.getBlockState(new BlockPos(MathHelper.floor(this.getX()), MathHelper.floor(this.getY()), MathHelper.floor(this.getZ())));
    if (blockState_1.matches(BlockTags.RAILS)) {
        RailShape railShape = (RailShape)blockState_1.get(((AbstractRailBlock)blockState_1.getBlock()).getShapeProperty());
        switch (railShape){
            case ASCENDING_EAST:
                return ascending_east_offset;
            case ASCENDING_WEST:
                return ascending_west_offset;
            case ASCENDING_NORTH:
                return ascending_north_offset;
            case ASCENDING_SOUTH:
                return ascending_south_offset;
            default:
                return upwardVec;
        }
    }
    return upwardVec;
}
 
Example 4
Source File: CoalGeneratorBlock.java    From Galacticraft-Rewoven with MIT License 6 votes vote down vote up
@Environment(EnvType.CLIENT)
@Override
public void randomDisplayTick(BlockState state, World world, BlockPos blockPos_1, Random rand) {
    if (world.getBlockEntity(blockPos_1) instanceof CoalGeneratorBlockEntity && ((CoalGeneratorBlockEntity) world.getBlockEntity(blockPos_1)).status == CoalGeneratorBlockEntity.CoalGeneratorStatus.ACTIVE || ((CoalGeneratorBlockEntity) world.getBlockEntity(blockPos_1)).status == CoalGeneratorBlockEntity.CoalGeneratorStatus.WARMING) {
        double x = (double) blockPos_1.getX() + 0.5D;
        double y = blockPos_1.getY();
        double z = (double) blockPos_1.getZ() + 0.5D;
        if (rand.nextDouble() < 0.1D) {
            world.playSound(x, y, z, SoundEvents.BLOCK_FURNACE_FIRE_CRACKLE, SoundCategory.BLOCKS, 1.0F, 1.0F, false);
        }

        Direction direction_1 = state.get(FACING);
        Direction.Axis direction$Axis_1 = direction_1.getAxis();
        double d = rand.nextDouble() * 0.6D - 0.3D;
        double xo = direction$Axis_1 == Direction.Axis.X ? (double) direction_1.getOffsetX() * 0.52D : d;
        double yo = rand.nextDouble() * 6.0D / 16.0D;
        double zo = direction$Axis_1 == Direction.Axis.Z ? (double) direction_1.getOffsetZ() * 0.52D : d;
        world.addParticle(ParticleTypes.SMOKE, x + xo, y + yo, z + zo, 0.0D, 0.0D, 0.0D);
        world.addParticle(ParticleTypes.FLAME, x + xo, y + yo, z + zo, 0.0D, 0.0D, 0.0D);

    }
}
 
Example 5
Source File: OxygenCollectorBlockEntity.java    From Galacticraft-Rewoven with MIT License 5 votes vote down vote up
private int collectOxygen(BlockPos center) {
    Optional<CelestialBodyType> celestialBodyType = CelestialBodyType.getByDimType(world.getRegistryKey());

    if (celestialBodyType.isPresent()) {
        if (celestialBodyType.get().getAtmosphere().getComposition().containsKey(AtmosphericGas.OXYGEN)) {
            int minX = center.getX() - 5;
            int minY = center.getY() - 5;
            int minZ = center.getZ() - 5;
            int maxX = center.getX() + 5;
            int maxY = center.getY() + 5;
            int maxZ = center.getZ() + 5;

            float leafBlocks = 0;

            for (BlockPos pos : BlockPos.iterate(minX, minY, minZ, maxX, maxY, maxZ)) {
                BlockState blockState = world.getBlockState(pos);
                if (blockState.isAir()) {
                    continue;
                }
                if (blockState.getBlock() instanceof LeavesBlock && !blockState.get(LeavesBlock.PERSISTENT)) {
                    leafBlocks++;
                } else if (blockState.getBlock() instanceof CropBlock) {
                    leafBlocks += 0.75F;
                }
            }

            if (leafBlocks < 2) return 0;

            double oxyCount = 20 * (leafBlocks / 14.0F);
            return (int) Math.ceil(oxyCount) / 20; //every tick
        } else {
            return 183 / 20;
        }
    } else {
        return 183 / 20;
    }
}
 
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/server/world/ServerWorld;getBlockState(Lnet/minecraft/util/math/BlockPos;)Lnet/minecraft/block/BlockState;",
        ordinal = 0
) )
private BlockState getWartBlockState(ServerWorld serverWorld, BlockPos pos)
{
    BlockState state = serverWorld.getBlockState(pos);
    if (isFarmingCleric && state.getBlock() == Blocks.NETHER_WART && state.get(NetherWartBlock.AGE) == 3)
    {
        return ((CropBlock)Blocks.WHEAT).withAge(((CropBlock)Blocks.WHEAT).getMaxAge());
    }
    return state;
}
 
Example 7
Source File: AluminumWireBlock.java    From Galacticraft-Rewoven with MIT License 5 votes vote down vote up
@Override
public VoxelShape getOutlineShape(BlockState blockState, BlockView blockView, BlockPos blockPos, ShapeContext context) {
    ArrayList<VoxelShape> shapes = new ArrayList<>();

    if (blockState.get(ATTACHED_NORTH)) {
        shapes.add(NORTH);
    }
    if (blockState.get(ATTACHED_SOUTH)) {
        shapes.add(SOUTH);
    }
    if (blockState.get(ATTACHED_EAST)) {
        shapes.add(EAST);
    }
    if (blockState.get(ATTACHED_WEST)) {
        shapes.add(WEST);
    }
    if (blockState.get(ATTACHED_UP)) {
        shapes.add(UP);
    }
    if (blockState.get(ATTACHED_DOWN)) {
        shapes.add(DOWN);
    }
    if (shapes.isEmpty()) {
        return NONE;
    } else {
        return VoxelShapes.union(NONE, shapes.toArray(new VoxelShape[0]));
    }
}
 
Example 8
Source File: FluidPipeBlock.java    From Galacticraft-Rewoven with MIT License 5 votes vote down vote up
@Override
public VoxelShape getOutlineShape(BlockState blockState, BlockView blockView, BlockPos blockPos, ShapeContext context) {
    ArrayList<VoxelShape> shapes = new ArrayList<>();

    if (blockState.get(ATTACHED_NORTH)) {
        shapes.add(NORTH);
    }
    if (blockState.get(ATTACHED_SOUTH)) {
        shapes.add(SOUTH);
    }
    if (blockState.get(ATTACHED_EAST)) {
        shapes.add(EAST);
    }
    if (blockState.get(ATTACHED_WEST)) {
        shapes.add(WEST);
    }
    if (blockState.get(ATTACHED_UP)) {
        shapes.add(UP);
    }
    if (blockState.get(ATTACHED_DOWN)) {
        shapes.add(DOWN);
    }
    if (shapes.isEmpty()) {
        return NONE;
    } else {
        return VoxelShapes.union(NONE, shapes.toArray(new VoxelShape[0]));
    }
}
 
Example 9
Source File: PistonHandler_movableTEMixin.java    From fabric-carpet with MIT License 5 votes vote down vote up
/**
 * @param blockState blockState of one double chest half block
 * @return Direction towards the other block of the double chest, null if the blockState is not a double chest
 * @author 2No2Name
 */
private Direction getDirectionToOtherChestHalf(BlockState blockState){
    ChestType chestType;
    try{
        chestType = blockState.get(ChestBlock.CHEST_TYPE);
    }catch(IllegalArgumentException e){return null;}
    if(chestType == ChestType.SINGLE)
        return null;
    return ChestBlock.getFacing(blockState);
}
 
Example 10
Source File: MoonBerryBushBlock.java    From Galacticraft-Rewoven with MIT License 5 votes vote down vote up
@Override
@Environment(EnvType.CLIENT)
public void randomDisplayTick(BlockState blockState, World world, BlockPos blockPos, Random random) {
    if (blockState.get(AGE) == 3) {

        double x = blockPos.getX() + 0.5D + (random.nextFloat() - random.nextFloat());
        double y = blockPos.getY() + random.nextFloat();
        double z = blockPos.getZ() + 0.5D + (random.nextFloat() - random.nextFloat());
        int times = random.nextInt(4);

        for (int i = 0; i < times; i++) {
            world.addParticle(new DustParticleEffect(0.5f, 0.5f, 1.0f, 0.6f), x, y, z, 0.0D, 0.0D, 0.0D);
        }
    }
}
 
Example 11
Source File: MoonBerryBushBlock.java    From Galacticraft-Rewoven with MIT License 5 votes vote down vote up
@Override
public ActionResult onUse(BlockState blockState, World world, BlockPos blockPos, PlayerEntity playerEntity, Hand hand, BlockHitResult blockHitResult) {
    int age = blockState.get(AGE);
    boolean mature = age == 3;

    if (mature) {
        int amount = 1 + world.random.nextInt(3);
        dropStack(world, blockPos, new ItemStack(GalacticraftItems.MOON_BERRIES, amount));
        world.playSound(null, blockPos, SoundEvents.ITEM_SWEET_BERRIES_PICK_FROM_BUSH, SoundCategory.BLOCKS, 1.0F, 0.8F + world.random.nextFloat() * 0.4F);
        world.setBlockState(blockPos, blockState.with(AGE, 1), 2);
        return ActionResult.SUCCESS;
    } else {
        return super.onUse(blockState, world, blockPos, playerEntity, hand, blockHitResult);
    }
}
 
Example 12
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 13
Source File: MoonBerryBushBlock.java    From Galacticraft-Rewoven with MIT License 5 votes vote down vote up
public VoxelShape getOutlineShape(BlockState blockState, BlockView blockView, BlockPos blockPos, ShapeContext context) {
    if (blockState.get(AGE) == 0) {
        return SMALL_SHAPE;
    } else {
        return blockState.get(AGE) < 3 ? LARGE_SHAPE : super.getOutlineShape(blockState, blockView, blockPos, context);
    }
}
 
Example 14
Source File: BlockRotator.java    From fabric-carpet with MIT License 4 votes vote down vote up
public static boolean flip_block(BlockState state, World world, PlayerEntity player, Hand hand, BlockHitResult hit)
{
    Block block = state.getBlock();
    BlockPos pos = hit.getBlockPos();
    Vec3d hitVec = hit.getPos().subtract(pos.getX(), pos.getY(), pos.getZ());
    Direction facing = hit.getSide();
    BlockState newState = null;
    if ( (block instanceof GlazedTerracottaBlock) || (block instanceof AbstractRedstoneGateBlock) || (block instanceof RailBlock) ||
         (block instanceof TrapdoorBlock)         || (block instanceof LeverBlock)         || (block instanceof FenceGateBlock))
    {
        newState = state.rotate(BlockRotation.CLOCKWISE_90);
    }
    else if ((block instanceof ObserverBlock) || (block instanceof EndRodBlock))
    {
        newState = state.with(FacingBlock.FACING, (Direction) state.get(FacingBlock.FACING).getOpposite());
    }
    else if (block instanceof DispenserBlock)
    {
        newState = state.with(DispenserBlock.FACING, state.get(DispenserBlock.FACING).getOpposite());
    }
    else if (block instanceof PistonBlock)
    {
        if (!(state.get(PistonBlock.EXTENDED)))
            newState = state.with(FacingBlock.FACING, state.get(FacingBlock.FACING).getOpposite());
    }
    else if (block instanceof SlabBlock)
    {
        if (((SlabBlock) block).hasSidedTransparency(state))
        {
            newState =  state.with(SlabBlock.TYPE, state.get(SlabBlock.TYPE) == SlabType.TOP ? SlabType.BOTTOM : SlabType.TOP);
        }
    }
    else if (block instanceof HopperBlock)
    {
        if ((Direction)state.get(HopperBlock.FACING) != Direction.DOWN)
        {
            newState =  state.with(HopperBlock.FACING, state.get(HopperBlock.FACING).rotateYClockwise());
        }
    }
    else if (block instanceof StairsBlock)
    {
        //LOG.error(String.format("hit with facing: %s, at side %.1fX, X %.1fY, Y %.1fZ",facing, hitX, hitY, hitZ));
        if ((facing == Direction.UP && hitVec.y == 1.0f) || (facing == Direction.DOWN && hitVec.y == 0.0f))
        {
            newState =  state.with(StairsBlock.HALF, state.get(StairsBlock.HALF) == BlockHalf.TOP ? BlockHalf.BOTTOM : BlockHalf.TOP );
        }
        else
        {
            boolean turn_right;
            if (facing == Direction.NORTH)
            {
                turn_right = (hitVec.x <= 0.5);
            }
            else if (facing == Direction.SOUTH)
            {
                turn_right = !(hitVec.x <= 0.5);
            }
            else if (facing == Direction.EAST)
            {
                turn_right = (hitVec.z <= 0.5);
            }
            else if (facing == Direction.WEST)
            {
                turn_right = !(hitVec.z <= 0.5);
            }
            else
            {
                return false;
            }
            if (turn_right)
            {
                newState = state.rotate(BlockRotation.COUNTERCLOCKWISE_90);
            }
            else
            {
                newState = state.rotate(BlockRotation.CLOCKWISE_90);
            }
        }
    }
    else
    {
        return false;
    }
    if (newState != null)
    {
        world.setBlockState(pos, newState, 2 | 1024);
        world.checkBlockRerender(pos, state, newState);
        return true;
    }
    return false;
}
 
Example 15
Source File: SawmillTileEntity.java    From Survivalist with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void tick()
{
    if (world.isRemote)
        return;

    boolean changes = false;

    if (needRefreshRecipe)
    {
        totalBurnTime = ForgeHooks.getBurnTime(inventory.getStackInSlot(1));
        totalCookTime = getSawmillTime(world, inventory.getStackInSlot(0));
        needRefreshRecipe = false;
    }

    if (this.isBurning())
    {
        --this.remainingBurnTime;
    }

    if (!this.world.isRemote)
    {
        ItemStack fuel = this.inventory.getStackInSlot(1);

        if (this.isBurning() || !fuel.isEmpty())
        {
            ChoppingContext ctx = new ChoppingContext(inventory, null, 0, 0, RANDOM);
            changes |= ChoppingRecipe.getRecipe(world, ctx).map(choppingRecipe -> {
                boolean changes2 = false;
                if (!this.isBurning() && this.canWork(ctx, choppingRecipe))
                {
                    this.totalBurnTime = ForgeHooks.getBurnTime(fuel);
                    this.remainingBurnTime = this.totalBurnTime;

                    if (this.isBurning())
                    {
                        changes2 = true;

                        if (!fuel.isEmpty())
                        {
                            Item item = fuel.getItem();
                            fuel.shrink(1);

                            if (fuel.isEmpty())
                            {
                                ItemStack containerItem = item.getContainerItem(fuel);
                                this.inventory.setStackInSlot(1, containerItem);
                            }
                        }
                    }
                }

                if (this.isBurning() && this.canWork(ctx, choppingRecipe))
                {
                    ++this.cookTime;

                    if (this.totalCookTime == 0)
                    {
                        this.totalCookTime = choppingRecipe.getSawingTime();
                    }

                    if (this.cookTime >= this.totalCookTime)
                    {
                        this.cookTime = 0;
                        this.totalCookTime = choppingRecipe.getSawingTime();
                        this.processItem(ctx, choppingRecipe);
                        changes2 = true;
                    }
                }
                else
                {
                    this.cookTime = 0;
                }

                return changes2;
            }).orElse(false);
        }
        if (!this.isBurning() && this.cookTime > 0)
        {
            this.cookTime = MathHelper.clamp(this.cookTime - 2, 0, this.totalCookTime);
        }
    }

    BlockState state = getBlockState();
    if (state.get(SawmillBlock.POWERED) != this.isBurning())
    {
        state = state.with(SawmillBlock.POWERED, this.isBurning());
        world.setBlockState(pos, state);
    }

    if (changes)
    {
        this.markDirty();
    }
}
 
Example 16
Source File: CoralBlockMixin.java    From fabric-carpet with MIT License 4 votes vote down vote up
public boolean isFertilizable(BlockView var1, BlockPos var2, BlockState var3, boolean var4)
{
    return CarpetSettings.renewableCoral && var3.get(CoralParentBlock.WATERLOGGED) && var1.getFluidState(var2.up()).matches(FluidTags.WATER);
}
 
Example 17
Source File: PumpkinPieBlock.java    From the-hallow with MIT License 4 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public int getComparatorOutput(BlockState state, World world, BlockPos pos) {
	return (state.get(BITES) * 4) - 1;
}
 
Example 18
Source File: ConfigurableElectricMachineBlock.java    From Galacticraft-Rewoven with MIT License 4 votes vote down vote up
public final SideOption getOption(BlockState state, BlockFace direction) {
    return state.get(getProperty(direction));
}
 
Example 19
Source File: TombstoneBlock.java    From the-hallow with MIT License 4 votes vote down vote up
@Override
public boolean canGrow(World world, Random random, BlockPos blockPos, BlockState state) {
	return state.get(AGE) == 0;
}
 
Example 20
Source File: IForgeBlock.java    From patchwork-api with GNU Lesser General Public License v2.1 3 votes vote down vote up
/**
 * Checks if this soil is fertile, typically this means that growth rates
 * of plants on this soil will be slightly sped up.
 * Only vanilla case is tilledField when it is within range of water.
 *
 * @param state The current state
 * @param world The current world
 * @param pos   Block position in world
 * @return True if the soil should be considered fertile.
 */
default boolean isFertile(BlockState state, BlockView world, BlockPos pos) {
	if (this.getBlock() == Blocks.FARMLAND) {
		return state.get(FarmlandBlock.MOISTURE) > 0;
	}

	return false;
}