Search in sources :

Example 1 with BlockStateParser

use of net.minecraft.commands.arguments.blocks.BlockStateParser in project fabric-carpet by gnembon.

the class BlockValue method fromString.

public static BlockValue fromString(String str) {
    try {
        BlockValue bv = bvCache.get(str);
        if (bv != null)
            return bv;
        BlockStateParser blockstateparser = (new BlockStateParser(new StringReader(str), false)).parse(true);
        if (blockstateparser.getState() != null) {
            CompoundTag bd = blockstateparser.getNbt();
            if (bd == null)
                bd = new CompoundTag();
            bv = new BlockValue(blockstateparser.getState(), null, null, bd);
            if (bvCache.size() > 10000)
                bvCache.clear();
            bvCache.put(str, bv);
            return bv;
        }
    } catch (CommandSyntaxException ignored) {
    }
    throw new ThrowStatement(str, Throwables.UNKNOWN_BLOCK);
}
Also used : BlockStateParser(net.minecraft.commands.arguments.blocks.BlockStateParser) StringReader(com.mojang.brigadier.StringReader) ThrowStatement(carpet.script.exception.ThrowStatement) CompoundTag(net.minecraft.nbt.CompoundTag) CommandSyntaxException(com.mojang.brigadier.exceptions.CommandSyntaxException)

Example 2 with BlockStateParser

use of net.minecraft.commands.arguments.blocks.BlockStateParser in project RoyalGrenadier by BotulToxin.

the class BlockArgumentImpl method listSuggestions.

public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder, boolean allowTag) {
    StringReader reader = new StringReader(builder.getInput());
    reader.setCursor(builder.getStart());
    BlockStateParser parser = new BlockStateParser(reader, allowTag);
    try {
        parser.parse(true);
    } catch (CommandSyntaxException ignored) {
    }
    return parser.fillSuggestions(builder, Registry.BLOCK);
}
Also used : StringReader(com.mojang.brigadier.StringReader) BlockStateParser(net.minecraft.commands.arguments.blocks.BlockStateParser) CommandSyntaxException(com.mojang.brigadier.exceptions.CommandSyntaxException)

Example 3 with BlockStateParser

use of net.minecraft.commands.arguments.blocks.BlockStateParser in project Patchouli by VazkiiMods.

the class StringStateMatcher method fromString.

public static IStateMatcher fromString(String s) throws CommandSyntaxException {
    s = s.trim();
    if (s.equals("ANY")) {
        return StateMatcher.ANY;
    }
    if (s.equals("AIR")) {
        return StateMatcher.AIR;
    }
    // c.f. BlockPredicateArgumentType. Similar, but doesn't use vanilla's weird caching class.
    BlockStateParser parser = new BlockStateParser(new StringReader(s), true).parse(false);
    BlockState state = parser.getState();
    if (state != null) {
        return new ExactMatcher(state, parser.getProperties());
    } else {
        return new TagMatcher(parser.getTag(), parser.getVagueProperties());
    }
}
Also used : BlockState(net.minecraft.world.level.block.state.BlockState) BlockStateParser(net.minecraft.commands.arguments.blocks.BlockStateParser) StringReader(com.mojang.brigadier.StringReader)

Example 4 with BlockStateParser

use of net.minecraft.commands.arguments.blocks.BlockStateParser in project Mohist by MohistMC.

the class CraftBlockData method newData.

public static CraftBlockData newData(Material material, String data) {
    Preconditions.checkArgument(material == null || material.isBlock(), "Cannot get data for not block %s", material);
    BlockState blockData;
    Block block = CraftMagicNumbers.getBlock(material);
    Map<net.minecraft.world.level.block.state.properties.Property<?>, Comparable<?>> parsed = null;
    // Data provided, use it
    if (data != null) {
        try {
            // Material provided, force that material in
            if (block != null) {
                data = net.minecraft.core.Registry.BLOCK.getKey(block) + data;
            }
            StringReader reader = new StringReader(data);
            BlockStateParser arg = new BlockStateParser(reader, false).parse(false);
            Preconditions.checkArgument(!reader.canRead(), "Spurious trailing data: " + data);
            blockData = arg.getState();
            parsed = arg.getProperties();
        } catch (CommandSyntaxException ex) {
            throw new IllegalArgumentException("Could not parse data: " + data, ex);
        }
    } else {
        blockData = block.defaultBlockState();
    }
    CraftBlockData craft = fromData(blockData);
    craft.parsedStates = parsed;
    return craft;
}
Also used : BlockStateParser(net.minecraft.commands.arguments.blocks.BlockStateParser) BlockState(net.minecraft.world.level.block.state.BlockState) StringReader(com.mojang.brigadier.StringReader) CraftBlock(org.bukkit.craftbukkit.v1_18_R2.block.CraftBlock) Block(net.minecraft.world.level.block.Block) BooleanProperty(net.minecraft.world.level.block.state.properties.BooleanProperty) EnumProperty(net.minecraft.world.level.block.state.properties.EnumProperty) IntegerProperty(net.minecraft.world.level.block.state.properties.IntegerProperty) CommandSyntaxException(com.mojang.brigadier.exceptions.CommandSyntaxException)

Aggregations

StringReader (com.mojang.brigadier.StringReader)4 BlockStateParser (net.minecraft.commands.arguments.blocks.BlockStateParser)4 CommandSyntaxException (com.mojang.brigadier.exceptions.CommandSyntaxException)3 BlockState (net.minecraft.world.level.block.state.BlockState)2 ThrowStatement (carpet.script.exception.ThrowStatement)1 CompoundTag (net.minecraft.nbt.CompoundTag)1 Block (net.minecraft.world.level.block.Block)1 BooleanProperty (net.minecraft.world.level.block.state.properties.BooleanProperty)1 EnumProperty (net.minecraft.world.level.block.state.properties.EnumProperty)1 IntegerProperty (net.minecraft.world.level.block.state.properties.IntegerProperty)1 CraftBlock (org.bukkit.craftbukkit.v1_18_R2.block.CraftBlock)1