use of com.sk89q.worldedit.registry.state.Property 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.registry.state.Property in project FastAsyncWorldEdit by IntellectualSites.
the class BlockDataCyler method handleCycle.
private boolean handleCycle(LocalConfiguration config, Player player, LocalSession session, Location clicked, boolean forward) {
World world = (World) clicked.getExtent();
BlockVector3 blockPoint = clicked.toVector().toBlockPoint();
BaseBlock block = world.getFullBlock(blockPoint);
if (!config.allowedDataCycleBlocks.isEmpty() && !player.hasPermission("worldedit.override.data-cycler") && !config.allowedDataCycleBlocks.contains(block.getBlockType().getId())) {
player.print(Caption.of("worldedit.tool.data-cycler.block-not-permitted"));
return true;
}
if (block.getStates().keySet().isEmpty()) {
player.print(Caption.of("worldedit.tool.data-cycler.cant-cycle"));
} else {
Property<?> currentProperty = selectedProperties.get(player.getUniqueId());
if (currentProperty == null || (forward && block.getState(currentProperty) == null)) {
currentProperty = block.getStates().keySet().stream().findFirst().get();
selectedProperties.put(player.getUniqueId(), currentProperty);
}
if (forward) {
block.getState(currentProperty);
int index = currentProperty.getValues().indexOf(block.getState(currentProperty));
index = (index + 1) % currentProperty.getValues().size();
@SuppressWarnings("unchecked") Property<Object> objProp = (Property<Object>) currentProperty;
BaseBlock newBlock = block.with(objProp, currentProperty.getValues().get(index));
try (EditSession editSession = session.createEditSession(player)) {
try {
editSession.setBlock(blockPoint, newBlock);
player.print(Caption.of("worldedit.tool.data-cycler.new-value", TextComponent.of(currentProperty.getName()), TextComponent.of(String.valueOf(currentProperty.getValues().get(index)))));
} catch (MaxChangedBlocksException e) {
player.print(Caption.of("worldedit.tool.max-block-changes"));
} finally {
session.remember(editSession);
}
}
} else {
List<Property<?>> properties = Lists.newArrayList(block.getStates().keySet());
int index = properties.indexOf(currentProperty);
index = (index + 1) % properties.size();
currentProperty = properties.get(index);
selectedProperties.put(player.getUniqueId(), currentProperty);
player.print(Caption.of("worldedit.tool.data-cycler.cycling", TextComponent.of(currentProperty.getName())));
}
}
return true;
}
use of com.sk89q.worldedit.registry.state.Property in project WorldGuard by EngineHub.
the class SpongeUtil method clearSpongeWater.
/**
* Remove water around a sponge.
*
* @param world The world the sponge is in
* @param ox The x coordinate of the 'sponge' block
* @param oy The y coordinate of the 'sponge' block
* @param oz The z coordinate of the 'sponge' block
*/
public static void clearSpongeWater(World world, int ox, int oy, int oz) {
WorldConfiguration wcfg = WorldGuard.getInstance().getPlatform().getGlobalStateManager().get(world);
for (int cx = -wcfg.spongeRadius; cx <= wcfg.spongeRadius; cx++) {
for (int cy = -wcfg.spongeRadius; cy <= wcfg.spongeRadius; cy++) {
for (int cz = -wcfg.spongeRadius; cz <= wcfg.spongeRadius; cz++) {
BlockVector3 vector = BlockVector3.at(ox + cx, oy + cy, oz + cz);
BaseBlock block = world.getFullBlock(vector);
BlockType blockType = block.getBlockType();
if (isReplacable(blockType)) {
try {
world.setBlock(vector, BlockTypes.AIR.getDefaultState());
} catch (WorldEditException e) {
e.printStackTrace();
}
} else {
@SuppressWarnings("unchecked") Property<Object> waterloggedProp = waterloggable.computeIfAbsent(blockType, (bt -> (Property<Object>) bt.getPropertyMap().get("waterlogged")));
if (waterloggedProp != null) {
try {
world.setBlock(vector, block.with(waterloggedProp, false));
} catch (WorldEditException e) {
e.printStackTrace();
}
}
}
}
}
}
}
Aggregations