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();
}
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);
}
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());
}
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;
}
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();
}
Aggregations