> C castToIPropertyValue(P iproperty, Object value) {
//noinspection unchecked
return (C) value;
}
/**
* Normalizes the specified blockstate by setting meta-affecting properties which
* are not being targeted by the meta parameter to their default values.
*
* For example, block variant/color is the primary target for the meta value, so properties
* such as rotation/facing direction will be set to default values in order to nullify
* the effect that they have on the state's meta value.
*
* @param state The state to normalize
* @return The normalized block state
*/
public static IBlockState normalize(IBlockState state) {
IBlockState newState = state;
for (IProperty> property : state.getProperties().keySet()) {
Class> valueClass = property.getValueClass();
if (normalizations.containsKey(property)) {
try {
newState = newState.withProperty(
castToIProperty(property),
castToIPropertyValue(property, normalizations.get(property))
);
} catch (IllegalArgumentException ignored) {}
} else if (normalizations.containsKey(state.getValue(property))) {
try {
newState = newState.withProperty(
castToIProperty(property),
castToIPropertyValue(property, normalizations.get(state.getValue(property)))
);
} catch (IllegalArgumentException ignored) {}
} else if (normalizations.containsKey(valueClass)) {
try {
newState = newState.withProperty(
castToIProperty(property),
castToIPropertyValue(property, normalizations.get(valueClass))
);
} catch (IllegalArgumentException ignored) {}
}
}
return newState;
}
/**
* Evaluate the target meta value for the specified state. The target meta value is
* most often that which is influenced by the variant/color property of the block state.
*
* @param state The state to check
* @return The target meta of the state
* @see #normalize(IBlockState)
*/
public static int stateMeta(IBlockState state) {
return state.getBlock().getMetaFromState(normalize(state));
}
private static Set getStates(@Nonnull Block block, @Nullable Integer meta) {
return block.getBlockState().getValidStates().stream()
.filter(blockstate -> meta == null || stateMeta(blockstate) == meta)
.collect(Collectors.toSet());
}
private static ImmutableSet getStateHashes(Set blockstates) {
return ImmutableSet.copyOf(
blockstates.stream()
.map(IBlockState::hashCode)
.toArray(Integer[]::new)
);
}
private static ImmutableSet getStackHashes(Set blockstates) {
//noinspection ConstantConditions
return ImmutableSet.copyOf(
blockstates.stream()
.map(state -> new ItemStack(
state.getBlock().getItemDropped(state, new Random(), 0),
state.getBlock().damageDropped(state)
))
.map(stack -> ((IItemStack) (Object) stack).getBaritoneHash())
.toArray(Integer[]::new)
);
}
public Block getBlock() {
return block;
}
public Integer getMeta() {
return noMeta ? null : meta;
}
public boolean matches(@Nonnull Block block) {
return block == this.block;
}
public boolean matches(@Nonnull IBlockState blockstate) {
Block block = blockstate.getBlock();
return block == this.block && stateHashes.contains(blockstate.hashCode());
}
public boolean matches(ItemStack stack) {
//noinspection ConstantConditions
int hash = ((IItemStack) (Object) stack).getBaritoneHash();
if (noMeta) {
hash -= stack.getItemDamage();
}
return stackHashes.contains(hash);
}
@Override
public String toString() {
return String.format("BlockOptionalMeta{block=%s,meta=%s}", block, getMeta());
}
public static IBlockState blockStateFromStack(ItemStack stack) {
//noinspection deprecation
return Block.getBlockFromItem(stack.getItem()).getStateFromMeta(stack.getMetadata());
}
public IBlockState getAnyBlockState() {
if (blockstates.size() > 0) {
return blockstates.iterator().next();
}
return null;
}
}