baritone/src/api/java/baritone/api/utils/BlockOptionalMeta.java

115 lines
3.8 KiB
Java
Raw Normal View History

2019-08-30 22:38:15 +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/>.
*/
package baritone.api.utils;
2019-08-31 17:28:01 +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;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
2019-08-31 17:28:01 +00:00
import net.minecraft.util.Rotation;
2019-08-30 22:38:15 +00:00
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
2019-08-31 17:28:01 +00:00
import java.util.ArrayList;
import java.util.List;
2019-08-30 22:38:15 +00:00
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
2019-08-31 17:28:01 +00:00
import java.util.stream.Collectors;
2019-08-30 22:38:15 +00:00
import static java.util.Objects.isNull;
public final class BlockOptionalMeta {
private final Block block;
private final int meta;
private final boolean noMeta;
2019-08-31 17:28:01 +00:00
private final ImmutableSet<Integer> stateMetas;
2019-08-30 22:38:15 +00:00
private static final Pattern pattern = Pattern.compile("^(.+?)(?::(\\d+))?$");
2019-08-31 17:28:01 +00:00
private static ImmutableSet<Integer> getStateMetas(@Nonnull Block block, @Nullable Integer meta) {
List<IBlockState> blockstates = block.getBlockState().getValidStates();
return ImmutableSet.copyOf(
(ArrayList<Integer>) blockstates.stream()
.filter(blockstate -> meta == null || block.getMetaFromState(blockstate.withRotation(Rotation.NONE)) == meta)
.map(IBlockState::hashCode)
.collect(Collectors.toCollection(ArrayList::new))
);
}
2019-08-30 22:38:15 +00:00
public BlockOptionalMeta(@Nonnull Block block, @Nullable Integer meta) {
this.block = block;
this.noMeta = isNull(meta);
this.meta = noMeta ? 0 : meta;
2019-08-31 17:28:01 +00:00
this.stateMetas = getStateMetas(block, meta);
2019-08-30 22:38:15 +00:00
}
public BlockOptionalMeta(@Nonnull Block block) {
this(block, null);
}
public BlockOptionalMeta(@Nonnull String selector) {
Matcher matcher = pattern.matcher(selector);
if (!matcher.find()) {
throw new IllegalArgumentException("invalid block selector");
}
MatchResult matchResult = matcher.toMatchResult();
noMeta = matchResult.group(2) == null;
2019-08-30 22:38:15 +00:00
ResourceLocation id = new ResourceLocation(matchResult.group(1));
if (!Block.REGISTRY.containsKey(id)) {
throw new IllegalArgumentException("Invalid block ID");
}
block = Block.REGISTRY.getObject(id);
meta = noMeta ? 0 : Integer.parseInt(matchResult.group(2));
2019-08-31 17:28:01 +00:00
stateMetas = getStateMetas(block, noMeta ? null : meta);
2019-08-30 22:38:15 +00:00
}
public Block getBlock() {
return block;
}
public Integer getMeta() {
return meta;
}
public boolean matches(@Nonnull Block block) {
return block == this.block;
2019-08-30 22:38:15 +00:00
}
public boolean matches(@Nonnull IBlockState blockstate) {
Block block = blockstate.getBlock();
2019-08-31 17:28:01 +00:00
return block == this.block && stateMetas.contains(blockstate.hashCode());
2019-08-30 22:38:15 +00:00
}
@Override
public String toString() {
return String.format("BlockOptionalMeta{block=%s,meta=%s}", block, meta);
}
public static IBlockState blockStateFromStack(ItemStack stack) {
//noinspection deprecation
return Block.getBlockFromItem(stack.getItem()).getStateFromMeta(stack.getMetadata());
}
}