-
-
Save MichaelPriebe/c4c9c24e37ef25073f3dbf43946a5cd3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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