Search in sources :

Example 16 with Property

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();
}
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 17 with Property

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;
}
Also used : EditSession(com.sk89q.worldedit.EditSession) World(com.sk89q.worldedit.world.World) BlockVector3(com.sk89q.worldedit.math.BlockVector3) BaseBlock(com.sk89q.worldedit.world.block.BaseBlock) Property(com.sk89q.worldedit.registry.state.Property) MaxChangedBlocksException(com.sk89q.worldedit.MaxChangedBlocksException)

Example 18 with Property

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();
                        }
                    }
                }
            }
        }
    }
}
Also used : Property(com.sk89q.worldedit.registry.state.Property) WorldEditException(com.sk89q.worldedit.WorldEditException) BlockTypes(com.sk89q.worldedit.world.block.BlockTypes) BlockVector3(com.sk89q.worldedit.math.BlockVector3) BlockType(com.sk89q.worldedit.world.block.BlockType) WorldConfiguration(com.sk89q.worldguard.config.WorldConfiguration) WorldGuard(com.sk89q.worldguard.WorldGuard) Map(java.util.Map) World(com.sk89q.worldedit.world.World) BaseBlock(com.sk89q.worldedit.world.block.BaseBlock) Maps(com.google.common.collect.Maps) WorldConfiguration(com.sk89q.worldguard.config.WorldConfiguration) BlockType(com.sk89q.worldedit.world.block.BlockType) BlockVector3(com.sk89q.worldedit.math.BlockVector3) WorldEditException(com.sk89q.worldedit.WorldEditException) BaseBlock(com.sk89q.worldedit.world.block.BaseBlock) Property(com.sk89q.worldedit.registry.state.Property)

Aggregations

Property (com.sk89q.worldedit.registry.state.Property)18 Map (java.util.Map)10 BooleanProperty (com.sk89q.worldedit.registry.state.BooleanProperty)8 EnumProperty (com.sk89q.worldedit.registry.state.EnumProperty)8 BlockVector3 (com.sk89q.worldedit.math.BlockVector3)7 DirectionalProperty (com.sk89q.worldedit.registry.state.DirectionalProperty)7 IntegerProperty (com.sk89q.worldedit.registry.state.IntegerProperty)7 BaseBlock (com.sk89q.worldedit.world.block.BaseBlock)6 BlockType (com.sk89q.worldedit.world.block.BlockType)5 Collectors (java.util.stream.Collectors)5 Maps (com.google.common.collect.Maps)4 WorldEditException (com.sk89q.worldedit.WorldEditException)4 Direction (com.sk89q.worldedit.util.Direction)4 BlockState (com.sk89q.worldedit.world.block.BlockState)4 HashMap (java.util.HashMap)4 List (java.util.List)4 Locale (java.util.Locale)4 Set (java.util.Set)4 Stream (java.util.stream.Stream)4 Block (net.minecraft.block.Block)4