@SuppressWarnings({ "rawtypes", "unchecked" }) private IBlockState applyProperties(BlockStateContainer stateContainer, IBlockState newState, Map<Property<?>, Object> states) { for (Map.Entry<Property<?>, Object> state : states.entrySet()) { IProperty property = stateContainer.getProperty(state.getKey().getName()); Comparable value = (Comparable) state.getValue(); // we may need to adapt this value, depending on the source prop if (property instanceof PropertyDirection) { Direction dir = (Direction) value; value = ForgeAdapter.adapt(dir); } else if (property instanceof PropertyEnum) { String enumName = (String) value; value = ((PropertyEnum<?>) property).parseValue((String) value).or(() -> { throw new IllegalStateException("Enum property " + property.getName() + " does not contain " + enumName); }); } newState = newState.withProperty(property, value); } return newState; }
public static PropertyState getPropertyState(Block block, BlockStateContainer stateContainer, String propName, String propValue) { IProperty<?> property = stateContainer.getProperty(propName); if (property == null) { //noinspection ConstantConditions throw new MissingResourceException("Block \"" + block.getRegistryName().toString() + "\" doesn't have \"" + propName + "\" property", IProperty.class.getName(), propName); } return getPropertyState(property, propName, propValue); }
public static <T> T getBlockState(Tuple<String, Map<String, String>> blockProps, Function<Block, T> init, AddPropertyFunction<T> addProperty) { String registryName = blockProps.getFirst(); Map<String, String> properties = blockProps.getSecond(); Block block = RegistryTools.getBlock(registryName); T ret = init.apply(block); BlockStateContainer stateContainer = block.getBlockState(); for (Map.Entry<String, String> prop : properties.entrySet()) { IProperty<?> property = stateContainer.getProperty(prop.getKey()); if (property == null) { //noinspection ConstantConditions throw new MissingResourceException("Block \"" + block.getRegistryName().toString() + "\" doesn't have \"" + prop.getKey() + "\" property", IProperty.class.getName(), prop.getKey()); } ret = addProperty.apply(ret, property, getPropertyState(block, stateContainer, prop.getKey(), prop.getValue()).getValue()); } return ret; }
@Override public IBlockState build() { if(!Block.REGISTRY.containsKey(this.rLoc)) throw new IllegalArgumentException("No block found for resource location "+this.rLoc); final Block block = Block.REGISTRY.getObject(this.rLoc); final BlockStateContainer container = block.getBlockState(); IBlockState state = block.getDefaultState(); final Iterator<Entry<String, String>> it = this.properties.entrySet().iterator(); while(it.hasNext()){ final Entry<String, String> entry = it.next(); final IProperty<?> prop = container.getProperty(entry.getKey()); if(prop == null){ LogHelper.error("No property "+entry.getKey()+" found for block "+this.rLoc+". Skipping it."); it.remove(); continue; } if(!prop.parseValue(entry.getValue()).isPresent()){ LogHelper.error("Value "+entry.getValue()+" is not valid for property "+entry.getKey()+". Skipping it."); continue; } state = this.withProperty(state, prop, entry.getValue()); } return state; }
public static IBlockState parseState(String blockInfo) { String[] split = blockInfo.split("\\["); split[1] = split[1].substring(0, split[1].lastIndexOf("]")); // Make sure brackets are removed from state Block block = ForgeRegistries.BLOCKS.getValue(new ResourceLocation(split[0])); // Find the block if (block == Blocks.AIR) return Blocks.AIR.getDefaultState(); // The block is air, so we're looking at invalid data BlockStateContainer blockState = block.getBlockState(); IBlockState returnState = blockState.getBaseState(); // Force our values into the state String[] stateValues = split[1].split(","); // Splits up each value for (String value : stateValues) { String[] valueSplit = value.split("="); // Separates property and value IProperty property = blockState.getProperty(valueSplit[0]); if (property != null) returnState = returnState.withProperty(property, (Comparable) property.parseValue(valueSplit[1]).get()); // Force the property into the state } return returnState; } }
@SuppressWarnings( { "unchecked", "rawtypes" } ) private static IBlockState withState( final IBlockState state, final Block blk, final String[] nameval ) { final IProperty prop = blk.getBlockState().getProperty( nameval[0] ); if ( prop == null ) { Log.info( nameval[0] + " is not a valid property for " + Block.REGISTRY.getNameForObject( blk ) ); return state; } final Optional pv = prop.parseValue( nameval[1] ); if ( pv.isPresent() ) { return state.withProperty( prop, (Comparable) pv.get() ); } else { Log.info( nameval[1] + " is not a valid value of " + nameval[0] + " for " + Block.REGISTRY.getNameForObject( blk ) ); return state; } }
@Override public FarmerResult tryHarvestPlant(World world, BlockPos pos, IFarmer farmer) { int use = 250; if (farmer.getEnergy() >= use) { IBlockState state = world.getBlockState(pos); Block block = state.getBlock(); if (block instanceof BlockCrops) { if (((BlockCrops) block).isMaxAge(state)) { return doFarmerStuff(state, world, pos, farmer); } } else if ((BlockCrops.AGE).equals(block.getBlockState().getProperty("age"))) { if (state.getValue(BlockCrops.AGE) >= 7 && !(block instanceof BlockStem)) return doFarmerStuff(state, world, pos, farmer); } } return FarmerResult.FAIL; }
seedItem, block, (IProperty<Integer>) block.getBlockState().getProperty("age"), maxAge