Passed
Push — main ( 6d86fc...b981dd )
by Etienne
01:40
created

getFolder(Identifier)   A

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
dl 0
loc 4
rs 10
1
package de.kleiner3.lasertag.resource;
2
3
import de.kleiner3.lasertag.LasertagMod;
4
import de.kleiner3.lasertag.common.util.StringUtil;
5
import net.fabricmc.fabric.api.resource.SimpleSynchronousResourceReloadListener;
6
import net.minecraft.resource.Resource;
7
import net.minecraft.resource.ResourceManager;
8
import net.minecraft.util.Identifier;
9
10
import java.util.HashMap;
11
import java.util.List;
12
import java.util.Map;
13
14
/**
15
 * Manages all .nbt file resources
16
 *
17
 * @author Étienne Muser
18
 */
19
public class StructureResourceManager implements SimpleSynchronousResourceReloadListener {
20
    //region Private fields
21
22
    private final Map<Identifier, Resource> structureResources = new HashMap<>();
23
24
    public static final String LITEMATIC_FILE_ENDING = ".litematic";
25
    public static final String NBT_FILE_ENDING = ".nbt";
26
    public static final String[] FILE_ENDINGS = new String[] { NBT_FILE_ENDING, LITEMATIC_FILE_ENDING };
27
28
    //endregion
29
30
    public Resource get(Identifier id) {
31
        return structureResources.get(id);
32
    }
33
34
    public List<Map.Entry<Identifier, Resource>> getFolder(Identifier folderId) {
35
        return structureResources.entrySet().stream()
36
                .filter(entry -> entry.getKey().getPath().startsWith(folderId.getPath()))
37
                .toList();
38
    }
39
40
    @Override
41
    public Identifier getFabricId() {
42
        return new Identifier(LasertagMod.ID, "lasertag_structure_resource_manager");
43
    }
44
45
    @Override
46
    public void reload(ResourceManager manager) {
47
        var resources = manager.findResources("structures", path -> StringUtil.stringEndsWithList(path.getPath(), FILE_ENDINGS));
48
        for(var entry : resources.entrySet()) {
49
            if (!entry.getKey().getNamespace().equals(LasertagMod.ID)) {
50
                continue;
51
            }
52
53
            structureResources.put(entry.getKey(), entry.getValue());
54
        }
55
    }
56
}
57