001/*
002 * PlotSquared, a land and world management plugin for Minecraft.
003 * Copyright (C) IntellectualSites <https://intellectualsites.com>
004 * Copyright (C) IntellectualSites team and contributors
005 *
006 * This program is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
018 */
019package com.plotsquared.core.util;
020
021import com.sk89q.worldedit.WorldEdit;
022import com.sk89q.worldedit.extension.input.InputParseException;
023import com.sk89q.worldedit.extension.input.ParserContext;
024import com.sk89q.worldedit.internal.registry.InputParser;
025import com.sk89q.worldedit.world.block.BaseBlock;
026import com.sk89q.worldedit.world.block.BlockState;
027import com.sk89q.worldedit.world.block.BlockType;
028import com.sk89q.worldedit.world.block.BlockTypes;
029import com.sk89q.worldedit.world.block.FuzzyBlockState;
030import com.sk89q.worldedit.world.registry.LegacyMapper;
031import org.checkerframework.checker.nullness.qual.NonNull;
032import org.checkerframework.checker.nullness.qual.Nullable;
033
034/**
035 * {@link BlockState} related utility methods
036 */
037public final class BlockUtil {
038
039    private static final ParserContext PARSER_CONTEXT = new ParserContext();
040    private static final InputParser<BaseBlock> PARSER;
041
042    static {
043        PARSER_CONTEXT.setRestricted(false);
044        PARSER_CONTEXT.setPreferringWildcard(false);
045        PARSER_CONTEXT.setTryLegacy(true);
046        PARSER = WorldEdit.getInstance().getBlockFactory().getParsers().get(0);
047    }
048
049    private BlockUtil() {
050    }
051
052    /**
053     * Get a {@link BlockState} from a legacy id
054     *
055     * @param id Legacy ID
056     * @return Block state, or {@code null}
057     */
058    public static @Nullable BlockState get(final int id) {
059        return LegacyMapper.getInstance().getBlockFromLegacy(id);
060    }
061
062    /**
063     * Get a {@link BlockState} from a legacy id-data pair
064     *
065     * @param id   Legacy ID
066     * @param data Legacy data
067     * @return Block state, or {@code null}
068     */
069    public static @Nullable BlockState get(final int id, final int data) {
070        return LegacyMapper.getInstance().getBlockFromLegacy(id, data);
071    }
072
073    /**
074     * Get a {@link BlockState} from its ID
075     *
076     * @param id String or integer ID
077     * @return Parsed block state, or {@code null} if none
078     *         could be parsed
079     */
080    public static @Nullable BlockState get(final @NonNull String id) {
081        if (id.length() == 1 && id.charAt(0) == '*') {
082            return FuzzyBlockState.builder().type(BlockTypes.AIR).build();
083        }
084        String mutableId;
085        mutableId = id.toLowerCase();
086        BlockType type = BlockTypes.get(mutableId);
087        if (type != null) {
088            return type.getDefaultState();
089        }
090        if (Character.isDigit(mutableId.charAt(0))) {
091            String[] split = mutableId.split(":");
092            if (MathMan.isInteger(split[0])) {
093                if (split.length == 2) {
094                    if (MathMan.isInteger(split[1])) {
095                        return get(Integer.parseInt(split[0]), Integer.parseInt(split[1]));
096                    }
097                } else {
098                    return get(Integer.parseInt(split[0]));
099                }
100            }
101        }
102        try {
103            BaseBlock block = PARSER.parseFromInput(mutableId, PARSER_CONTEXT);
104            return block.toImmutableState();
105        } catch (InputParseException e) {
106            return null;
107        }
108    }
109
110}