Passed
Push — main ( 6b9c84...d238e2 )
by Etienne
01:40
created

de.kleiner3.lasertag.resource.WebResourceManager   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 21
c 1
b 0
f 0
dl 0
loc 44
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A reload(ResourceManager) 0 29 3
A getWebSite(Identifier) 0 2 1
A getFabricId() 0 3 1
1
package de.kleiner3.lasertag.resource;
2
3
import de.kleiner3.lasertag.LasertagMod;
4
import de.kleiner3.lasertag.util.StringUtil;
5
import de.kleiner3.lasertag.util.Tuple;
6
import net.fabricmc.fabric.api.resource.SimpleSynchronousResourceReloadListener;
7
import net.minecraft.resource.Resource;
8
import net.minecraft.resource.ResourceManager;
9
import net.minecraft.util.Identifier;
10
11
import java.util.ArrayList;
12
import java.util.HashMap;
13
import java.util.List;
14
import java.util.Map;
15
16
/**
17
 * Manages all local web site resources
18
 *
19
 * @author Étienne Muser
20
 */
21
public class WebResourceManager implements SimpleSynchronousResourceReloadListener {
22
    private static final String[] fileEndings = new String[] { ".html", ".js", ".css", ".png", ".svg", ".jgp", "htm", ".ts", ".ico"};
23
24
    private static final Map<Identifier, List<Tuple<Identifier, Resource>>> resourcesMap = new HashMap<>();
25
26
    @Override
27
    public Identifier getFabricId() {
28
        return new Identifier(LasertagMod.ID, "lasertag_web_resource_manager");
29
    }
30
31
    @Override
32
    public void reload(ResourceManager manager) {
33
        var resources = manager.findResources("web", path -> StringUtil.stringEndsWithList(path.getPath(), fileEndings));
34
35
        for (var resourceTuple : resources.entrySet()) {
36
            // Get key
37
            var key = resourceTuple.getKey();
38
39
            // Get path
40
            var path = key.getPath();
41
42
            // Split
43
            var split = StringUtil.splitAtNthChar(path, '/', 2);
44
45
            // Create map key
46
            var resourceKey = new Identifier(split[0]);
47
48
            // Create sub path
49
            var subPath = new Identifier(split[1]);
50
51
            // Create new list if not exist
52
            if (!resourcesMap.containsKey(resourceKey)) {
53
                resourcesMap.put(resourceKey, new ArrayList<>());
54
            }
55
56
            // Get web site
57
            var webSite = resourcesMap.get(resourceKey);
58
59
            webSite.add(new Tuple<>(subPath, resourceTuple.getValue()));
60
        }
61
    }
62
63
    public List<Tuple<Identifier, Resource>> getWebSite(Identifier id) {
64
        return resourcesMap.get(id);
65
    }
66
}
67