001/*
002 * This file is part of Baritone.
003 *
004 * Baritone is free software: you can redistribute it and/or modify
005 * it under the terms of the GNU Lesser General Public License as published by
006 * the Free Software Foundation, either version 3 of the License, or
007 * (at your option) any later version.
008 *
009 * Baritone is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012 * GNU Lesser General Public License for more details.
013 *
014 * You should have received a copy of the GNU Lesser General Public License
015 * along with Baritone.  If not, see <https://www.gnu.org/licenses/>.
016 */
017
018package baritone.api.utils;
019
020import baritone.api.utils.accessor.IItemStack;
021import com.google.common.collect.ImmutableSet;
022import net.minecraft.world.item.ItemStack;
023import net.minecraft.world.level.block.Block;
024import net.minecraft.world.level.block.state.BlockState;
025
026import java.util.Arrays;
027import java.util.HashSet;
028import java.util.List;
029import java.util.Set;
030import java.util.stream.Stream;
031
032public class BlockOptionalMetaLookup {
033    private final ImmutableSet<Block> blockSet;
034    private final ImmutableSet<BlockState> blockStateSet;
035    private final ImmutableSet<Integer> stackHashes;
036    private final BlockOptionalMeta[] boms;
037
038    public BlockOptionalMetaLookup(BlockOptionalMeta... boms) {
039        this.boms = boms;
040        Set<Block> blocks = new HashSet<>();
041        Set<BlockState> blockStates = new HashSet<>();
042        Set<Integer> stacks = new HashSet<>();
043        for (BlockOptionalMeta bom : boms) {
044            blocks.add(bom.getBlock());
045            blockStates.addAll(bom.getAllBlockStates());
046            stacks.addAll(bom.stackHashes());
047        }
048        this.blockSet = ImmutableSet.copyOf(blocks);
049        this.blockStateSet = ImmutableSet.copyOf(blockStates);
050        this.stackHashes = ImmutableSet.copyOf(stacks);
051    }
052
053    public BlockOptionalMetaLookup(Block... blocks) {
054        this(Stream.of(blocks)
055                .map(BlockOptionalMeta::new)
056                .toArray(BlockOptionalMeta[]::new));
057
058    }
059
060    public BlockOptionalMetaLookup(List<Block> blocks) {
061        this(blocks.stream()
062                .map(BlockOptionalMeta::new)
063                .toArray(BlockOptionalMeta[]::new));
064    }
065
066    public BlockOptionalMetaLookup(String... blocks) {
067        this(Stream.of(blocks)
068                .map(BlockOptionalMeta::new)
069                .toArray(BlockOptionalMeta[]::new));
070    }
071
072    public boolean has(Block block) {
073        return blockSet.contains(block);
074    }
075
076    public boolean has(BlockState state) {
077        return blockStateSet.contains(state);
078    }
079
080    public boolean has(ItemStack stack) {
081        int hash = ((IItemStack) (Object) stack).getBaritoneHash();
082        hash -= stack.getDamageValue();
083        return stackHashes.contains(hash);
084    }
085
086    public List<BlockOptionalMeta> blocks() {
087        return Arrays.asList(boms);
088    }
089
090    @Override
091    public String toString() {
092        return String.format(
093                "BlockOptionalMetaLookup{%s}",
094                Arrays.toString(boms)
095        );
096    }
097}