use of net.minecraft.state.Property in project Bookshelf by Darkhax-Minecraft.
the class SerializerBlockState method readProperty.
private BlockState readProperty(BlockState state, String propName, JsonElement propValue) {
final Property blockProperty = state.getBlock().getStateDefinition().getProperty(propName);
if (blockProperty != null) {
if (propValue.isJsonPrimitive()) {
final String valueString = propValue.getAsString();
final Optional<Comparable> parsedValue = blockProperty.getValue(valueString);
if (parsedValue.isPresent()) {
try {
return state.setValue(blockProperty, parsedValue.get());
} catch (final Exception e) {
Bookshelf.LOG.error("Failed to update state for block {}. The mod that adds this block may have an issue.", state.getBlock().getRegistryName());
Bookshelf.LOG.catching(e);
throw e;
}
} else {
throw new JsonSyntaxException("The property " + propName + " with value " + valueString + " coul not be parsed!");
}
} else {
throw new JsonSyntaxException("Expected property value for " + propName + " to be primitive string. Got " + JSONUtils.getType(propValue));
}
} else {
throw new JsonSyntaxException("The property " + propName + " is not valid for block " + state.getBlock().getRegistryName());
}
}
use of net.minecraft.state.Property in project Bookshelf by Darkhax-Minecraft.
the class SerializerBlockState method write.
@Override
public JsonElement write(BlockState toWrite) {
final JsonObject json = new JsonObject();
json.add("block", Serializers.BLOCK.write(toWrite.getBlock()));
final JsonObject properties = new JsonObject();
for (final Property prop : toWrite.getProperties()) {
if (prop instanceof IntegerProperty) {
properties.addProperty(prop.getName(), (int) toWrite.getValue((IntegerProperty) prop));
} else if (prop instanceof BooleanProperty) {
properties.addProperty(prop.getName(), (boolean) toWrite.getValue((BooleanProperty) prop));
} else {
properties.addProperty(prop.getName(), prop.getName(toWrite.getValue(prop)));
}
}
json.add("properties", properties);
return json;
}
use of net.minecraft.state.Property in project Bookshelf by Darkhax-Minecraft.
the class PacketUtils method serializeBlockState.
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void serializeBlockState(PacketBuffer buffer, BlockState state) {
buffer.writeResourceLocation(state.getBlock().getRegistryName());
final Collection<Property<?>> properties = state.getProperties();
buffer.writeInt(properties.size());
for (final Property property : properties) {
buffer.writeUtf(property.getName());
buffer.writeUtf(state.getValue(property).toString());
}
}
use of net.minecraft.state.Property in project Bookshelf by Darkhax-Minecraft.
the class PacketUtils method deserializeBlockState.
@SuppressWarnings({ "rawtypes", "unchecked" })
public static BlockState deserializeBlockState(PacketBuffer buffer) {
final ResourceLocation id = buffer.readResourceLocation();
final Block block = ForgeRegistries.BLOCKS.getValue(id);
if (block != null) {
final int size = buffer.readInt();
BlockState state = block.defaultBlockState();
for (int i = 0; i < size; i++) {
final String propName = buffer.readUtf();
final String value = buffer.readUtf();
// Check the block for the property. Keys = property names.
final Property blockProperty = block.getStateDefinition().getProperty(propName);
if (blockProperty != null) {
// Attempt to parse the value with the the property.
final Optional<Comparable> propValue = blockProperty.getValue(value);
if (propValue.isPresent()) {
// Update the state with the new property.
try {
state = state.setValue(blockProperty, propValue.get());
} catch (final Exception e) {
Bookshelf.LOG.error("Failed to read state for block {}. The mod that adds this block has issues.", block.getRegistryName());
Bookshelf.LOG.catching(e);
}
}
}
}
return state;
}
return Blocks.AIR.defaultBlockState();
}
Aggregations