aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gregtech/api/util/GTCLSCompat.java
blob: b8259d254728484d74c670d9f49ed90fce69393a (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package gregtech.api.util;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;

import alexiil.mods.load.MinecraftDisplayer;
import alexiil.mods.load.ProgressDisplayer;
import cpw.mods.fml.common.ProgressManager;
import gregtech.GTMod;
import gregtech.api.enums.Materials;
import gregtech.common.GTProxy;
import gregtech.loaders.postload.GTPostLoad;

@SuppressWarnings("rawtypes, deprecation")
public class GTCLSCompat {

    private static Class cpwProgressBar;
    private static Field progressBarStep;

    static {
        try {
            cpwProgressBar = Class.forName("cpw.mods.fml.common.ProgressManager$ProgressBar");
        } catch (ClassNotFoundException ex) {
            GTMod.GT_FML_LOGGER.catching(ex);
        }
        try {
            progressBarStep = cpwProgressBar.getDeclaredField("step");
            progressBarStep.setAccessible(true);
        } catch (NoSuchFieldException ex) {
            GTMod.GT_FML_LOGGER.catching(ex);
        }
    }

    private GTCLSCompat() {}

    private static <T> void registerAndReportProgression(String materialsType, Collection<T> materials,
        ProgressManager.ProgressBar progressBar, Function<T, Object> getName, Consumer<T> action) {
        int sizeStep = materials.size();
        final long progressionReportsEvery = 100;
        final long bakingMsgEvery = 1000;
        long nextProgressionReportAt = 0;
        long nextBakingMsgAt = 0;
        int currentStep = 0;

        for (T m : materials) {
            long now = System.currentTimeMillis();

            if (nextProgressionReportAt < now) {
                nextProgressionReportAt = now + progressionReportsEvery;
                String materialName = getName.apply(m)
                    .toString();
                try {
                    ProgressDisplayer.displayProgress(materialName, (float) currentStep / sizeStep);
                } catch (IOException e) {
                    GTMod.GT_FML_LOGGER.error("While updating progression", e);
                }
                try {
                    progressBarStep.set(progressBar, currentStep);
                } catch (IllegalAccessException iae) {
                    GTMod.GT_FML_LOGGER.error("While updating intermediate progression steps number", iae);
                }
                progressBar.step(materialName);
            }
            if (nextBakingMsgAt < now) {
                nextBakingMsgAt = now + bakingMsgEvery;
                GTMod.GT_FML_LOGGER
                    .info(String.format("%s - Baking: %d%%", materialsType, currentStep * 100 / sizeStep));
            }
            action.accept(m);
            currentStep += 1;
        }
        GTMod.GT_FML_LOGGER.info(String.format("%s - Baking: Done", materialsType));
        try {
            progressBarStep.set(progressBar, currentStep);
        } catch (IllegalAccessException iae) {
            GTMod.GT_FML_LOGGER.error("While updating final progression steps number", iae);
        }
    }

    public static void stepMaterialsCLS(Collection<GTProxy.OreDictEventContainer> mEvents,
        ProgressManager.ProgressBar progressBar) {
        MinecraftDisplayer.isRegisteringGTmaterials = true;
        registerAndReportProgression(
            "GregTech materials",
            mEvents,
            progressBar,
            m -> m.mMaterial,
            GTProxy::registerRecipes);
        ProgressManager.pop(progressBar);
        MinecraftDisplayer.isRegisteringGTmaterials = false;
    }

    public static void doActualRegistrationCLS(ProgressManager.ProgressBar progressBar,
        Set<Materials> replacedVanillaItemsSet) {
        MinecraftDisplayer.isReplacingVanillaMaterials = true;
        registerAndReportProgression(
            "Vanilla materials",
            replacedVanillaItemsSet,
            progressBar,
            m -> m.mDefaultLocalName,
            GTPostLoad::doActualRegistration);
    }

    public static void pushToDisplayProgress() {
        MinecraftDisplayer.isReplacingVanillaMaterials = false;
        try {
            ProgressDisplayer
                .displayProgress("Post Initialization: loading GregTech", MinecraftDisplayer.getLastPercent());
        } catch (IOException e) {
            GTMod.GT_FML_LOGGER.error("Exception caught when updating loading screen", e);
        }
    }
}