Search in sources :

Example 1 with BehaviorTreeFormat

use of org.terasology.logic.behavior.asset.BehaviorTreeFormat in project Terasology by MovingBlocks.

the class BehaviorSystem method save.

public void save(BehaviorTree tree) {
    Path savePath;
    ResourceUrn uri = tree.getUrn();
    if (BEHAVIORS.equals(uri.getModuleName())) {
        savePath = PathManager.getInstance().getHomeModPath().resolve(BEHAVIORS.toString()).resolve("assets").resolve("behaviors");
    } else {
        Path overridesPath = PathManager.getInstance().getHomeModPath().resolve(BEHAVIORS.toString()).resolve("overrides");
        savePath = overridesPath.resolve(uri.getModuleName().toString()).resolve("behaviors");
    }
    BehaviorTreeFormat loader = new BehaviorTreeFormat();
    try {
        Files.createDirectories(savePath);
        Path file = savePath.resolve(uri.getResourceName() + ".behavior");
        try (FileOutputStream fos = new FileOutputStream(file.toFile())) {
            loader.save(fos, tree.getData());
        }
    } catch (IOException e) {
        throw new RuntimeException("Cannot save asset " + uri + " to " + savePath, e);
    }
}
Also used : Path(java.nio.file.Path) BehaviorTreeFormat(org.terasology.logic.behavior.asset.BehaviorTreeFormat) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) ResourceUrn(org.terasology.assets.ResourceUrn)

Example 2 with BehaviorTreeFormat

use of org.terasology.logic.behavior.asset.BehaviorTreeFormat in project Terasology by MovingBlocks.

the class BehaviorEditor method save.

public String save() {
    BehaviorTreeFormat loader = new BehaviorTreeFormat();
    ByteArrayOutputStream baos = new ByteArrayOutputStream(10000);
    try {
        loader.save(baos, tree.getData());
        return baos.toString(Charsets.UTF_8.name());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : BehaviorTreeFormat(org.terasology.logic.behavior.asset.BehaviorTreeFormat) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)2 BehaviorTreeFormat (org.terasology.logic.behavior.asset.BehaviorTreeFormat)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileOutputStream (java.io.FileOutputStream)1 Path (java.nio.file.Path)1 ResourceUrn (org.terasology.assets.ResourceUrn)1