mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2025-08-16 08:15:50 +00:00
restore types in block states
This commit is contained in:
parent
d76affa005
commit
89fb9a8ea2
@ -1,19 +1,82 @@
|
|||||||
package com.volmit.iris.core.nms.container;
|
package com.volmit.iris.core.nms.container;
|
||||||
|
|
||||||
|
import com.volmit.iris.util.json.JSONArray;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.*;
|
||||||
import java.util.Set;
|
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
public record BlockProperty(String name, String defaultValue, Set<String> value) {
|
public final class BlockProperty {
|
||||||
public static <T> BlockProperty of(String name, T defaultValue, Collection<T> values, Function<T, String> nameFunction) {
|
private final String name;
|
||||||
return new BlockProperty(name, nameFunction.apply(defaultValue), values.stream().map(nameFunction).collect(Collectors.toSet()));
|
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
|
@Override
|
||||||
public @NotNull String toString() {
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -399,9 +399,9 @@ public class SchemaBuilder {
|
|||||||
if (!definitions.containsKey(propertiesKey)) {
|
if (!definitions.containsKey(propertiesKey)) {
|
||||||
JSONObject props = new JSONObject();
|
JSONObject props = new JSONObject();
|
||||||
properties.forEach(property -> {
|
properties.forEach(property -> {
|
||||||
JSONArray values = new JSONArray();
|
props.put(property.name(), new JSONObject()
|
||||||
property.value().forEach(values::put);
|
.put("type", property.jsonType())
|
||||||
props.put(property.name(), new JSONObject().put("enum", values));
|
.put("enum", property.valuesAsJson()));
|
||||||
});
|
});
|
||||||
|
|
||||||
definitions.put(propertiesKey, new JSONObject()
|
definitions.put(propertiesKey, new JSONObject()
|
||||||
|
@ -64,10 +64,6 @@ public class IrisBlockData extends IrisRegistrant {
|
|||||||
@RegistryMapBlockState("block")
|
@RegistryMapBlockState("block")
|
||||||
@Desc("Optional properties for this block data such as 'waterlogged': true")
|
@Desc("Optional properties for this block data such as 'waterlogged': true")
|
||||||
private KMap<String, Object> data = new KMap<>();
|
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")
|
@Desc("Optional tile data for this block data")
|
||||||
private KMap<String, Object> tileData = new KMap<>();
|
private KMap<String, Object> tileData = new KMap<>();
|
||||||
|
|
||||||
|
@ -701,7 +701,7 @@ public class NMSBinding implements INMSBinding {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private <T extends Comparable<T>> BlockProperty createProperty(Property<T> property, BlockState state) {
|
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) {
|
public LevelStem levelStem(RegistryAccess access, ChunkGenerator raw) {
|
||||||
|
@ -704,7 +704,7 @@ public class NMSBinding implements INMSBinding {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private <T extends Comparable<T>> BlockProperty createProperty(Property<T> property, BlockState state) {
|
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) {
|
public LevelStem levelStem(RegistryAccess access, ChunkGenerator raw) {
|
||||||
|
@ -705,7 +705,7 @@ public class NMSBinding implements INMSBinding {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private <T extends Comparable<T>> BlockProperty createProperty(Property<T> property, BlockState state) {
|
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) {
|
public LevelStem levelStem(RegistryAccess access, ChunkGenerator raw) {
|
||||||
|
@ -723,7 +723,7 @@ public class NMSBinding implements INMSBinding {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private <T extends Comparable<T>> BlockProperty createProperty(Property<T> property, BlockState state) {
|
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) {
|
public LevelStem levelStem(RegistryAccess access, ChunkGenerator raw) {
|
||||||
|
@ -734,7 +734,7 @@ public class NMSBinding implements INMSBinding {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private <T extends Comparable<T>> BlockProperty createProperty(Property<T> property, BlockState state) {
|
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) {
|
public LevelStem levelStem(RegistryAccess access, ChunkGenerator raw) {
|
||||||
|
@ -731,7 +731,7 @@ public class NMSBinding implements INMSBinding {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private <T extends Comparable<T>> BlockProperty createProperty(Property<T> property, BlockState state) {
|
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) {
|
public LevelStem levelStem(RegistryAccess access, ChunkGenerator raw) {
|
||||||
|
@ -731,7 +731,7 @@ public class NMSBinding implements INMSBinding {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private <T extends Comparable<T>> BlockProperty createProperty(Property<T> property, BlockState state) {
|
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) {
|
public LevelStem levelStem(RegistryAccess access, ChunkGenerator raw) {
|
||||||
|
@ -731,7 +731,7 @@ public class NMSBinding implements INMSBinding {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private <T extends Comparable<T>> BlockProperty createProperty(Property<T> property, BlockState state) {
|
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) {
|
public LevelStem levelStem(RegistryAccess access, ChunkGenerator raw) {
|
||||||
|
@ -730,7 +730,7 @@ public class NMSBinding implements INMSBinding {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private <T extends Comparable<T>> BlockProperty createProperty(Property<T> property, BlockState state) {
|
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) {
|
public LevelStem levelStem(RegistryAccess access, ChunkGenerator raw) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user