aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/shcm/shsupercm/fabric/citresewn/cit/ActiveCITs.java
blob: c7ca38c4b26a307101b68a6393f3318094864f33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package shcm.shsupercm.fabric.citresewn.cit;

import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.resource.ResourceManager;
import net.minecraft.util.profiler.Profiler;
import shcm.shsupercm.fabric.citresewn.api.CITDisposable;
import shcm.shsupercm.fabric.citresewn.api.CITTypeContainer;
import shcm.shsupercm.fabric.citresewn.pack.GlobalProperties;
import shcm.shsupercm.fabric.citresewn.pack.PackParser;

import java.util.*;

public class ActiveCITs implements CITDisposable { private ActiveCITs() {}
    private static ActiveCITs active = null;

    public static ActiveCITs getActive() {
        return active;
    }

    public static boolean isActive() {
        return active != null;
    }

    public final GlobalProperties globalProperties = new GlobalProperties();

    public final Map<Class<? extends CITType>, List<CIT<?>>> cits = new IdentityHashMap<>();

    public static ActiveCITs load(ResourceManager resourceManager, Profiler profiler) {
        profiler.push("citresewn:disposing");
        if (active != null) {
            active.dispose();
            active = null;
        }

        ActiveCITs active = new ActiveCITs();

        profiler.swap("citresewn:load_global_properties");
        PackParser.loadGlobalProperties(resourceManager, active.globalProperties);
        active.globalProperties.callHandlers();

        profiler.swap("citresewn:load_cits");
        for (CIT<?> cit : PackParser.loadCITs(resourceManager))
            active.cits.computeIfAbsent(cit.type.getClass(), type -> new ArrayList<>()).add(cit);
        for (Map.Entry<Class<? extends CITType>, List<CIT<?>>> entry : active.cits.entrySet()) {
            entry.getValue().sort(Comparator.<CIT<?>>comparingInt(cit -> cit.weight).reversed().thenComparing(cit -> cit.propertiesIdentifier.toString()));
            for (CITTypeContainer<? extends CITType> typeContainer : CITRegistry.TYPES.values())
                if (typeContainer.type == entry.getKey()) {
                    typeContainer.loadUntyped(entry.getValue());
                    break;
                }
        }

        profiler.pop();

        return ActiveCITs.active = active;
    }

    @Override
    public void dispose() {
        for (CITDisposable disposable : FabricLoader.getInstance().getEntrypoints(CITDisposable.ENTRYPOINT, CITDisposable.class))
            disposable.dispose();

        for (CITTypeContainer<? extends CITType> typeContainer : CITRegistry.TYPES.values())
            typeContainer.unload();
    }
}