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
034import java.util.Map;
035
036/**
037 * {@link BlockState} related utility methods
038 */
039public final class BlockUtil {
040
041    private static final ParserContext PARSER_CONTEXT = new ParserContext();
042    private static final InputParser<BaseBlock> PARSER;
043
044    static {
045        PARSER_CONTEXT.setRestricted(false);
046        PARSER_CONTEXT.setPreferringWildcard(false);
047        PARSER_CONTEXT.setTryLegacy(true);
048        PARSER = WorldEdit.getInstance().getBlockFactory().getParsers().get(0);
049    }
050
051    private BlockUtil() {
052    }
053
054    /**
055     * Get a {@link BlockState} from a legacy id
056     *
057     * @param id Legacy ID
058     * @return Block state, or {@code null}
059     */
060    public static @Nullable BlockState get(final int id) {
061        return LegacyMapper.getInstance().getBlockFromLegacy(id);
062    }
063
064    /**
065     * Get a {@link BlockState} from a legacy id-data pair
066     *
067     * @param id   Legacy ID
068     * @param data Legacy data
069     * @return Block state, or {@code null}
070     */
071    public static @Nullable BlockState get(final int id, final int data) {
072        return LegacyMapper.getInstance().getBlockFromLegacy(id, data);
073    }
074
075    /**
076     * Get a {@link BlockState} from its ID
077     *
078     * @param id String or integer ID
079     * @return Parsed block state, or {@code null} if none
080     *         could be parsed
081     */
082    public static @Nullable BlockState get(final @NonNull String id) {
083        if (id.length() == 1 && id.charAt(0) == '*') {
084            return FuzzyBlockState.builder().type(BlockTypes.AIR).build();
085        }
086        String mutableId;
087        mutableId = id.toLowerCase();
088        BlockType type = BlockTypes.get(mutableId);
089        if (type != null) {
090            return type.getDefaultState();
091        }
092        if (Character.isDigit(mutableId.charAt(0))) {
093            String[] split = mutableId.split(":");
094            if (MathMan.isInteger(split[0])) {
095                if (split.length == 2) {
096                    if (MathMan.isInteger(split[1])) {
097                        return get(Integer.parseInt(split[0]), Integer.parseInt(split[1]));
098                    }
099                } else {
100                    return get(Integer.parseInt(split[0]));
101                }
102            }
103        }
104        try {
105            BaseBlock block = PARSER.parseFromInput(mutableId, PARSER_CONTEXT);
106            return block.toImmutableState();
107        } catch (InputParseException e) {
108            return null;
109        }
110    }
111
112    /**
113     * Parse a comma delimited list of block states
114     *
115     * @param commaDelimited List of block states
116     * @return Parsed block states
117     * @deprecated Unused internally. Scheduled for removal in next major release.
118     */
119    @Deprecated(forRemoval = true, since = "6.11.1")
120    public static @NonNull BlockState[] parse(final @NonNull String commaDelimited) {
121        final String[] split = commaDelimited.split(",(?![^\\(\\[]*[\\]\\)])");
122        final BlockState[] result = new BlockState[split.length];
123        for (int i = 0; i < split.length; i++) {
124            result[i] = get(split[i]);
125        }
126        return result;
127    }
128
129    /**
130     * Deserialize a serialized {@link BlockState}
131     *
132     * @param map Serialized block state
133     * @return Deserialized block state, or {@code null} if the map is
134     *         not a properly serialized block state
135     * @deprecated Unused internally. Scheduled for removal in next major release.
136     */
137    @Deprecated(forRemoval = true, since = "6.11.1")
138    public static @Nullable BlockState deserialize(final @NonNull Map<String, Object> map) {
139        if (map.containsKey("material")) {
140            final Object object = map.get("material");
141            return get(object.toString());
142        }
143        return null;
144    }
145
146}