Search in sources :

Example 1 with MutableCharSequence

use of com.fastasyncworldedit.core.util.MutableCharSequence in project FastAsyncWorldEdit by IntellectualSites.

the class BlockState method get.

/**
 * Returns a temporary BlockState for a given type and string.
 *
 * <p>It's faster if a BlockType is provided compared to parsing the string.</p>
 *
 * @param type  BlockType e.g., BlockTypes.STONE (or null)
 * @param state String e.g., minecraft:water[level=4]
 * @return BlockState
 */
public static BlockState get(@Nullable BlockType type, String state, BlockState defaultState) throws InputParseException {
    int propStrStart = state.indexOf('[');
    if (type == null) {
        CharSequence key;
        if (propStrStart == -1) {
            key = state;
        } else {
            MutableCharSequence charSequence = MutableCharSequence.getTemporal();
            charSequence.setString(state);
            charSequence.setSubstring(0, propStrStart);
            key = charSequence;
        }
        type = BlockTypes.get(key);
        if (type == null) {
            String input = key.toString();
            throw new SuggestInputParseException("Does not match a valid block type: " + input, input, () -> Stream.of(BlockTypesCache.values).map(BlockType::getId).filter(id -> StringMan.blockStateMatches(input, id)).sorted(StringMan.blockStateComparator(input)).collect(Collectors.toList()));
        }
    }
    if (propStrStart == -1) {
        return type.getDefaultState();
    }
    List<? extends Property<?>> propList = type.getProperties();
    if (state.charAt(state.length() - 1) != ']') {
        state = state + "]";
    }
    MutableCharSequence charSequence = MutableCharSequence.getTemporal();
    charSequence.setString(state);
    if (propList.size() == 1) {
        AbstractProperty<?> property = (AbstractProperty<?>) propList.get(0);
        String name = property.getName();
        charSequence.setSubstring(propStrStart + name.length() + 2, state.length() - 1);
        int index = charSequence.length() <= 0 ? -1 : property.getIndexFor(charSequence);
        if (index != -1) {
            return type.withPropertyId(index);
        }
    }
    int stateId;
    if (defaultState != null) {
        stateId = defaultState.getInternalId();
    } else {
        stateId = type.getDefaultState().getInternalId();
    }
    int length = state.length();
    AbstractProperty<?> property = null;
    int last = propStrStart + 1;
    for (int i = last; i < length; i++) {
        char c = state.charAt(i);
        switch(c) {
            case ']':
            case ',':
                {
                    charSequence.setSubstring(last, i);
                    if (property != null) {
                        int index = property.getIndexFor(charSequence);
                        if (index == -1) {
                            throw SuggestInputParseException.of(charSequence.toString(), (List<Object>) property.getValues());
                        }
                        stateId = property.modifyIndex(stateId, index);
                    } else {
                        // suggest
                        PropertyKey key = PropertyKey.getByName(charSequence);
                        if (key == null || !type.hasProperty(key)) {
                            // Suggest property
                            String input = charSequence.toString();
                            BlockType finalType = type;
                            throw new SuggestInputParseException("Invalid property " + key + ":" + input + " for type " + type, input, () -> finalType.getProperties().stream().map(Property::getName).filter(p -> StringMan.blockStateMatches(input, p)).sorted(StringMan.blockStateComparator(input)).collect(Collectors.toList()));
                        } else {
                            throw new SuggestInputParseException("No operator for " + state, "", () -> Collections.singletonList("="));
                        }
                    }
                    property = null;
                    last = i + 1;
                    break;
                }
            case '=':
                {
                    charSequence.setSubstring(last, i);
                    property = (AbstractProperty) type.getPropertyMap().get(charSequence);
                    last = i + 1;
                    break;
                }
            default:
                continue;
        }
    }
    return type.withPropertyId(stateId >> BlockTypesCache.BIT_OFFSET);
}
Also used : SuggestInputParseException(com.fastasyncworldedit.core.command.SuggestInputParseException) Property(com.sk89q.worldedit.registry.state.Property) BlockVector3(com.sk89q.worldedit.math.BlockVector3) SingleBlockStateMask(com.fastasyncworldedit.core.function.mask.SingleBlockStateMask) BlanketBaseBlock(com.fastasyncworldedit.core.world.block.BlanketBaseBlock) LazyReference(com.sk89q.worldedit.util.concurrency.LazyReference) StringMan(com.fastasyncworldedit.core.util.StringMan) WorldEditException(com.sk89q.worldedit.WorldEditException) InputParseException(com.sk89q.worldedit.extension.input.InputParseException) Map(java.util.Map) PropertyKey(com.fastasyncworldedit.core.registry.state.PropertyKey) WorldEdit(com.sk89q.worldedit.WorldEdit) CompoundBinaryTag(com.sk89q.worldedit.util.nbt.CompoundBinaryTag) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) Function(com.google.common.base.Function) NullExtent(com.sk89q.worldedit.extent.NullExtent) MutableCharSequence(com.fastasyncworldedit.core.util.MutableCharSequence) OutputExtent(com.sk89q.worldedit.extent.OutputExtent) AbstractProperty(com.sk89q.worldedit.registry.state.AbstractProperty) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) List(java.util.List) Stream(java.util.stream.Stream) Capability(com.sk89q.worldedit.extension.platform.Capability) CompoundInput(com.fastasyncworldedit.core.world.block.CompoundInput) CompoundTag(com.sk89q.jnbt.CompoundTag) Mask(com.sk89q.worldedit.function.mask.Mask) ITileInput(com.fastasyncworldedit.core.queue.ITileInput) Pattern(com.sk89q.worldedit.function.pattern.Pattern) Collections(java.util.Collections) Extent(com.sk89q.worldedit.extent.Extent) BlockMaterial(com.sk89q.worldedit.world.registry.BlockMaterial) SuggestInputParseException(com.fastasyncworldedit.core.command.SuggestInputParseException) MutableCharSequence(com.fastasyncworldedit.core.util.MutableCharSequence) AbstractProperty(com.sk89q.worldedit.registry.state.AbstractProperty) MutableCharSequence(com.fastasyncworldedit.core.util.MutableCharSequence) List(java.util.List) PropertyKey(com.fastasyncworldedit.core.registry.state.PropertyKey)

Aggregations

SuggestInputParseException (com.fastasyncworldedit.core.command.SuggestInputParseException)1 SingleBlockStateMask (com.fastasyncworldedit.core.function.mask.SingleBlockStateMask)1 ITileInput (com.fastasyncworldedit.core.queue.ITileInput)1 PropertyKey (com.fastasyncworldedit.core.registry.state.PropertyKey)1 MutableCharSequence (com.fastasyncworldedit.core.util.MutableCharSequence)1 StringMan (com.fastasyncworldedit.core.util.StringMan)1 BlanketBaseBlock (com.fastasyncworldedit.core.world.block.BlanketBaseBlock)1 CompoundInput (com.fastasyncworldedit.core.world.block.CompoundInput)1 Function (com.google.common.base.Function)1 Maps (com.google.common.collect.Maps)1 CompoundTag (com.sk89q.jnbt.CompoundTag)1 WorldEdit (com.sk89q.worldedit.WorldEdit)1 WorldEditException (com.sk89q.worldedit.WorldEditException)1 InputParseException (com.sk89q.worldedit.extension.input.InputParseException)1 Capability (com.sk89q.worldedit.extension.platform.Capability)1 Extent (com.sk89q.worldedit.extent.Extent)1 NullExtent (com.sk89q.worldedit.extent.NullExtent)1 OutputExtent (com.sk89q.worldedit.extent.OutputExtent)1 Mask (com.sk89q.worldedit.function.mask.Mask)1 Pattern (com.sk89q.worldedit.function.pattern.Pattern)1