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

The following examples show how to use net.minecraft.block.BlockState#with() . 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: AluminumWireBlock.java    From Galacticraft-Rewoven with MIT License 7 votes vote down vote up
@Override
public BlockState getPlacementState(ItemPlacementContext context) {
    BlockState state = this.getDefaultState();
    for (Direction direction : Direction.values()) {
        Block block = context.getWorld().getBlockState(context.getBlockPos().offset(direction)).getBlock();
        if (block instanceof WireConnectable) {
            if (((WireConnectable) block).canWireConnect(context.getWorld(), direction.getOpposite(), context.getBlockPos(), context.getBlockPos().offset(direction)) != WireConnectionType.NONE) {
                state = state.with(propFromDirection(direction), true);
            }
        } else if (block instanceof ComponentProvider && ((ComponentProvider) block).hasComponent(UniversalComponents.CAPACITOR_COMPONENT)) {
            state = state.with(propFromDirection(direction), true);
        }
    }

    return state;
}
 
Example 2
Source File: GratingBlock.java    From Galacticraft-Rewoven with MIT License 5 votes vote down vote up
@Override
public BlockState getPlacementState(ItemPlacementContext context) {
    FluidState fluidState = context.getWorld().getFluidState(context.getBlockPos());
    BlockState blockState = this.getDefaultState().with(GRATING_STATE, GratingState.LOWER)
            .with(FLUID, Registry.FLUID.getId(fluidState.getFluid()))
            .with(FlowableFluid.LEVEL, Math.max(fluidState.getLevel(), 1));
    BlockPos blockPos = context.getBlockPos();
    Direction direction = context.getPlayerFacing();

    return direction != Direction.DOWN && (direction == Direction.UP || context.getBlockPos().getY() - (double) blockPos.getY() <= 0.5D) ? blockState : blockState.with(GRATING_STATE, GratingState.UPPER);
}
 
Example 3
Source File: FluidPipeBlock.java    From Galacticraft-Rewoven with MIT License 5 votes vote down vote up
@Override
public BlockState getPlacementState(ItemPlacementContext context) {
    BlockState state = this.getDefaultState();
    for (Direction direction : Direction.values()) {
        Block block = context.getWorld().getBlockState(context.getBlockPos().offset(direction)).getBlock();
        if (block instanceof FluidPipeBlock)
            state = state.with(propFromDirection(direction), true);
    }
    return state;
}
 
Example 4
Source File: FluidPipeBlock.java    From Galacticraft-Rewoven with MIT License 5 votes vote down vote up
@Override
public BlockState getStateForNeighborUpdate(BlockState state, Direction direction_1, BlockState blockState_2, WorldAccess world, BlockPos thisWire, BlockPos otherConnectable) {
    return state.with(getPropForDirection(direction_1), (
            !(blockState_2).isAir()
                    && blockState_2.getBlock() instanceof FluidPipeBlock //todo fluid things (network etc.)
    ));
}
 
Example 5
Source File: AluminumWireBlock.java    From Galacticraft-Rewoven with MIT License 5 votes vote down vote up
@Override
public BlockState getStateForNeighborUpdate(BlockState state, Direction direction_1, BlockState blockState_2, WorldAccess world, BlockPos thisWire, BlockPos otherConnectable) {
    return state.with(getPropForDirection(direction_1), (
            !(blockState_2).isAir()
                    && blockState_2.getBlock() instanceof WireConnectable
                    // get opposite of direction so the WireConnectable can check from its perspective.
                    && (((WireConnectable) blockState_2.getBlock()).canWireConnect(world, direction_1.getOpposite(), thisWire, otherConnectable) != WireConnectionType.NONE)
    ));
}
 
Example 6
Source File: TinyPumpkinBlock.java    From the-hallow with MIT License 5 votes vote down vote up
@Override
public BlockState getPlacementState(ItemPlacementContext placementContext) {
	final BlockState blockState = this.getDefaultState().with(FACING, placementContext.getPlayerFacing().getOpposite());
	if (blockState.contains(Properties.WATERLOGGED)) {
		final FluidState fluidState = placementContext.getWorld().getFluidState(placementContext.getBlockPos());
		return blockState.with(Properties.WATERLOGGED, fluidState.getFluid() == Fluids.WATER);
	}
	
	return blockState;
}
 
Example 7
Source File: CarpetExpression.java    From fabric-carpet with MIT License 5 votes vote down vote up
private <T extends Comparable<T>> BlockState setProperty(Property<T> property, String name, String value,
                                                          BlockState bs)
{
    Optional<T> optional = property.parse(value);

    if (optional.isPresent())
    {
        bs = bs.with(property, optional.get());
    }
    else
    {
        throw new InternalExpressionException(value + " is not a valid value for property " + name);
    }
    return bs;
}
 
Example 8
Source File: StandardWrenchItem.java    From Galacticraft-Rewoven with MIT License 4 votes vote down vote up
private static <T extends Comparable<T>> BlockState cycle(BlockState state, Property<T> property, boolean sneaking) {
    return state.with(property, cycle(property.getValues(), state.get(property), sneaking));
}
 
Example 9
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 10
Source File: RedstoneWireTurbo.java    From fabric-carpet with MIT License 4 votes vote down vote up
private BlockState calculateCurrentChanges(final World worldIn, final UpdateNode upd)
{
    BlockState state = upd.currentState;
    final int i = state.get(RedstoneWireBlock.POWER);
    int j = 0;
    j = getMaxCurrentStrength(upd, j);
    int l = 0;
 
    ((RedstoneWireBlockInterface)wire).setWiresGivePower(false);
    // Unfortunately, World.isBlockIndirectlyGettingPowered is complicated,
    // and I'm not ready to try to replicate even more functionality from
    // elsewhere in Minecraft into this accelerator.  So sadly, we must
    // suffer the performance hit of this very expensive call.  If there
    // is consistency to what this call returns, we may be able to cache it.
    final int k = worldIn.getReceivedRedstonePower(upd.self);
    ((RedstoneWireBlockInterface)wire).setWiresGivePower(true);
 
    // The variable 'k' holds the maximum redstone power value of any adjacent blocks.
    // If 'k' has the highest level of all neighbors, then the power level of this 
    // redstone wire will be set to 'k'.  If 'k' is already 15, then nothing inside the 
    // following loop can affect the power level of the wire.  Therefore, the loop is 
    // skipped if k is already 15. 
    if (k<15) {
        if (upd.neighbor_nodes == null) {
            // If this node's neighbors are not known, expand the node
            findNeighbors(worldIn, upd);
        }
 
        // These remain constant, so pull them out of the loop.
        // Regardless of which direction is forward, the UpdateNode for the
        // position directly above the node being calculated is always
        // at index 1.
        UpdateNode center_up = upd.neighbor_nodes[1];
        boolean center_up_is_cube = center_up.currentState.isSimpleFullBlock(worldIn, center_up.self);
 
        for (int m=0; m<4; m++) {
            // Get the neighbor array index of each of the four cardinal
            // neighbors.
            int n = rs_neighbors[m];
 
            // Get the max redstone power level of each of the cardinal
            // neighbors
            UpdateNode neighbor = upd.neighbor_nodes[n];
            l = getMaxCurrentStrength(neighbor, l);
 
            // Also check the positions above and below the cardinal
            // neighbors
            boolean neighbor_is_cube = neighbor.currentState.isSimpleFullBlock(worldIn, neighbor.self);
            if (!neighbor_is_cube) {
                UpdateNode neighbor_down = upd.neighbor_nodes[rs_neighbors_dn[m]];
                l = getMaxCurrentStrength(neighbor_down, l);
            } else
            if (!center_up_is_cube) {
                UpdateNode neighbor_up = upd.neighbor_nodes[rs_neighbors_up[m]];
                l = getMaxCurrentStrength(neighbor_up, l);
            }
        }
    }
 
    // The new code sets this redstonewire block's power level to the highest neighbor
    // minus 1.  This usually results in wire power levels dropping by 2 at a time.
    // This optimization alone has no impact on opdate order, only the number of updates.
    j = l-1;
         
    // If 'l' turns out to be zero, then j will be set to -1, but then since 'k' will
    // always be in the range of 0 to 15, the following if will correct that.
    if (k>j) j=k;
 
    if (i != j) {
        // If the power level has changed from its previous value, compute a new state
        // and set it in the world.  
        // Possible optimization:  Don't commit state changes to the world until they
        // need to be known by some nearby non-redstone-wire block.
        state = state.with(RedstoneWireBlock.POWER, j);
        // [gnembon] added state check cause other things in the tick may have popped it up already
        // https://github.com/gnembon/fabric-carpet/issues/117
        if (worldIn.getBlockState(upd.self).getBlock() == Blocks.REDSTONE_WIRE)
            worldIn.setBlockState(upd.self, state, 2);
    }
 
    return state;
}
 
Example 11
Source File: SawmillBlock.java    From Survivalist with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public BlockState rotate(BlockState state, Rotation rot)
{
    return state.with(FACING, rot.rotate(state.get(FACING)));
}
 
Example 12
Source File: SawmillBlock.java    From Survivalist with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public BlockState mirror(BlockState state, Mirror mirrorIn)
{
    return state.with(FACING, mirrorIn.toRotation(state.get(FACING)).rotate(state.get(FACING)));
}
 
Example 13
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();
    }
}