Search in sources :

Example 1 with Property

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());
    }
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) BooleanProperty(net.minecraft.state.BooleanProperty) IntegerProperty(net.minecraft.state.IntegerProperty) Property(net.minecraft.state.Property) JsonParseException(com.google.gson.JsonParseException) JsonSyntaxException(com.google.gson.JsonSyntaxException)

Example 2 with Property

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;
}
Also used : IntegerProperty(net.minecraft.state.IntegerProperty) BooleanProperty(net.minecraft.state.BooleanProperty) JsonObject(com.google.gson.JsonObject) BooleanProperty(net.minecraft.state.BooleanProperty) IntegerProperty(net.minecraft.state.IntegerProperty) Property(net.minecraft.state.Property)

Example 3 with Property

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());
    }
}
Also used : Property(net.minecraft.state.Property)

Example 4 with Property

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();
}
Also used : BlockState(net.minecraft.block.BlockState) ResourceLocation(net.minecraft.util.ResourceLocation) Block(net.minecraft.block.Block) Property(net.minecraft.state.Property)

Aggregations

Property (net.minecraft.state.Property)4 BooleanProperty (net.minecraft.state.BooleanProperty)2 IntegerProperty (net.minecraft.state.IntegerProperty)2 JsonObject (com.google.gson.JsonObject)1 JsonParseException (com.google.gson.JsonParseException)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 Block (net.minecraft.block.Block)1 BlockState (net.minecraft.block.BlockState)1 ResourceLocation (net.minecraft.util.ResourceLocation)1