Search in sources :

Example 1 with Property

use of net.minecraft.world.level.block.state.properties.Property in project MinecraftForge by MinecraftForge.

the class VariantBlockStateBuilder method forAllStatesExcept.

public VariantBlockStateBuilder forAllStatesExcept(Function<BlockState, ConfiguredModel[]> mapper, Property<?>... ignored) {
    Set<PartialBlockstate> seen = new HashSet<>();
    for (BlockState fullState : owner.getStateDefinition().getPossibleStates()) {
        Map<Property<?>, Comparable<?>> propertyValues = Maps.newLinkedHashMap(fullState.getValues());
        for (Property<?> p : ignored) {
            propertyValues.remove(p);
        }
        PartialBlockstate partialState = new PartialBlockstate(owner, propertyValues, this);
        if (seen.add(partialState)) {
            setModels(partialState, mapper.apply(fullState));
        }
    }
    return this;
}
Also used : BlockState(net.minecraft.world.level.block.state.BlockState) Property(net.minecraft.world.level.block.state.properties.Property) HashSet(java.util.HashSet)

Example 2 with Property

use of net.minecraft.world.level.block.state.properties.Property in project SpongeCommon by SpongePowered.

the class BlockStatePropertiesGenerator method computeUsedProperties.

private Map<PropertyType, Map<Property<?>, Set<ResourceLocation>>> computeUsedProperties() {
    // get all block state properties
    final Map<PropertyType, Map<Property<?>, Set<ResourceLocation>>> propertyUsages = new HashMap<>();
    for (final Block block : Registry.BLOCK) {
        for (final Property<?> property : block.defaultBlockState().getProperties()) {
            final var type = PropertyType.ofProperty(property);
            if (type == null) {
                Logger.warn("Unknown property type for state property {} in block {}", property, Registry.BLOCK.getKey(block));
            }
            propertyUsages.computeIfAbsent(type, $ -> new IdentityHashMap<>()).computeIfAbsent(property, $ -> new HashSet<>()).add(Registry.BLOCK.getKey(block));
        }
    }
    return propertyUsages;
}
Also used : ResourceLocation(net.minecraft.resources.ResourceLocation) IdentityHashMap(java.util.IdentityHashMap) Modifier(javax.lang.model.element.Modifier) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration) MethodSpec(com.squareup.javapoet.MethodSpec) FieldSpec(com.squareup.javapoet.FieldSpec) ClassName(com.squareup.javapoet.ClassName) BlockState(net.minecraft.world.level.block.state.BlockState) Set(java.util.Set) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) IOException(java.io.IOException) HashMap(java.util.HashMap) Property(net.minecraft.world.level.block.state.properties.Property) Logger(org.tinylog.Logger) Registry(net.minecraft.core.Registry) HashSet(java.util.HashSet) BooleanProperty(net.minecraft.world.level.block.state.properties.BooleanProperty) IntegerProperty(net.minecraft.world.level.block.state.properties.IntegerProperty) EnumProperty(net.minecraft.world.level.block.state.properties.EnumProperty) Locale(java.util.Locale) Map(java.util.Map) TypeName(com.squareup.javapoet.TypeName) Block(net.minecraft.world.level.block.Block) Comparator(java.util.Comparator) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) ResourceLocation(net.minecraft.resources.ResourceLocation) Block(net.minecraft.world.level.block.Block) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 3 with Property

use of net.minecraft.world.level.block.state.properties.Property 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)

Example 4 with Property

use of net.minecraft.world.level.block.state.properties.Property in project MC-Prefab by Brian-Wuest.

the class BuildBlock method SetBlockState.

public static BuildBlock SetBlockState(StructureConfiguration configuration, BlockPos originalPos, BuildBlock block, Block foundBlock, BlockState blockState, Direction structureDirection) {
    try {
        if (!block.blockStateData.equals("")) {
            return BuildBlock.SetBlockStateFromTagData(configuration, originalPos, block, foundBlock, blockState, structureDirection);
        }
        Direction vineFacing = BuildBlock.getVineFacing(configuration, foundBlock, block, structureDirection);
        Direction.Axis logFacing = BuildBlock.getBoneFacing(configuration, foundBlock, block, structureDirection);
        Direction.Axis boneFacing = BuildBlock.getBoneFacing(configuration, foundBlock, block, structureDirection);
        Direction leverOrientation = BuildBlock.getLeverOrientation(configuration, foundBlock, block, structureDirection);
        Map<Direction, Boolean> fourWayFacings = BuildBlock.getFourWayBlockFacings(configuration, foundBlock, block, structureDirection);
        Map<Direction, WallSide> wallShapes = BuildBlock.getWallFacings(configuration, foundBlock, block, structureDirection);
        // expected to place the block.
        if (block.getProperties().size() > 0) {
            Collection<Property<?>> properties = blockState.getProperties();
            // applied.
            for (Property<?> property : properties) {
                BuildProperty buildProperty = block.getProperty(property.getName());
                // mod (or sometimes vanilla) adds properties to vanilla blocks.
                if (buildProperty != null) {
                    try {
                        Optional<?> propertyValue = property.getValue(buildProperty.getValue());
                        if (!propertyValue.isPresent() || propertyValue.getClass().getName().equals("com.google.common.base.Absent")) {
                            Prefab.LOGGER.warn("Property value for property name [" + property.getName() + "] for block [" + block.getBlockName() + "] is considered Absent, figure out why.");
                            continue;
                        }
                        Comparable<?> comparable = property.getValueClass().cast(propertyValue.get());
                        comparable = BuildBlock.setComparable(comparable, foundBlock, property, configuration.houseFacing, block, propertyValue, vineFacing, logFacing, boneFacing, leverOrientation, structureDirection, fourWayFacings, wallShapes);
                        if (comparable == null) {
                            continue;
                        }
                        try {
                            if (blockState.getValue(property) != comparable) {
                                blockState = BuildBlock.setProperty(blockState, property, comparable);
                            }
                        } catch (Exception ex) {
                            System.out.println("Error setting properly value for property name [" + property.getName() + "] property value [" + buildProperty.getValue() + "] for block [" + block.getBlockName() + "] The default value will be used.");
                        }
                    } catch (Exception ex) {
                        System.out.println("Error getting properly value for property name [" + property.getName() + "] property value [" + buildProperty.getValue() + "] for block [" + block.getBlockName() + "]");
                        throw ex;
                    }
                }
            }
        }
        block.setBlockState(blockState);
        return block;
    } catch (Exception ex) {
        System.out.println("Error setting block state for block [" + block.getBlockName() + "] for structure configuration class [" + configuration.getClass().getName() + "]");
        throw ex;
    }
}
Also used : WallSide(net.minecraft.world.level.block.state.properties.WallSide) Direction(net.minecraft.core.Direction) CommandSyntaxException(com.mojang.brigadier.exceptions.CommandSyntaxException) Property(net.minecraft.world.level.block.state.properties.Property)

Example 5 with Property

use of net.minecraft.world.level.block.state.properties.Property in project MinecraftForge by MinecraftForge.

the class MultiPartBlockStateBuilder method toJson.

private static JsonObject toJson(Multimap<Property<?>, Comparable<?>> conditions, boolean useOr) {
    JsonObject groupJson = new JsonObject();
    for (Entry<Property<?>, Collection<Comparable<?>>> e : conditions.asMap().entrySet()) {
        StringBuilder activeString = new StringBuilder();
        for (Comparable<?> val : e.getValue()) {
            if (activeString.length() > 0)
                activeString.append("|");
            activeString.append(((Property) e.getKey()).getName(val));
        }
        groupJson.addProperty(e.getKey().getName(), activeString.toString());
    }
    if (useOr) {
        JsonArray innerWhen = new JsonArray();
        for (Entry<String, JsonElement> entry : groupJson.entrySet()) {
            JsonObject obj = new JsonObject();
            obj.add(entry.getKey(), entry.getValue());
            innerWhen.add(obj);
        }
        groupJson = new JsonObject();
        groupJson.add("OR", innerWhen);
    }
    return groupJson;
}
Also used : JsonArray(com.google.gson.JsonArray) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) Collection(java.util.Collection) Property(net.minecraft.world.level.block.state.properties.Property)

Aggregations

Property (net.minecraft.world.level.block.state.properties.Property)6 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 Direction (net.minecraft.core.Direction)2 BlockState (net.minecraft.world.level.block.state.BlockState)2 TypeDeclaration (com.github.javaparser.ast.body.TypeDeclaration)1 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 CommandSyntaxException (com.mojang.brigadier.exceptions.CommandSyntaxException)1 ClassName (com.squareup.javapoet.ClassName)1 FieldSpec (com.squareup.javapoet.FieldSpec)1 MethodSpec (com.squareup.javapoet.MethodSpec)1 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)1 TypeName (com.squareup.javapoet.TypeName)1 FullDyeColor (com.wuest.prefab.blocks.FullDyeColor)1 IOException (java.io.IOException)1 Collection (java.util.Collection)1 Comparator (java.util.Comparator)1