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.world.item.ItemType;
022import com.sk89q.worldedit.world.item.ItemTypes;
023import com.sk89q.worldedit.world.registry.LegacyMapper;
024
025import java.util.Locale;
026
027public final class ItemUtil {
028
029    private ItemUtil() {
030    }
031
032    public static ItemType get(String input) {
033        if (input == null || input.isEmpty()) {
034            return ItemTypes.AIR;
035        }
036        input = input.toLowerCase(Locale.ROOT);
037        if (Character.isDigit(input.charAt(0))) {
038            String[] split = input.split(":");
039            if (MathMan.isInteger(split[0])) {
040                if (split.length == 2) {
041                    if (MathMan.isInteger(split[1])) {
042                        return LegacyMapper.getInstance()
043                                .getItemFromLegacy(
044                                        Integer.parseInt(split[0]),
045                                        Integer.parseInt(split[1])
046                                );
047                    }
048                } else {
049                    return LegacyMapper.getInstance().getItemFromLegacy(Integer.parseInt(split[0]));
050                }
051            }
052        }
053        if (!input.split("\\[", 2)[0].contains(":")) {
054            input = "minecraft:" + input;
055        }
056        return ItemTypes.get(input);
057    }
058
059    public static final ItemType[] parse(String commaDelimited) {
060        String[] split = commaDelimited.split(",(?![^\\(\\[]*[\\]\\)])");
061        ItemType[] result = new ItemType[split.length];
062        for (int i = 0; i < split.length; i++) {
063            result[i] = get(split[i]);
064        }
065        return result;
066    }
067
068}