Search in sources :

Example 1 with StringRepresentable

use of net.minecraft.util.StringRepresentable in project MC-Prefab by Brian-Wuest.

the class Structure method createBuildBlockFromBlockState.

/**
 * Creates a build block from the current block state.
 *
 * @param currentState The block state.
 * @param currentBlock The current block.
 * @param currentPos   The current position.
 * @return A new Build block object.
 */
public static BuildBlock createBuildBlockFromBlockState(BlockState currentState, Block currentBlock, BlockPos currentPos, BlockPos originalPos) {
    BuildBlock buildBlock = new BuildBlock();
    buildBlock.setBlockDomain(currentBlock.getRegistryName().getNamespace());
    buildBlock.setBlockName(currentBlock.getRegistryName().getPath());
    buildBlock.setStartingPosition(Structure.getStartingPositionFromOriginalAndCurrentPosition(currentPos, originalPos));
    buildBlock.blockPos = currentPos;
    Collection<Property<?>> properties = currentState.getProperties();
    for (Property<?> entry : properties) {
        BuildProperty property = new BuildProperty();
        property.setName(entry.getName());
        Comparable<?> value = currentState.getValue(entry);
        try {
            if (currentBlock instanceof RotatedPillarBlock && property.getName().equals("axis")) {
                property.setValue(((Direction.Axis) value).getSerializedName());
            } else if (currentBlock instanceof CarpetBlock && property.getName().equals("color")) {
                DyeColor dyeColor = (DyeColor) value;
                property.setValue(dyeColor.getSerializedName());
            } else if (value instanceof StringRepresentable) {
                StringRepresentable stringSerializable = (StringRepresentable) value;
                property.setValue(stringSerializable.getSerializedName());
            } else {
                property.setValue(value.toString());
            }
        } catch (Exception ex) {
            Prefab.LOGGER.error("Unable to set property [" + property.getName() + "] to value [" + value + "] for Block [" + buildBlock.getBlockDomain() + ":" + buildBlock.getBlockName() + "].");
            throw ex;
        }
        buildBlock.getProperties().add(property);
    }
    return buildBlock;
}
Also used : StringRepresentable(net.minecraft.util.StringRepresentable) DyeColor(net.minecraft.world.item.DyeColor) FullDyeColor(com.wuest.prefab.blocks.FullDyeColor) Property(net.minecraft.world.level.block.state.properties.Property) Direction(net.minecraft.core.Direction)

Aggregations

FullDyeColor (com.wuest.prefab.blocks.FullDyeColor)1 Direction (net.minecraft.core.Direction)1 StringRepresentable (net.minecraft.util.StringRepresentable)1 DyeColor (net.minecraft.world.item.DyeColor)1 Property (net.minecraft.world.level.block.state.properties.Property)1