aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gregtech/api/recipe/check/SimpleCheckRecipeResult.java
blob: ed4c95f880f36c0892167dfb74a04d211126a874 (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
package gregtech.api.recipe.check;

import java.util.Objects;

import net.minecraft.network.PacketBuffer;
import net.minecraft.util.StatCollector;

import com.gtnewhorizons.modularui.common.internal.network.NetworkUtils;

/**
 * Simple implementation of {@link CheckRecipeResult}. You can create new object without registering it.
 */
public class SimpleCheckRecipeResult implements CheckRecipeResult {

    private boolean success;
    private String key;
    private boolean persistsOnShutdown;

    SimpleCheckRecipeResult(boolean success, String key, boolean persistsOnShutdown) {
        this.success = success;
        this.key = key;
        this.persistsOnShutdown = persistsOnShutdown;
    }

    @Override
    public String getID() {
        return "simple_result";
    }

    @Override
    public boolean wasSuccessful() {
        return success;
    }

    @Override
    public String getDisplayString() {
        return StatCollector.translateToLocal("GT5U.gui.text." + key);
    }

    @Override
    public CheckRecipeResult newInstance() {
        return new SimpleCheckRecipeResult(false, "", false);
    }

    @Override
    public void encode(PacketBuffer buffer) {
        buffer.writeBoolean(success);
        NetworkUtils.writeStringSafe(buffer, key);
        buffer.writeBoolean(persistsOnShutdown);
    }

    @Override
    public void decode(PacketBuffer buffer) {
        success = buffer.readBoolean();
        key = NetworkUtils.readStringSafe(buffer);
        persistsOnShutdown = buffer.readBoolean();
    }

    @Override
    public boolean persistsOnShutdown() {
        return persistsOnShutdown;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        SimpleCheckRecipeResult that = (SimpleCheckRecipeResult) o;
        return success == that.success && Objects.equals(key, that.key)
            && persistsOnShutdown == that.persistsOnShutdown;
    }

    /**
     * Creates new result with successful state. Add your localized description with `GT5U.gui.text.{key}`.
     * This is already registered to registry.
     */
    public static CheckRecipeResult ofSuccess(String key) {
        return new SimpleCheckRecipeResult(true, key, false);
    }

    /**
     * Creates new result with failed state. Add your localized description with `GT5U.gui.text.{key}`.
     * This is already registered to registry.
     */
    public static CheckRecipeResult ofFailure(String key) {
        return new SimpleCheckRecipeResult(false, key, false);
    }

    /**
     * Creates new result object with failed state that does not get reset on shutdown. Add your localized description
     * with `GT5U.gui.text.{key}`. This is already registered to registry.
     */
    public static CheckRecipeResult ofFailurePersistOnShutdown(String key) {
        return new SimpleCheckRecipeResult(false, key, true);
    }
}