aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gregtech/api/objects/overclockdescriber/OverclockDescriber.java
blob: 9f995e47581f88e7fa41fce37b16fa56fc80b383 (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
package gregtech.api.objects.overclockdescriber;

import static gregtech.api.util.GTUtility.trans;

import javax.annotation.ParametersAreNonnullByDefault;

import gregtech.GTMod;
import gregtech.api.util.GTRecipe;
import gregtech.api.util.GTUtility;
import gregtech.api.util.MethodsReturnNonnullByDefault;
import gregtech.api.util.OverclockCalculator;
import gregtech.nei.RecipeDisplayInfo;

/**
 * Provides an overclock behavior that will run on machines with the ability to draw information about it on NEI.
 * <p>
 * Implement {@link gregtech.api.interfaces.tileentity.IOverclockDescriptionProvider} for corresponding machine to use
 * derivative of this class when looking up NEI recipe catalyst.
 */
@ParametersAreNonnullByDefault
@MethodsReturnNonnullByDefault
public abstract class OverclockDescriber {

    /**
     * Tier of the (maybe virtual) machine this object belongs to.
     */
    protected final byte tier;

    public OverclockDescriber(byte tier) {
        this.tier = tier;
    }

    /**
     * @return Tier of this object. Used to limit recipes shown on NEI, based on recipe EU/t.
     */
    public final byte getTier() {
        return tier;
    }

    /**
     * @return Tier display of this object, shown on NEI header in a form of {@code Machine Name (tier)}
     */
    public abstract String getTierString();

    /**
     * Creates overclock calculator from given template. This template should be used instead of building from the
     * ground to avoid issues coming from different caller using different templates, but it's not applicable when using
     * {@link OverclockCalculator#ofNoOverclock(GTRecipe)}.
     *
     * @param template Calculator that can be used as template. Recipe EU/t and duration are already set.
     * @param recipe   Recipe to calculate.
     */
    public abstract OverclockCalculator createCalculator(OverclockCalculator template, GTRecipe recipe);

    /**
     * Draws info about the energy this object can handle on NEI recipe GUI.
     */
    public abstract void drawEnergyInfo(RecipeDisplayInfo recipeInfo);

    public void drawDurationInfo(RecipeDisplayInfo recipeInfo) {
        if (getDurationTicks(recipeInfo.calculator) <= 0) return;

        String textToDraw = trans("158", "Time: ");
        if (GTMod.gregtechproxy.mNEIRecipeSecondMode) {
            textToDraw += getDurationStringSeconds(recipeInfo.calculator);
            if (getDurationSeconds(recipeInfo.calculator) <= 1.0d) {
                textToDraw += String.format(" (%s)", getDurationStringTicks(recipeInfo.calculator));
            }
        } else {
            textToDraw += getDurationStringTicks(recipeInfo.calculator);
        }
        recipeInfo.drawText(textToDraw);
    }

    /**
     * Used to limit the shown recipes when searching recipes with NEI recipe catalyst. Unless overridden, this method
     * doesn't do anything special (except for a bit worse performance).
     * <p>
     * In order to make use of this method, {@link gregtech.api.recipe.RecipeMapBuilder#useCustomFilterForNEI}
     * should be enabled for the recipemap.
     *
     * @return If this object can handle the supplied recipe
     */
    public boolean canHandle(GTRecipe recipe) {
        byte tier = GTUtility.getTier(recipe.mEUt);
        return this.tier >= tier;
    }

    private int getDurationTicks(OverclockCalculator calculator) {
        return calculator.getDuration();
    }

    private double getDurationSeconds(OverclockCalculator calculator) {
        return 0.05d * getDurationTicks(calculator);
    }

    private String getDurationStringSeconds(OverclockCalculator calculator) {
        return GTUtility.formatNumbers(getDurationSeconds(calculator)) + GTUtility.trans("161", " secs");
    }

    private String getDurationStringTicks(OverclockCalculator calculator) {
        String ticksString = getDurationTicks(calculator) == 1 ? GTUtility.trans("209.1", " tick")
            : GTUtility.trans("209", " ticks");
        return GTUtility.formatNumbers(getDurationTicks(calculator)) + ticksString;
    }
}