Search in sources :

Example 36 with BlockType

use of com.sk89q.worldedit.world.block.BlockType in project FastAsyncWorldEdit by IntellectualSites.

the class SuggestionHelper method getBlockPropertySuggestions.

public static Stream<String> getBlockPropertySuggestions(String blockType, String props) {
    BlockType type = BlockTypes.get(blockType.toLowerCase(Locale.ROOT));
    if (type == null) {
        return Stream.empty();
    }
    if (!props.toLowerCase(Locale.ROOT).equals(props)) {
        return Stream.empty();
    }
    final Map<String, ? extends Property<?>> propertyMap = type.getPropertyMap();
    Set<String> matchedProperties = new HashSet<>();
    String[] propParts = props.split(",", -1);
    for (int i = 0; i < propParts.length; i++) {
        String[] propVal = propParts[i].split("=");
        final String matchProp = propVal[0].toLowerCase(Locale.ROOT);
        if (i == propParts.length - 1) {
            // suggest for next property
            String previous = Arrays.stream(propParts, 0, propParts.length - 1).collect(Collectors.joining(",")) + (propParts.length == 1 ? "" : ",");
            String lastValidInput = (blockType + "[" + previous).toLowerCase(Locale.ROOT);
            if (propVal.length == 1) {
                // only property, no value yet
                final List<? extends Property<?>> matchingProps = propertyMap.entrySet().stream().filter(p -> !matchedProperties.contains(p.getKey()) && p.getKey().startsWith(matchProp)).map(Map.Entry::getValue).collect(Collectors.toList());
                switch(matchingProps.size()) {
                    case 0:
                        return propertyMap.keySet().stream().filter(p -> !matchedProperties.contains(p)).map(prop -> lastValidInput + prop + "=");
                    case 1:
                        return matchingProps.get(0).getValues().stream().map(val -> lastValidInput + matchingProps.get(0).getName() + "=" + val.toString().toLowerCase(Locale.ROOT));
                    default:
                        return matchingProps.stream().map(p -> lastValidInput + p.getName() + "=");
                }
            } else {
                Property<?> prop = propertyMap.get(matchProp);
                if (prop == null) {
                    return propertyMap.keySet().stream().map(p -> lastValidInput + p);
                }
                final List<String> values = prop.getValues().stream().map(v -> v.toString().toLowerCase(Locale.ROOT)).collect(Collectors.toList());
                String matchVal = propVal[1].toLowerCase(Locale.ROOT);
                List<String> matchingVals = values.stream().filter(val -> val.startsWith(matchVal)).collect(Collectors.toList());
                if (matchingVals.isEmpty()) {
                    return values.stream().map(val -> lastValidInput + prop.getName() + "=" + val);
                } else {
                    if (matchingVals.size() == 1 && matchingVals.get(0).equals(matchVal)) {
                        String currProp = lastValidInput + prop.getName() + "=" + matchVal;
                        if (matchingVals.size() < values.size()) {
                            return Stream.of(currProp + "] ", currProp + ",");
                        }
                        return Stream.of(currProp + "] ");
                    }
                    return matchingVals.stream().map(val -> lastValidInput + prop.getName() + "=" + val);
                }
            }
        } else {
            // validate previous properties
            if (propVal.length != 2) {
                return Stream.empty();
            }
            Property<?> prop = propertyMap.get(matchProp);
            if (prop == null) {
                return Stream.empty();
            }
            try {
                prop.getValueFor(propVal[1]);
                matchedProperties.add(prop.getName());
            } catch (IllegalArgumentException ignored) {
                return Stream.empty();
            }
        }
    }
    return Stream.empty();
}
Also used : IntStream(java.util.stream.IntStream) Property(com.sk89q.worldedit.registry.state.Property) Arrays(java.util.Arrays) BlockTypes(com.sk89q.worldedit.world.block.BlockTypes) BlockType(com.sk89q.worldedit.world.block.BlockType) Predicate(java.util.function.Predicate) Set(java.util.Set) Registry(com.sk89q.worldedit.registry.Registry) SuggestionHelper.byPrefix(org.enginehub.piston.converter.SuggestionHelper.byPrefix) BlockCategory(com.sk89q.worldedit.world.block.BlockCategory) SuggestionHelper.limitByPrefix(org.enginehub.piston.converter.SuggestionHelper.limitByPrefix) Collectors(java.util.stream.Collectors) MathMan(com.fastasyncworldedit.core.util.MathMan) HashSet(java.util.HashSet) List(java.util.List) Stream(java.util.stream.Stream) NamespacedRegistry(com.sk89q.worldedit.registry.NamespacedRegistry) Locale(java.util.Locale) Map(java.util.Map) Keyed(com.sk89q.worldedit.registry.Keyed) BlockType(com.sk89q.worldedit.world.block.BlockType) Map(java.util.Map) HashSet(java.util.HashSet)

Example 37 with BlockType

use of com.sk89q.worldedit.world.block.BlockType in project FastAsyncWorldEdit by IntellectualSites.

the class DefaultBlockParser method getSuggestions.

@Override
public Stream<String> getSuggestions(String input) {
    final int idx = input.lastIndexOf('[');
    if (idx < 0) {
        return SuggestionHelper.getNamespacedRegistrySuggestions(BlockType.REGISTRY, input);
    }
    String blockType = input.substring(0, idx);
    BlockType type = BlockTypes.get(blockType.toLowerCase(Locale.ROOT));
    if (type == null) {
        return Stream.empty();
    }
    String props = input.substring(idx + 1);
    if (props.isEmpty()) {
        return type.getProperties().stream().map(p -> input + p.getName() + "=");
    }
    return SuggestionHelper.getBlockPropertySuggestions(blockType, props);
}
Also used : BlockType(com.sk89q.worldedit.world.block.BlockType)

Example 38 with BlockType

use of com.sk89q.worldedit.world.block.BlockType in project FastAsyncWorldEdit by IntellectualSites.

the class SaturatePattern method apply.

@Override
public boolean apply(Extent extent, BlockVector3 get, BlockVector3 set) throws WorldEditException {
    BlockType type = get.getBlock(extent).getBlockType();
    TextureUtil util = holder.getTextureUtil();
    int currentColor;
    if (type == BlockTypes.GRASS_BLOCK) {
        currentColor = holder.getTextureUtil().getColor(extent.getBiome(get));
    } else {
        currentColor = holder.getTextureUtil().getColor(type);
    }
    if (currentColor == 0) {
        return false;
    }
    int newColor = TextureUtil.multiplyColor(currentColor, color);
    BlockType newBlock = util.getNearestBlock(newColor);
    if (newBlock.equals(type)) {
        return false;
    }
    return set.setBlock(extent, newBlock.getDefaultState());
}
Also used : TextureUtil(com.fastasyncworldedit.core.util.TextureUtil) BlockType(com.sk89q.worldedit.world.block.BlockType)

Example 39 with BlockType

use of com.sk89q.worldedit.world.block.BlockType in project FastAsyncWorldEdit by IntellectualSites.

the class ShadePattern method apply.

@Override
public boolean apply(Extent extent, BlockVector3 get, BlockVector3 set) throws WorldEditException {
    BlockType type = get.getBlock(extent).getBlockType();
    BlockType newType;
    if (type == BlockTypes.GRASS_BLOCK) {
        int color = util.getColor(extent.getBiome(get));
        newType = (darken ? util.getDarkerBlock(color) : util.getLighterBlock(color));
    } else {
        newType = (darken ? util.getDarkerBlock(type) : util.getLighterBlock(type));
    }
    if (type != newType) {
        return set.setBlock(extent, newType.getDefaultState());
    }
    return false;
}
Also used : BlockType(com.sk89q.worldedit.world.block.BlockType)

Example 40 with BlockType

use of com.sk89q.worldedit.world.block.BlockType in project FastAsyncWorldEdit by IntellectualSites.

the class AverageColorPattern method applyBlock.

@Override
public BaseBlock applyBlock(BlockVector3 position) {
    BaseBlock block = getExtent().getFullBlock(position);
    TextureUtil util = holder.getTextureUtil();
    BlockType type = block.getBlockType();
    int currentColor;
    if (type == BlockTypes.GRASS_BLOCK) {
        currentColor = holder.getTextureUtil().getColor(getExtent().getBiome(position));
    } else {
        currentColor = holder.getTextureUtil().getColor(type);
    }
    int newColor = TextureUtil.averageColor(currentColor, color);
    return util.getNearestBlock(newColor).getDefaultState().toBaseBlock();
}
Also used : TextureUtil(com.fastasyncworldedit.core.util.TextureUtil) BlockType(com.sk89q.worldedit.world.block.BlockType) BaseBlock(com.sk89q.worldedit.world.block.BaseBlock)

Aggregations

BlockType (com.sk89q.worldedit.world.block.BlockType)66 BlockState (com.sk89q.worldedit.world.block.BlockState)20 BlockVector3 (com.sk89q.worldedit.math.BlockVector3)19 Map (java.util.Map)12 HashMap (java.util.HashMap)10 BaseBlock (com.sk89q.worldedit.world.block.BaseBlock)9 World (com.sk89q.worldedit.world.World)8 ArrayList (java.util.ArrayList)8 TextureUtil (com.fastasyncworldedit.core.util.TextureUtil)7 List (java.util.List)7 EditSession (com.sk89q.worldedit.EditSession)6 IOException (java.io.IOException)6 Set (java.util.Set)6 CompoundTag (com.sk89q.jnbt.CompoundTag)5 Tag (com.sk89q.jnbt.Tag)5 Region (com.sk89q.worldedit.regions.Region)5 Property (com.sk89q.worldedit.registry.state.Property)5 Direction (com.sk89q.worldedit.util.Direction)5 Locale (java.util.Locale)5 MutableBlockVector3 (com.fastasyncworldedit.core.math.MutableBlockVector3)4