de.pewpewproject.lasertag.resource.StructureResourceManager   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 41
rs 10
c 0
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getFolder(Identifier) 0 4 1
A reload() 0 20 1
A get(Identifier) 0 2 1
1
package de.pewpewproject.lasertag.resource;
2
3
import de.pewpewproject.lasertag.LasertagMod;
4
import de.pewpewproject.lasertag.common.util.StringUtil;
5
import net.minecraft.resource.Resource;
6
import net.minecraft.util.Identifier;
7
8
import java.io.File;
9
import java.io.IOException;
10
import java.net.URISyntaxException;
11
import java.nio.file.Files;
12
import java.nio.file.Path;
13
import java.util.HashMap;
14
import java.util.List;
15
import java.util.Map;
16
17
/**
18
 * Manages all .nbt file resources
19
 *
20
 * @author Étienne Muser
21
 */
22
public class StructureResourceManager {
23
    //region Private fields
24
25
    private final Map<Identifier, Resource> structureResources = new HashMap<>();
26
27
    public static final String LITEMATIC_FILE_ENDING = ".litematic";
28
    public static final String NBT_FILE_ENDING = ".nbt";
29
    public static final String[] FILE_ENDINGS = new String[]{NBT_FILE_ENDING, LITEMATIC_FILE_ENDING};
30
31
    //endregion
32
33
    public Resource get(Identifier id) {
34
        return structureResources.get(id);
35
    }
36
37
    public List<Map.Entry<Identifier, Resource>> getFolder(Identifier folderId) {
38
        return structureResources.entrySet().stream()
39
                .filter(entry -> entry.getKey().getPath().startsWith(folderId.getPath()))
40
                .toList();
41
    }
42
43
    public void reload() throws IOException, URISyntaxException {
44
45
        var uri = getClass().getClassLoader().getResource("data/lasertag/structures").toURI();
46
47
        structureResources.clear();
48
        try (var stream = Files.walk(Path.of(uri))) {
49
50
            stream.filter(Files::isRegularFile)
51
                    .filter(path -> StringUtil.stringEndsWithList(path.toString(), FILE_ENDINGS))
52
                    .forEach(path -> {
53
54
                        // Split the path by "structures"
55
                        var splitPath = path.toString().split("structures");
56
57
                        // Get the resource identifier id path
58
                        var resourceIdPath = ("structures" + splitPath[splitPath.length - 1]).replace(File.separatorChar, '/');
59
60
                        structureResources.put(
61
                                new Identifier(LasertagMod.ID, resourceIdPath),
62
                                new Resource(LasertagMod.ID, () -> Files.newInputStream(path)));
63
                    });
64
        }
65
    }
66
}
67