restore types in block states

This commit is contained in:
Julian Krings 2025-08-14 13:35:43 +02:00
parent d76affa005
commit 89fb9a8ea2
No known key found for this signature in database
GPG Key ID: 208C6E08C3B718D2
12 changed files with 82 additions and 23 deletions

View File

@ -1,19 +1,82 @@
package com.volmit.iris.core.nms.container;
import com.volmit.iris.util.json.JSONArray;
import org.jetbrains.annotations.NotNull;
import java.util.Collection;
import java.util.Set;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
public record BlockProperty(String name, String defaultValue, Set<String> value) {
public static <T> BlockProperty of(String name, T defaultValue, Collection<T> values, Function<T, String> nameFunction) {
return new BlockProperty(name, nameFunction.apply(defaultValue), values.stream().map(nameFunction).collect(Collectors.toSet()));
public final class BlockProperty {
private final String name;
private final Class<?> type;
private final Object defaultValue;
private final Set<Object> values;
private final Function<Object, String> nameFunction;
private final Function<Object, Object> jsonFunction;
public <T extends Comparable<T>> BlockProperty(
String name,
Class<T> type,
T defaultValue,
Collection<T> values,
Function<T, String> nameFunction
) {
this.name = name;
this.type = type;
this.defaultValue = defaultValue;
this.values = Collections.unmodifiableSet(new TreeSet<>(values));
this.nameFunction = (Function<Object, String>) (Object) nameFunction;
jsonFunction = type == Boolean.class || type == Integer.class ?
Function.identity() :
this.nameFunction::apply;
}
@Override
public @NotNull String toString() {
return name + "=" + defaultValue + " [" + String.join(",", value) + "]";
return name + "=" + nameFunction.apply(defaultValue) + " [" + String.join(",", names()) + "]";
}
public String name() {
return name;
}
public String defaultValue() {
return nameFunction.apply(defaultValue);
}
public List<String> names() {
return values.stream().map(nameFunction).toList();
}
public Object defaultValueAsJson() {
return jsonFunction.apply(defaultValue);
}
public JSONArray valuesAsJson() {
return new JSONArray(values.stream().map(jsonFunction).toList());
}
public String jsonType() {
if (type == Boolean.class)
return "boolean";
if (type == Integer.class)
return "integer";
return "string";
}
@Override
public boolean equals(Object obj) {
if (obj == this) return true;
if (obj == null || obj.getClass() != this.getClass()) return false;
var that = (BlockProperty) obj;
return Objects.equals(this.name, that.name) &&
Objects.equals(this.values, that.values) &&
Objects.equals(this.type, that.type);
}
@Override
public int hashCode() {
return Objects.hash(name, values, type);
}
}

View File

@ -399,9 +399,9 @@ public class SchemaBuilder {
if (!definitions.containsKey(propertiesKey)) {
JSONObject props = new JSONObject();
properties.forEach(property -> {
JSONArray values = new JSONArray();
property.value().forEach(values::put);
props.put(property.name(), new JSONObject().put("enum", values));
props.put(property.name(), new JSONObject()
.put("type", property.jsonType())
.put("enum", property.valuesAsJson()));
});
definitions.put(propertiesKey, new JSONObject()

View File

@ -64,10 +64,6 @@ public class IrisBlockData extends IrisRegistrant {
@RegistryMapBlockState("block")
@Desc("Optional properties for this block data such as 'waterlogged': true")
private KMap<String, Object> data = new KMap<>();
@RegistryMapBlockState("block")
@ArrayType(type = KMap.class)
@Desc("Optional properties for this block data such as 'waterlogged': true")
private KList<KMap<String, Object>> altData = new KList<>();
@Desc("Optional tile data for this block data")
private KMap<String, Object> tileData = new KMap<>();

View File

@ -701,7 +701,7 @@ public class NMSBinding implements INMSBinding {
}
private <T extends Comparable<T>> BlockProperty createProperty(Property<T> property, BlockState state) {
return BlockProperty.of(property.getName(), state.getValue(property), property.getPossibleValues(), property::getName);
return new BlockProperty(property.getName(), property.getValueClass(), state.getValue(property), property.getPossibleValues(), property::getName);
}
public LevelStem levelStem(RegistryAccess access, ChunkGenerator raw) {

View File

@ -704,7 +704,7 @@ public class NMSBinding implements INMSBinding {
}
private <T extends Comparable<T>> BlockProperty createProperty(Property<T> property, BlockState state) {
return BlockProperty.of(property.getName(), state.getValue(property), property.getPossibleValues(), property::getName);
return new BlockProperty(property.getName(), property.getValueClass(), state.getValue(property), property.getPossibleValues(), property::getName);
}
public LevelStem levelStem(RegistryAccess access, ChunkGenerator raw) {

View File

@ -705,7 +705,7 @@ public class NMSBinding implements INMSBinding {
}
private <T extends Comparable<T>> BlockProperty createProperty(Property<T> property, BlockState state) {
return BlockProperty.of(property.getName(), state.getValue(property), property.getPossibleValues(), property::getName);
return new BlockProperty(property.getName(), property.getValueClass(), state.getValue(property), property.getPossibleValues(), property::getName);
}
public LevelStem levelStem(RegistryAccess access, ChunkGenerator raw) {

View File

@ -723,7 +723,7 @@ public class NMSBinding implements INMSBinding {
}
private <T extends Comparable<T>> BlockProperty createProperty(Property<T> property, BlockState state) {
return BlockProperty.of(property.getName(), state.getValue(property), property.getPossibleValues(), property::getName);
return new BlockProperty(property.getName(), property.getValueClass(), state.getValue(property), property.getPossibleValues(), property::getName);
}
public LevelStem levelStem(RegistryAccess access, ChunkGenerator raw) {

View File

@ -734,7 +734,7 @@ public class NMSBinding implements INMSBinding {
}
private <T extends Comparable<T>> BlockProperty createProperty(Property<T> property, BlockState state) {
return BlockProperty.of(property.getName(), state.getValue(property), property.getPossibleValues(), property::getName);
return new BlockProperty(property.getName(), property.getValueClass(), state.getValue(property), property.getPossibleValues(), property::getName);
}
public LevelStem levelStem(RegistryAccess access, ChunkGenerator raw) {

View File

@ -731,7 +731,7 @@ public class NMSBinding implements INMSBinding {
}
private <T extends Comparable<T>> BlockProperty createProperty(Property<T> property, BlockState state) {
return BlockProperty.of(property.getName(), state.getValue(property), property.getPossibleValues(), property::getName);
return new BlockProperty(property.getName(), property.getValueClass(), state.getValue(property), property.getPossibleValues(), property::getName);
}
public LevelStem levelStem(RegistryAccess access, ChunkGenerator raw) {

View File

@ -731,7 +731,7 @@ public class NMSBinding implements INMSBinding {
}
private <T extends Comparable<T>> BlockProperty createProperty(Property<T> property, BlockState state) {
return BlockProperty.of(property.getName(), state.getValue(property), property.getPossibleValues(), property::getName);
return new BlockProperty(property.getName(), property.getValueClass(), state.getValue(property), property.getPossibleValues(), property::getName);
}
public LevelStem levelStem(RegistryAccess access, ChunkGenerator raw) {

View File

@ -731,7 +731,7 @@ public class NMSBinding implements INMSBinding {
}
private <T extends Comparable<T>> BlockProperty createProperty(Property<T> property, BlockState state) {
return BlockProperty.of(property.getName(), state.getValue(property), property.getPossibleValues(), property::getName);
return new BlockProperty(property.getName(), property.getValueClass(), state.getValue(property), property.getPossibleValues(), property::getName);
}
public LevelStem levelStem(RegistryAccess access, ChunkGenerator raw) {

View File

@ -730,7 +730,7 @@ public class NMSBinding implements INMSBinding {
}
private <T extends Comparable<T>> BlockProperty createProperty(Property<T> property, BlockState state) {
return BlockProperty.of(property.getName(), state.getValue(property), property.getPossibleValues(), property::getName);
return new BlockProperty(property.getName(), property.getValueClass(), state.getValue(property), property.getPossibleValues(), property::getName);
}
public LevelStem levelStem(RegistryAccess access, ChunkGenerator raw) {