meme fix, things work now

This commit is contained in:
Leijurv 2019-09-30 17:57:09 -07:00
parent d256f05787
commit ae9671bff0
No known key found for this signature in database
GPG Key ID: 44A3EA646EADAC6A
1 changed files with 9 additions and 30 deletions

View File

@ -30,39 +30,29 @@ import net.minecraft.util.registry.IRegistry;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.Collections; import java.util.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.regex.MatchResult; import java.util.regex.MatchResult;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors;
public final class BlockOptionalMeta { public final class BlockOptionalMeta {
private final Block block; private final Block block;
private final int meta;
private final boolean noMeta;
private final Set<IBlockState> blockstates; private final Set<IBlockState> blockstates;
private final ImmutableSet<Integer> stateHashes; private final ImmutableSet<Integer> stateHashes;
private final ImmutableSet<Integer> stackHashes; private final ImmutableSet<Integer> stackHashes;
private static final Pattern pattern = Pattern.compile("^(.+?)(?::(\\d+))?$"); private static final Pattern pattern = Pattern.compile("^(.+?)(?::(\\d+))?$");
private static final Map<Object, Object> normalizations; private static final Map<Object, Object> normalizations;
public BlockOptionalMeta(@Nonnull Block block, @Nullable Integer meta) { public BlockOptionalMeta(@Nonnull Block block) {
this.block = block; this.block = block;
this.noMeta = meta == null; this.blockstates = getStates(block);
this.meta = noMeta ? 0 : meta;
this.blockstates = getStates(block, meta);
this.stateHashes = getStateHashes(blockstates); this.stateHashes = getStateHashes(blockstates);
this.stackHashes = getStackHashes(blockstates); this.stackHashes = getStackHashes(blockstates);
} }
public BlockOptionalMeta(@Nonnull Block block) {
this(block, null);
}
public BlockOptionalMeta(@Nonnull String selector) { public BlockOptionalMeta(@Nonnull String selector) {
Matcher matcher = pattern.matcher(selector); Matcher matcher = pattern.matcher(selector);
@ -71,7 +61,6 @@ public final class BlockOptionalMeta {
} }
MatchResult matchResult = matcher.toMatchResult(); MatchResult matchResult = matcher.toMatchResult();
noMeta = matchResult.group(2) == null;
ResourceLocation id = new ResourceLocation(matchResult.group(1)); ResourceLocation id = new ResourceLocation(matchResult.group(1));
@ -80,8 +69,7 @@ public final class BlockOptionalMeta {
} }
block = IRegistry.BLOCK.get(id); block = IRegistry.BLOCK.get(id);
meta = noMeta ? 0 : Integer.parseInt(matchResult.group(2)); blockstates = getStates(block);
blockstates = getStates(block, getMeta());
stateHashes = getStateHashes(blockstates); stateHashes = getStateHashes(blockstates);
stackHashes = getStackHashes(blockstates); stackHashes = getStackHashes(blockstates);
} }
@ -219,14 +207,11 @@ public final class BlockOptionalMeta {
} }
public static int stateMeta(IBlockState state) { public static int stateMeta(IBlockState state) {
throw new UnsupportedOperationException(); return state.hashCode();
} }
private static Set<IBlockState> getStates(@Nonnull Block block, @Nullable Integer meta) { private static Set<IBlockState> getStates(@Nonnull Block block) {
throw new UnsupportedOperationException(); return new HashSet<>(block.getStateContainer().getValidStates());
/*return block.getBlockState().getValidStates().stream()
.filter(blockstate -> meta == null || stateMeta(blockstate) == meta)
.collect(Collectors.toSet());*/
} }
private static ImmutableSet<Integer> getStateHashes(Set<IBlockState> blockstates) { private static ImmutableSet<Integer> getStateHashes(Set<IBlockState> blockstates) {
@ -255,10 +240,6 @@ public final class BlockOptionalMeta {
return block; return block;
} }
public Integer getMeta() {
return noMeta ? null : meta;
}
public boolean matches(@Nonnull Block block) { public boolean matches(@Nonnull Block block) {
return block == this.block; return block == this.block;
} }
@ -272,16 +253,14 @@ public final class BlockOptionalMeta {
//noinspection ConstantConditions //noinspection ConstantConditions
int hash = ((IItemStack) (Object) stack).getBaritoneHash(); int hash = ((IItemStack) (Object) stack).getBaritoneHash();
if (noMeta) {
hash -= stack.getDamage(); hash -= stack.getDamage();
}
return stackHashes.contains(hash); return stackHashes.contains(hash);
} }
@Override @Override
public String toString() { public String toString() {
return String.format("BlockOptionalMeta{block=%s,meta=%s}", block, getMeta()); return String.format("BlockOptionalMeta{block=%s}", block);
} }
public IBlockState getAnyBlockState() { public IBlockState getAnyBlockState() {