Search in sources :

Example 1 with Node

use of org.pepsoft.worldpainter.mapexplorer.Node in project WorldPainter by Captain-Chaos.

the class MapTreeModel method getPath.

public TreePath getPath(File dir) {
    LinkedList<File> components = new LinkedList<>();
    while (dir != null) {
        components.add(0, dir);
        dir = dir.getParentFile();
    }
    // Components now contains the path's components in the correct order
    Object[] path = new Object[components.size() + 1];
    path[0] = rootNode;
    Node node = rootNode;
    int index = 1;
    for (File component : components) {
        String componentName = FileSystemNode.getName(component);
        for (Node childNode : node.getChildren()) {
            if (childNode.getName().equals(componentName)) {
                path[index++] = childNode;
                node = childNode;
                break;
            }
        }
    }
    return new TreePath(path);
}
Also used : TreePath(javax.swing.tree.TreePath) Node(org.pepsoft.worldpainter.mapexplorer.Node) File(java.io.File) LinkedList(java.util.LinkedList)

Example 2 with Node

use of org.pepsoft.worldpainter.mapexplorer.Node in project WorldPainter by Captain-Chaos.

the class NBTFileNode method loadChildren.

@Override
protected Node[] loadChildren() {
    try (InputStream in = new BufferedInputStream(new FileInputStream(file))) {
        in.mark(2);
        Tag tag;
        if ((in.read() == 0x1f) && (in.read() == 0x8b)) {
            // Gzip signature
            in.reset();
            tag = new NBTInputStream(new GZIPInputStream(in)).readTag();
        } else {
            tag = new NBTInputStream(in).readTag();
        }
        return new Node[] { new TagNode(tag) };
    } catch (IOException e) {
        throw new RuntimeException("I/O error while reading level.dat file", e);
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) NBTInputStream(org.jnbt.NBTInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) Node(org.pepsoft.worldpainter.mapexplorer.Node) NBTInputStream(org.jnbt.NBTInputStream) Tag(org.jnbt.Tag)

Example 3 with Node

use of org.pepsoft.worldpainter.mapexplorer.Node in project WorldPainter by Captain-Chaos.

the class DirectoryNode method loadChildren.

@Override
protected Node[] loadChildren() {
    File[] contents = file.listFiles(showFiles ? null : File::isDirectory);
    if (contents != null) {
        Node[] children = new Node[contents.length];
        for (int i = 0; i < contents.length; i++) {
            if (contents[i].isDirectory()) {
                for (MapRecognizer mapRecognizer : MAP_RECOGNIZERS) {
                    if (mapRecognizer.isMap(contents[i])) {
                        children[i] = mapRecognizer.getMapNode(contents[i]);
                        break;
                    }
                }
                if (children[i] == null) {
                    children[i] = new DirectoryNode(contents[i], showFiles);
                }
            } else {
                children[i] = new FileSystemNode(contents[i]);
            }
        }
        Arrays.sort(children, (node1, node2) -> COLLATOR.compare(node1.getName(), node2.getName()));
        return children;
    } else {
        return new Node[0];
    }
}
Also used : Node(org.pepsoft.worldpainter.mapexplorer.Node) MapRecognizer(org.pepsoft.worldpainter.mapexplorer.MapRecognizer) File(java.io.File)

Example 4 with Node

use of org.pepsoft.worldpainter.mapexplorer.Node in project WorldPainter by Captain-Chaos.

the class MinecraftDirectoryNode method loadChildren.

@Override
protected Node[] loadChildren() {
    File[] contents = file.listFiles();
    // Can't happen since this node is only created for existing directories
    @SuppressWarnings("ConstantConditions") List<Node> children = new ArrayList<>(contents.length);
    for (File file : contents) {
        if (file.isDirectory()) {
            children.add(new MinecraftDirectoryNode(file));
        } else if (file.isFile()) {
            String name = file.getName();
            if (regionFilenamePatternVersion1.matcher(name).matches() || regionFilenamePatternVersion2.matcher(name).matches()) {
                children.add(new RegionFileNode(file));
            } else {
                String lowercaseName = name.toLowerCase();
                if (lowercaseName.endsWith(".nbt") || lowercaseName.endsWith(".dat") || lowercaseName.endsWith(".dat_old")) {
                    children.add(new NBTFileNode(file));
                } else if (isGzip(file)) {
                    // Gamble that any gzipped file is an NBT file
                    children.add(new NBTFileNode(file));
                }
            }
        }
    }
    children.sort((node1, node2) -> COLLATOR.compare(node1.getName(), node2.getName()));
    return children.toArray(new Node[children.size()]);
}
Also used : Node(org.pepsoft.worldpainter.mapexplorer.Node) ArrayList(java.util.ArrayList) File(java.io.File)

Example 5 with Node

use of org.pepsoft.worldpainter.mapexplorer.Node in project WorldPainter by Captain-Chaos.

the class RegionFileNode method loadChildren.

@Override
protected Node[] loadChildren() {
    try {
        List<Node> chunks = new ArrayList<>();
        RegionFile regionFile = new RegionFile(file);
        for (int chunkX = 0; chunkX < 32; chunkX++) {
            for (int chunkZ = 0; chunkZ < 32; chunkZ++) {
                if (regionFile.containsChunk(chunkX, chunkZ)) {
                    chunks.add(new ChunkNode(regionFile, chunkX, chunkZ));
                }
            }
        }
        return chunks.toArray(new Node[chunks.size()]);
    } catch (IOException e) {
        throw new RuntimeException("I/O error while reading region file", e);
    }
}
Also used : RegionFile(org.pepsoft.minecraft.RegionFile) Node(org.pepsoft.worldpainter.mapexplorer.Node) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Aggregations

Node (org.pepsoft.worldpainter.mapexplorer.Node)5 File (java.io.File)3 ArrayList (java.util.ArrayList)2 IOException (java.io.IOException)1 LinkedList (java.util.LinkedList)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 TreePath (javax.swing.tree.TreePath)1 NBTInputStream (org.jnbt.NBTInputStream)1 Tag (org.jnbt.Tag)1 RegionFile (org.pepsoft.minecraft.RegionFile)1 MapRecognizer (org.pepsoft.worldpainter.mapexplorer.MapRecognizer)1