Skip to content

Instantly share code, notes, and snippets.

@MichaelPriebe
Created February 7, 2021 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MichaelPriebe/c4c9c24e37ef25073f3dbf43946a5cd3 to your computer and use it in GitHub Desktop.
Save MichaelPriebe/c4c9c24e37ef25073f3dbf43946a5cd3 to your computer and use it in GitHub Desktop.
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
public class BlockStateTest {
public static void main(String[] args) {
BlockState state = new BlockState(
1965,
new EnumBlockStateProperty("facing", HorizontalFacingProperty.NORTH),
new EnumBlockStateProperty("half", HalfProperty.BOTTOM),
new EnumBlockStateProperty("shape", ShapeProperty.STRAIGHT),
new BooleanBlockStateProperty("waterlogged", false)
);
}
}
class BlockState {
final double defaultState;
BlockState(double defaultState, BlockStateProperty... properties) {
this.defaultState = defaultState;
List<Set<String>> values = new ArrayList<>();
List<String> defaults = new ArrayList<>();
for (final BlockStateProperty property : properties) {
values.add(ImmutableSet.copyOf(property.values));
defaults.add(property.value);
}
final Set<List<String>> states = Sets.cartesianProduct(values);
double offset = defaultState;
for (final List<String> state : states) {
if (state.equals(defaults)) break;
offset--;
}
for (final List<String> state : states) {
System.out.println(offset + " " + state);
offset++;
}
}
}
class BlockStateProperty {
final String name;
final String value;
final String[] values;
BlockStateProperty(String name, String value, String[] values) {
this.name = name;
this.value = value;
this.values = values;
}
}
class BooleanBlockStateProperty extends BlockStateProperty {
BooleanBlockStateProperty(String name, boolean value) {
super(name, value ? "true" : "false", new String[]{"true", "false"});
}
}
class EnumBlockStateProperty extends BlockStateProperty {
EnumBlockStateProperty(String name, Enum<?> value) {
super(name, value.name().toLowerCase(),
Arrays.stream(value.getClass().getEnumConstants())
.map(e -> e.name().toLowerCase())
.toArray(String[]::new)
);
}
}
enum HorizontalFacingProperty {
NORTH,
SOUTH,
WEST,
EAST;
}
enum HalfProperty {
TOP,
BOTTOM;
}
enum ShapeProperty {
STRAIGHT,
INNER_LEFT,
INNER_RIGHT,
OUTER_LEFT,
OUTER_RIGHT;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment