baritone/src/api/java/baritone/api/utils/BlockOptionalMetaLookup.java

98 lines
3.1 KiB
Java
Raw Permalink Normal View History

2019-09-04 16:16:36 +00:00
/*
* This file is part of Baritone.
*
* Baritone is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Baritone is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
2019-08-30 22:38:15 +00:00
package baritone.api.utils;
2023-06-16 23:40:19 +00:00
import baritone.api.utils.accessor.IItemStack;
2022-12-28 05:42:38 +00:00
import com.google.common.collect.ImmutableSet;
2019-08-30 22:38:15 +00:00
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
2019-09-01 03:43:03 +00:00
import net.minecraft.item.ItemStack;
2019-08-30 22:38:15 +00:00
import java.util.Arrays;
2022-12-24 19:11:26 +00:00
import java.util.HashSet;
2019-08-30 22:38:15 +00:00
import java.util.List;
2022-12-24 19:11:26 +00:00
import java.util.Set;
import java.util.stream.Stream;
2019-08-30 22:38:15 +00:00
public class BlockOptionalMetaLookup {
2023-06-16 23:40:19 +00:00
private final ImmutableSet<Block> blockSet;
private final ImmutableSet<IBlockState> blockStateSet;
private final ImmutableSet<Integer> stackHashes;
private final BlockOptionalMeta[] boms;
2019-08-30 22:38:15 +00:00
public BlockOptionalMetaLookup(BlockOptionalMeta... boms) {
this.boms = boms;
2022-12-28 05:42:38 +00:00
Set<Block> blocks = new HashSet<>();
Set<IBlockState> blockStates = new HashSet<>();
Set<Integer> stacks = new HashSet<>();
2022-12-24 19:11:26 +00:00
for (BlockOptionalMeta bom : boms) {
2022-12-28 05:42:38 +00:00
blocks.add(bom.getBlock());
blockStates.addAll(bom.getAllBlockStates());
stacks.addAll(bom.stackHashes());
2022-12-24 19:11:26 +00:00
}
2022-12-28 05:42:38 +00:00
this.blockSet = ImmutableSet.copyOf(blocks);
this.blockStateSet = ImmutableSet.copyOf(blockStates);
2023-06-16 23:40:19 +00:00
this.stackHashes = ImmutableSet.copyOf(stacks);
2019-08-30 22:38:15 +00:00
}
public BlockOptionalMetaLookup(Block... blocks) {
2022-12-28 05:42:38 +00:00
this(Stream.of(blocks)
2019-09-19 19:53:15 +00:00
.map(BlockOptionalMeta::new)
2022-12-28 05:42:38 +00:00
.toArray(BlockOptionalMeta[]::new));
2019-08-30 22:38:15 +00:00
}
public BlockOptionalMetaLookup(List<Block> blocks) {
2022-12-28 05:42:38 +00:00
this(blocks.stream()
2019-09-19 19:53:15 +00:00
.map(BlockOptionalMeta::new)
2022-12-28 05:42:38 +00:00
.toArray(BlockOptionalMeta[]::new));
2019-08-30 22:38:15 +00:00
}
2019-09-06 10:59:10 +00:00
public BlockOptionalMetaLookup(String... blocks) {
2022-12-28 05:42:38 +00:00
this(Stream.of(blocks)
2019-09-19 19:53:15 +00:00
.map(BlockOptionalMeta::new)
2022-12-28 05:42:38 +00:00
.toArray(BlockOptionalMeta[]::new));
2019-09-06 10:59:10 +00:00
}
public boolean has(Block block) {
2022-12-24 19:11:26 +00:00
return blockSet.contains(block);
2019-08-30 22:38:15 +00:00
}
public boolean has(IBlockState state) {
2022-12-24 19:11:26 +00:00
return blockStateSet.contains(state);
2019-08-30 22:38:15 +00:00
}
2019-09-01 03:43:03 +00:00
public boolean has(ItemStack stack) {
2023-06-16 23:40:19 +00:00
int hash = ((IItemStack) (Object) stack).getBaritoneHash();
return stackHashes.contains(hash)
|| stackHashes.contains(hash - stack.getItemDamage());
2019-09-01 03:43:03 +00:00
}
public List<BlockOptionalMeta> blocks() {
2019-09-19 21:38:13 +00:00
return Arrays.asList(boms);
}
@Override
public String toString() {
return String.format(
2019-09-19 19:53:15 +00:00
"BlockOptionalMetaLookup{%s}",
Arrays.toString(boms)
);
2019-08-30 22:38:15 +00:00
}
}