All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.valaphee.netcode.mcbe.pack.Data.kt Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2021-2022, Valaphee.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.valaphee.netcode.mcbe.pack

import com.fasterxml.jackson.annotation.JsonSubTypes
import com.fasterxml.jackson.annotation.JsonTypeInfo
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.node.ObjectNode
import com.fasterxml.jackson.databind.node.TextNode
import com.fasterxml.jackson.module.kotlin.convertValue
import com.fasterxml.jackson.module.kotlin.readValue
import com.valaphee.netcode.mcbe.world.block.Block
import com.valaphee.netcode.mcbe.world.item.crafting.BrewingContainerRecipe
import com.valaphee.netcode.mcbe.world.item.crafting.BrewingMixRecipe
import com.valaphee.netcode.mcbe.world.item.crafting.FurnaceRecipe
import com.valaphee.netcode.mcbe.world.item.crafting.MaterialReductionRecipe
import com.valaphee.netcode.mcbe.world.item.crafting.ShapedRecipe
import com.valaphee.netcode.mcbe.world.item.crafting.ShapelessRecipe
import java.io.File

/**
 * @author Kevin Ludwig
 */
@JsonTypeInfo(
    use = JsonTypeInfo.Id.NAME,
    include = JsonTypeInfo.As.WRAPPER_OBJECT
)
@JsonSubTypes(
    JsonSubTypes.Type(Block::class),
    JsonSubTypes.Type(ShapedRecipe::class),
    JsonSubTypes.Type(ShapelessRecipe::class),
    JsonSubTypes.Type(FurnaceRecipe::class),
    JsonSubTypes.Type(BrewingMixRecipe::class),
    JsonSubTypes.Type(BrewingContainerRecipe::class),
    JsonSubTypes.Type(MaterialReductionRecipe::class)
)
interface Data

inline fun  ObjectMapper.readData(file: File): Pair {
    val data = readValue>(file)
    val version = data.remove("format_version") as? String
    return convertValue(data) to version
}

fun ObjectMapper.writeData(file: File, data: Data, version: String) {
    writeValue(file, valueToTree(data).apply { set("format_version", TextNode(version)) })
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy