aboutsummaryrefslogtreecommitdiff
path: root/src/client/java/dev/isxander/yacl/impl/YetAnotherConfigLibImpl.java
blob: 380929c55e00964fcb9360a3dca57dcd7d416956 (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
package dev.isxander.yacl.impl;

import com.google.common.collect.ImmutableList;
import dev.isxander.yacl.api.ConfigCategory;
import dev.isxander.yacl.api.YetAnotherConfigLib;
import dev.isxander.yacl.gui.YACLScreen;
import dev.isxander.yacl.impl.utils.YACLConstants;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.text.Text;

import java.util.Objects;
import java.util.function.Consumer;

public final class YetAnotherConfigLibImpl implements YetAnotherConfigLib {
    private final Text title;
    private final ImmutableList<ConfigCategory> categories;
    private final Runnable saveFunction;
    private final Consumer<YACLScreen> initConsumer;

    private boolean generated = false;

    public YetAnotherConfigLibImpl(Text title, ImmutableList<ConfigCategory> categories, Runnable saveFunction, Consumer<YACLScreen> initConsumer) {
        this.title = title;
        this.categories = categories;
        this.saveFunction = saveFunction;
        this.initConsumer = initConsumer;
    }

    @Override
    public Screen generateScreen(Screen parent) {
        if (generated)
            throw new UnsupportedOperationException("To prevent memory leaks, you should only generate a Screen once per instance. Please re-build the instance to generate another GUI.");

        YACLConstants.LOGGER.info("Generating YACL screen");
        generated = true;
        return new YACLScreen(this, parent);
    }

    @Override
    public Text title() {
        return title;
    }

    @Override
    public ImmutableList<ConfigCategory> categories() {
        return categories;
    }

    @Override
    public Runnable saveFunction() {
        return saveFunction;
    }

    @Override
    public Consumer<YACLScreen> initConsumer() {
        return initConsumer;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == this) return true;
        if (obj == null || obj.getClass() != this.getClass()) return false;
        var that = (YetAnotherConfigLibImpl) obj;
        return Objects.equals(this.title, that.title) &&
                Objects.equals(this.categories, that.categories) &&
                Objects.equals(this.saveFunction, that.saveFunction) &&
                Objects.equals(this.initConsumer, that.initConsumer);
    }

    @Override
    public int hashCode() {
        return Objects.hash(title, categories, saveFunction, initConsumer);
    }

    @Override
    public String toString() {
        return "YetAnotherConfigLibImpl[" +
                "title=" + title + ", " +
                "categories=" + categories + ", " +
                "saveFunction=" + saveFunction + ", " +
                "initConsumer=" + initConsumer + ']';
    }

}