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

The following examples show how to use net.minecraft.block.BlockState#getProperties() . 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: IForgeBlock.java    From patchwork-api with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Get the rotations that can apply to the block at the specified coordinates. Null means no rotations are possible.
 * Note, this is up to the block to decide. It may not be accurate or representative.
 *
 * @param state The current state
 * @param world The world
 * @param pos   Block position in world
 * @return An array of valid axes to rotate around, or null for none or unknown
 */
@Nullable
default Direction[] getValidRotations(BlockState state, BlockView world, BlockPos pos) {
	for (Property<?> prop : state.getProperties()) {
		if ((prop.getName().equals("facing") || prop.getName().equals("rotation")) && prop.getType() == Direction.class) {
			@SuppressWarnings("unchecked")
			Collection<Direction> values = ((Collection<Direction>) prop.getValues());
			return values.toArray(new Direction[values.size()]);
		}
	}

	return null;
}
 
Example 3
Source File: BlockValue.java    From fabric-carpet with MIT License 5 votes vote down vote up
@Override
public Tag toTag(boolean force)
{
    if (!force) throw new NBTSerializableValue.IncompatibleTypeException(this);
    // follows falling block convertion
    CompoundTag tag =  new CompoundTag();
    CompoundTag state = new CompoundTag();
    BlockState s = getBlockState();
    state.put("Name", StringTag.of(Registry.BLOCK.getId(s.getBlock()).toString()));
    Collection<Property<?>> properties = s.getProperties();
    if (!properties.isEmpty())
    {
        CompoundTag props = new CompoundTag();
        for (Property<?> p: properties)
        {
            props.put(p.getName(), StringTag.of(s.get(p).toString().toLowerCase(Locale.ROOT)));
        }
        state.put("Properties", props);
    }
    tag.put("BlockState", state);
    CompoundTag dataTag = getData();
    if (dataTag != null)
    {
        tag.put("TileEntityData", dataTag);
    }
    return tag;
}