diff --git a/src/main/java/com/volmit/block/BlockState.java b/src/main/java/com/volmit/block/BlockState.java index 8fa6303ce..b19a9ad1b 100644 --- a/src/main/java/com/volmit/block/BlockState.java +++ b/src/main/java/com/volmit/block/BlockState.java @@ -12,6 +12,10 @@ import java.util.stream.Collectors; * Resolve through BlockRegistry before writing. Custom-content IDs are allowed. */ public record BlockState(ResourceId material, Map properties) { + private static final String PROPERTIES_START = "["; + private static final String PROPERTIES_END = "]"; + private static final String PROPERTY_SEPARATOR = ","; + public BlockState { Objects.requireNonNull(material, "material"); var copy = new TreeMap(); @@ -27,6 +31,24 @@ public record BlockState(ResourceId material, Map properties) { public BlockState(String material) { this(ResourceId.parse(material), Map.of()); } + /** Reads what {@link #key()} writes: {@code minecraft:oak_log} or {@code minecraft:oak_log[axis=y]}. */ + public static BlockState parse(String key) { + Objects.requireNonNull(key, "key"); + int open = key.indexOf(PROPERTIES_START); + if (open < 0) return new BlockState(key); + if (!key.endsWith(PROPERTIES_END)) throw new IllegalArgumentException("Unclosed block properties: " + key); + var properties = new TreeMap(); + var list = key.substring(open + 1, key.length() - 1); + for (var property : list.isEmpty() ? new String[0] : list.split(PROPERTY_SEPARATOR, -1)) { + int equals = property.indexOf('='); + if (equals < 0) throw new IllegalArgumentException("Block property without value: " + property + " in " + key); + if (properties.put(property.substring(0, equals), property.substring(equals + 1)) != null) { + throw new IllegalArgumentException("Block property set twice: " + property + " in " + key); + } + } + return new BlockState(ResourceId.parse(key.substring(0, open)), properties); + } + public BlockState withProperty(String name, String value) { var changed = new TreeMap<>(properties); changed.put(name, value); @@ -38,6 +60,6 @@ public record BlockState(ResourceId material, Map properties) { if (properties.isEmpty()) return material.toString(); return material + properties.entrySet().stream() .map(entry -> entry.getKey() + "=" + entry.getValue()) - .collect(Collectors.joining(",", "[", "]")); + .collect(Collectors.joining(PROPERTY_SEPARATOR, PROPERTIES_START, PROPERTIES_END)); } } diff --git a/src/test/java/com/volmit/ApiValuesTest.java b/src/test/java/com/volmit/ApiValuesTest.java index 02bbd1822..a73747256 100644 --- a/src/test/java/com/volmit/ApiValuesTest.java +++ b/src/test/java/com/volmit/ApiValuesTest.java @@ -31,6 +31,20 @@ class ApiValuesTest { assertThrows(IllegalArgumentException.class, () -> new ResourceKey<>("test:value", int.class)); } + @Test + void blockStatesParseWhatTheirKeyWrites() { + var log = new BlockState("minecraft:oak_log").withProperty("waterlogged", "false").withProperty("axis", "y"); + + assertEquals(log, BlockState.parse(log.key())); + assertEquals(log, BlockState.parse("minecraft:oak_log[waterlogged=false,axis=y]")); + assertEquals(new BlockState("minecraft:stone"), BlockState.parse("minecraft:stone")); + assertEquals(new BlockState("minecraft:stone"), BlockState.parse("minecraft:stone[]")); + assertThrows(IllegalArgumentException.class, () -> BlockState.parse("minecraft:oak_log[axis=y")); + assertThrows(IllegalArgumentException.class, () -> BlockState.parse("minecraft:oak_log[axis]")); + assertThrows(IllegalArgumentException.class, () -> BlockState.parse("minecraft:oak_log[axis=y,axis=x]")); + assertThrows(IllegalArgumentException.class, () -> BlockState.parse("stone")); + } + @Test void moduleInfoRequiresANameAndVersion() { var info = ModuleInfo.of("example:moss", "Moss", "1.0.0");