Search in sources :

Example 1 with Attribute

use of mekanism.common.block.attribute.Attribute in project Mekanism by mekanism.

the class BlockStateHelper method copyStateData.

public static BlockState copyStateData(BlockState oldState, BlockState newState) {
    Block oldBlock = oldState.getBlock();
    Block newBlock = newState.getBlock();
    for (Attribute attr : Attribute.getAll(oldBlock)) {
        if (attr instanceof AttributeState) {
            newState = ((AttributeState) attr).copyStateData(oldState, newState);
        }
    }
    if (oldBlock instanceof IStateStorage && newBlock instanceof IStateStorage) {
        newState = newState.setValue(storageProperty, oldState.getValue(storageProperty));
    }
    if (oldBlock instanceof IStateFluidLoggable && newBlock instanceof IStateFluidLoggable) {
        IStateFluidLoggable oldFluidLoggable = (IStateFluidLoggable) oldBlock;
        IStateFluidLoggable newFluidLoggable = (IStateFluidLoggable) newBlock;
        if (oldFluidLoggable.getSupportedFluids().length == newFluidLoggable.getSupportedFluids().length) {
            // Basic check if the number of supported fluids is the same copy it over
            // TODO: Eventually maybe we want a better check? In theory they should always match but just in case
            newState = newState.setValue(newFluidLoggable.getFluidLoggedProperty(), oldState.getValue(oldFluidLoggable.getFluidLoggedProperty()));
        }
    }
    return newState;
}
Also used : AttributeState(mekanism.common.block.attribute.AttributeState) Attribute(mekanism.common.block.attribute.Attribute) AbstractBlock(net.minecraft.block.AbstractBlock) Block(net.minecraft.block.Block)

Example 2 with Attribute

use of mekanism.common.block.attribute.Attribute in project Mekanism by mekanism.

the class BlockStateHelper method getStateForPlacement.

@Contract("_, null, _, _, _, _ -> null")
public static BlockState getStateForPlacement(Block block, @Nullable BlockState state, @Nonnull IWorld world, @Nonnull BlockPos pos, @Nullable PlayerEntity player, @Nonnull Direction face) {
    if (state == null) {
        return null;
    }
    for (Attribute attr : Attribute.getAll(block)) {
        if (attr instanceof AttributeState) {
            state = ((AttributeState) attr).getStateForPlacement(block, state, world, pos, player, face);
        }
    }
    if (block instanceof IStateFluidLoggable) {
        IStateFluidLoggable fluidLoggable = (IStateFluidLoggable) block;
        FluidState fluidState = world.getFluidState(pos);
        state = state.setValue(fluidLoggable.getFluidLoggedProperty(), fluidLoggable.getSupportedFluidPropertyIndex(fluidState.getType()));
    }
    return state;
}
Also used : AttributeState(mekanism.common.block.attribute.AttributeState) Attribute(mekanism.common.block.attribute.Attribute) FluidState(net.minecraft.fluid.FluidState) Contract(org.jetbrains.annotations.Contract)

Aggregations

Attribute (mekanism.common.block.attribute.Attribute)2 AttributeState (mekanism.common.block.attribute.AttributeState)2 AbstractBlock (net.minecraft.block.AbstractBlock)1 Block (net.minecraft.block.Block)1 FluidState (net.minecraft.fluid.FluidState)1 Contract (org.jetbrains.annotations.Contract)1