aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gregtech/api/util/shutdown/ShutDownReasonRegistry.java
blob: 1cdfb60ddb6b8caf721d54f79d56dc22ee9f12df (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
118
package gregtech.api.util.shutdown;

import static gregtech.api.util.GTModHandler.getWater;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Nonnull;

import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.FluidStack;

public class ShutDownReasonRegistry {

    private static final Map<String, ShutDownReason> registry = new HashMap<>();

    /**
     * Registers ShutDownReason. No duplicated IDs are allowed.
     *
     * @param sample Sample object to register
     */
    public static void register(ShutDownReason sample) {
        if (isRegistered(sample.getID())) {
            throw new IllegalStateException(
                String.format(
                    "ID %s is already registered for %s",
                    sample.getID(),
                    registry.get(sample.getID())
                        .getClass()
                        .getCanonicalName()));
        }
        registry.put(sample.getID(), sample);
    }

    public static ShutDownReason getSampleFromRegistry(String id) {
        if (!isRegistered(id)) {
            throw new RuntimeException("Unknown id: " + id);
        }
        return registry.get(id);
    }

    public static boolean isRegistered(String id) {
        return registry.containsKey(id);
    }

    /**
     * Shut down due to power loss.
     */
    @Nonnull
    public static final ShutDownReason POWER_LOSS = SimpleShutDownReason.ofCritical("power_loss");
    /**
     * Failed to output the pollution.
     */
    @Nonnull
    public static final ShutDownReason POLLUTION_FAIL = SimpleShutDownReason.ofCritical("pollution_fail");
    /**
     * Shut down due to incomplete structure.
     */
    @Nonnull
    public static final ShutDownReason STRUCTURE_INCOMPLETE = SimpleShutDownReason.ofNormal("structure_incomplete");
    /**
     * Shut down due to machine damage.
     */
    @Nonnull
    public static final ShutDownReason NO_REPAIR = SimpleShutDownReason.ofNormal("no_repair");
    /**
     * No valid turbine found.
     */
    @Nonnull
    public static final ShutDownReason NO_TURBINE = SimpleShutDownReason.ofNormal("no_turbine");
    /**
     * No correct machine part in controller slot.
     */
    @Nonnull
    public static final ShutDownReason NO_MACHINE_PART = SimpleShutDownReason.ofNormal("no_machine_part");
    /**
     * Default unknown state.
     */
    @Nonnull
    public static final ShutDownReason NONE = SimpleShutDownReason.ofNormal("none");
    /**
     * Critical unknown state.
     */
    @Nonnull
    public static final ShutDownReason CRITICAL_NONE = SimpleShutDownReason.ofCritical("none");

    /**
     * Fluid that needs to be constantly supplied are out. E.g. PCB coolant with cooling upgrades enabled.
     */
    @Nonnull
    public static ShutDownReason outOfFluid(@Nonnull FluidStack required) {
        return new ReasonOutOfFluid(required);
    }

    /**
     * Item that needs to be constantly supplied are out.
     */
    @Nonnull
    public static ShutDownReason outOfItem(@Nonnull ItemStack required) {
        return new ReasonOutOfItem(required);
    }

    /**
     * Stuff that needs to be constantly supplied are out.
     */
    @Nonnull
    public static ShutDownReason outOfStuff(@Nonnull String required, int amount) {
        return new ReasonOutOfStuff(required, amount);
    }

    static {
        register(new SimpleShutDownReason("", false));
        register(new ReasonOutOfFluid(getWater(0)));
        register(new ReasonOutOfItem(new ItemStack(Items.feather, 1)));
        register(new ReasonOutOfStuff("stuff", 1));
    }
}